「已注销」 2016-12-12 12:40 采纳率: 11.1%
浏览 3146
已结题

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

我们课程设计叫写一个大整数求和的算法,但是在重载加法运算符的时候出现了一个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条回答 默认 最新

  • threenewbee 2016-12-12 15:45
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘