ACM@BIT Another A+B Problem

Description

Calculate a+b

Input

Two integer a,b;(-10^1000<=a,b<=10^1000)

Output

Output a+b

Sample Input

1 2

Sample Output

3

   
     
1 #include < iostream >
2 #include < string >
3 #include < cstring >
4   using namespace std;
5
6   const int MAXNUM = 1002 ;
7 class BigNum {
8 public :
9 BigNum();
10 BigNum( const string & );
11 BigNum operator + (BigNum & );
12 void print() const ;
13 void setLen( int );
14 private :
15 char num[MAXNUM];
16 int len;
17 int flag; // 标记正负 ,0为正
18 };
19
20 BigNum::BigNum() {
21
22 }
23
24 BigNum::BigNum( const string & param) {
25 strcpy(num, param.c_str());
26 len = param.length();
27 for ( int i = 0 ; i < len / 2 ; i ++ ) {
28 char a = num[len - i - 1 ];
29 num[len - i - 1 ] = num[i];
30 num[i] = a;
31 }
32 if (num[len - 1 ] == ' - ' ) {
33 flag = 1 ;
34 } else {
35 flag = 0 ;
36 }
37 }
38
39 BigNum BigNum:: operator + (BigNum & param) {
40 BigNum sum;
41
42 int maxLen = (len - flag) > (param.len - param.flag) ? (len - flag)
43 : (param.len - param.flag);
44 setLen(maxLen);
45 param.setLen(maxLen); // 调整长度,使数字部分对齐
46
47 if (flag == param.flag) {
48 int singal = 0 ; // 进位
49 int i = 0 ;
50 for (; i < maxLen; i ++ ) {
51 sum.num[i] = (num[i] - 96 + param.num[i] + singal) % 10 + 48 ;
52 if (num[i] + param.num[i] - 96 + singal > 9 ) {
53 singal = 1 ;
54 } else {
55 singal = 0 ;
56 } // 设置进位
57 }
58 if (singal == 1 ) {
59 sum.num[i] = ' 1 ' ;
60 i ++ ;
61 } // 最高位进位
62 if (flag == 1 ) {
63 sum.num[i] = ' - ' ;
64 i ++ ;
65 } // 结果的符号
66 sum.len = i;
67 sum.flag = flag;
68 } else {
69 int flag1 = 0 ; // 标记绝对值大小
70 for ( int i = maxLen; i > 0 ; i -- ) {
71 if (num[i - 1 ] > param.num[i - 1 ]) {
72 flag1 = 1 ;
73 break ;
74 }
75 if (num[i - 1 ] < param.num[i - 1 ]) {
76 flag1 = - 1 ;
77 break ;
78 }
79 }
80 if (flag1 == 0 ) {
81 sum = BigNum( " 0 " );
82 }
83 if (flag1 == 1 ) {
84 sum.flag = flag;
85 int osingal = 0 ; // 标记借位
86 int Len = maxLen;
87 for ( int i = 0 ; i < maxLen; i ++ ) {
88 sum.num[i] = (num[i] + 10 - param.num[i] - osingal) % 10 + 48 ;
89 if (num[i] - param.num[i] - osingal < 0 ) {
90 osingal = 1 ;
91 } else {
92 osingal = 0 ;
93 }
94 }
95 for (; Len > 0 ; Len -- ) {
96 if (sum.num[Len - 1 ] != ' 0 ' )
97 break ;
98 }
99 if (sum.flag == 1 ) {
100 sum.num[Len] = ' - ' ;
101 Len ++ ;
102 }
103 sum.len = Len;
104 }
105 if (flag1 == - 1 ) {
106 sum.flag = param.flag;
107 int osingal = 0 ; // 标记借位
108 int Len = maxLen;
109 for ( int i = 0 ; i < maxLen; i ++ ) {
110 sum.num[i] = (param.num[i] + 10 - num[i] - osingal) % 10 + 48 ;
111 if (param.num[i] - num[i] - osingal < 0 ) {
112 osingal = 1 ;
113 } else {
114 osingal = 0 ;
115 }
116 }
117 for (; Len > 0 ; Len -- ) {
118 if (sum.num[Len - 1 ] != ' 0 ' )
119 break ;
120 }
121 if (sum.flag == 1 ) {
122 sum.num[Len] = ' - ' ;
123 Len ++ ;
124 }
125 sum.len = Len;
126 }
127 }
128 return sum;
129 }
130
131 void BigNum::print() const {
132 for ( int i = len; i > 0 ; i -- ) {
133 cout << num[i - 1 ];
134 }
135 cout << endl;
136 }
137
138 void BigNum::setLen( int param) { // 调整,使两数除符号位后长度相同
139 if (flag == 1 ) {
140 num[param] = ' - ' ;
141 }
142 for ( int i = len - flag; i < param; i ++ ) {
143 num[i] = ' 0 ' ;
144 }
145 }
146
147 int main( void ) {
148 string a, b;
149 cin >> a >> b;
150 if (a.length() > 1002 || b.length() > 1002 )
151 return 0 ;
152 BigNum add1(a), add2(b), temp;
153 temp = add1 + add2;
154 temp.print();
155 return 1 ;
156 }

大数加减法

你可能感兴趣的:(ACM)