//=============================================================================================================#include "Big__CALC.h"int main(){char str1[200]="9876543210987"; char str2[200]="1234567890"; char result[200]={0};printf("str1 = %s\t%d\n",str1,strlen(str1)-1);printf("str2 = %s\t%d\n",str2,strlen(str2)-1);int ret = Compare(str1,str2);if (ret==1){printf("str1 > str2\n");}else if(ret==-1) printf("str1 < str2\n");else printf("str1 = str2\n");Add(str1,str2,result);printf("str1 + str2 = %s\t%d\n",result,strlen(result)-1);Sub(str1,str2,result);printf("str1 - str2 = %s\t%d\n",result,strlen(result)-1);Chen(str1,str2,result);printf("str1 * str2 = %s\t%d\n",result,strlen(result)-1);char div_result[200]={0};Mod(str1,str2,result,div_result);printf("str1 M str2 = %s\t%d\n",result,strlen(result)-1);printf("str1 / str2 = %s\t%d\n",div_result,strlen(div_result)-1); int num=10;char str[200]="999"; ChenFang(str,num,result);ThrowAway_0(str);printf("%s ^ %d = %s\t%d\n",str,num,result,strlen(result)-1);getchar();return 1;}void Trans(char *str_num1, char *str_num2, char *tempbuf1, char *tempbuf2){int len_num1=0;int len_num2=0;int i=0;while(str_num1[i]!='\0'){len_num1++;i++;}//printf("字串1的長度: length1=%d\n",len_num1);i=0;while(str_num2[i]!='\0'){len_num2++;i++;}//printf("字串2的長度: length2=%d\n\n",len_num2);tempbuf1[0]='0';tempbuf2[0]='0';//=======================================================================if(len_num2>=len_num1) //補成相同長度{for(i=1;i<=(len_num2-len_num1);i++){tempbuf1[i]='0';}for(i=len_num2-len_num1+1;i<=len_num2;i++){tempbuf1[i]=str_num1[i-(len_num2-len_num1+1)];}for(i=1;i<=len_num2;i++){tempbuf2[i]=str_num2[i-1];}}//------------------------------------------else if(len_num2<len_num1){for(i=1;i<=(len_num1-len_num2);i++){tempbuf2[i]='0';}for(i=len_num1-len_num2+1;i<=len_num1;i++){tempbuf2[i]=str_num2[i-(len_num1-len_num2+1)];}for(i=1;i<=len_num1;i++){tempbuf1[i]=str_num1[i-1];}}/*printf("-------------------------------------------------------\n轉換之後:\n");printf("str1=: %s\n",tempbuf1);printf("str2=: %s\n",tempbuf2);//==========================================================================int len=0;i=0;while(tempbuf1[i]!='\0'){len++;i++;}*/}//====================================================================================================//extern "C" __declspec(dllexport)void Chen(char *tempbuf1, char *tempbuf2 , char *result){char buf1[200]={0};char buf2[200]={0};Trans(tempbuf1,tempbuf2,buf1,buf2);strcpy(tempbuf1,buf1);strcpy(tempbuf2,buf2);int len=0;int i=0;int j=0;int n=0;int jinwei=0;while(tempbuf1[i]!='\0') // 字串長度{len++;i++;}int temp[200]={0}; // 該數組用來儲存各次方的係數 10為底int max=2*len;for(i=0;i<=max;i++){if(i<len-1){for(j=0;(j<=len-1)&&(j>=i+1-len)&&(j<=i);j++){temp[i]+=((int)tempbuf1[len-1-j]-48 )*((int)tempbuf2[len-1-i+j]-48); }temp[i]+=jinwei;if (temp[i]>=10) //&&temp[i]<100{jinwei = temp[i]/10;temp[i] = temp[i]%10;}else jinwei=0;}else if (i>=len-1){for(j=i-len+2;(j<=len-1)&&(j>=i+1-len)&&(j<=i);j++){temp[i]+=((int)tempbuf1[len-1-j]-48 )*((int)tempbuf2[len-1-i+j]-48); }temp[i]+=jinwei;if (temp[i]>=10) //&&temp[i]<100{jinwei = temp[i]/10;temp[i] = temp[i]%10;}else jinwei=0;}} i=max;while(i>=0){if(temp[i]!=0)break;else i--;}//printf("str1 * str2= ");int num=i; // 科學計數法次數/*for (j=num;j>=0;j--){printf("%d",temp[j]);}printf("\t%d\n",num);*///===========================================memset(result,0,200);for(i=num;i>=0;i--){result[num-i]=(char)(temp[i]+48);}//printf("result = %s\t%d\n",result,strlen(result)-1);}//=====================================================================//extern "C" __declspec(dllexport) void ChenFang(char *tempbuf1, int num, char *result) // 大數乘方{static int count=0;if (count==0){char c[200]={'1',0,0};memset(result,0,200);Chen( tempbuf1 , c , result);}count++;if(num==1){return ;} else if(num>1){Chen( tempbuf1 , result , result);num--; ChenFang(tempbuf1,num,result);}ThrowAway_0 (result);}//=================================================//extern "C" __declspec(dllexport) void Mod(char *tempbuf1, char *tempbuf2, char *result , char *div_result ) // 大數求餘{memset(result,0,200);ThrowAway_0 (tempbuf1);ThrowAway_0 (tempbuf2);int max1 = strlen(tempbuf1);int max2 = strlen(tempbuf2);int temp=0;int ret=1;char div[200]={0};int count=0;char AfterSub[200]={0};if(max1-max2>=0) {while(ret==1) { Sub (tempbuf1,tempbuf2,AfterSub); //減去之後在判斷是否得到結果----即:tempbuf1 < tempbuf2memset(tempbuf1,0,200);strcpy (tempbuf1,AfterSub);ret=Compare(tempbuf1,tempbuf2);ThrowAway_0 (tempbuf1);ThrowAway_0 (tempbuf2); count++;if(count>=1000){itoa(count,div,10);Add(div_result,div,div_result);count=0;}}strcpy (result,tempbuf1);ThrowAway_0 (result);itoa(count,div,10);Add(div_result,div,div_result);}else // if (Compare(tempbuf1,tempbuf2)==-1) // 後者大的話,mod 就是 tempbuf1{strcpy (result,tempbuf1);ThrowAway_0 (result);}}//========================================================//extern "C" __declspec(dllexport) void Add(char *tempbuf1, char *tempbuf2, char *result) // 大數相加 result = tempbuf1 + tempbuf2{char buf1[200]={0};char buf2[200]={0};Trans(tempbuf1,tempbuf2,buf1,buf2);strcpy(tempbuf1,buf1);strcpy(tempbuf2,buf2);int i=0;int temp=0;int jinwei=0;int len=0;while(tempbuf1[i]!='\0'){len++;i++;}for(i=len-1;i>=0;i--){ temp=(int)(tempbuf1[i]+tempbuf2[i]+jinwei-96);if(temp>=10){temp=temp-10;jinwei=1;}else jinwei=0;result[i]=(char)(temp+48);}ThrowAway_0 (result);}//================================================void ThrowAway_0 (char *tempbuf ) // 去除結果前面的 連續的無意義的 "0"{char buf[200]={0};int n = strlen(tempbuf)-1;int i=0;while(i<n){if (tempbuf[i]!='0'){break;}else {i++;}}int Throw = i;for (i=0;i<=n-Throw;i++){buf[i]=tempbuf[i+Throw];}strcpy(tempbuf,buf);}//=======================================================//extern "C" __declspec(dllexport) void Sub(char *tempbuf1, char *tempbuf2, char *result) // 大數相減 result = tempbuf1 - tempbuf2{ThrowAway_0 (tempbuf1);ThrowAway_0 (tempbuf2);memset(result,0,200);char buf1[200]={0};char buf2[200]={0};Trans(tempbuf1,tempbuf2,buf1,buf2);memset(tempbuf1,0,200);memset(tempbuf2,0,200);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,200);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,200);sprintf(buf1,"-%s",result);strcpy(result,buf1);}}//======================================================================//===================================================================================int Compare(char *tempbuf1,char *tempbuf2){ThrowAway_0 (tempbuf1);ThrowAway_0 (tempbuf2);char buf1[200]={0};char buf2[200]={0};Trans(tempbuf1,tempbuf2,buf1,buf2);memset(tempbuf1,0,200);memset(tempbuf2,0,200);strcpy(tempbuf1,buf1);strcpy(tempbuf2,buf2);int ret=1;int count=0;while(count<200){int m=(int)tempbuf1[count]-48;int n=(int)tempbuf2[count]-48;if(m==n){count++;ret=0;}else if(m>n){//printf("tempbuf1>tempbuf2\n");ret=1;break;}else if(m<n){//printf("tempbuf1<tempbuf2\n");ret=-1;break;}}return ret;}
2.標頭檔
#include <stdio.h> #include <string.h>#include <stdlib.h>//extern "C" __declspec(dllexport) void Add (char *tempbuf1, char *tempbuf2, char *result); // 大數相加 result = tempbuf1 + tempbuf2//extern "C" __declspec(dllexport) void Sub(char *tempbuf1, char *tempbuf2, char *result); // 大數相減 result = tempbuf1 - tempbuf2 (預設前者大)int Compare(char *tempbuf1,char *tempbuf2);//extern "C" __declspec(dllexport) void Chen (char *tempbuf1, char *tempbuf2, char *result); // 大數相乘 result = tempbuf1 * tempbuf2void Trans (char *str_num1, char *str_num2, char *tempbuf1, char *tempbuf2); // 大數轉化為等長,且最前面添加 “0”//extern "C" __declspec(dllexport) // 大數除法 div_result = tempbuf1 / tempbuf2void Mod(char *tempbuf1, char *tempbuf2, char *result , char *div_result ); // 大數求餘 result = tempbuf1 % tempbuf2 //extern "C" __declspec(dllexport) void ChenFang (char *tempbuf1, int num, char *result); // 大數乘方 result = tempbuf1 ^ numvoid ThrowAway_0 (char *tempbuf ); // 去除結果前面的 連續的無意義的 "0"