大数相加、相减、

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

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

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

 

#include <iostream>
using namespace std;
#include <string.h>
#include <stdlib.h>
#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]<ch2[j])
		{
			ch1[i]=ch1[i]+10-ch2[j]+48;
			ch1[i-1]=ch1[i-1]-1;
		}
		else
		{
			ch1[i]=ch1[i]-ch2[j]+48;
		}
		j--;
	}
	return ch1;
}

void del_0(char *ans)
{
     int len=strlen(ans);
     int temp=0,i=0;
     for (i=0; i<len; i++)
     {
         if ('0'==ans[i])
         {
            ans[i]=NULL;
            ++temp;
         }
         else
            break;
     }
     for (i=0; i<len; i++)
     {
         ans[i]=ans[temp+i];
     }
}

void mul(char *ch1, char *ch2)
{
	int len1=strlen(ch1),len2=strlen(ch2);
	char ch3[1000009];
	int i,j,carry;
	for (i=0; i<1000009; i++)
		ch3[i]='\0';//=0
	for (i=0; i<len1; i++)
	{
		for (j=0; j<len2; j++)//ÏÂÃæ¿ÉÄÜascÂë´óÓÚ128
		{
			ch3[i+j]=ch3[i+j]+(ch1[i]-'0')*(ch2[j]-'0');//ÕâÀïÓÃ×Ö·ûÐͱíʾch3ҪעÒâ
			if (ch3[i+j]>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<<ch3[0]/10;
		ch3[0]=ch3[0]%10;
	}
	for (i=0; i<len1+len2-1; i++)
		cout<<char(ch3[i]+48);
	cout<<endl;
}
int main()
{
    char ch1[1005];
    char ch2[1005];
    char ans[1005];
    while (cin>>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<<ans[i];
             cout<<endl;
          }
          else if (ch=='-')
          {
		       int len1=strlen(ch1),len2=strlen(ch2);//a-b
		       if (len1<len2)
		       {
			       cout<<"-";
			       swap(ch1,ch2);
			       strcpy(ans,sub(ch1,ch2));
			       del_0(ans);
			       for (int i=0; i<len2; i++)
				       cout<<ans[i];
			       cout<<endl;
		       }
		       else
		       {
			        int temp=1;
	                if (len1==len2)//55-66
				       temp=strcmp(ch1,ch2);
			        if (temp<0)
			        {
				       swap(ch1,ch2);
				       cout<<"-";
			        }
			        strcpy(ans,sub(ch1,ch2));
			        del_0(ans);
			        for (int i=0; i<len1; i++)
                        cout<<ans[i];			  
                    cout<<endl;
		       }
          }
          else if (ch=='*')
          {
		       mul(ch1,ch2);//ÕâÀïÎÒÃÇûÓп¼ÂǸºÊýµÄÇé¿ö£¬¶ÁÕß¿ÉÒÔ×Ô¼º¸Ä£¬ch[0]Ϊ·ûºÅ
          }
    }
    return 0;
}


 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值