高精度加、减、乘法运算 原创,嘿嘿。。。 效率应该不错。 #include <stdio.h> #include <string.h> #define DIGIT 2 //num length of a array unit #define MAXDIGIT 100 //10^DIGIT, max-1 of a array unit #define MAX 1000 //Max of array #define LL long //type of num struct BigInt { LL data[MAX]; int size; }; //把string类型转换为BigInt类型 BigInt StrToBigInt(char *str) { int i,j,k; LL sum; BigInt ret; memset(&ret, 0, sizeof(ret)); int len; len = strlen(str); i=0;k=0; while (len-i>=DIGIT) { sum = 0; for (j=len-i-DIGIT;j<len-i;j++) sum = sum*10+(str[j]-'0'); ret.data[k++] = sum; i+=DIGIT; } if (i<len) { sum = 0; for (j=0;j<len-i;j++) sum = sum*10+(str[j]-'0'); ret.data[k++] = sum; } ret.size = k; return ret; } BigInt BigIntMul(BigInt a, BigInt b) { BigInt ret; memset(&ret, 0, sizeof(ret)); if (a.size == 1&&a.data[0] == 0) { ret.data[0] = 0;ret.size = 1;return ret; } if (b.size == 1&&b.data[0] == 0) { ret.data[0] = 0;ret.size = 1;return ret; } int i,j; for (i=0;i<a.size;i++) { for (j=0;j<b.size;j++) { ret.data[i+j] += b.data[j] * a.data[i]; ret.data[i+j+1] += ret.data[i+j] / MAXDIGIT; ret.data[i+j] = ret.data[i+j] % MAXDIGIT; } } ret.size = i+j; if (ret.data[ret.size]!=0) ret.size++; while(ret.data[ret.size-1]==0&&ret.size!=1) ret.size--; return ret; } BigInt BigIntSum(BigInt a, BigInt b) { BigInt ret; int i; int max; memset(&ret, 0, sizeof(ret)); a.size>b.size?max=a.size:max=b.size; for (i=0;i<max;i++) { ret.data[i] += a.data[i]+b.data[i]; ret.data[i+1] += ret.data[i] / MAXDIGIT; ret.data[i] %= MAXDIGIT; } ret.size = i; if (ret.data[ret.size]!=0) ret.size++; while(ret.data[ret.size-1]==0&&ret.size!=1) ret.size--; return ret; } //1:a>b -1:a<b 0:equal int CompareBigInt(BigInt a, BigInt b) { if (a.size>b.size) return 1; if (a.size<b.size) return -1; int i; for (i=a.size-1;i>=0;i++) { if (a.data[i]>b.data[i]) return 1; if (a.data[i]<b.data[i]) return -1; } return 0; } void PrintBigInt(BigInt a) { int i; if (a.size == 0) { printf("0/n");return; } if (a.data[a.size-1]!=0||a.size==1) printf("%d",a.data[a.size-1]); for (i=a.size-2;i>=0;i--) { int w = MAXDIGIT / 10; while (w > 1) { if (a.data[i] >= w) break; printf("0"); w /= 10; } printf("%d",a.data[i]); } printf("/n"); } BigInt BigIntSub(BigInt a, BigInt b) { BigInt ret; BigInt SubNum; int flag; int i; if (CompareBigInt(a,b) == -1) { ret = b; SubNum = a; flag = 1; } else { ret = a; SubNum = b; flag = 0; } for (i=0;i<ret.size;i++) { if (ret.data[i]<SubNum.data[i]) { ret.data[i+1]--; ret.data[i] += MAXDIGIT; ret.data[i] -= SubNum.data[i]; } else { ret.data[i] -= SubNum.data[i]; } } while (ret.data[ret.size-1]==0||ret.data[ret.size-1]==-1) ret.size--; if (flag) ret.data[ret.size-1] = -ret.data[ret.size-1]; return ret; } int main() { char a[300], b[300]; BigInt BigNum_a, BigNum_b, BigNum_c; while (scanf("%s%s", a,b)!=EOF) { BigNum_a = StrToBigInt(a); BigNum_b = StrToBigInt(b); BigNum_c = BigIntMul(BigNum_a, BigNum_b); printf("MUL:"); PrintBigInt(BigNum_c); BigNum_c = BigIntSum(BigNum_a, BigNum_b); printf("SUM:"); PrintBigInt(BigNum_c); BigNum_c = BigIntSub(BigNum_a, BigNum_b); printf("SUB:"); PrintBigInt(BigNum_c); } return 0; }