1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 6 #define MAX_LENGTH 1000 7 8 #define Char2Num(Char) (Char - '0') 9 #define Num2Char(Num) ((char)(Num + '0')) 10 11 char gacInput[MAX_LENGTH]; 12 char gacResult[MAX_LENGTH]; 13 char gacRemainder[MAX_LENGTH]; 14 15 void GetInput() 16 { 17 int wLoop = -1; 18 while('\n' != (gacInput[++wLoop] = getchar())); 19 gacInput[wLoop] = '\0'; 20 } 21 22 void TurnResult(char *acArray) 23 { 24 char tmp; 25 int i = 0; 26 int len = strlen(acArray); 27 for(i=0; i<(int)(len/2); i++) 28 { 29 tmp = acArray[len-1-i]; 30 acArray[len-1-i] = acArray[i]; 31 acArray[i] = tmp; 32 } 33 } 34 35 void KillZero(char *acArray) 36 { 37 int i = strlen(acArray); 38 while(acArray[i-1] == '0' && i>1) 39 { 40 acArray[i-1] = '\0'; 41 i--; 42 } 43 } 44 45 void TidyResult() 46 { 47 KillZero(gacResult); 48 TurnResult(gacResult); 49 } 50 51 void Add(char *acArray, int wLength, int wLablePosition) 52 { 53 char *Num1 = NULL; 54 char *Num2 = NULL; 55 int wNum1Length = wLablePosition; 56 int wNum2Length = wLength - wLablePosition - 1; 57 int wTmp = 0; 58 59 if (wNum1Length > wNum2Length) 60 { 61 Num1 = &acArray[0]; 62 Num2 = &acArray[wLablePosition+1]; 63 } 64 else 65 { 66 Num1 = &acArray[wLablePosition+1]; 67 Num2 = &acArray[0]; 68 wTmp = wNum1Length; 69 wNum1Length = wNum2Length; 70 wNum2Length = wTmp; 71 } 72 73 int wResult = 0; 74 int wCarry = 0; 75 int i = 0; 76 char acTmpResult[MAX_LENGTH] = {0}; 77 78 while(i < wNum1Length || wCarry != 0) 79 { 80 wResult = ((i > wNum1Length-1)? 0 : Char2Num(Num1[wNum1Length-i-1])) 81 + ((i > wNum2Length-1)? 0 : Char2Num(Num2[wNum2Length-i-1])) 82 + wCarry; 83 wCarry = (int)(wResult / 10); 84 acTmpResult[i] = Num2Char(wResult - wCarry * 10); 85 i++; 86 } 87 memset(gacResult, 0, sizeof(gacResult)); 88 strcpy(gacResult, acTmpResult); 89 } 90 91 void Minus(char *acArray, int wLength, int wLablePosition) 92 { 93 char *Num1 = &acArray[0]; 94 char *Num2 = &acArray[wLablePosition+1]; 95 int wNum1Length = wLablePosition; 96 int wNum2Length = wLength - wLablePosition - 1; 97 98 int wResult = 0; 99 int wBorrow = 0; 100 int i = 0; 101 int j = 0; 102 char acTmpResult[MAX_LENGTH] = {0}; 103 104 while(i < wNum1Length || wBorrow != 0) 105 { 106 wBorrow = (i > wNum2Length-1 || Num1[wNum1Length-i-1] >= Num2[wNum2Length-i-1])? 0 : 1; 107 if (wBorrow == 0) 108 { 109 wResult = Num1[wNum1Length-i-1] - ((i > wNum2Length-1)? '0' : Num2[wNum2Length-i-1]); 110 } 111 else 112 { 113 for(j=0; Num1[wNum1Length-i-2-j] == '0'; j++) 114 { 115 Num1[wNum1Length-i-2-j] = '9'; 116 } 117 Num1[wNum1Length-i-2-j] = (char)(Num1[wNum1Length-i-2-j] - 1); 118 wResult = Char2Num(Num1[wNum1Length-i-1]) + 10 - Char2Num((i > wNum2Length-1)? '0' : Num2[wNum2Length-i-1]); 119 } 120 acTmpResult[i] = Num2Char(wResult); 121 i++; 122 } 123 memset(gacResult, 0, sizeof(gacResult)); 124 strcpy(gacResult, acTmpResult); 125 } 126 127 void Calc(char *acArray); 128 129 void Multiply(char *acArray, int wLength, int wLablePosition) 130 { 131 char *Num1 = &acArray[0]; 132 char *Num2 = &acArray[wLablePosition+1]; 133 int wNum1Length = wLablePosition; 134 int wNum2Length = wLength - wLablePosition - 1; 135 136 char acTmpResult[MAX_LENGTH] = {0}; 137 int wResult = 0; 138 int wCarry = 0; 139 int i = 0; 140 int j = 0; 141 memset(gacResult, 0, sizeof(gacResult)); 142 for(i=0; i<wNum2Length; i++) 143 { 144 if(i!=0) 145 { 146 gacResult[strlen(gacResult)] = '+'; 147 } 148 for(j=0; j<wNum1Length || wCarry != 0; j++) 149 { 150 wResult = ((i > wNum2Length-1)? 0 : Char2Num(Num2[wNum2Length-i-1])) 151 * ((j > wNum1Length-1)? 0 : Char2Num(Num1[wNum1Length-j-1])) 152 + wCarry; 153 wCarry = (int)(wResult / 10); 154 acTmpResult[j] = Num2Char(wResult - wCarry * 10); 155 } 156 TurnResult(acTmpResult); 157 for(j=0; j<i; j++) 158 { 159 acTmpResult[strlen(acTmpResult)] ='0'; 160 } 161 strcat(gacResult, acTmpResult); 162 memset(acTmpResult, 0, sizeof(acTmpResult)); 163 Calc(gacResult); 164 } 165 } 166 167 int CmpStr(char *str1, char *str2) 168 { 169 if(strlen(str1) == strlen(str2)) 170 { 171 return strcmp(str1,str2); 172 } 173 else if(strlen(str1) > strlen(str2)) 174 { 175 return 1; 176 } 177 else 178 return -1; 179 } 180 181 void Div(char *acArray, int wLength, int wLablePosition) 182 { 183 char *Num1 = &acArray[0]; 184 char *Num2 = &acArray[wLablePosition+1]; 185 int wNum1Length = wLablePosition; 186 int wNum2Length = wLength - wLablePosition - 1; 187 188 int i = 0; 189 int wSP = wNum2Length-1; 190 char acTmpNum3[MAX_LENGTH] = {0}; 191 char acTmpRemainder[MAX_LENGTH] = {0}; 192 char acTmpResult[MAX_LENGTH] = {0}; 193 char acTmpMulResult[MAX_LENGTH] = {0}; 194 for(i=0; i<2; i++) 195 { 196 wSP++; 197 strncpy(acTmpNum3, Num1, wSP); 198 if(strcmp(acTmpNum3, Num2) >= 0) break; 199 }; 200 201 int j = 0; 202 int k = 0; 203 for(j=wSP; j<=wNum1Length; j++) 204 { 205 for(i=9; i>0; i--) 206 { 207 sprintf(acTmpMulResult, "%c*%s", Num2Char(i), Num2); 208 Calc(acTmpMulResult); 209 if(CmpStr(gacResult, acTmpNum3) <= 0) 210 { 211 memset(acTmpRemainder, 0, sizeof(acTmpRemainder)); 212 acTmpResult[k++] = Num2Char(i); 213 sprintf(acTmpMulResult, "%s-%s", acTmpNum3, gacResult); 214 Calc(acTmpMulResult); 215 strcpy(acTmpRemainder, gacResult); 216 if(gacResult[0] != '0') 217 { 218 memset(acTmpNum3, 0, sizeof(acTmpNum3)); 219 strcpy(acTmpNum3, gacResult); 220 } 221 else 222 memset(acTmpNum3, 0, sizeof(acTmpNum3)); 223 break; 224 } 225 } 226 if(i == 0) 227 { 228 acTmpResult[k++] = '0'; 229 strcpy(acTmpRemainder, acTmpNum3); 230 } 231 TurnResult(acTmpNum3); 232 KillZero(acTmpNum3); 233 TurnResult(acTmpNum3); 234 acTmpNum3[strlen(acTmpNum3)] = Num1[j]; 235 } 236 TurnResult(acTmpRemainder); 237 KillZero(acTmpRemainder); 238 TurnResult(acTmpRemainder); 239 strcpy(gacRemainder, acTmpRemainder); 240 memset(gacResult, 0, sizeof(gacResult)); 241 strcpy(gacResult, acTmpResult); 242 } 243 244 void Calc(char *acArray) 245 { 246 int i = 0; 247 int wLength = strlen(acArray); 248 while( acArray[i] != '\0' ) 249 { 250 switch(acArray[i]) 251 { 252 case '+': 253 Add(acArray, wLength, i); 254 TidyResult(); 255 return; 256 case '-': 257 Minus(acArray, wLength, i); 258 TidyResult(); 259 return; 260 case '*': 261 Multiply(acArray, wLength, i); 262 return; 263 case '/': 264 Div(acArray, wLength, i); 265 return; 266 default: 267 i++; 268 } 269 } 270 } 271 272 int main(void) 273 { 274 GetInput(); 275 Calc(gacInput); 276 printf("%s = %s...%s \n length of gacInput: %d\n length of gacResult: %d\n length of gacRemainder: %d\n", 277 gacInput, gacResult, gacRemainder, strlen(gacInput), strlen(gacResult), strlen(gacRemainder)); 278 279 return 0; 280 }
说明:算法参见手工算法。仅支持整数运算。