- 论坛徽章:
- 0
|
做的是书上的c++类的习题,把一个整数放入整数数组,
代码如下,编译通过,
如果main函数内,BigInt testbi(2345678),
那么运行正常,结果不是期望的:后面跟了一大串数字,这是怎么回事啊
这是我电脑上的显示:
I:\clang\class>bigint2
0
8-7-6-5-4-3-2-4198887-1-205920-207736-4468736-2359204--1-2359208-1-6-207736-
如果把main函数内,BigInt testbi(2345678),增加一位,如 12345678,或者23456789,编译正常,但是运行时跳出异常。
还有一个问题是成员函数void BigInt::printBint() const,想把整数数组打印出来,但是while 的条件不好定,现在这样用bi[i],但是如果正好整数数组的这个元素为0,打印程序就不往下打了。而且 sizeof(bi) 返回为0,不好确定数组长度,我想不出怎么确定while循环的条件。给我出点主意啊
#include <iostream>
using std::cout;
class BigInt
{
public:
BigInt(unsigned int);
void setBint(unsigned int aa);
void printBint() const ;
private:
int bi[];
//int si[];
} ;
BigInt::BigInt(unsigned int a)
{
setBint(a);
}
void BigInt::setBint(unsigned int aa)
{
int i=0;
while (aa!=0)
{
bi[i]=aa%10;
aa/=10;
i++ ;
}
}
void BigInt::printBint() const
{
int i=0;
cout<<sizeof(bi)<<"\n";
while (bi[i])
{
cout<<bi[i]<<"-";
i++ ;
}
}
int main()
{
BigInt testbi(2345678);
testbi.printBint();
return 0;
}
[[i] 本帖最后由 i802 于 2007-4-11 23:35 编辑 [/i]] |
|