z__y 2022-11-09 09:50 采纳率: 88.9%
浏览 78
已结题

尝试定义了一个大整数类,但是出错

我在VS2022上尝试定义了一个大整数类,使用了int*指针和string,并且使用了动态内存

#include<iostream>
#include<string>
using namespace std;
class HugeInteger
{
public:
    HugeInteger(long n);
    HugeInteger(string str);
    HugeInteger();
    HugeInteger(int* int_p, string str_p, int lent);
    ~HugeInteger();
    void display()
    {
        cout << len << endl;
    }
    HugeInteger operator+(HugeInteger one_huge);
    HugeInteger operator+(long ln);
    HugeInteger operator+(string sn);
    HugeInteger operator=(HugeInteger a_huge);
    friend istream& operator>>(istream& sin, HugeInteger& Huge);
    friend ostream& operator<<(ostream& sout, const HugeInteger& Huge);
private:
    
    int* int_huge;      //存储大整数
    string str_huge;    //暂时存储字符型的大整数
    int len;            //大整数的位数
};
HugeInteger::HugeInteger(long n)           //参数为整数型构造函数
{
    int i = 0;
    long temp = n;
    while (temp)
    {
        temp /= 10;
        i++;
    }
    temp = n;
    int_huge = new int[i];            
    for (int j = 0; j < i; j++)
    {                                        
        int_huge[j] = temp%10;
        temp /= 10;
    }
    len = str_huge.size();
}
HugeInteger::HugeInteger(string str)          //参数为字符串型构造函数
{
    int_huge = new int[str.size()];
    str_huge = str;
    for (int k = 0; k < str.size(); k++)
    {
        int_huge[k] = int(str[str.size() - 1 ]-48);
        cout << int_huge[k ];
        k++;
    }
    len = str.size();

}
HugeInteger::HugeInteger()                   //默认构造函数
{
    str_huge = "\0";
    int_huge = new int[1]();
    len = 1;
}
HugeInteger::HugeInteger(int* int_p, string str_p, int lent)//全参构造函数
{
    len = lent;
    int_huge = new int[lent];
    for (int t = 0; t < lent; t++)
    {
        int_huge[t] = int_p[t];
    }
    str_huge = str_p;
    

}
HugeInteger::~HugeInteger()                 //析构函数
{
    delete[]int_huge;
    int_huge = 0;
    str_huge = "\0";
    len = 0;
}
HugeInteger HugeInteger:: operator+(HugeInteger one_huge)
{
    int lenth = len > one_huge.len ? len : one_huge.len;
    int* add = new int[lenth + 1];//存储相加后的结果
    int x = 0;//存储上一位的进位
    int j = 0;//表示运算到哪一位
    for (j = 0; j < lenth; j++)
    {
        add[j] = int_huge[j] + one_huge.int_huge[j] + x;//两数相加,并加上上一次的进位
        x = add[j] / 10;
        add[j] %= 10;
    }
    HugeInteger huge(add, "\0", j);

    delete[]add;
    return huge;

}
HugeInteger HugeInteger:: operator=(HugeInteger a_huge)
{
    if (this != & a_huge)
    {
        if (int_huge)
        {
            delete[] int_huge;
        }
        int_huge = new int[a_huge.len ];
        for (int i = 0; i < a_huge.len; i++)
        {
            int_huge[i] = a_huge.int_huge[i];
        }

        len = a_huge.len;
        str_huge = a_huge.str_huge;
    }


    return *this;
}
HugeInteger HugeInteger::operator+(long ln)
{
    HugeInteger asdf(ln);
    *this = *this + asdf;
    return *this;
}
HugeInteger HugeInteger::operator+(string sn)
{
    HugeInteger qwer(sn);
    *this = *this + qwer;
    return *this;
}

istream& operator>>(istream& sin,  HugeInteger& Huge)
{
    string s;
    sin >> s;;
    HugeInteger _a(s);
    Huge = _a;
    return sin;
}
ostream& operator<<(ostream& sout, const HugeInteger& Huge)
{
    for(int jk=0;jk<Huge.len;jk++)
    { 
        sout << Huge.int_huge[Huge.len - 1 - jk];
    }
    return sout;
}




int main()
{
    HugeInteger n1(7654321);
    HugeInteger n2(7891234);
    HugeInteger n3("99999999999999999999999999999");
    HugeInteger n4("1");
    HugeInteger n5;
    n1.display();
    n2.display();
    n3.display();
    n4.display();
    n5.display();
    
    cout << "n1 is " << n1 << "\nn2 is " << n2
        << "\nn3 is " << n3 << "\nn4 is " << n4
        << "\nn5 is " << n5 << "\n\n";
    n5 = n1 + n2;
    cout << n1 << " + " << n2 << " = " << n5 << "\n\n";
    cout << n3 << " + " << n4 << "\n= " << (n3 + n4) << "\n\n";
    n5 = n1 + 9;
    cout << n1 << " + " << 9 << " = " << n5 << "\n\n";
    n5 = n2 + "10000";
    cout << n2 << " + " << "10000" << " = " << n5 << endl;

    return 0;
}


运行结果:
99999999999999910
0
29
1
1
n1 is
n2 is
n3 is 9-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519-8421504519
n4 is 1
n5 is 0

报了个错:

img

我在调试中发现,如果想打印n3的每一位数,只会打印到第14个数。

请问,这该怎么修改?

  • 写回答

2条回答 默认 最新

  • jwchu 2022-11-09 11:14
    关注

    1 第44行,构造函数参数如果是长整型的话,长度是0。因为str_huge此时是空串
    2 第54行,k++,for循环中也加了一次,加了两次。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月17日
  • 已采纳回答 11月9日
  • 赞助了问题酬金15元 11月9日
  • 创建了问题 11月9日

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。