-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlarge-number.cpp
68 lines (57 loc) · 1.33 KB
/
large-number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <math.h>
#include <iostream>
#include <string.h>
using namespace std;
char* strrev( char* s )
{
char c;
char* s0 = s - 1;
char* s1 = s;
/* Find the end of the string */
while (*s1) ++s1;
/* Reverse it */
while (s1-- > ++s0)
{
c = *s0;
*s0 = *s1;
*s1 = c;
}
return s;
}
int main()
{
//read char array
char big_number[51] = "37107287533902102798797998220837590246510135740250";
char big_number2[51] = "46376937677490009712648124896970078050417018260538";
char ans[60] = {'0'};
char* big_number_1 = strrev(big_number);
char* big_number_2 = strrev(big_number2);
//get length
cout << strlen(big_number) << endl;
int carry = 0;
int number1;
int number2;
for(int i = 0;i < 50;i++)
{
cout << big_number_1[i] << endl;
number1 = big_number_1[i] - '0';
number2 = big_number_2[i] - '0';
int answer = number1 + number2 + carry;
cout << number1 << '+' << number2 << '=' << answer << endl;
if(answer > 9)
{
carry = 1;
answer = answer - 10;
}
else
{
carry = 0;
}
char aChar = '0' + answer;
ans[i] = aChar;
cout << ans[i] << endl;
}
cout << ans << endl;
cout << ans[0] << endl;
cout << strrev(ans) << endl;
}