大数相加(顺序表中)

//-----------------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
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, SeqList B, int array[]){
	int n = A.length, m = B.length;
	int common = n=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
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, 2, 3, 4, 5, 9, 4, 7, 5, 3, 2, 6, 5, 4, 3, 6, 4, 4, 3, 6, 3, 9, 1, 3, 6, 5, 7, 3, 2, 1, 9};
	int b[100] = {5, 6, 7, 8, 9, 8, 4, 5, 3, 5, 6, 7, 8, 3, 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;
}
大数相加(顺序表中)_第1张图片

你可能感兴趣的:(大数相加(顺序表中))