```
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define N 100
void nizhi_str(char str[])
{
int len;
int i;
char temp;
len = strlen(str);
for (i = 0; i < len / 2; i++)
{
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
void sum(char str1[], char str2[], char result[])
{
//逆置
nizhi_str(str1);
nizhi_str(str2);
int len1, len2;
int i;
int jinwei = 0;
int temp;
len1 = strlen(str1);
len2 = strlen(str2);
for (i = 0; i < len1 && i < len2; i++)
{
temp = (str1[i] - '0' + str2[i] - '0') % 10;
result[i] = temp + jinwei + '0';
if (str1[i] - '0' + str2[i] - '0' >= 10)
{
jinwei = 1;
}
else
{
jinwei = 0;
}
}
if (i < len1)
{
for (; i < len1; i++)
{
temp = (str1[i] - '0') % 10;
result[i] = temp + jinwei + '0';
if (str1[i] - '0' >= 10)
{
jinwei = 1;
}
else
{
jinwei = 0;
}
}
}
if (i < len2)
{
for (; i < len2; i++)
{
temp = (str2[i] - '0') % 10;
result[i] = temp + jinwei + '0';
if (str2[i] - '0' >= 10)
{
jinwei = 1;
}
else
{
jinwei = 0;
}
}
}
if (jinwei == 1)
{
result[i++] = '1';
}
result[i] = '\0';
nizhi_str(result);
}
void main()
{
char str1[100] = { 0 };
char str2[100] = { 0 };
char result[100];
printf("input number1:\n");
gets(str1);
printf("input number2:\n");
gets(str2);
printf("number1 = %s\n", str1);
printf("number2 = %s\n", str2);
sum(str1, str2, result);
printf("result = %s\n", result);
system("pause");
}
C 大整数加法计算实现
最新推荐文章于 2023-03-11 17:55:22 发布