程序代码:
#include <stdio.h>
#include <math.h>
#include <tchar.h>
#include <string.h>
#define N 2001
#define M 1001
void AddResult(char *, char *);
int main(int argc, TCHAR *argv[])
{ int LineNum, i, j, k;
char Together[N];
char SepFirst[20][M], SepSecond[20][M];
scanf("%d", &LineNum);
getchar();
for (i = 0; i < LineNum; i++)
{
gets(Together);
for (j = 0; Together[j] != ' '; j++)
{
SepFirst[i][j] = Together[j];
}
SepFirst[i][j] = 0;
j++;
for (k = 0; Together[j] != '\0'; k++, j++)
{
SepSecond[i][k] = Together[j];
}
SepSecond[i][k] = 0;
}
putchar(10);
for (i = 0; i < LineNum; i++)
{
printf("Case %d:\n", i + 1);
for (j = 0; j < (int)strlen(SepFirst[i]); j++)
{
printf("%c", SepFirst[i][j]);
}
printf("+");
for (j = 0; j < (int)strlen(SepSecond[i]); j++)
{
printf("%c", SepSecond[i][j]);
}
printf("=");
AddResult(SepFirst[i], SepSecond[i]);
putchar(10);
}
return 0;
}
void AddResult(char *SepFirst, char *SepSecond)
{
int Result[M] = { 0 }, Assistant[M] = { 0 }, LenFirst, LenSecond, Lenth;
int i, j, CarryFlag = 0;
LenFirst = strlen(SepFirst);
LenSecond = strlen(SepSecond);
if (LenFirst <= LenSecond)
{
for (i = LenFirst - 1, j = 0; i >= 0; i--, j++)
{
Assistant[j] = (int)(SepFirst[i] - '0');
}
for (i = LenSecond - 1, j = 0; i >= 0; i--, j++)
{
Result[j] = (Assistant[j] + (int)(SepSecond[i] - '0') + CarryFlag) % 10;
CarryFlag = (Assistant[j] + (int)(SepSecond[i] - '0') + CarryFlag) / 10;
}
if (CarryFlag == 1)
{
Result[j] = CarryFlag;
Lenth = j;
}
else
Lenth = j - 1;
}
else
{
for (i = LenSecond - 1, j = 0; i >= 0; i--, j++)
{
Assistant[j] = (int)(SepSecond[i] - '0');
}
for (i = LenFirst - 1, j = 0; i >= 0; i--, j++)
{
Result[j] = (Assistant[j] + (int)(SepFirst[i] - '0') + CarryFlag) % 10;
CarryFlag = (Assistant[j] + (int)(SepFirst[i] - '0') + CarryFlag) / 10;
}
if (CarryFlag == 1)
{
Result[j] = CarryFlag;
Lenth = j;
}
else
Lenth = j - 1;
}
for (i = Lenth; i >= 0; i--)
{
printf("%d", Result[i]);
}
putchar(10);
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[
本帖最后由 有容就大 于 2012-2-25 21:36 编辑 ]