大数相加C语言实现
自己先写了一个,但是在De上面根本就过不了,求大佬用C语言指点#include<stdio.h>
#include<string.h>
char add(char s1, char s2){//大数求和
char addResult={0};
int result = 0;
int len = 0;
int i = 0;
int len1 = strlen(s1);
int len2 = strlen(s2);
int maxLen = len1 > len2 ? len1 : len2;
int minLen = len1 < len2 ? len1 : len2;
/*printf("%d\n", len);
len = strlen(s1) > strlen(s2) ? strlen(s1) : strlen(s2);
printf("%d\n", len);*/
for(i=maxLen-1;i>maxLen-minLen;i--){
result = (s1+0) + (s2+0);
if(result>9){
addResult = ((result % 10)+'0')+1;
s1 = s1+'1';
}
}
if(result>10){
s1 += '1';
}
for(i=maxLen-minLen-1;i>=0;i--){
if((s1+0)>9){
addResult = (s1+0)%10;
s1 += '1';
}
else{
addResult = s1+0;
}
if((addResult+0)>9){
{
for(i=len;i>1;i--){
addResult = addResult;
addResult = (addResult+0)%10;
addResult = (addResult+0)/10;
}
}
}
printf("%s\n", addResult);
return 0;
}
int main()
{
char s1={0};
char s2={0};
scanf("%s%s", s1, s2);
add(s1, s2);
return 0;
} 一点注释都没,看的难受 抱歉,这个已经解决了,谢谢帮助!
ps.下次提问一定加上注释!
页:
[1]