說明:做了個計算器,本來是要上傳整個項目的,但突然發現不能上傳資源了,就貼出來 算法部分的代碼,大家多多指點啊,呵呵,比較難讀,以後再上傳整個項目,呵呵
//轉換,供乘法部分調用
int turn(char s[], int num[])
{
int i = 0, len, j;
while(1)
{
if(s[i] == 0) break;
else if(s[i] == '-' || s[i] == '0') i++;
else break;
} //i指向第一個非零數字
for (j = 0; s[j] != 0; j++) ;
j--; //j指向最後數字
for(len = 0; j >= i; j--, len++)
{
num[len] = (int)s[j] - 48;
}
return(len);
}
//轉換,供除法部分調用
int turn2(char s[], int num[])
{
int i = 0, len, j;
while(1)
{
if(s[i] == 0) break;
else if(s[i] == '-' || s[i] == '0') i++;
else break;
} //i指向第一個非零數字
for (j = 0; s[j] != 0; j++) ;
j--; //j指向最後數字
for(len = 0; j >= i; i++, len++)
{
num[len] = (int)s[i] - 48;
}
return(len);
}
void mul(int *a, int *b, int *c, int len1, int len2)
{
int i, j;
for (i = 0; i < len1; i++)
{
for (j = 0; j < len2; j++)
{
c[i + j] += a[i] * b[j];
if (c[i+j] >= 10)
{
c[i+j+1] += c[i+j] / 10;
c[i+j] = c[i+j] % 10;
}
}
}
}
//比較大小,被Division函數調用
int cmp(int *a, int *b, int len1, int len2)
{
int i, len, result = 0, j;
for (i = 0; a[i] == 0; i++);
len = len1 - i;
if(len < len2) result = -1;
else if(len > len2) result = 1;
else
{
for (j = 0; i < len1, j < len2; i++, j++)
{
if (a[i] > b[j])
{
result = 1;
break;
}
if (a[i] < b[j])
{
result = -1;
break;
}
}
}
return(result);
}
//兩數相除,被Division函數調用
void div(int *a, int *b1, int *c, int len1, int len2)
{
int i, j, m, n, p, q, b[101], chb[101] = {0}, chlen, result_of_cmp;
for (i = 1; i <= len1; i++)
{
for (j = 1; ;j++)
{
for (m = 0; m < len2; m++) b[m] = b1[m];
for (m = len2 - 1; m >= 0; m--) b[m] *= j;
for (m = len2 - 1; m > 0; m--)
{
if (b[m] >= 10)
{
b[m-1] += b[m] / 10;
b[m] = b[m] % 10;
}
}
if (b[0] >= 10)
{
chlen = len2 + 1;
chb[0] = b[0] / 10;
chb[1] = b[0] % 10;
for (n = 2; n < chlen; n++) chb[n] = b[n-1];
}
else
{
chlen = len2;
for (n = 0; n < chlen; n++) chb[n] = b[n];
}
result_of_cmp = cmp(a, chb, i, chlen);
if(result_of_cmp == 0 || result_of_cmp == -1) break;
}
if(result_of_cmp == 0) c[i-1] = j;
else c[i-1] = j - 1;
for (p = i - 1, q = len2 - 1; p >= 0 && q >= 0; p--, q--)
{
a[p] = a[p] - b1[q] * c[i-1];
while (a[p] < 0)
{
a[p-1]--;
a[p] = a[p] + 10;
}
}
}
}
//////////////乘法部分
HIGHPRECDLL_API void Multiply(char* a,char* b,char* c)
{
int i, j, num1[101] = {0}, num2[101] = {0}, tmp[10001] = {0}, len1, len2;
if((a[0] == '0' && a[1] == 0) || (b[0] == '0' && b[1] == 0)) { c[0] = '0'; c[1] = 0; return;}
if((a[0] == '-' && a[1] == 0) || (b[0] == '-' && b[1] == 0)) { c[0] = '0'; c[1] = 0; return;}
if((a[0] == '-' && a[1] == '0' && a[2] == 0) || (b[0] == '-' && b[1] == '0' || b[2] == 0)) { c[0] = '0'; c[1] = 0; return;}
len1 = turn(a, num1);
len2 = turn(b, num2);
mul(num1, num2, tmp, len1, len2);
for (i = 10000; tmp[i] == 0; i--) ;
if ((a[0] == '-' && b[0] != '-') || (a[0] != '-' && b[0] == '-'))
{
c[0] = '-';
for(j = 1; i >= 0; i--, j++) c[j] = '0' + tmp[i];
c[j] = 0;
}
else
{
for(j = 0; i >= 0; i--, j++) c[j] = '0' + tmp[i];
c[j] = 0;
}
}
///////////////除法部分
HIGHPRECDLL_API void Division(char* a,char* b,char* c)
{
int i, j, num1[101] = {0}, num2[101] = {0}, tmp[101] = {0}, len1, len2, mark;
if(strcmp(b, "0") == 0 || strcmp(b, "-0") == 0) { strcpy(c, "Error"); return;}
len1 = turn2(a, num1);
len2 = turn2(b, num2);
mark = cmp(num1, num2, len1, len2);
if(mark == -1) { strcpy(c, "0"); return; }
div(num1, num2, tmp, len1, len2);
for (i = 0; tmp[i] == 0; i++) ;
if ((a[0] == '-' && b[0] != '-') || (a[0] != '-' && b[0] == '-'))
{
c[0] = '-';
for(j = 1 ; i < len1; i++, j++) c[j] = tmp[i] + '0';
c[j] = 0;
}
else
{
for(j = 0; i < len1; j++, i++) c[j] = tmp[i] + '0';
c[j] = 0;
}
}