C++大整数求和算法求解 20C

我们课程设计叫写一个大整数求和的算法,但是在重载加法运算符的时候出现了一个bug,一直不知道错在哪里,怎么改,但是大概知道应该是数据类型不匹配的缘故,可就是不知道到底怎么改,求解!!!
主函数部分:
#include#includeusing namespace std;#include"bigInt.h"//功能函数声明 void menu();//主界面函数 int main() { //测试 string a = "992"; string b = "74"; bigInt b1(a);//定义测试数据 bigInt b2(b); bigInt temp;//定义一个临时变量用来存储计算结果 我们课程设计叫写一个大整数求和的算法,但是在重载加法运算符的时候出现了一个bug,一直不知道错在哪里,怎么改,但是大概知道应该是数据类型不匹配的缘故,可就是不知道到底怎么改,求解!!!
主函数部分:
#include#includeusing namespace std;#include"bigInt.h"//功能函数声明 void menu();//主界面函数 int main() { //测试 string a = "992"; string b = "74"; bigInt b1(a);//定义测试数据 bigInt b2(b); bigInt temp;//定义一个临时变量用来存储计算结果 temp = b1 + b2; system("pause"); return 0;}

bigInt.h:
#ifndef BIG_INT_H#define BIG_INT_H#include//大整数的结构 class bigInt{public: friend ostream& operator<<(ostream &output, bigInt &a);//重载输出流运算符 friend bigInt operator+ (bigInt &a, bigInt &b);//重载加法运算 friend bigInt operator- (bigInt &a, bigInt &b);//重载减法运算 friend bigInt operator* (bigInt &a, bigInt &b);//重载乘法运算 friend bigInt operator/ (bigInt &a, bigInt &b);//重载除法运算 bigInt(string a);//构造函数,初始化表示大整数的这个数组 bigInt();//默认构造函数 ~bigInt();//析构函数 bigInt& operator= (bigInt &a);//重载赋值运算 private: int * m_bigInt; //顺序表的数组 int m_length; const int maxCapcity;};#endif

bigInt.cpp:
#include#includeusing namespace std;#include"bigInt.h"//成员函数实现bigInt& bigInt::operator=(bigInt &a){ for (int i = 0; i= 0; i--, j++)//将大整数的低位存储到数组的低位 { m_bigInt[j] = 0 + a[i] - 48; m_length++; }}bigInt::~bigInt(){ delete[]m_bigInt; m_bigInt = NULL; m_length = 0;}//友元函数的实现 ostream& operator<<(ostream &output, bigInt &a){ for (int i = a.m_length - 1; i >= 0; i--) output << a.m_bigInt[i]; return output;}bigInt operator+ (bigInt &a, bigInt &b){ int flag = 0;//进位标志 bigInt temp; int m = a.m_length>b.m_length ? (b.m_length) : (a.m_length); int i = 0;//循环变量 for (i = 0; ib.m_length ? (a.m_length + 1) : (b.m_length + 1); } return temp;} temp = b1 + b2; system("pause"); return 0;}

bigInt.h:
#ifndef BIG_INT_H#define BIG_INT_H#include//大整数的结构 class bigInt{public: friend ostream& operator<<(ostream &output, bigInt &a);//重载输出流运算符 friend bigInt operator+ (bigInt &a, bigInt &b);//重载加法运算 friend bigInt operator- (bigInt &a, bigInt &b);//重载减法运算 friend bigInt operator* (bigInt &a, bigInt &b);//重载乘法运算 friend bigInt operator/ (bigInt &a, bigInt &b);//重载除法运算 bigInt(string a);//构造函数,初始化表示大整数的这个数组 bigInt();//默认构造函数 ~bigInt();//析构函数 bigInt& operator= (bigInt &a);//重载赋值运算 private: int * m_bigInt; //顺序表的数组 int m_length; const int maxCapcity;};#endif

bigInt.cpp:
#include#includeusing namespace std;#include"bigInt.h"//成员函数实现bigInt& bigInt::operator=(bigInt &a){ for (int i = 0; i= 0; i--, j++)//将大整数的低位存储到数组的低位 { m_bigInt[j] = 0 + a[i] - 48; m_length++; }}bigInt::~bigInt(){ delete[]m_bigInt; m_bigInt = NULL; m_length = 0;}//友元函数的实现 ostream& operator<<(ostream &output, bigInt &a){ for (int i = a.m_length - 1; i >= 0; i--) output << a.m_bigInt[i]; return output;}bigInt operator+ (bigInt &a, bigInt &b){ int flag = 0;//进位标志 bigInt temp; int m = a.m_length>b.m_length ? (b.m_length) : (a.m_length); int i = 0;//循环变量 for (i = 0; ib.m_length ? (a.m_length + 1) : (b.m_length + 1); } return temp;}

2个回答

你的代码都堆在一起没法看
http://www.cnblogs.com/FourLeafCloverZc/p/4017707.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问