C-study/07-1/4/add.c

80 lines
1.1 KiB
C
Raw Permalink Normal View History

2021-12-06 15:40:38 +00:00
#include<stdio.h>
#include<string.h>
int add(char a, char b,int jinwei){
return a-48+b-48+jinwei;
}
int str_move(char a[80],int n){
for(int i=0;i<n-1;i++){
a[i] = a[i+1];
}
return n-1;
}
void str_extend(char a[80],int n){
for(int i=n-1;i>=0;i--){
a[i+1] = a[i];
}
a[n+1] = '\0';
a[0] = '0';
}
int main(){
char a[90],b[90],c[90];
int n,n_,max,max_,temp,jinwei=0;
scanf("%s %s", a, b);
n=strlen(a);
n_=strlen(b);
for(int i=0;i<n;i++){
if(a[0]!='0') break;
if(a[0]=='0'){
n=str_move(a,n);
continue;
}
}
for(int i=0;i<n_;i++){
if(b[0]!='0') break;
if(b[0]=='0'){
n=str_move(b,n);
continue;
}
break;
}
max_ = max = n;
if(n_>n) {
max_ = max = n_;
while(1){
str_extend(a,n);
n=strlen(a);
if(n==n_) break;
}
}
if(n>n_) {
while(1){
str_extend(b,n_);
n_=strlen(b);
if(n==n_) break;
}
}
if(add(a[0],b[0],0)>9) {
max_++;
c[max] = '\0';
c[0] = '1';
}
for(int i=0;i<max;i++){
temp=add(a[max-1-i],b[max-1-i],jinwei);
if(temp>9) jinwei = 1;
else jinwei = 0;
c[max_-1-i] = temp%10 + 48;
}
for(int i=0;i<max_;i++){
printf("%c",c[i]);
}
return 0;
}