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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| #include <iostream>
#include <cstring>
#include <cstdio> #include <cstdlib>
using namespace std;
void Trans(char *str_num1, char *str_num2, char *tempduf1, char *tempduf2) { int len_num1 = 0, len_num2 = 0, i = 0; while(str_num1[i] != '\0') { //查看字符串长度 len_num1++; i++; } i = 0; while(str_num2[i] != '\0') { len_num2++; i++; } tempduf1[0] = '0'; tempduf2[0] = '0'; if(len_num2 >= len_num1) { //补成相同长度 for(i=1; i <= (len_num2 - len_num1); i++) tempduf1[i] = '0'; for(i = len_num2 - len_num1 + 1; i <= len_num2; i++) tempduf1[i] = str_num1[i - (len_num2 - len_num1 + 1)]; for(i = 1; i <= len_num2; i++) tempduf2[i] = str_num2[i-1]; } else if(len_num2 < len_num1) { for (i = 1; i <= (len_num1 - len_num2); i++) tempduf2[i] = '0'; for(i = len_num1 - len_num2 + 1; i <= len_num1; i++) tempduf2[i] = str_num2[i - (len_num1 - len_num2 + 1)]; for(i = 1;i <= len_num1; i++) tempduf1[i] = str_num1[i-1]; } } void Add(char *tempduf1, char *tempduf2, char *result) { //加法 char buf1[100001] = {0}; char buf2[100001] = {0}; Trans(tempduf1,tempduf2,buf1,buf2); strcpy(tempduf1,buf1); strcpy(tempduf2,buf2); int i = 0, temp = 0, jinwei = 0, len = 0; while(tempduf1[i] != '\0') { len++; i++; } for(i = len-1; i >= 0; i--) { temp = (int)(tempduf1[i] + tempduf2[i] + jinwei - 96); if(temp >= 10) { temp = temp-10; jinwei = 1; } else jinwei = 0; result[i] = (char)(temp + 48); } } void ThrowAway_0(char *tempduf) { //去掉多余的0 char buf[100000] = {0}; int n = strlen(tempduf)-1, i = 0; while(i < n) { if(tempduf[i] != '0') break; else i++; } int Throw = i; for (i = 0;i <= n-Throw; i++) buf[i]= tempduf[i + Throw]; strcpy(tempduf, buf); } int Compare(char *tempduf1,char *tempduf2) { //减法比较大小 ThrowAway_0(tempduf1); ThrowAway_0(tempduf2); char buf1[100001] = {0}; char buf2[100001] = {0}; Trans(tempduf1,tempduf2,buf1,buf2); memset(tempduf1,0,100001); memset(tempduf1,0,100001); strcpy(tempduf1,buf1); strcpy(tempduf2,buf2); int ret = 1,count = 0; while(count < 100001) { int m = (int)tempduf1[count]-48; int n = (int)tempduf2[count] - 48; if(m == n) { count++; ret=0; } else if (m > n) { ret = 1; break; } else if(m < n) { ret = -1; break; } } return ret; } void Sub(char *tempbuf1, char *tempbuf2, char *result) // 大数相减 result = tempbuf1 - tempbuf2 { ThrowAway_0 (tempbuf1); ThrowAway_0 (tempbuf2); memset(result,0,200); char buf1[100001]={0}; char buf2[100001]={0}; Trans(tempbuf1,tempbuf2,buf1,buf2); memset(tempbuf1,0,100001); memset(tempbuf2,0,100001); strcpy(tempbuf1,buf1); strcpy(tempbuf2,buf2); int i=0; int temp=0; int jiewei=0; int len=0; int ret=1; while(tempbuf1[i]!='\0') // tempbuf1 和 tempbuf2 的长度相等 { len++; i++; } ret = Compare(tempbuf1,tempbuf2); if(ret==1) { for(i=len-1;i>=0;i--) { temp = (int)tempbuf1[i] - (int)tempbuf2[i] - jiewei; if (temp>=0) { result[i]=(char)(temp+48); jiewei=0; } else if (temp<0) { result[i]=(char)(temp+10+48); jiewei=1; } } ThrowAway_0 (result); } else if(ret==0) { memset(result,0,100001); result[0]='0'; } else if(ret==-1) { for(i=len-1;i>=0;i--) { temp = (int)tempbuf2[i] - (int)tempbuf1[i] - jiewei; if (temp>=0) { result[i]=(char)(temp+48); jiewei=0; } else if (temp<0) { result[i]=(char)(temp+10+48); jiewei=1; } } ThrowAway_0 (result); memset(buf1,0,100001); sprintf(buf1,"-%s",result); strcpy(result,buf1); }
} int main(){ char str1[100001]; char str2[100001]; char result[100001] = {0}; short result_flag = 0; cin >> str1 >> str2; if(str1[0 ]== '-' && str2[0] == '-') { str2[0] = str1[0] = '0'; Add(str1,str2,result); if(result[1] == '0') { cout << "-"; for(int i = 2; i < strlen(result); i++) { int temp = (int)(result[i] - '0'); cout << temp; } } else { cout << "-"; for(int i = 1; i < strlen(result); i++) { int temp = (int)(result[i] - '0'); cout << temp; } } } else if(str1[0] == '-' && str2[0] != '-') { str1[0] = '0'; int flag = Compare(str1,str2); Sub(str2,str1,result); /* if(flag != 0) { cout << "-"; for(int i = 1; i < strlen(result); i++) { cout << result[i]; } } else cout << "0"; }*/ cout << result; } else if(str1[0] != '-' && str2[0] == '-') { str2[0] = '0'; Sub(str1, str2, result); /*for(int i = 1;i < strlen(result); i++) { cout << "-"; cout << result[i]; } // cout << result << endl; }*/ cout << result; } else { Add(str1,str2,result); if(result[0] == '0') { for(int i=1; i< strlen(result); i++){ int temp = (int)(result[i] - '0'); cout << temp; } } else cout << result << endl; } return 0; }
|