目前的代码只实现了结果,代码想体现具体过程,如图所示样子
有专家帮忙完善一下吗
#include<iostream>
#include <stdio.h>
#define N 100
using namespace std;
int Lengh(char a[])
{
int i=0,j=0;
while(a[i]!='\0')
{
if(a[i]!=' ')
j++;
i++;
}
return j;
}
void mul(char int1[],char int2[],int n)
{
char *sum = new char[2*(n+1)];
char *temp = new char[2*(n+1)];
for(int i = 0; i < 2*(n+1); i++) {
sum[i] = '0';
temp[i] = '0';
}
for(int i = n; i > -1; i--) {
if(int2[i]=='1')
{
for(int k = 0; k < 2*(n+1); k++)
{
temp[k] = '0';
}
for(int j = n; j > -1; j--)
{
temp[j+i+1] = int1[j];
}
bool carry = false;
for(int i = (2*n + 1); i > -1; i--)
{
if(!carry)
{
if((sum[i]^temp[i])==0)
{
if((sum[i]=='1')&&(temp[i]=='1'))
{
carry = true;
}
sum[i] = '0';
}
else
{
sum[i] = '1';
}
} else
{
if((sum[i]^temp[i])==0)
{
carry = ((sum[i]=='1')&&(temp[i]=='1')) ? true : false;
sum[i] = '1';
}
else
{
sum[i] = '0';
carry = true;
}
}
}
}
}
for(int i = 0; i < (n+1); i++)
{
int1[i] = sum[i];
}
for(int i = (n+1); i < 2*(n+1); i++)
{
int2[i-n-1] = sum[i];
}
}
int main()
{
LOOP:
int a=0,b=0;
char *c=new char[N];
char *d=new char[N];
cout<<"请输入两个二进制乘数,可以加空格,输入quit退出程序"<<endl;
cout<<"A=";
cin>>c;
cout<<"B=";
cin>>d;
a=Lengh(c);
b=Lengh(d);
if(a!=b)
{
cout<<"输入的二进制数不正确,两数长度不匹配"<<endl;
goto LOOP;
}
a=a-1;
mul(c,d,a);
cout<<c<<d;
goto LOOP;
}