Last active
March 11, 2020 13:08
-
-
Save ggzeng/83c7ccb294bdac1a82f4355759fcf294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#define MAX 100000 | |
typedef struct | |
{ | |
int arr[MAX]; | |
}NUMBER; | |
/* 读取数字字符串,并转为int数组保存 */ | |
void read(NUMBER *add_num) | |
{ | |
int i,digit=0; | |
char ch[101]; | |
scanf("%s",ch); | |
while(ch[digit]) | |
digit++; | |
for(i=0;i < MAX;i++) | |
{ | |
digit--; | |
if(digit >= 0) | |
add_num->arr[i]=ch[digit]-'0'; | |
else | |
add_num->arr[i]=0; | |
} | |
} | |
/* 两个int数组从低为开始相机,注意累加 carry 值 */ | |
void addition(NUMBER a,NUMBER b,NUMBER *add_res) | |
{ | |
int carry=0; | |
int i,temp; | |
for(i=0;i < MAX;i++) | |
{ | |
temp=a.arr[i]+b.arr[i]+carry; | |
add_res->arr[i]=temp % 10; | |
carry=temp / 10; | |
} | |
} | |
/* 从高为开始答应,最终的结果数组 */ | |
void print(NUMBER *add_num) | |
{ | |
int i; | |
for(i=MAX-1;add_num->arr[i]==0;i--); | |
for(;i>=0;i--) | |
printf("%d",add_num->arr[i]); | |
} | |
int main() | |
{ | |
NUMBER x,y,z; | |
printf("enter two positive integers: \n"); | |
read(&x); | |
read(&y); | |
printf("addition result: "); | |
addition(x,y,&z); | |
print(&z); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment