大数相乘

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(int argc, const char * argv[])
{
    void BigNumPlus();
    int str_multiplication(char multiple[],char multiplicand[],char accumulate[]);
    char ch1[] = "0";
    char ch2[] = "0";
    char ch3[1000];
    str_multiplication(ch1,ch2,ch3);
    cout<<ch3<<endl;
    
    return 0;
}


void BigNumPlus()
{
    char a[1001],b[1001],c[2000];
    int x,y,m,z,n;
    cin>>n;getchar();
    for(int j = 0;j < n;j++)
    {
        z=0;
        cout << "Case " << j + 1<< ":" << endl;
        for(x=1,a[0] = '0';(a[x]=getchar()) >= '0'&&a[x]<='9';x++){cout<<a[x];};
        cout<<" + ";
        x--;
        for(y=1,b[0] = '0';(b[y] = getchar()) >= '0'&&b[y]<='9';y++){cout<<b[y];};
        cout<<" = ";
        y--;
        for(m = 0 ; x > 0 || y > 0;m++)
        {
            c[m] = (z = (a[x]-'0' + z + b[y]-'0'))%10 + '0';z /= 10;
            x>0?x--:x = 0;
            y>0?y--:y = 0;
        }
        if(z!=0){ c[m] = z + '0' ; m++; }
        for(m -= 1 ; m >= 0 ; m--)
            cout<<c[m];
        if(j != n-1)cout<<endl<<endl;
    }
}

long str_multiplication(char multiple[],char multiplicand[],char accumulate[])
{
	int i=0,j=0,m,n;
	accumulate[0]='0';
	accumulate[1]='0';
	for (i=0;i<(int)strlen(multiple);i++)
	{
		for (j=0;j<(int)strlen(multiplicand);j++)
		{
			m=((int)(multiple[i]-48)*(int)(multiplicand[j]-48))%10;		//个位
			n=((int)(multiple[i]-48)*(int)(multiplicand[j]-48))/10;		//十位
			accumulate[i+j]=(char)(n+(int)accumulate[i+j]);
			if (accumulate[i+j+1]=='\0')
			{
				accumulate[i+j+1]=char(m+48);
			}
			else
			{
                accumulate[i+j+1]=(char)(m+(int)accumulate[i+j+1]);
			}
		}
		for (long x=i+strlen(multiplicand);x>0;x--)
		{
			if (accumulate[x]>'9')
			{
				accumulate[x]-=10;
				accumulate[x-1]++;
			}
		}
	}
	accumulate[i+j+1]='\0';
	if (accumulate[0]=='0')
	{
		for (i=0;i<strlen(accumulate);i++)
		{
			accumulate[i]=accumulate[i+1];
		}
	}
    return strlen(accumulate);
}

你可能感兴趣的:(大数相乘)