超长整数乘法

#include<stdlib.h>
#include<iostream>
#define max 100  //最大位数
using namespace std;
void reverse(int *a,int i)   //倒置
{
int m,n;
int temp;
n=i/2;
for(m=0;m<n;m++)
{
temp=*(a+m);
   *(a+m)=*(a+i-m-1);
*(a+i-m-1)=temp; 
}
}
void jinwei(int *a,int m)   //进位
{
int i,j;
for(j=0;*(a+m+j)>=10;j++)
{
i=*(a+m+j)/10;
      *(a+m+j)=*(a+m+j)%10;
   *(a+m+j+1)=*(a+m+j+1)+i;
}
}
void *cal()
{
  char *st1,*st2;
  int *a,*b;
  int *c;
  int num[max]={0};
  int i,j;
  int k,m;
  int out;
  c=&num[0];
  cout<<"input first number size:";
  cin>>i;
  st1=new char(i);
  cout<<"input first number:";
  cin>>st1;
  cout<<"input second number size:";
  cin>>j;
  st2=new char(j); 
  cout<<"input second number:";
  cin>>st2;
  a=new int(i);
  b=new int(j);
  for(k=0;k<i;k++)
  {
  a[k]=st1[k]-48;
  }
  for(k=0;k<j;k++)
  {
  b[k]=st2[k]-48;
  }
  delete st1;
  delete st2;
  reverse(a,i);
  reverse(b,j);
  for(k=0;k<i;k++)
    for(m=0;m<j;m++)
    {
      out=a[k]*b[m];
      if(out<10)
      {
  c[k+m]=c[k+m]+out;
  if(c[k+m]>=10)
  {
  jinwei(c,(k+m));
  }
}
      else
      {
      c[k+m]=c[k+m]+out;
      jinwei(c,(k+m));
}
}
j=0;
for(i=max;i>=0;i--)
{
  if(c[i]==0&&j==0)
   continue;
  if(c[i]!=0||j!=0)
   {
                cout<<c[i]; 
j=1;
}
}
cout<<endl;
}
int main()
{
        cal();
system("pause");
return 0;
 } 

猜你喜欢

转载自blog.csdn.net/fight_snail/article/details/80393518