关于大整数,问过以后,我还是不会,在线等待一一指点
标准C++的内部数据类型int 在Win32 平台下占用4 个字节存储数据(sizeof(int) = 4),
其能够表达的整数范围为:-2147483648 — 2147483647 (-2^15 — 2^15-1)。
现请设计出一个新的数据类型(class MyInt),使其能够完成任意整数间的基本运算。
并再设计一个新的数据类型(class ComplexInt),使其能够完成任意复数(限制条件:
复数的实部和虚部都均为整数)之间的基本运算。
要求:
1. 定义一个新类(class MyInt),使其能够实现任意位整数间的加法和减法。
2. 定义一个新类(class ComplexInt),用于实现任意复数(限制条件:复数的实部和
虚部都均为整数)间的加法和减法。
3. 能够通过类似于如下的语句,并在屏幕上输出正确的结果。
void main(int argc, char* argv[])
{
MyInt iM("1234567890123"); // 整数对象初始化
MyInt iN("-3210987654321");
MyInt iResult1;
iResult1 = iM;
iResult1 += iN; // 整数加法操作
MyInt iResult2 = iM;
iResult2 -= iN; // 整数减法操作
// 结果输出
Display(iResult1); // iResult1: -1976419764198
Display(iResult2); // iResult2: 4445555544444
// 实部: 1234567890123;虚部: 4445555544444
ComplexInt ciM("1234567890123", "4445555544444"); // 复数对象初始化
// 实部: -4445555544444;虚部: 0
ComplexInt ciN("-4445555544444"); // 复数对象初始化
ComplexInt ciResult1 = ciM;
ComplexInt ciResult2 = ciM;
ciResult1 += ciN; // 复数加法操作
ciResult2 -= ciN; // 复数减法操作
// 结果输出
Display(ciResult1);
Display(ciResult2);
}
其中,Display 为一个全局函数,它可以根据所代入的参数类型进行相应的结果输
出(代入的参数是MyInt,则输出相应的整数;代入的是ComplexInt,则输出相应复
数的实部和虚部)。
得到的屏幕输出应该为:
This is MyInt::Display, the result is: -1976419764198
This is MyInt::Display, the result is: 4445555544444
This is ComplexInt::Display, the result is: -3210987654321 + 4445555544444 i
This is ComplexInt::Display, the result is: 5680123434567 + 4445555544444 i
4. 需要考虑程序的稳定性(各种错误处理)及健壮性(算法是否对所有的整数都适用),
不能有内存泄漏。
问题点数:99、回复次数:29Top
1 楼zhengsy()回复于 2005-04-02 19:36:42 得分 0
MyInt iM("1234567890123"); // 整数对象初始化
MyInt iResult1;
iResult1 = iM;
这个初始化,和=不会编。
后面的也不太明白。我真是笨阿,哎马上要交了,如何是好Top
2 楼zhengsy()回复于 2005-04-02 20:08:27 得分 0
各位xdjm,帮我一把Top
3 楼llf_hust()回复于 2005-04-02 20:27:40 得分 0
自己看看书写个重载=的函数就OK了呀
最好自己动手写写Top
4 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-02 20:28:54 得分 0
不用vector等很麻烦,还要自己创建链表Top
5 楼zhengsy()回复于 2005-04-02 20:42:08 得分 0
重载=不会啊。马上要交了,可是,我觉得我 c++跟没学似的Top
6 楼zhengsy()回复于 2005-04-02 20:57:47 得分 0
要是哪位好人帮我写点代码,分都给了Top
7 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-02 21:12:49 得分 0
你要是能等,我用vector给你重新写一个Top
8 楼zhengsy()回复于 2005-04-02 21:22:42 得分 0
我要么明天再来Top
9 楼zhengsy()回复于 2005-04-02 21:25:27 得分 0
要是用vector的话,我可能会不太懂。
要是不用,我可能,有启发,矛盾中Top
10 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-02 21:29:33 得分 0
那我试试自己做链表实现吧,不一定能写出来,明天给你Top
11 楼zhengsy()回复于 2005-04-02 21:35:17 得分 0
明天白天行吗?晚上,要上课Top
12 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-02 21:38:20 得分 0
我尽量吧Top
13 楼zhengsy()回复于 2005-04-02 21:42:08 得分 0
嗯Top
14 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 01:50:06 得分 99
#include <iostream>
#include <vector>
using namespace std;
class MyInt
{
friend void Display(const MyInt& iM);
public:
MyInt(){}
//返回下标为i的值,若大于等于大整数位数,则返回'0'
char operator[](int i) const
{
if (i<cvec.size())
return cvec[i];
else
return '0';
}
MyInt(const char *ch)
{
while(*ch!='\0')
{
reverse(cvec.begin(),cvec.end());
cvec.push_back(*ch);
reverse(cvec.begin(),cvec.end());
++ch;
}
}
MyInt& operator=(const MyInt &iM)
{
cvec=iM.cvec;
return *this;
}
void absPlus(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
int n=max(sz1,sz2);
int carry=0;//保存进位
for (int i=0;i<n;++i)
{
char c=rhs[i];
char c2=(*this)[i];
int tmp=((*this)[i]-48)+(rhs[i]-48)+carry;
carry=tmp/10;
if (i>=cvec.size())
{
reverse(cvec.begin(),cvec.end());
cvec.push_back(tmp%10+48);
reverse(cvec.begin(),cvec.end());
}
else
cvec[i]=tmp%10+48;
}
if (carry>=1)
{
cvec.push_back(carry+48);
}
}
void absSub(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
int n=max(sz1,sz2);
int carry=0;//保存借位
for (int i=0;i<n;++i)
{
int tmp=(cvec[i]-48)-(rhs[i]-48)+carry;
if (tmp<0)
{
tmp=tmp=(cvec[i]-48)-(rhs[i]-48)+carry+10;
carry=-1;
}
else
carry=0;
cvec[i]=tmp+48;
}
//去掉高位多余的0
int sz=cvec.size();
while(cvec[sz-1]=='0')
{
cvec.pop_back();
--sz;
}
}
bool operator<(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
//异号
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]!='-')
return true;
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]=='-')
return false;
//同号
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]!='-')
{
if (sz1<sz2) return true;
if (sz1==sz2)
{
int i=sz1-1;
while(i>=0)
{
if (cvec[i]<rhs.cvec[i]) return true;
--i;
}
return false;//相等
}
if (sz1>sz2) return false;
}
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]=='-')
{
if (sz1<sz2) return false;
if (sz1==sz2)
{
int i=sz1-1;
while(i>=0)
{
if (cvec[i]>rhs.cvec[i]) return true;
--i;
}
return false;//相等
}
if (sz1>sz2) return true;
}
}
MyInt& operator+=(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
//同号
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]!='-')
{
this->absPlus(rhs);
return *this;
}
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]=='-')
{
cvec.pop_back();
MyInt tmp=rhs;
tmp.cvec.pop_back();
this->absPlus(tmp);
cvec.push_back('-');
return *this;
}
//异号
if (*this<rhs)
{
cvec.pop_back();
if (*this<rhs)
{
MyInt tmp=rhs;
tmp.absSub(*this);
*this=tmp;
}
else
{
absSub(rhs);
cvec.push_back('-');
}
}
else
{
MyInt tmp=rhs;
tmp.cvec.pop_back();
if (*this<tmp)
{
tmp.absSub(*this);
*this=tmp;
cvec.push_back('-');
}
else
{
absSub(tmp);
}
}
}
MyInt& operator-=(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
//同号
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]!='-')
{
if(*this<rhs)
{
MyInt tmp=rhs;
tmp.cvec.push_back('-');
(*this)+=tmp;
}
else
absSub(rhs);
return *this;
}
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]=='-')
{
MyInt tmp=rhs;
tmp.cvec.pop_back();
(*this)+=tmp;
return *this;
}
//异号
if (*this<rhs)
{
MyInt tmp=rhs;
tmp.cvec.push_back('-');
(*this)+=tmp;
}
else
{
MyInt tmp=rhs;
tmp.cvec.pop_back();
absPlus(tmp);
}
}
private:
vector<char> cvec;
};
void Display(const MyInt& iM)
{
vector<char>::const_reverse_iterator it=iM.cvec.rbegin();
while(it!=iM.cvec.rend())
{
cout<<*it;
++it;
}
//cout<<endl;
}
class ComplexInt
{
friend void Display(const ComplexInt& ri);
public:
ComplexInt(const char* ch1,const char* ch2="0")
:real(ch1),imag(ch2){}
ComplexInt& operator=(const ComplexInt &rhs)
{
real=rhs.real;
imag=rhs.imag;
return *this;
}
ComplexInt& operator+=(const ComplexInt &rhs)
{
real+=rhs.real;
imag+=rhs.imag;
return *this;
}
ComplexInt& operator-=(const ComplexInt &rhs)
{
real-=rhs.real;
imag-=rhs.imag;
return *this;
}
MyInt getReal(){return real;}
MyInt getImag(){return imag;}
private:
MyInt real;
MyInt imag;
};
void Display(const ComplexInt& ri)
{
Display(ri.real);
cout<<'+';
Display(ri.imag);
cout<<'i'<<endl;
}
int main(int argc, char* argv[])
{
MyInt iM("1234567890123"); // 整数对象初始化
MyInt iN("-3210987654321");
MyInt iResult1;
iResult1 = iM;
iResult1 += iN; // 整数加法操作
MyInt iResult2 = iM;
iResult2 -= iN; // 整数减法操作
// 结果输出
Display(iResult1); // iResult1: -1976419764198
cout<<endl;
Display(iResult2); // iResult2: 4445555544444
cout<<endl;
// 实部: 1234567890123;虚部: 4445555544444
ComplexInt ciM("1234567890123", "4445555544444"); // 复数对象初始化
// 实部: -4445555544444;虚部: 0
ComplexInt ciN("-4445555544444"); // 复数对象初始化
ComplexInt ciResult1 = ciM;
ComplexInt ciResult2 = ciM;
ciResult1 += ciN; // 复数加法操作
ciResult2 -= ciN; // 复数减法操作
// 结果输出
Display(ciResult1);
Display(ciResult2);
system("pause");
}
Top
15 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 01:51:12 得分 0
我用vector写的,在DevCpp下编译通过,先做这一半吧,去睡觉Top
16 楼zhousqy(标准C匪徒)(甩拉,甩拉)回复于 2005-04-03 06:26:32 得分 0
markTop
17 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 11:22:52 得分 0
T_T,搞不定自定义链表的方式了Top
18 楼zhengsy()回复于 2005-04-03 12:52:56 得分 0
什么是devcpp啊?
如果,我们规定用win32 sole……怎么办呀?Top
19 楼nasi00(莫傲·逍遥)回复于 2005-04-03 13:04:59 得分 0
devcpp也是一个IDE,和VC差不多,不过是用gcc的。
其实用VC也是一样的。Top
20 楼zhengsy()回复于 2005-04-03 13:10:09 得分 0
那如果我在vc的环境下,想要用venctor怎么办,刚才我试了一下,不行嘛!Top
21 楼zhengsy()回复于 2005-04-03 13:26:44 得分 0
要是,我不用venctor可以怎么修改呢?Top
22 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 13:52:35 得分 0
这个在VC里可以运行
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class MyInt
{
friend void Display(const MyInt& iM);
public:
MyInt(){}
//返回下标为i的值,若大于等于大整数位数,则返回'0'
char operator[](int i) const
{
if (i<cvec.size())
return cvec[i];
else
return '0';
}
MyInt(const char *ch)
{
while(*ch!='\0')
{
reverse(cvec.begin(),cvec.end());
cvec.push_back(*ch);
reverse(cvec.begin(),cvec.end());
++ch;
}
}
MyInt& operator=(const MyInt &iM)
{
cvec=iM.cvec;
return *this;
}
void absPlus(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
int n=(sz1<sz2)?sz1:sz2;
int carry=0;//保存进位
for (int i=0;i<n;++i)
{
char c=rhs[i];
char c2=(*this)[i];
int tmp=((*this)[i]-48)+(rhs[i]-48)+carry;
carry=tmp/10;
if (i>=cvec.size())
{
reverse(cvec.begin(),cvec.end());
cvec.push_back(tmp%10+48);
reverse(cvec.begin(),cvec.end());
}
else
cvec[i]=tmp%10+48;
}
if (carry>=1)
{
cvec.push_back(carry+48);
}
}
void absSub(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
int n=(sz1<sz2)?sz1:sz2;
int carry=0;//保存借位
for (int i=0;i<n;++i)
{
int tmp=(cvec[i]-48)-(rhs[i]-48)+carry;
if (tmp<0)
{
tmp=tmp=(cvec[i]-48)-(rhs[i]-48)+carry+10;
carry=-1;
}
else
carry=0;
cvec[i]=tmp+48;
}
//去掉高位多余的0
int sz=cvec.size();
while(cvec[sz-1]=='0')
{
cvec.pop_back();
--sz;
}
}
bool operator<(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
//异号
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]!='-')
return true;
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]=='-')
return false;
//同号
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]!='-')
{
if (sz1<sz2) return true;
if (sz1==sz2)
{
int i=sz1-1;
while(i>=0)
{
if (cvec[i]<rhs.cvec[i]) return true;
--i;
}
return false;//相等
}
if (sz1>sz2) return false;
}
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]=='-')
{
if (sz1<sz2) return false;
if (sz1==sz2)
{
int i=sz1-1;
while(i>=0)
{
if (cvec[i]>rhs.cvec[i]) return true;
--i;
}
return false;//相等
}
if (sz1>sz2) return true;
}
}
MyInt& operator+=(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
//同号
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]!='-')
{
this->absPlus(rhs);
return *this;
}
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]=='-')
{
cvec.pop_back();
MyInt tmp=rhs;
tmp.cvec.pop_back();
this->absPlus(tmp);
cvec.push_back('-');
return *this;
}
//异号
if (*this<rhs)
{
cvec.pop_back();
if (*this<rhs)
{
MyInt tmp=rhs;
tmp.absSub(*this);
*this=tmp;
}
else
{
absSub(rhs);
cvec.push_back('-');
}
}
else
{
MyInt tmp=rhs;
tmp.cvec.pop_back();
if (*this<tmp)
{
tmp.absSub(*this);
*this=tmp;
cvec.push_back('-');
}
else
{
absSub(tmp);
}
}
}
MyInt& operator-=(const MyInt &rhs)
{
int sz1=cvec.size();
int sz2=rhs.cvec.size();
//同号
if (cvec[sz1-1]!='-' && rhs.cvec[sz2-1]!='-')
{
if(*this<rhs)
{
MyInt tmp=rhs;
tmp.cvec.push_back('-');
(*this)+=tmp;
}
else
absSub(rhs);
return *this;
}
if (cvec[sz1-1]=='-' && rhs.cvec[sz2-1]=='-')
{
MyInt tmp=rhs;
tmp.cvec.pop_back();
(*this)+=tmp;
return *this;
}
//异号
if (*this<rhs)
{
MyInt tmp=rhs;
tmp.cvec.push_back('-');
(*this)+=tmp;
}
else
{
MyInt tmp=rhs;
tmp.cvec.pop_back();
absPlus(tmp);
}
}
private:
vector<char> cvec;
};
void Display(const MyInt& iM)
{
vector<char>::const_reverse_iterator it=iM.cvec.rbegin();
while(it!=iM.cvec.rend())
{
cout<<*it;
++it;
}
//cout<<endl;
}
class ComplexInt
{
friend void Display(const ComplexInt& ri);
public:
ComplexInt(const char* ch1,const char* ch2="0")
:real(ch1),imag(ch2){}
ComplexInt& operator=(const ComplexInt &rhs)
{
real=rhs.real;
imag=rhs.imag;
return *this;
}
ComplexInt& operator+=(const ComplexInt &rhs)
{
real+=rhs.real;
imag+=rhs.imag;
return *this;
}
ComplexInt& operator-=(const ComplexInt &rhs)
{
real-=rhs.real;
imag-=rhs.imag;
return *this;
}
MyInt getReal(){return real;}
MyInt getImag(){return imag;}
private:
MyInt real;
MyInt imag;
};
void Display(const ComplexInt& ri)
{
Display(ri.real);
cout<<'+';
Display(ri.imag);
cout<<'i'<<endl;
}
int main(int argc, char* argv[])
{
MyInt iM("1234567890123"); // 整数对象初始化
MyInt iN("-3210987654321");
MyInt iResult1;
iResult1 = iM;
iResult1 += iN; // 整数加法操作
MyInt iResult2 = iM;
iResult2 -= iN; // 整数减法操作
// 结果输出
Display(iResult1); // iResult1: -1976419764198
cout<<endl;
Display(iResult2); // iResult2: 4445555544444
cout<<endl;
// 实部: 1234567890123;虚部: 4445555544444
ComplexInt ciM("1234567890123", "4445555544444"); // 复数对象初始化
// 实部: -4445555544444;虚部: 0
ComplexInt ciN("-4445555544444"); // 复数对象初始化
ComplexInt ciResult1 = ciM;
ComplexInt ciResult2 = ciM;
ciResult1 += ciN; // 复数加法操作
ciResult2 -= ciN; // 复数减法操作
// 结果输出
Display(ciResult1);
Display(ciResult2);
return 0;
}
Top
23 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 13:54:30 得分 0
如果不用vector,你需要自己构建链表,完成大整数的每一位的插入,删除等Top
24 楼zhengsy()回复于 2005-04-03 14:03:57 得分 0
fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.
怎么回事啊?Top
25 楼zhengsy()回复于 2005-04-03 14:10:12 得分 0
在win32console下, 我怎么才可以运行呢Top
26 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 14:22:45 得分 0
新建一个空的win32console工程,在里面新建一个c++源文件,然后拷入代码,运行Top
27 楼zhengsy()回复于 2005-04-03 14:31:23 得分 0
不行嘛。出来的就是fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.
Top
28 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-04-03 14:34:46 得分 0
留下QQTop
29 楼zhengsy()回复于 2005-04-03 14:36:34 得分 0
52742784Top




