大数相加(顺序表中)
发布时间:2020-12-14 02:12:32 所属栏目:大数据 来源:网络整理
导读://-----------------SeqList.h-------------------#ifndef SeqList_H //注意这里#ifndef、#endif的用法#define SeqList_Hconst int MaxSize = 100;const int MAXN = 1000;class SeqList{public:SeqList(){length = 0;}SeqList(int a[],int n);SeqList(int ar
//-----------------SeqList.h------------------- #ifndef SeqList_H //注意这里#ifndef、#endif的用法 #define SeqList_H const int MaxSize = 100; const int MAXN = 1000; class SeqList{ public: SeqList(){length = 0;} SeqList(int a[],int n); SeqList(int array[]);//逆序 ~SeqList(){} void Insert(int i,int x); int Delete(int i); int Locate(int x); void Add(SeqList A,SeqList B,int array[]); void PrintList(); void Print_sum(); private: int data[MaxSize]; int length; }; extern int _long; extern int C[MAXN]; #endif //------------SeqList.cpp------------------ #include<iostream> using namespace std; #include "SeqList.h" SeqList::SeqList(int a[],int n) { //有参构造函数 if(n > MaxSize) throw "参数非法"; for(int i = 0; i < n; i++) { data[i] = a[i]; length = n; } } void SeqList::Insert(int i,int x) { //插入操作函数 if(length >= MaxSize) throw "上溢"; if(i < 1 || i > length+1) throw "位置非法"; for(int j = length; j >= i; j--) data[j] = data[j-1]; data[i-1] = x; length++; } int SeqList::Delete(int i){ //删除操作函数 if(length == 0) throw "下溢"; if(i<1 || i > length) throw "位置非法"; int x = data[i-1]; for(int j = i; j < length; j++) data[j-1] = data[j]; length--; return x; } int SeqList::Locate(int x) { //查找操作函数 for(int i = 0; i < length; i++) if(data[i]==x) return i+1; return 0; } void SeqList::PrintList() { //打印操作函数 for(int i = 0; i < length; i++) cout << data[i] << " "; cout << endl; } void SeqList::Print_sum(){ for(int i = 0; i < length; i++){ cout << data[i]; } } void SeqList::Add(SeqList A,int array[]){ int n = A.length,m = B.length; int common = n<m?n:m; //共有长度common int flag_a = n-1,flag_b = m-1;//两个数组的末尾下标 _long = 0; //C数组下标 int c = 0; //进位 for(int i = 0; i < common; i++) { //计算共有长度common个数 int he = A.data[flag_a--]+B.data[flag_b--]+c; array[_long++] = he%10; c = he/10; } while(flag_b>=0){//如果B比A长 int he = B.data[flag_b--]+c; array[_long++] = he%10; c = he/10; } while(flag_a>=0){//如果A比B长 int he = A.data[flag_a--]+c; array[_long++] = he%10; c = he/10; } if(c) array[_long++] = c;//如果有进位 length = _long; for(int j = 0; j < _long; j++) data[j] = array[length-j-1]; } //-----------------SeqList_main.cpp--------------- #include<iostream> using namespace std; #include "SeqList.h" int _long; int C[MAXN]; void main() { int r[5] = {1,2,3,4,5}; SeqList L(r,5); cout << "执行插入操作前数据为:" << endl; L.PrintList(); try{ L.Insert(2,3); } catch(char* s) //这个s捕捉的就是“上溢”这个字符串 { cout << s << endl; } cout << "执行插入操作后数据为:"<< endl; L.PrintList(); cout << "值为3的元素位置为:"; cout << L.Locate(3) << endl; cout << "执行删除第一个元素操作,删除前数据为:"<< endl; L.PrintList(); try { L.Delete(1); } catch(char* s) //这个捕捉到的s就是“下溢”或者“位置非法”这两个中的一个 { cout << s << endl; } cout << "删除后数据为: " << endl; L.PrintList(); int a[100] = {1,5,9,7,6,1,9}; int b[100] = {5,8,1}; SeqList A(a,31); SeqList B(b,15); for(int i = 0; i < _long; i++){ cout << C[i]; } cout << endl; SeqList sum; sum.Add(A,B,C); cout << "大数相加:" << endl; A.Print_sum(); cout << " + " << endl; B.Print_sum(); cout << " = " << endl; sum.Print_sum(); cout << endl << endl; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |