1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
| //高精度运算
#include<iostream>
#include<string.h>
using namespace std;
struct bign{
int d[1000];
int len;
bign(){
memset(d,0,sizeof(d));
len=0;
}
};
bign change(char str[]){//将str的整数转为bign
bign a;
a.len=strlen(str);
for(int i=0;i<a.len;i++){
a.d[i]=str[a.len-i-1]-'0';//逆着赋值
}
return a;
}
int compare(bign a,bign b){
if(a.len>b.len)return 1;//a大
else if(a.len<b.len)return -1;//a小
else {
for(int i=a.len-1;i>=0;i--){//因为从d的0坐标开始存
if(a.d[i]>b.d[i])return 1;
else if(a.d[i]<b.d[i])return -1;
}
return 0;//相等
}
}
bign add(bign a,bign b){
bign c;
int carry=0;
for(int i=0;i<a.len||i<b.len;i++){//以较长的作为界限
int temp=a.d[i]+b.d[i]+carry;
c.d[c.len++]=temp%10;
carry=temp/10;
}
if(carry!=0){
c.d[c.len++]=carry;//新增一位
}
return c;
}
bign sub(bign a,bign b){
bign c;
for(int i=0;i<a.len||i<b.len;i++){
if(a.d[i]<b.d[i]){
a.d[i+1]--;//不够减,向高位借
a.d[i]+=10;
}
c.d[c.len++]=a.d[i]-b.d[i];//计算结果储存起来
}
while(c.len-1>=1&&c.d[c.len-1]==0){
c.len--;//取出高为的0,并且保留最低位的0
}
return c;
}
bign multi(bign a,int b){
bign c;
int carry;//进位
for(int i=0;i<a.len;i++){
int temp=a.d[i]*b+carry;
c.d[c.len++]=temp%10;
carry=temp/10;
}
while(carry!=0){//乘法的进位可能不止一位
c.d[c.len++]=carry%10;
carry/=10;
}
return c;
}
bign divide(bign a,int b,int &r){//r为余数
bign c;
c.len=a.len;
for(int i=a.len-1;i>=0;i--){//从高为开始除
r=r*10+a.d[i];
if(r<b)c.d[i]=0;//不够除
else {//够除
c.d[i]=r/b;
r=r%b;
}
}
while(c.len-1>=1&&c.d[c.len-1]==0){
c.len--;
}
return c;
}
void print(bign a){
for(int i=a.len-1;i>=0;i--){
cout<<a.d[i];
}
cout<<endl;
}
int main(){
char str1[1000],str2[1000];
int r=0;
int c=-2;
scanf("%s",str1);
scanf("%s",str2);
bign a=change(str1);
bign b=change(str2);
cout<<"a是否小于b"<<compare(a,b)<<endl;
cout<<"a+b="; print(add(a,b));
cout<<"a-b="; if(compare(a,b)==1)print(sub(a,b));
else print(sub(b,a));
cout<<"a*c=";
if(c<0){
cout<<"-";print(multi(a,-c));
}else{
print(multi(a,c));
}
cout<<"a/c=";if(c<0){
cout<<"-";print(divide(a,-c,r));
}else{
print(divide(a,c,r));
}cout<<" 余数为:"<<r<<endl;
}
|