您的位置:首页 > 其它

online_judge_1037

2015-12-12 13:43 337 查看
这道题目我也是被自己搞醉了。弄了一上午。代码有三百行。仅仅是上交一道考研机试题。利用封装思想写大整数类。。AC之后很激动,,这期间还有很多C++运算符重载细节忘记了,只能说自己不熟练。联系太少。这个例子告诉我,读源码是多么的重要……

每当我们把API用的不亦乐乎的时候,是否该思考这些API是如何实现的呢。。看来得多看看源码才能有较大进步啊!

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
const int maxSize = 205;
struct bigInteger
{
int digit[maxSize];
int sizes;
bool flag;
bigInteger()
{
for(int i=0;i<maxSize;++i)
digit[i] = 0;
sizes = 0;
}
void setInteger(string s)
{
if(s[0] == '-')
flag = true;
else
flag = false;
int len = s.size();
sizes = 0;
int t = flag;
for(int i=len-1; i>=t; i -= 4)
{
digit[sizes] = s[i] - '0';
if(i-1 >= t)
{
digit[sizes] += (s[i-1] - '0')*10;
}
if(i-2 >= t)
{
digit[sizes] += (s[i-2] - '0')*100;
}
if(i-3 >= t)
{
digit[sizes] += (s[i-3] - '0')*1000;
}
sizes++;
}
while(digit[sizes-1] == 0)
sizes--;
if(sizes < 0)
sizes = 0;
}
void opposite()
{
this->flag = !this->flag;
}
bool operator > (const bigInteger &b) const
{
if(sizes > b.sizes)
return true;
else if(sizes < b.sizes)
return false;
for(int i=sizes-1; i>=0; --i)
{
if(digit[i] > b.digit[i])
return true;
else if(digit[i] < b.digit[i])
return false;
}
return true;
}
bool operator < (const bigInteger &b) const
{
return !(*this > b);
}
bool operator == (const bigInteger &b) const
{
if(sizes != b.sizes)
return false;
for(int i=sizes-1; i>=0; --i)
{
if(digit[i] != b.digit[i])
return false;
}
return true;
}
bigInteger operator + (const bigInteger &b)
{
int sizeb = b.sizes;
int i;
int value = 0;
bigInteger sum;
for(i=0; i<sizes&&i<sizeb; ++i)
{
sum.digit[i] = (digit[i] + b.digit[i] + value)%10000;
value = (digit[i] + b.digit[i] + value)/10000;
sum.sizes++;
}
while(i<sizeb)
{
sum.digit[i] = (b.digit[i] + value)%10000;
value = (b.digit[i] + value)/10000;
i++;
sum.sizes++;
}
while(i<sizes)
{
sum.digit[i] = (digit[i] + value)%10000;
value = (digit[i] + value)/10000;
i++;
sum.sizes++;
}
sum.flag = true;
return sum;
}
bigInteger operator - (const bigInteger &b)
{
int sizeb = b.sizes;
int i,j;
bigInteger sub;
for(i=0; i<sizeb; ++i)
{
if(digit[i] < b.digit[i])
{
j = i+1;
while(digit[j] == 0)
{
digit[j] = 9999;
j++;
}
digit[j]--;
digit[i] += 10000;
}
sub.digit[i] = digit[i] - b.digit[i];
sub.sizes++;
}
while(i<sizes)
{
sub.digit[i] = digit[i];
i++;
sub.sizes++;
}
while(sub.digit[sub.sizes-1] == 0)
{
sub.sizes--;
}
return sub;
}
bigInteger operator * (bigInteger b)
{
bigInteger muilt;
bigInteger temp;
int sizeb = b.sizes;
int i,j,k;
int value;
for(i=0; i<sizes; ++i)
{
value = 0;
k = 0;
for(j=0; j<sizeb; k++,++j)
{
temp.digit[k] = (b.digit[j] * digit[i] + value)%10000;
value = (b.digit[j] * digit[i] + value)/10000;
}
temp.sizes = k;
if(value != 0)
temp.digit[temp.sizes++] = value;
muilt = muilt + temp;
for(j=b.sizes; j>0; --j)
{
b.digit[j] = b.digit[j-1];
}
b.digit[j] = 0;
sizeb = ++b.sizes;
}
while(muilt.digit[muilt.sizes-1] == 0)
{
muilt.sizes--;
}
return muilt;
}
friend ostream & operator << (ostream &os, bigInteger &c)
{
int i;
os<<c.digit[c.sizes-1];
for(i=c.sizes-2; i>=0; i--)
os<<setfill('0')<<setw(4)<<c.digit[i];
os<<endl;
return os;
}
void output()
{
int i;
cout<<digit[sizes-1];
for(i=sizes-2; i>=0; i--)
cout<<setfill('0')<<setw(4)<<digit[i];
cout<<endl;
}

};

int main()
{
char s1[405],s2[405];
bigInteger a,b,c;
while(cin>>s1>>s2)
{
a.setInteger(s1);
b.setInteger(s2);
if((!a.flag)&&(!b.flag))
{
c = a+b;
cout<<c;

if(a==b)
{
cout<<0<<endl;
}
else if(a>b)
{
c = a-b;
cout<<c;
}
else
{
c = b-a;
cout<<'-'<<c;
}
c = a*b;
cout<<c;
}
else if((a.flag)&&(!b.flag))
{
if(a==b)
{
cout<<0<<endl;

c = a+b;
cout<<'-'<<c;
}
else if(a>b)
{
c = b+a;
cout<<'-'<<c;

c = a+b;
cout<<'-'<<c;
}
else
{
c = b-a;
cout<<c;

c = a+b;
cout<<'-'<<c;
}
c = a*b;
cout<<'-'<<c;
}
else if((!a.flag)&&b.flag)
{
if(a==b)
{
cout<<0<<endl;

c = a+b;
cout<<c;
}
else if(a<b)
{
c = b-a;
cout<<'-'<<c;

c = a+b;
cout<<c;
}
else
{
c = a-b;
cout<<c;

c = a+b;
cout<<c;
}
c = a*b;
cout<<'-'<<c;
}
else
{
c = a+b;
cout<<'-'<<c;
if(a==b)
{
cout<<0<<endl;
}
else if(a<b)
{
c = b-a;
cout<<c;
}
else
{
c = a-b;
cout<<'-'<<c;
}
c = a*b;
cout<<c;
}
}
return 0;
}


还是老习惯,没有注释。但是这道题目写代码之前做了大量思考,基本上把细节都涵盖了。所以后期没什么问题……C++的好多语法知识还是不熟练。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: