c语言大整数的四则运算,大整数的四则运算中碰到的问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

1L贴代码

#include

using namespace std;

struct LargeInt

{

public:

//构造跟析构

LargeInt();

~LargeInt();

//拷贝构造

LargeInt(const LargeInt & b);

//输入函数

void GetIn();

//交换

void Swap(char &a, char &b);

//重载 "<

friend ostream &operator << (ostream &output, LargeInt &a);

//重载 "+"号

friend LargeInt operator + (LargeInt &a, LargeInt &b);

//重载 "="

LargeInt &operator = (LargeInt &str);

char &operator [] (int i);

void Upsidedown ();

int Max(int a, int b);

int Length();

private:

char *a;

};

char & LargeInt::operator [] (int n)

{

if (n > Length())

{

cout << "下表超界!";

exit(0);

}

return a[n];

}

int LargeInt::Length()

{

return strlen(a);

}

ostream &operator << (ostream &stream,LargeInt &a)

{

int i;

for (i = 0; i < a.Length() ; i++)

{

cout <

}

return stream;

}

LargeInt::LargeInt()

{

a = new char[1];

a[0] = '\0';

}

LargeInt::~LargeInt()

{

delete[] a;

}

//拷贝构造

LargeInt::LargeInt(const LargeInt &b)

{

int blen = Length();

delete [] a;

a = new char [blen+1];

//strcpy(a, b.a);

for (int i = 0; i < blen; i++)

a[i] = b.a[i];

a[i] = '\0';

}

void LargeInt::Swap(char &a, char &b)

{

char c;

c = a;

a = b;

b = c;

}

void LargeInt::Upsidedown ()

{

//cout << "++++\n";

int m = Length();

for(int i = 0, j = m - 1; i < j; i++, j--)

{

Swap(a[i], a[j]);

}

}

//输入函数

void LargeInt::GetIn()

{

char *p=NULL;

char c;

int i=1;

p = new char[1];

p[0] = '\0';

delete [] a;

while((c = getchar()) != '\n')

{

i++;

a = new char [i];

for(int j = 0; j < i - 1; j++)

{

a[j] = p[j];

}

a[j-1] = c;

a[j] = '\0';

strcpy(p, a);

}

Upsidedown();

}

int LargeInt::Max(int a, int b)

{

return a >= b ? a : b;

}

LargeInt &LargeInt::operator= (LargeInt &str)

{

if (this == &str) return *this;

delete a;

a = new char[strlen(str.a) + 1];

strcpy(a, str.a);

return *this;

}

LargeInt operator + (LargeInt &a, LargeInt &b)

{

LargeInt c;

int alen = a.Length(), blen = b.Length(), clen;

int max = c.Max(alen, blen);

delete [] c.a;

c.a = new char [max + 2];

clen = max + 1;

//对c.a进行初始化

for (int i = 0; i < clen; i++)

c.a[i] = '0';

c.a[i] = '\0';

i = 0;

while (i < alen && i < blen)

{

c.a[i] += a.a[i] + b.a[i] - '0' - '0';

if (c.a[i] > '9')

{

c.a[i] -= 10;

c.a[i+1]++;

}

i++;

}

while (i < alen)

{

c.a[i] += a.a[i] - '0';

if (c.a[i] > '9')

{

c.a[i] -= 10;

c.a[i+1]++;

}

i++;

}

while (i < blen)

{

c.a[i] += b.a[i] - '0';

if (c.a[i] >= 10)

{

c.a[i] -= 10;

c.a[i+1]++;

}

i++;

}

c.a[clen] = '\0';

if (c.a[clen - 1] == '0')

c.a[clen - 1] = '\0';

c.Upsidedown();

//cout << "c.Length() = "<< c.Length()<< endl;

//在这个地方输出c都能正常,到主函数中就不行了

cout << "这是重载函数中的输出:c = "<< c<< endl;

return c;

}

int main()

{

LargeInt a, b, c;

a.GetIn();

//cout << "a = "<

b.GetIn();

c = a + b;

cout << " Kitty!!"<

cout << "c.length = "<

cout << "这是主函数中的输出:c ="<

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值