超长正整数相加

#include "string.h"
#include "stdio.h"
#include "stdlib.h"
void main()
{
char pcAdd[] = "99999999999999999999999";
char pcAug[] = "1";
char *pcResult;
unsigned int addlen = strlen(pcAdd);
unsigned int auglen = strlen(pcAug);
int maxlen;
int minlen;
if (addlen > auglen)
{
maxlen = addlen;
minlen = auglen;
}
else
{
maxlen = auglen;
minlen = addlen;
}
pcResult = (char*)malloc(maxlen + 1);
for (int i = 0; i < minlen; i++)
{
if (pcAdd[addlen - i -1] + pcAug[auglen - i -1] >= '9' + '1')
{
pcResult[maxlen - i - 1] = pcAdd[addlen - i - 1] + pcAug[auglen - i - 1] -'9' - '1' +'0';
pcResult[maxlen - i-2] = '1';
}
else
{
pcResult[maxlen - i - 1] = pcAdd[addlen - i - 1] + pcAug[auglen - i - 1] - '0';
}
}
for (int i = minlen; i < maxlen; i++)
{
pcResult[maxlen - i - 1] = pcAdd[addlen - i - 1] + pcResult[maxlen - i - 1];
if (pcResult[maxlen - i - 1] >= '9' + '1')
{
pcResult[maxlen - i - 1] = pcResult[maxlen - i - 1] - '9' - '1' + '0';
pcResult[maxlen - i-2] = '1';
}
}
if (pcResult[0] >= '9' + '1' || pcResult[0] == '0')
{
for (int i = 0; i < maxlen; i++)
{
pcResult[maxlen - i] = pcResult[maxlen - i - 1];
}
pcResult[0] = '1';
pcResult[maxlen + 1] = '\0';
}
if (strcmp(pcResult,"100000000000000000000000") == 0)
printf("yes");
return;
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值