c++ - 我的代码问题出在哪里,为什么不能运行?
怪我咯
怪我咯 2017-04-17 13:32:52
[C++讨论组]
#include
#include
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 =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;
}
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(2)
大家讲道理

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

送你一篇提问的智慧

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号