2 1 2 112233445566778899 998877665544332211
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
1#include <stdio.h>
2
3int main()
4{
5 int num, i, k; //num: The number of lines, i & j: loop counter
6 int lengtha = -1, lengthb = -1, lengthl, lengths; //The length of summand, the length of addend, the longer one, the shorter one
7 char a[1000], b[1000]; //summand, addend
8 char* c = new char[1000]; //result
9 char temp;
10 int rlt = 0, adrlt = 0; //result of a sigle digit, to carry to the next digit
11
12 scanf("%d", &num); //input the first line, the number of input lines
13 getchar();
14
15 for(k = 0; k < num; k++)
16 {
17 /* initialization */
18 lengtha = 0;
19 lengthb = 0;
20 for (i = 0; i < 1000; i++)
21 {
22 a[i] = '0';
23 b[i] = '0';
24 c[i] = '0';
25 }
26
27 /* get the input value */
28 for(i = 0; (temp = getchar()) != ' ' && i < 1000; i++) //summand until space
29 {
30 a[i] = temp;
31 lengtha++;
32 }
33 for(i = 0; (temp = getchar()) != '\n' && i < 1000; i++) //addend until '\n'
34 {
35 b[i] = temp;
36 lengthb++;
37 }
38
39 /* print the result format */
40 printf("Case %d:\n", k + 1);
41 for (i = 0; i < lengtha; i++)
42 {
43 printf("%c", a[i]);
44 }
45 printf(" + ");
46 for (i = 0; i < lengthb; i++)
47 {
48 printf("%c", b[i]);
49 }
50 printf(" = ");
51
52 /* find the longer one between summand and addend */
53 if(lengtha > lengthb)
54 {
55 lengths = lengthb;
56 lengthl = lengtha;
57 c = a;
58 }
59 else
60 {
61 lengths = lengtha;
62 lengthl = lengthb;
63 c = b;
64 }
65
66 /* add process */
67 for(i = 0; i <= lengths; i++)
68 {
69 rlt = 0;
70 rlt = (a[lengtha - i] - '0') + (b[lengthb - i] - '0') + adrlt;
71 if(rlt > 9)
72 {
73 rlt = rlt - 10; //if any result of digit needs to carry to the next digit
74 adrlt = 1;
75 }
76 else
77 adrlt = 0;
78 c[lengthl - i] = rlt + '0';
79 }
80 /* carrying for the highest digit of the shorter one */
81 if(adrlt == 1)
82 {
83 c[lengthl - lengths - 1] = (c[lengthl - lengths - 1] - '0') + 1 + '0';
84 }
85
86 /* print the result */
87 for (i = 0; i < lengthl; i++)
88 {
89 printf("%c", c[i]);
90 }
91 if(k != num - 1)
92 printf("\n\n");
93 else
94 printf("\n");
95 }
96}
Summary:
Solving this problem makes me absolutely overjoyed. You konw. for me, a newbie, to figure out such a nut witout any referance. Ok, let's get to business.
Besides the puzzling logic, the input-output format is still the key point in this problem. How to read and store the big decimal in a character array by digit? How to make the reading-terminal-condition?
Another gist is that how to make the additon when summand and addend don't have the same numbers of digits. So take the lengths of them respectively into account is very important.
When taking the addition, begin the whole process from the lowest digit, so you're suppose to iterate the arrays in a reverse order. At this time, the longer length and shorter one will be used for the for condition.
About the convertion of data types:
Int and char are actually of the same data type which is represented by a integer(emu as well). So when you use char a = (int) b, or int a = (char) b; a actually gets the ASCII value of b, not b itselt. That is to say will get 49 for (int)'1'.
To make '2' to 2,
int a = 1;
char b;
b = '0' + a;
or
char a = '1';
int b;
b = a - '0';