順序線性表之大整數求和C++實現


順序線性表之大整數求和

 

大整數求和偽代碼
  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

  1 //順序線性表的頭文件
  2 #include<iostream>
  3 
  4 const int MaxSize = 100;
  5 //定義順序表SeqList的模板類
  6 template<class DataType>
  7 class SeqList{
  8 public:
  9   //順序表無參構造器(創建一個空的順序表)
 10   SeqList(){ length = 0; }
 11   //順序表有參構造器(創建一個長度為n的順序表)
 12   SeqList(DataType array[], int n);
 13   //順序表析構函數
 14   ~SeqList(){}
 15   //求順序表的長度
 16   int GetLength(){ return length; }
 17   //順序表按位查找,返回i位置的元素
 18   DataType GetElement(int i);
 19   //順序表按值查找,返回該元素所在的位置
 20   int GetLocal(DataType x);
 21   //順序表在指定的位置插入指定的元素
 22   void Insert(int i, DataType x);
 23   //順序表刪除元素,返回刪除的元素
 24   DataType Delete(int i);
 25   //輸出順序表中的元素
 26   void PrintSeqList();
 27 private:
 28   //一維數組,存放數據元素
 29   DataType data[MaxSize];
 30   //順序表的長度
 31   int length;
 32 };
 33 
 34 //實現順序表有參構造器
 35 template<class DataType>
 36 SeqList<DataType>::SeqList(DataType array[], int n)
 37 {
 38   if (n > MaxSize)
 39   {
 40     throw "傳入的順序表長度過長";
 41   }
 42   //給順序表的存儲元素的數組賦值
 43   for (int i = 0; i < n; i++)
 44   {
 45     data[i] = array[i];
 46   }
 47   //給順序表的長度賦值
 48   length = n;
 49 }
 50 
 51 //實現順序表按位查找
 52 template<class DataType>
 53 DataType SeqList<DataType>::GetElement(int i)
 54 {
 55   //判斷是定的位置是否合理
 56   if (i < 1 || i >length)
 57   {
 58     throw "位置有誤";
 59   }
 60   else
 61   {
 62     //返回指定位置的元素
 63     return data[i - 1];
 64   }
 65 }
 66 
 67 //實現順序表按值查找,返回該元素所在的位置
 68 template<class DataType>
 69 int SeqList<DataType>::GetLocal(DataType x)
 70 {
 71   //遍歷順序表的元素
 72   for (int i = 0; i < length; i++)
 73   {
 74     //判斷指定的元素是否在順序表中
 75     if (data[i] == x)
 76     {
 77       //返回指定元素在順序表中的位置
 78       return (i + 1);
 79     }
 80   }
 81   //如果指定的元素不在順序表中,則返回位置為0
 82   return 0;
 83 }
 84 
 85 //實現順序表插入元素
 86 template<class DataType>
 87 void SeqList<DataType>::Insert(int index, DataType x)
 88 {
 89   //判斷插入的位置是否合理
 90   if (length >= MaxSize)
 91   {
 92     throw "順序表已存放滿";
 93   }
 94   if (index<1 || index>length + 1)
 95   {
 96     throw "插入元素的位置有誤";
 97   }
 98   //如何插入的位置合理,則把順序表中從最后位置到指定插位置的元素整體向后移動一個位置
 99   for (int j = length; j >= index; j--)
100   {
101     data[j] = data[j - 1];
102   }
103   //給插入的位置放入指定的元素
104   data[index - 1] = x;
105   length++;
106 }
107 
108 //實現順序表刪除指定位置的元素
109 template<class DataType>
110 DataType SeqList<DataType>::Delete(int index)
111 {
112   //聲明要取出的元素
113   DataType x;
114   //判斷要刪除的位置是否合理
115   if (index<1 || index>length)
116   {
117     throw "刪除的位置有誤";
118   }
119   else
120   {
121     //取出指定位置的元素
122     x = data[index - 1];
123     //將指定位置后的元素全部都向前移動一個位置
124     for (int i = index; i < length; i++)
125     {
126       data[i - 1] = data[i];
127     }
128     //刪除順序表中的元素后,其長度減1
129     length--;
130   }
131   return x;
132 }
133 
134 //順序輸出順序表中的元素
135 template<class DataType>
136 void SeqList<DataType>::PrintSeqList()
137 {
138   if (length < 1)
139   {
140     throw "順序表中沒有元素";
141   }
142   else
143   {
144     //順序輸出順序表元素
145     for (int i = 0; i < length; i++)
146     {
147       cout << data[i] << " ";
148     }
149     cout << endl;
150   }
151 }

二、大整數求和頭文件:BidIntegerAdd.h

 1 //順序線性表之大整數求和
 2 #include<iostream>
 3 //引入順序線性表的頭文件
 4 #include"SeqList.h"
 5 using namespace std;
 6 
 7 SeqList<int> Add(SeqList<int>a, SeqList<int>b)
 8 {
 9   //定義中間變量,順序線性表
10   SeqList<int> c = SeqList<int>();
11   //flag 是進位標志,i 為大整數的某一位
12   int flag = 0, i = 0;
13   //求大整數 a,b 的位數
14   int aLength = a.GetLength();
15   int bLength = b.GetLength();
16   //逐位計算加法直到某個大整數計算完畢
17   while (i < aLength&&i < bLength)
18   {
19     //計算第 i 位的值
20     c.Insert(i+1, (a.GetElement(i + 1) + b.GetElement(i + 1) + flag) % 10);
21     //計算第 i 位的進位
22     flag = (a.GetElement(i + 1) + b.GetElement(i + 1) + flag) / 10;
23     i++;
24   }
25   //計算大整數 A 余下的部分
26   for (; i < aLength; i++)
27   {
28     c.Insert(i + 1, (a.GetElement(i + 1) + flag) % 10);
29     flag = (a.GetElement(i + 1) + flag) / 10;
30   }
31   //計算大整數 B 余下的部分
32   for (; i < bLength; i++)
33   {
34     c.Insert(i + 1, (b.GetElement(i + 1) + flag) % 10);
35     flag = (b.GetElement(i + 1) + flag) / 10;
36   }
37   //如果最后有進位,則結果會多一位
38   if (flag == 1)
39   {
40     c.Insert(c.GetLength()+1, 1);
41   }
42   return c;
43 }

三、測試順序線性表之大整數求和:TestBigIntegerAdd.h

 1 //測試順序線性表之大整數求和
 2 #include<iostream>
 3 //引入順序表之大整數求和頭文件
 4 #include"BigIntegerAdd.h"
 5 using namespace std;
 6 int main()
 7 {
 8   //定義兩個順序線性表
 9   int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
10   int array2[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
11   SeqList<int>a = SeqList<int>(array1,9);
12   cout << "第一個大整數是:" << endl;
13   a.PrintSeqList();
14   SeqList<int>b = SeqList<int>(array2,8);
15   cout << "第二個大整數是:" << endl;
16   b.PrintSeqList();
17   SeqList<int>c = Add(a, b);
18   cout << "他們的和是:" << endl;
19   c.PrintSeqList();
20   return 0;
21 }

四、運行示例結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM