大数相乘:
1、两个大数,要记着,memset(),否则,容易造成不正确;
2、记着:
模板:
被乘数[];
乘数[];
temp1[];
temp2[];
for(乘数索引)
{
for(被乘数索引)
{}
for(加数)
{}
}
3、合理利用memset(),strlen().合理利用倒序
//ps:一下两个代码都没有屏蔽全0
SOURCE CODE:
/*
大数相乘
*/
#include <iostream>
using namespace std;
#define MAXSIZE 1000
char bchengshu[MAXSIZE];
char temp[MAXSIZE];
char chengshu[MAXSIZE/2];
int main()
{
memset(bchengshu,0,MAXSIZE*sizeof(char));
memset(chengshu,0,MAXSIZE/2*sizeof(char));
cout<<"输入被乘数";
char n;
int i=0;
while((n=getchar())!='\n')
{
bchengshu[i++]=n;
}
i-=1;
//把bchengshu[]掉个个
int i_1=0;
while(i>=0)
{
temp[i_1++]=bchengshu[i--];
}
temp[i_1]='\0';
strcpy(bchengshu,temp);
int j=0;
cout<<"输入乘数";
while((n=getchar())!='\n')
{
chengshu[j++]=n;
}
j--;
i_1=0;
memset(temp,0,MAXSIZE*sizeof(char));
while(j>=0)
{
temp[i_1++]=chengshu[j--];
}
strcpy(chengshu,temp);
memset(temp,0,MAXSIZE*sizeof(char));
//cout<<bchengshu<<'*'<<chengshu<<endl;
//system("PAUSE");
/*以上反转完毕,开始乘了*/
//所用到的几个变量:i,j,i_1
// chengshu[],bchengshu[],temp[]
// char n
char mid[MAXSIZE];
memset(mid,0,MAXSIZE*sizeof(char));
int bc_len=strlen(bchengshu);
int c_len=strlen(chengshu);
//cout<<bc_len<<c_len<<endl;
//
int offset=0;
int te;
int j_t=0;
for(i=0;i<c_len;i++)
{
memset(temp,0,MAXSIZE*sizeof(char));
te=chengshu[i]-'0';
i_1=0;
int temp_=0;
int temp_l=0;
for(j=0;j<bc_len;j++)
{
temp_=te*(bchengshu[j]-'0')+j_t;
j_t=temp_/10;
temp_l=temp_%10;
temp[i_1++]=temp_l+'0';
}
if(j_t!=0)
temp[i_1]=j_t+'0';
/*计算出临时变量了*/
//下面的就是相加了mid+temp
int temp_len=strlen(temp);
int mid_len=strlen(mid);
if(0==mid_len)
strcpy(mid,temp);
else
{
/*
int len;
if(temp_len>mid_len)
len=temp_len;
else
len=mid_len;
*/
int temp_te;
int j_te=0;
int l_te=0;
int i_add=0;
for(i_add=0;i_add<temp_len;i_add++)
{
if(mid[i_add+offset]==0)
temp_te=temp[i_add]-'0'+j_te;
else
temp_te=mid[i_add+offset]-'0'+temp[i_add]-'0'+j_te;
j_te=temp_te/10;
l_te=temp_te%10;
mid[i_add+offset]=l_te+'0';
}//end for
if(j_te!=0)
mid[i_add+offset]=j_te+'0';
}
offset++;
}
int strlen_=strlen(mid);
strlen_-=1;
j=0;
memset(bchengshu,0,MAXSIZE*sizeof(char));
while(strlen_>=0)
{
bchengshu[j++]=mid[strlen_];
strlen_--;
}
//strcpy(bchengshu,mid);
// cout<<mid<<endl;
cout<<bchengshu<<endl;
system("pause");
return 0;
}
/优化代码后:
#include <iostream>
using namespace std;
#define SIZE 1000
char cheng[SIZE];
char bcheng[SIZE];
char temp[SIZE];
char temp_add[SIZE];
//初始化
void Init(char ch[SIZE])
{
memset(ch,0,SIZE*sizeof(char));
}
//reverse
void Rever(char * ch)
{
int len_ch=strlen(ch);
char Mid[SIZE];
Init(Mid);
int i=0;
len_ch-=1;
while(len_ch>=0)
{
Mid[i++]=ch[len_ch--];
}
strcpy(ch,Mid);
Init(Mid);
}
void Imulti(char * p,char * q)
{//p:被乘数 q:乘数
Init(temp_add);
Rever(p);
Rever(q);
int strlen_p=strlen(p);
int strlen_q=strlen(q);
int i_p,i_q;
int offset=0;
for(i_q=0;i_q<strlen_q;i_q++)
{
int temp_q=q[i_q]-'0';
int jw=0;
int lw=0;
Init(temp);
for(i_p=0;i_p<strlen_p;i_p++)
{
int temp_=(p[i_p]-'0')*temp_q+jw;
lw=temp_%10;
jw=temp_/10;
temp[i_p]=lw+'0';
}
if(jw!=0)
temp[i_p]=jw+'0';
//
int strlen_temp_add=strlen(temp_add);
if(strlen_temp_add==0)
strcpy(temp_add,temp);
else
{//大数相加:temp_add[]+temp[]
lw=0;
jw=0;
int strlen_temp=strlen(temp)+offset;
int len=0;
if(strlen_temp_add>strlen_temp)
len=strlen_temp_add-offset;//同下
else
len=strlen_temp-offset;//这一点容易出错
for(int i=0;i<len;i++)
{
int temp_temp=0;
int temp_i,temp_a;
if(temp[i])
temp_i=temp[i]-'0';
else
temp_i=0;
if(temp_add[i+offset])
temp_a=temp_add[i+offset]-'0';
else
temp_a=0;
temp_temp=temp_i+temp_a+jw;
jw=temp_temp/10;
lw=temp_temp%10;
temp_add[i+offset]=lw+'0';
}
if(jw!=0)
temp_add[i+offset]=jw+'0';
}
offset++;
}
strcpy(p,temp_add);
Rever(p);
cout<<p<<endl;
}
int main()
{
cin>>bcheng;
cin>>cheng;
Imulti(bcheng,cheng);
return 0;
}