现在的位置: 首页 > 综合 > 正文

大数相乘代码-字符串实现

2013年02月22日 ⁄ 综合 ⁄ 共 713字 ⁄ 字号 评论关闭

#include<iostream>
#include <vector>
using namespace std;
void Multiply(char A[],char B[],char C[])
{
 int Asize=strlen(A),Bsize=strlen(B),Csize=strlen(C);
 int index=Csize-1;
 for(int i=Asize-1;i>=0;i--)
 {
  int position=index--,left=0;
  if(A[i]=='0') continue;
  for(int j=Bsize-1;j>=0;j--)
  {
   int t=(A[i]-'0')*(B[j]-'0')+left+(C[position]-'0');
   C[position--]=t%10+'0';
   left=t/10;
  }
  if(left) C[position]=left+'0';
 }
}
void show(char * p)
{
 while(*p=='0')p++;
 while(*p!='\0')
 {
     cout<<*p;
  p++;
 }
 cout<<endl;
}

void main()
{
 while(1)
 {
 char * a=new char[20];char * b=new char[20];
 char * c=new char[40];
 memset(c,'0',39); c[39]='\0';
 cin>>a>>b;
 cout<<a<<" * "<<b<<" = ";
 Multiply(a,b,c);
 show(c);
 delete [] a;delete [] b; delete[] c;
 }
 
 ::system("pause");
}

抱歉!评论已关闭.