数据结构课程设计*大数乘大数

2009-11-19 19:32

#include "stdafx.h"
#include "iostream"
using namespace std;
int count(char *s)//判断字符串大小
{
int count1=0;
char ch;
ch=*s;
while(ch!='/0')
{
   count1++;
   s++;
   ch=*s;
}
return count1;
}
int *zh1(char *s,int count1)
{
char ch;
int i,*array1;
array1=new int[count1];
ch=*s;//cout<<ch;测试
for(i=0;i<count1;i++)
{
   //cout<<ch<<endl;//看下这一行为何没有输出?
   *(array1+count1-i-1)=ch-'0';
   s++;
   ch=*s;
}
for(i=0;i<count1;i++)
   cout<<array1[i];
cout<<endl;///测试
return array1;
}
void zh2(int a[],int m)
{
char *s;
int i;
cout<<"看作实数:";
for(i=m-1;i>=0;i--)
{

   cout<<a[i];
}
cout<<endl;
}
int *jisuan(int *a,int m,int *b,int n)
{
int *array3;
array3=new int[m+n];
for(int l=0;l<m+n;l++)
   array3[l]=0;
int i,j,k;
for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   {  
    if(a[i]*b[j]<10)
     array3[i+j]=array3[i+j]+a[i]*b[j];
    else
    {
     array3[i+j]=array3[i+j]+a[i]*b[j]%10;
     array3[i+j+1]=array3[i+j+1]+(a[i]*b[j]-a[i]*b[j]%10)/10;
    }
            for(int l=0;l<m+n;l++)
                if(array3[l]>=10)
     {
     array3[l+1]=array3[l+1]+(array3[l]-array3[l]%10)/10;
     array3[l]=array3[l]%10;//必须立马进位
     }
   }
   cout<<"倒序输出:";
        for(int l=0;l<m+n;l++)//测试
              cout<<array3[l];
   cout<<endl;
   /*for(int l=0;l<m+n;l++)
   {
    if(array3[l]>=10)
    {
     array3[l+1]=array3[l+1]+(array3[l]-array3[l]%10)/10;
     array3[l]=array3[l]%10;
      }
   cout<<array3[l];
   }*/
   return array3;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *a1={"4545446647465354654956565659/0"},*a2={"54787867635464897654458458475454545454545454/0"};
int n1,n2,*b1,*b2,*shu;
n1=count(a1);
n2=count(a2);
cout<<"输入乘数1的位数:"<<n1<<endl;
cout<<"输入乘数2的位数:"<<n2<<endl;
    b1=new int[n1];
b1=zh1(a1,n1);
b2=new int[n2];
b2=zh1(a2,n2);
shu=new int[n1+n2];
shu=jisuan(b1,n1,b2,n2);
//for(int i=0;i<n;i++)测试用
// cout<<b[i];
//char *s;
zh2(shu,n1+n2);
//puts(s);测试用
//cout<<endl;
delete[] b1;
delete[] b2;
delete[] shu;
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值