大数相加、相减、

这里直接贴代码了,代码里面有注释

另外里面还有一个是 乘法的,乘法的再上一博客就有提到了,读者可以           clickhere

里面 由于输入的问题可能 有乱码情况   具体的源代码我放在   点击这里进入下载    

 

#include 
using namespace std;
#include 
#include 
#define max(a,b)  ((a)>(b)?(a):(b))
void swap(char *ch1, char *ch2)
{
     int len1=strlen(ch1),len2=strlen(ch2);
     char temp[1005];
     if (len1<=len2)
     {
        strcpy(temp,ch1);
        strcpy(ch1,ch2);
        strcpy(ch2,temp);
     }
}

char *add(char *ch1, char *ch2)
{
	int len1=strlen(ch1),len2=strlen(ch2);
	int temp=0;
    ch1[len1-1]=((ch1[len1-1]-'0')+(ch2[len2-1]-'0'))%10+48;
	int i=len1-2,j=len2-2;
	for (i=len1-2; i>=0; i--)//³¤¶ÈÏàµÈÇé¿ö
	{
		if (j<0)
	       break;
		if (((ch1[i]-'0')+(ch2[j]-'0')+temp)>9)
			temp=((ch1[i]-'0')+(ch2[j]-'0')+temp)/10;
		else
			temp=0;
		ch1[i]=((ch1[i]-'0')+(ch2[j]-'0'))%10+temp+48;
		j--;
	}
	if (temp==1)//³¤¶È²»µÈÇÒÇ°ÃæÓнøλÇé¿ö
	{
		for (i=len1-len2-1; i>0; i--)
		{
		
		    ch1[i]=(ch1[i]-'0'+temp)%10+48;
		    temp=(ch1[i]-'0'+temp)/10;
		}
	}
	if (temp==1)//×î¸ßλ½øλÇé¿ö
	{
		ch1[0]=ch1[0]+1;
		if (ch1[0]>'9')
		{
			ch1[0]='0';
			for (i=len1; i>0; i--)
				ch1[i]=ch1[i-1];
			ch1[0]='1';
			ch1[len1+1]='\0';
		}
	}
	return ch1;
}

char *sub(char *ch1, char *ch2)
{
	int len1=strlen(ch1),len2=strlen(ch2);
	int i=len1-1,j=len2-1;
	for (i=len1-1;i>=0; i-- )//³¤¶ÈÏàµÈ
	{
		if (j<0)
			break;
        if (ch1[i]9&&(i+j)>0)
			{
			   ch3[i+j-1]+=ch3[i+j]/10;
			   ch3[i+j]=ch3[i+j]%10;
			}
		}
	}
	for (i=len1+len2-1; i>0; --i)
	{
		if (ch3[i]>9)
		{
			ch3[i-1]=ch3[i-1]+ch3[i]/10;
			ch3[i]%=10;
		}
	}

	if (ch3[0]>9)//if (ch[3]>99)
	{
		cout<>ch1>>ch2)
    {
          char ch;
          cout<<"input operator: ";
          cin>>ch;
          if (ch=='+')
          {
             swap(ch1,ch2);      //a+b
             int len=strlen(ch1);
             strcpy(ans,add(ch1,ch2));
             for (int i=0; i<=len; i++)//"="½øλʱºò
                 cout<


 


 

你可能感兴趣的:(数论)