用c语言编程完成两个1000位以内的正整数的加法运算

问题描述:

用c语言编程完成两个1000位以内的正整数的加法运算
是c语言!
我找到了一段代码 能正确执行出来 但不是很能看懂
#include
#include
#include
#include
void main()
{
char longnumber[1000];
void lturntoa(char*,int*);
int numa[125]={0},numb[125]={0},result[125]={0},i;
printf("input the first long number:\n");
gets(longnumber);
lturntoa(longnumber,numa);
printf("input the second long number:\n");
gets(longnumber);
lturntoa(longnumber,numb);
for(i=0;(*(numa+i)!=0)&&(*(numb+i)!=0);i++);
for(;i=100000000)
{
*(result+i)-=100000000;
*(result+i-1)=1;
}
}
printf("result is:\n");
for(i=0;*(result+i)==0;i++);
for(;i
1个回答 分类:综合 2014-11-20

问题解答:

我来补答
#include <stdio.h>
#include <string.h>
#include <conio.h>

int main() {
int i,j,k,T,carry;
int lena,lenb,num;
char ch,c[1000] = {0};
char a[1000],b[1000];
scanf("%d",&T);
while(T > 0) {
i = 0;
printf("\na[] = ");
while(1) { // 读取a[],'\n'结束读入过程
fflush(stdin);
ch = _getch();
if(ch == '\r') break;
if(ch >= '0' && ch <= '9') {
a[i++] = ch;
printf("%c",ch);
}
}
a[i] = '\0';
printf("\nb[] = ");
i = 0;
while(1) { // 读取b[],'\n'结束读入过程
fflush(stdin);
ch = _getch();
if(ch == '\r') break;
if(ch >= '0' && ch <= '9') {
b[i++] = ch;
printf("%c",ch);
}
}
b[i] = '\0';
lena = strlen(a);
lenb = strlen(b);
carry = 0; // 进位
k = 0;
// 从个位开始加,逆向存储相加结果
for(i = lena - 1,j = lenb - 1; i >= 0 && j >= 0; --i,--j) {
num = a[i] + b[j] - '0' - '0' + carry; // 求得该位的和
c[k++] = num % 10 + '0'; // 获取该位数字
carry = num / 10; // 获取进位
}
while(i >= 0) { // 处理更长的数
num = a[i--] + carry - '0';
c[k++] = num % 10 + '0';
carry = num / 10;
}
while(j >= 0) { // 处理更长的数
num = a[j--] + carry - '0';
c[k++] = num % 10 + '0';
carry = num / 10;
}
if(carry) c[k++] = carry + '0';
c[k] = '\0';
for(i = 0;i < k / 2;i++) { // 把计算结果改作惯用顺序
ch = c[i];
c[i] = c[k - 1 - i];
c[k - 1 - i] = ch;
}
printf("\n%s + %s = %s\n",a,b,c);
--T;
}
return 0;
}
 
 
展开全文阅读
剩余:2000
上一页:ghhhhh
下一页:概括每段段意
也许感兴趣的知识