大数运算

//=============================================================================================================
#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=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 < tempbuf2
			memset(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=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


 

你可能感兴趣的:(C/C++)