int main(int ac, char **av) { char Mp1[MAXLEN] = {0}, Mp2[MAXLEN] = {0}; char temp[MAXLEN + 3] = {0}, rst[RSTMAX] = {0}; int i = 0, j = 0, t = 0, s = 0; int len1 = 0, len2 = 0; int bit = 0;
printf("============= Welcome to Mutip Calculate ============
"); printf("Please enter two number you want to calculate :
"); scanf("%s%s", Mp1, Mp2);
len1 = strlen(Mp1); len2 = strlen(Mp2); for(j = len2 - 1; j >=0; --j){ for(i = len1 - 1, t = len1; i >= 0; --i, --t){ // let two number not two character to multiply temp[t] = (Mp1[i] - 0x30) * (Mp2[j] - 0x30); // 0x30 == 48 == "0" }
// adjust temp"s each bit number which more than 9 to between 0 to 9 for(t = len1; t >0; --t){ if(temp[t] > 9){ temp[t-1] += temp[t]/10; temp[t] %= 10; } }
// sum the new result to the original result; for(s = len1 + len2 - bit, t = len1; t >= 0; --s, --t){ rst[s] += temp[t]; }
// ajust the new result which more than 9 for(s = len1 + len2; s > 0; --s){ if(rst[s] > 9){ rst[s-1] += rst[s]/10; rst[s] %= 10; } }
// bzero the temp array for(t = len1; t >= 0; --t){ temp[t] = 0; } bit++; }
// in order to narmal output the result as a string not a interge rst[len1 + len2 + 1] = " ";
// switch rst"s each bit to character save into the result array. for(s = 0; s <= len1 + len2; ++s){ rst[s] += 0x30; }
// delete the first zero before output the result. for(s = 0; s < len1 + len2; ++s){ if(0x30 == rst[0]){ for(t = 0; t <= len1 + len2 - s; ++t){ rst[t] = rst[t+1]; } }else{ break; } }
// output the result; printf("%s * %s = %s
", Mp1, Mp2, rst); printf("========== Bye Bye ==========
"); return 0; }运行结果如下: [root@anna-laptop C]# ./Multip ============= Welcome to Mutip Calculate ============ Please enter two number you want to calculate : 123456789 987654321 123456789 * 987654321 = 121932631112635269 ========== Bye Bye ==========本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130741.htm