你好,游客 登录 注册 搜索
背景:
阅读新闻

顺序线性表之大整数求和C++实现

[日期:2017-04-03] 来源:Linux社区  作者:zfc-java [字体:大 中 小]

顺序线性表之大整数求和

 

大整数求和伪代码
  1、初始化进位标志 flag=0;
  2、求大整数 A 和 B 的长度:
    int aLength = a.GetLength();
    int bLength = b.GetLength();
  3、从各位开始逐位进行第 i 位的加法,直到 A 或 B 计算完毕:
    3.1、计算第 i 位的值:c.Insert(i+1, (a.GetElement(i + 1) + b.GetElement(i + 1) + flag) % 10);
    3.2、计算该位的进位:flag = (a.GetElement(i + 1) + b.GetElement(i + 1) + flag) / 10;
  4、计算大整数 A 或 B 余下的部分;
  5、计算结果的进位
注:用数组来存放大整数

一、顺序线性表头文件:SeqList.h

//顺序线性表的头文件
#include<iostream>

const int MaxSize = 100;
//定义顺序表SeqList的模板类
template<class DataType>
class SeqList{
public:
  //顺序表无参构造器(创建一个空的顺序表)
  SeqList(){ length = 0; }
  //顺序表有参构造器(创建一个长度为n的顺序表)
  SeqList(DataType array[], int n);
  //顺序表析构函数
  ~SeqList(){}
  //求顺序表的长度
  int GetLength(){ return length; }
  //顺序表按位查找,返回i位置的元素
  DataType GetElement(int i);
  //顺序表按值查找,返回该元素所在的位置
  int GetLocal(DataType x);
  //顺序表在指定的位置插入指定的元素
  void Insert(int i, DataType x);
  //顺序表删除元素,返回删除的元素
  DataType Delete(int i);
  //输出顺序表中的元素
  void PrintSeqList();
private:
  //一维数组,存放数据元素
  DataType data[MaxSize];
  //顺序表的长度
  int length;
};

//实现顺序表有参构造器
template<class DataType>
SeqList<DataType>::SeqList(DataType array[], int n)
{
  if (n > MaxSize)
  {
    throw "传入的顺序表长度过长";
  }
  //给顺序表的存储元素的数组赋值
  for (int i = 0; i < n; i++)
  {
    data[i] = array[i];
  }
  //给顺序表的长度赋值
  length = n;
}

//实现顺序表按位查找
template<class DataType>
DataType SeqList<DataType>::GetElement(int i)
{
  //判断是定的位置是否合理
  if (i < 1 || i >length)
  {
    throw "位置有误";
  }
  else
  {
    //返回指定位置的元素
    return data[i - 1];
  }
}

//实现顺序表按值查找,返回该元素所在的位置
template<class DataType>
int SeqList<DataType>::GetLocal(DataType x)
{
  //遍历顺序表的元素
  for (int i = 0; i < length; i++)
  {
    //判断指定的元素是否在顺序表中
    if (data[i] == x)
    {
      //返回指定元素在顺序表中的位置
      return (i + 1);
    }
  }
  //如果指定的元素不在顺序表中,则返回位置为0
  return 0;
}

//实现顺序表插入元素
template<class DataType>
void SeqList<DataType>::Insert(int index, DataType x)
{
  //判断插入的位置是否合理
  if (length >= MaxSize)
  {
    throw "顺序表已存放满";
  }
  if (index<1 || index>length + 1)
  {
    throw "插入元素的位置有误";
  }
  //如何插入的位置合理,则把顺序表中从最后位置到指定插位置的元素整体向后移动一个位置
  for (int j = length; j >= index; j--)
  {
    data[j] = data[j - 1];
  }
  //给插入的位置放入指定的元素
  data[index - 1] = x;
  length++;
}

//实现顺序表删除指定位置的元素
template<class DataType>
DataType SeqList<DataType>::Delete(int index)
{
  //声明要取出的元素
  DataType x;
  //判断要删除的位置是否合理
  if (index<1 || index>length)
  {
    throw "删除的位置有误";
  }
  else
  {
    //取出指定位置的元素
    x = data[index - 1];
    //将指定位置后的元素全部都向前移动一个位置
    for (int i = index; i < length; i++)
    {
      data[i - 1] = data[i];
    }
    //删除顺序表中的元素后,其长度减1
    length--;
  }
  return x;
}

//顺序输出顺序表中的元素
template<class DataType>
void SeqList<DataType>::PrintSeqList()
{
  if (length < 1)
  {
    throw "顺序表中没有元素";
  }
  else
  {
    //顺序输出顺序表元素
    for (int i = 0; i < length; i++)
    {
      cout << data[i] << " ";
    }
    cout << endl;
  }
}

 二、大整数求和头文件:BidIntegerAdd.h

//顺序线性表之大整数求和
#include<iostream>
//引入顺序线性表的头文件
#include"SeqList.h"
using namespace std;

SeqList<int> Add(SeqList<int>a, SeqList<int>b)
{
  //定义中间变量,顺序线性表
  SeqList<int> c = SeqList<int>();
  //flag 是进位标志,i 为大整数的某一位
  int flag = 0, i = 0;
  //求大整数 a,b 的位数
  int aLength = a.GetLength();
  int bLength = b.GetLength();
  //逐位计算加法直到某个大整数计算完毕
  while (i < aLength&&i < bLength)
  {
    //计算第 i 位的值
    c.Insert(i+1, (a.GetElement(i + 1) + b.GetElement(i + 1) + flag) % 10);
    //计算第 i 位的进位
    flag = (a.GetElement(i + 1) + b.GetElement(i + 1) + flag) / 10;
    i++;
  }
  //计算大整数 A 余下的部分
  for (; i < aLength; i++)
  {
    c.Insert(i + 1, (a.GetElement(i + 1) + flag) % 10);
    flag = (a.GetElement(i + 1) + flag) / 10;
  }
  //计算大整数 B 余下的部分
  for (; i < bLength; i++)
  {
    c.Insert(i + 1, (b.GetElement(i + 1) + flag) % 10);
    flag = (b.GetElement(i + 1) + flag) / 10;
  }
  //如果最后有进位,则结果会多一位
  if (flag == 1)
  {
    c.Insert(c.GetLength()+1, 1);
  }
  return c;
}

三、测试顺序线性表之大整数求和:TestBigIntegerAdd.h

//测试顺序线性表之大整数求和
#include<iostream>
//引入顺序表之大整数求和头文件
#include"BigIntegerAdd.h"
using namespace std;
int main()
{
  //定义两个顺序线性表
  int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  int array2[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
  SeqList<int>a = SeqList<int>(array1,9);
  cout << "第一个大整数是:" << endl;
  a.PrintSeqList();
  SeqList<int>b = SeqList<int>(array2,8);
  cout << "第二个大整数是:" << endl;
  b.PrintSeqList();
  SeqList<int>c = Add(a, b);
  cout << "他们的和是:" << endl;
  c.PrintSeqList();
  return 0;
}

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/142464.htm

linux
相关资讯       顺序线性表  大整数求和 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款