OUC_liuxiang 2017-03-23 12:15 采纳率: 0%
浏览 1312
已结题

一个大数加法程序,本机测试正常,无法通过学校openjudge测评,还请各位帮忙分析

要求按照给出的main函数,编写一个类,处理大数与整数的加法;
以下是题目,源码在二楼贴出:

总时间限制:
1000ms
内存限制:
65536kB

描述

程序填空,输出指定结果

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <cstdio> 
using namespace std;
const int MAX = 110; 
class CHugeInt {

// 在此处补充你的代码

};
int  main() 
{ 
 char s[210];
 int n;

 while (cin >> s >> n) {
 CHugeInt a(s);
 CHugeInt b(n);

 cout << a + b << endl;
 cout << n + a << endl;
 cout << a + n << endl;
 b += n;
 cout  << ++ b << endl;
 cout << b++ << endl;
 cout << b << endl;
 }
 return 0;
}

输入
多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示
输出
对每组数据,输出6行,内容分别是:
样例输入

99999999999999999999999999888888888888888812345678901234567789 12
6 6

样例输出

99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
  • 写回答

2条回答 默认 最新

  • OUC_liuxiang 2017-03-23 12:17
    关注

    #include
    #include
    #include
    #include
    using namespace std;
    const int MAX = 110;
    class CHugeInt {
    private:
    int val = 0;
    char str[210];
    int num[210];
    int len;
    public:
    CHugeInt(){}
    CHugeInt(int n)
    {
    val = n;
    memset(str,0,210*sizeof(char));
    memset(num,0,210*sizeof(int));
    len = 0;
    while(n != 0)
    {
    num[len] = n%10;
    len ++;
    n /= 10;
    }
    }

    CHugeInt(const char * ch)
    {
    memset(num,0,210*sizeof(int));
    memset(str,0,210*sizeof(char));
    strcpy(str,ch);
    len = strlen(str);
    for(int i = 0; i < len; i ++)
    num[i] = (int)(str[len-1-i] - 48);
    }
    ~CHugeInt(){}
    friend CHugeInt operator+(const CHugeInt & a,const CHugeInt & b) //MDZZ!!我自己都不知道是怎么编出来的...
    {
    CHugeInt oth;
    int length = a.len;
    length += 1; //考虑到进位情况
    oth.len = length;
    for(int k = 0; k < 210; k ++)
    oth.num[k] = 0;
    for( int i = 0; i < oth.len; i ++)
    {
    oth.num[i] += a.num[i] + b.num[i];
    oth.num[i+1] += oth.num[i] / 10;
    oth.num[i] = oth.num[i] % 10;
    }
    while((oth.len > 1) && (oth.num[oth.len-1] == 0))
    oth.len --;
    //神TM知道为什么要有下一步??!!
    /* int k = 0;
    for( int i = 0; i < oth.len; i ++)
    oth.str[k ++] = (char)(oth.num[oth.len-1-i] + 48);
    */ return oth;
    }

    friend CHugeInt operator+(const int n,const CHugeInt & a) //为什么不能返回引用?
    {
    CHugeInt c;
    int length = a.len;
    length ++;
    c.len = length;
    int temp = n, i = 0;
    for(int k = 0; k < 210; k ++)
    c.num[k] = 0;
    while(temp)
    {
    c.num[i] = temp%10;
    i ++;
    temp /= 10;
    }
    for(int j = 0; j < c.len; j ++)
    {
    c.num[j] += a.num[j];
    c.num[j+1] += c.num[j] / 10;
    c.num[j] = c.num[j] % 10;
    }
    while(c.len > 1 && c.num[c.len - 1] == 0) c.len --;
    /* int k = 0;
    for( int i = 0; i < c.len; i ++)
    c.str[k ++] = (char)(c.num[c.len-1-i] + 48);
    / return c;
    }
    friend CHugeInt operator+(const CHugeInt &a, const int n)
    {
    CHugeInt c;
    int length = a.len;
    length ++;
    c.len = length;
    int temp = n, i = 0;
    for(int k = 0; k < 210; k ++)
    c.num[k] = 0;
    while( temp)
    {
    c.num[i] += temp % 10;
    i ++;
    temp /= 10;
    }
    for(int j = 0; j < c.len; j ++)
    {
    c.num[j] += a.num[j];
    c.num[j+1] += c.num[j] / 10;
    c.num[j] = c.num[j] % 10;
    }
    while( c.len > 1 && c.num[c.len-1] == 0) c.len --;
    /
    int k = 0;
    for( int i = 0; i < c.len; i ++)
    c.str[i] = (char)( c.num[c.len-1-i] + 48);
    */ return c;
    }

    CHugeInt & operator+=(int n)
    {
    val += n;
    return *this;
    }
    CHugeInt & operator++()
    {
    ++val;
    return *this;
    }
    CHugeInt operator++(int n) //此处一般不返回引用
    {
    CHugeInt ss = *this;
    val ++;
    return ss;
    }

    friend ostream & operator<<(ostream & out, const CHugeInt & c)
    {
    if(c.val == 0){
    for( int i = c.len-1;i >= 0; i --)
    out << c.num[i];
    return out;}
    else {
    // cout << c.val;
    out << c.val;
    return out;}
    }
    };
    int main()
    {
    char s[210];
    int n;

    while (cin >> s >> n) {
    CHugeInt a(s);
    CHugeInt b(n);

    cout << a + b << endl;
    cout << n + a << endl;
    cout << a + n << endl;
    b += n;
    cout << ++ b << endl;
    cout << b++ << endl;
    cout << b << endl;
    }
    return 0;
    }

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!