Heim  >  Fragen und Antworten  >  Hauptteil

c++ - 我的代码问题出在哪里,为什么不能运行?

#include<iostream>
#include<cstring>
using namespace std;
class INT
{
    char *p_num;
    int len;
public:
    //构造函数
    INT()
    {
        p_num = NULL; len = 0;
    }
    INT(const char *p)
    {
     len = strlen(p)+1;
      p_num=new char[strlen(p)+1];
      strcpy(p_num,p);
    }//拷贝函数
    INT(const INT &s)
    {
        len = s.len;
        p_num=new char[strlen(s.p_num)+1];
           strcpy(p_num,s.p_num);
    }

    //赋值函数
    INT & operator=(const INT &s)
    {
    
        delete[]p_num;
        len = s.len;
        p_num = new char[strlen(s.p_num) + 1];
        strcpy(p_num, s.p_num);
        return *this;
    }
    //析构函数
    ~INT()
    {
        delete []p_num;
        p_num=NULL;
        len = 0;
    }
    //下面三个重载函数实现INT型与int型混合运算
    friend INT operator+(const INT &x1, const INT &x2);
    friend INT operator+(const INT &x, int y);
    friend INT operator+(int y, const INT &x);
    //显示数据
    void display()const
    {
        for (int i = 0; i <len; i++)
        cout << p_num[i];
        cout << endl;
    }
};
INT operator+(const INT &x1, const INT &x2)
{
    INT temp;
    temp.p_num = new char[x1.len+x2.len+2];
    if (x1.len>=x2.len)
    {
    
        for (int i = temp.len-1; i >= 0; i--) { temp.p_num[i] = '0'; }
        for (int i = x2.len-1; i >= 0; i--)
        {
            temp.p_num[i] = temp.p_num[i] + x1.p_num[i] + x2.p_num[i] - '0';
            if (temp.p_num[i] - '0' > 10)
            {
                temp.p_num[i] -= 10; temp.p_num[i - 1] += 1;
            }
        }
        for (int i = temp.len-x2.len-1; i >= 0; i--)
        {
            temp.p_num[i] +=x1.p_num[i];
        }
    }
    else
    {
        for (int i = temp.len-1; i >= 0; i--) { temp.p_num[i] = '0'; }
        for (int i = x1.len-1; i >= 0; i--)
        {
            temp.p_num[i] = temp.p_num[i] + x1.p_num[i] + x2.p_num[i] - '0';
            if (temp.p_num[i] - '0' > 10)
            {
                temp.p_num[i] -= 10; temp.p_num[i - 1] += 1;
            }
        }
        
        for (int i = temp.len-1; i >= 0; i--)
        {
            
            temp.p_num[i] += x2.p_num[i];
        }
       }
    return temp;
}
int main()
{
    INT x,y,z;
    x = "123456789";
    y = "12";
    z=x+y;
    z.display();
    system("pause");
    return 0;
}
怪我咯怪我咯2713 Tage vor486

Antworte allen(3)Ich werde antworten

  • 大家讲道理

    大家讲道理2017-04-17 13:34:52

    如果没有报错信息, 基本上没人会帮你debug.
    如果没有报错信息, 基本上没人会帮你debug.
    如果没有报错信息, 基本上没人会帮你debug.

    送你一篇提问的智慧

    Antwort
    0
  • PHP中文网

    PHP中文网2017-04-17 13:34:52

    建议题主能够先描述下您这个程序的目的,具体运行时遇到的是什么样的错。比如INT类应该是一个内部使用字符串表示的BigInterger实现,运行时实际上发现调用z.display()没有任何的输出(期望应该是打印出整数内容)。这样可以为真正愿意帮助你的人节省不少的时间,对自己也是一个锻炼。

    首先你需要修改字符串的表达方式,在内存中,你应该使用如下的方式来存储整数,这样才能比较简单的做加法对齐:

    程序的问题在于重载加法函数的实现。

    首先在进行temp变量初始化,没有正确的设置temp.len的值:

    INT temp;
    // initialize temp INT object
    temp.len = x1.len + 1; // you don't need to add x1 and x2 size, actually just the large one's length + 1 is enough
    temp.p_num = new char[temp.len];
    

    其次你的在各个位的操作上也有问题,我不方便一一解释,这边贴上我写的一个样例实现(注意在该实现中,字符串存储是逆序的),请注意比较两者的区别:

    INT operator+(const INT& x1, const INT& x2)
    {
        if(x1.len < x2.len) {
            return x2 + x1;
        }
    
        INT temp;
        // initialize temp INT object
        temp.len = x1.len + 1; // save for extra increment
        temp.p_num = new char[temp.len];
        /* replace this loop with a memset call to be more efficient and readable
        for(int i = temp.len - 1; i >= 0; i--) {
            temp.p_num[i] = '0';
        }*/
        memset(temp.p_num, '0', temp.len);
        temp.p_num[temp.len - 1] = '\0';
    
        for(int i = 0; i < x1.len - 1; ++i) {
            temp.p_num[i] += (x1.p_num[i] - '0') + 
                ((i < x2.len - 1) ? (x2.p_num[i] - '0') : 0);
            if(temp.p_num[i] - '0' >= 10) {
                temp.p_num[i] -= 10;
                temp.p_num[i + 1] += 1;
            }
        }
        // truncate extra byte of 0
        if (temp.p_num[temp.len - 2] == '0')
            temp.p_num[temp.len - 2] = '\0';
        return temp;
    }
    

    当然你的display函数实现也要做相应的修改:

    void display() const
    {
        for(int i = len - 2; i >= 0; --i)
            if (p_num[i] != '\0')
                cout << p_num[i];
        cout << endl;
    }
    

    程序运行结果如下:

    123456801

    Antwort
    0
  • ringa_lee

    ringa_lee2017-04-17 13:34:52

    //赋值函数
    INT & operator=(const INT &s)
    {
        delete[] p_num;
        //...
     

    这里没有 考虑 p_num == NULL的情况

    Antwort
    0
  • StornierenAntwort