加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用字符串实现大数的相加,没有考虑存在负数的情况

发布时间:2020-12-14 04:01:36 所属栏目:大数据 来源:网络整理
导读:题目:用字符串实现大数的相加,没有考虑存在负数的情况 ? ? Code(C): ? #includestdio.h #includestring.h void add(char *s1,char *s2,char *s); void main() { ?char s1[100]; ?char s2[100]; ?char s[100] = {'0'}; ?printf("请输入第一个数字n"); ?get

题目:用字符串实现大数的相加,没有考虑存在负数的情况

?

?

Code(C):

?

#include<stdio.h>
#include<string.h>

void add(char *s1,char *s2,char *s);

void main()
{
?char s1[100];
?char s2[100];
?char s[100] = {'0'};
?printf("请输入第一个数字n");
?gets(s1);
?printf("请输入第二个数字n");
?gets(s2);
?add(s1,s2,s);
?printf("%s+%s = %sn",s1,s);
}

void add(char *s1,char *s) { ?int len1,len2; ?int flag = 0,i1,i2,i; ?len1 = strlen(s1); ?len2 = strlen(s2); ?i1 = len1-1; ?i2 = len2-1; ?i = (len1 > len2 ? len1:len2)+1; ?s[i] = ''; ?i--; ?while(i1 >= 0 && i2 >= 0) ?{ ??int temp = s1[i1]-'0'+s2[i2]-'0'; ??if(flag == 1) ???temp++; ??if(temp >= 10) ??{ ???flag = 1; ???temp -= 10; ??} ??s[i] = temp+'0'; ??i--; ??i1--; ??i2--; ?} ?if(i2 >=0) ?{ ??while(i2 >= 0) ??{ ???int temp = s2[i2]-'0'; ???if(flag == 1) ???{ ????temp++; ???} ???if(temp >= 10) ???{ ????flag = 1; ????temp -= 10; ???} ???s[i] = temp+'0'; ???i--; ???i2--; ??} ?} ?? else if(i1 >=0) ?{ ??while(i1 >= 0) ??{ ???int temp = s1[i1]-'0'; ???if(flag == 1) ???{ ????temp++; ???} ???if(temp >= 10) ???{ ????flag = 1; ????temp -= 10; ???} ???s[i] = temp+'0'; ???i--; ???i1--; ??} ?} ?if(flag == 1) ??s[i] = '1'; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读