代码改变世界

题目1037:Powerful Calculator

2013-08-12 14:38  心、静如止水  阅读(206)  评论(0编辑  收藏  举报

题目链接地址:http://ac.jobdu.com/problem.php?pid=1037

#include<stdio.h>
#include<string.h>
#define N 1000
struct bigint
{
int digit[N];
int size;
int symbol;
void Init()
{
for(int i=0;i<N;i++)
digit[i]=0;
size=0;
}
void Set(char ch[],int n)
{
Init();
if(ch[0]=='-')
{
symbol=1;
for(int i=n-1;i>0;i--)
{
digit[size++]=ch[i]-'0';
}
}
else
{
symbol=0;
for(int i=n-1;i>=0;i--)
{
digit[size++]=ch[i]-'0';
}
}
}
bool operator<(const bigint&A)const
{
if(size!=A.size)
return size<A.size;
else
{
for(int i=size-1;i>=0;i--)
{
if(digit[i]!=A.digit[i])
return digit[i]<A.digit[i];
}
}
return true;
}
bigint operator+(const bigint &A)const
{
bigint ret;ret.Init();int carry=0;
for(int i=0;i<size||i<A.size;i++)
{
int tmp=digit[i]+A.digit[i]+carry;
carry=tmp/10;
ret.digit[ret.size++]=tmp%10;
}
if(carry!=0)
ret.digit[ret.size++]=carry;
return ret;
}
bigint operator-(const bigint &A)const
{
bigint ret;ret.Init();int carry=0;
for(int i=0;i<size;i++)
{
int tmp=digit[i]-A.digit[i]-carry;
if(tmp<0)
{
tmp+=10;
carry=1;
}
else
{
carry=0;
}
ret.digit[ret.size++]=tmp;
}
if(ret.digit[ret.size-1]==0)
ret.size--;
return ret;
}
bigint operator*(const int x)const
{
bigint ret;ret.Init();int carry=0;
for(int i=0;i<size;i++)
{
int tmp=digit[i]*x+carry;
ret.digit[ret.size++]=tmp%10;
carry=tmp/10;
}
while(carry!=0)
{
ret.digit[ret.size++]=carry%10;
carry=carry/10;
}
return ret;
}
void Mul_10()
{
for(int i=size;i>=1;i--)
{
digit[i]=digit[i-1];
}
digit[0]=0;
size++;
}
void output()
{
char ch[1000];int len=0;
bool flag=true;
bool out=false;
if(symbol==1)
ch[len++]='-';
for(int i=size-1;i>=0;i--)
{
if(digit[i]==0&&flag)
{
size--;
continue;
}
else
{
flag=false;
ch[len++]=digit[i]+'0';
out=true;
}
}
if(out==false)
printf("0");
else
{
for(int i=0;i<len;i++)
printf("%c",ch[i]);
}
printf("\n");
}
}A,B,C;
bigint Addition(bigint a,bigint b)
{
bigint ret;ret.Init();
if(a.symbol==0&&b.symbol==0)
{
ret=a+b;
ret.symbol=0;
}
else if(a.symbol==0&&b.symbol==1)
{
if(a<b)
{	
ret=b-a;
ret.symbol=1;
}
else
{
ret=a-b;
ret.symbol=0;
}
}
else if(a.symbol==1&&b.symbol==0)
{
if(a<b)
{
ret=b-a;
ret.symbol=0;
}
else
{
ret=a-b;
ret.symbol=1;
}
}
else
{
ret=a+b;
ret.symbol=1;
}
return ret;
}
bigint Sub(bigint a,bigint b)
{
bigint ret;ret.Init();
if(a.symbol==0&&b.symbol==0)
{
if(b<a)
{
ret=a-b;
ret.symbol=0;
}
else
{
ret=b-a;
ret.symbol=1;
}
}
else if(a.symbol==0&&b.symbol==1)
{
ret=a+b;
ret.symbol=0;
}
else if(a.symbol==1&&b.symbol==0)
{
ret=a+b;
ret.symbol=1;
}
else
{
if(b<a)
{
ret=a-b;
ret.symbol=1;
}
else
{
ret=b-a;
ret.symbol=0;
}
}
return ret;
}
bigint Mul(bigint a,bigint b)
{
bigint ret;ret.Init();
bigint carry;carry.Set("1",1);
bigint tmp; 
for(int i=0;i<b.size;i++)
{
tmp=a*b.digit[i];
for(int j=0;j<i;j++)
tmp.Mul_10();
ret=tmp+ret;
}
ret.size=a.size+b.size;
if(a.symbol==b.symbol)
ret.symbol=0;
else
ret.symbol=1;
return ret;
}
char input1[500],input2[500];
int main()
{
while(scanf("%s",input1)!=EOF)
{
int len1=strlen(input1);
A.Set(input1,len1);
scanf("%s",input2);
int len2=strlen(input2);
B.Set(input2,len2);
C=Addition(A,B);
C.output();
C.Init();
C=Sub(A,B);
C.output();
C.Init();
C=Mul(A,B);
C.output();
C.Init();
}
return 0;
}