题目描述
Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work. In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop. For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are: 20000000000000000 + 4000000000000000 = 24 000 000 000 000 000 20000000000000000 - 4000000000000000 = 16 000 000 000 000 000 20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000 Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need “24000000000000000” rather than 2.4×10^16. As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.
输入描述:
Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).
输出描述:
For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
输入例子:
20000000000000000
4000000000000000
输出例子:
24000000000000000
16000000000000000
80000000000000000000000000000000
#include<stdio.h>
#include<string.h>
#define MAX_SIZE 1000
#define DIVIDE_UNIT 10000//每个数组元素的阀值
int indexes;//用于高精度整数计算乘法时腾出尾数为零的个数,4个为一组,刚好是一个数组元素,减小计算量
struct bigInteger{
int data[MAX_SIZE];
int size;
void init(){//清零
for(int i=0;i<MAX_SIZE;i++)data[i]=0;
size=0;
}
void set(int x){//初始化
init();
do{
data[size++]=x%DIVIDE_UNIT;
x/=DIVIDE_UNIT;
}while(x);
}
bigInteger operator -(const bigInteger A){//高精度整形的减法
bigInteger result;
result.init();
bool pos=false;//结果是否为正
if(size>A.size )pos=true;//前者更大
else if(size<A.size) pos=false;//后者更大
else {//占的数组元素个数相等
int k=size-1;
while(k>=0 && data[k]==A.data[k])k--;//对应数组元素相等则向后比较
if(k==-1)pos=true;//表明两个数相等
else if(data[k]>A.data[k]) pos=true;//前者大
else pos=false;//后者大
}
int carry=0;//借位
int down_size=0;//相减后减少的数组元素个数
if(pos){//结果为正
for(int i=0;i<A.size || i<size;i++)
{
int tmp=data[i]-A.data[i]+carry;
if(tmp<0){//对应数组元素相减为负的计算
carry=-1;
result.data[result.size++]=DIVIDE_UNIT+tmp;
}
else {//对应数组元素相减为正的计算
carry=0;
result.data[result.size++]=tmp;
}
}
}
else{//结果为负
for(int j=0;j<A.size || j<size;j++)
{
int tmp=A.data[j]-data[j]+carry;
if(tmp<0){//对应数组元素相减为负的计算
carry=-1;
result.data[result.size++]=DIVIDE_UNIT+tmp;
}
else {//对应数组元素相减为正的计算
carry=0;
result.data[result.size++]=tmp;
}
}
}
for(int k=result.size-1;k>=0 && result.data[k]==0;k--)down_size++;//计算结果的size减少的个数
result.size-=down_size;//修正结果所占的数据元素的个数,把高位的全零清掉
if(pos==false)result.data[result.size-1]=0-result.data[result.size-1];//如果是负数,最高位数组元素取相反数
return result;
}
bigInteger operator +(const bigInteger A){//高精度整形的加法
bigInteger result;
result.init();
int carry=0;
for(int i=0;i<A.size || i<size;i++)
{
int tmp=data[i]+A.data[i]+carry;
result.data[result.size++]=tmp%DIVIDE_UNIT;
carry=tmp/DIVIDE_UNIT;
}
if(carry)result.data[result.size++]=carry;
return result;
}
bigInteger operator *(const bigInteger A){//大整型乘以大整型
bigInteger result;
result.set(0);//结果初始化为0
for(int i=0;i<A.size;i++) {
indexes=i;
result=result+(*this)*(A.data[i]);
}
indexes=0;//重置index
return result;
}
bigInteger operator *(const int x){//大整型乘以一个整数
bigInteger result;
result.init();
result.size=indexes;用于高精度整数计算乘法时腾出尾数为零的个数,4个为一组,刚好是一个数组元素,减小计算量
int carry=0;
for(int i=0;i<size;i++){
int tmp=data[i]*x+carry;
result.data[result.size++]=tmp%DIVIDE_UNIT;
carry=tmp/DIVIDE_UNIT;
}
if(carry)result.data[result.size++]=carry;
return result;
}
void output(){//输出数字
for(int i=size-1;i>=0;i--){
if(i!=size-1)printf("%04d",data[i]);
else printf("%d",data[i]);
}
printf("\n");
}
}num1,num2,a,b;
int main(){
char s1[MAX_SIZE];
char s2[MAX_SIZE];
int len1;
int len2;
bigInteger multi_res;//乘法的结果
bigInteger sub_res;//减法的结果
bigInteger add_res;//加法的结果
while(scanf("%s",s1)!=EOF){
scanf("%s",s2);
len1=strlen(s1);
len2=strlen(s2);
num1.set(0);
num2.set(0);
a.set(1);
b.set(1);
int max_len,min_len;
if(len1>len2)
for(int i=len1-1;i>=0;i--){//把数字1转化为高精度整形
num1=num1+a*(s1[i]-'0');
a=a*10;
}
for(int j=len2-1;j>=0;j--){//把数字2转化成高精度整形
num2=num2+b*(s2[j]-'0');
b=b*10;
}
add_res.init();
sub_res.init();
multi_res.init();
add_res=num1+num2;
sub_res=num1-num2;
multi_res=num1*num2;
add_res.output();
sub_res.output();
multi_res.output();
}
return 0;
}