C ++中的长手乘法 [英] Long Hand Multplication In C++

查看:63
本文介绍了C ++中的长手乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这段代码,用于模拟存储在数组BeforeDecimal1&中的4位二进制数的乘法运算。 BeforeDecimal2。乘法的结果存储在数组s []中。问题是这段代码没有提供正确的答案。我试图找出问题但没有价值,所以现在我寻求专家的建议。这是操纵长手乘法的代码:



我正在尝试做这个:



I wrote this code for simulating multiplication of 4 bit binary numbers stored in arrays BeforeDecimal1 & BeforeDecimal2. The result of multiplication is stored in arrays s[]. The problem is this code isn''t providing correct answer. I tried to figure out the problem but worthless, so now I seek the advice of experts.Here is the code for manipulating Long Hand Multiplication:

I am trying to do this:

1000

x

1110
-------------------

   0000

  1000

 1000

1000
-------------
= 1110000










int i=3,x=0,carry=0;
while(true)
{
    if(BeforeDecimal2[i]!=0)
    {
      for(int j=x;j>=0;j--)
      {
        if(s[j]==1 && BeforeDecimal1[j]==1 && carry==0)
        {
          carry=1;
          s[j]=0;
        }
        else if(s[j]==1 && BeforeDecimal1[j]==0 && carry==1)
        {
          carry=1;
          s[j]=0;
        }
        else if(s[j]==0 && BeforeDecimal1[j]==0 && carry==1)
        {
          carry=0;
          s[j]=1;
        }
        else if(s[j]==0 && BeforeDecimal1[j]==0 && carry==0)
        {
          carry=0;
          s[j]=0;
        }

        x++;
      }
    }
    else
    {
      for(int h=7;h>=0;h--)
      {
        if(h==0)
        {
          BeforeDecimal1[0]=0; // that is inserting zeros from the right
        }
        else
        {
          BeforeDecimal1[h]=BeforeDecimal1[h-1];
          BeforeDecimal1[h-1]=0;
        }
      }
    }
    if(i==0)
      break;

    i--;
  }





问候



索引和否则如果在同一行



Regards

Indexation and else if in the same line

推荐答案

我很抱歉这样说,但是...那很漂亮很不可读。



我不确定你使用的是什么算法,但我怀疑它是否包含了很多嵌套,如果是'。

我认为你需要做的第一件事就是修改那一批的缩进,因为它目前非常令人困惑。

首先尝试改变:

I''m sorry to say this, but...that is pretty much unreadable.

I''m not sure what algorithm you are using, but I doubt if it included quite so many nested if''s.
I think the first thing you need to do is look at revising the indentation of that lot, because it is seriously confusing at the moment.
Try starting by changing:
if(reverse==4 && carry==1 && temp==0)
{
    s[reverse]=1;
}
else
    if(reverse==4 && carry==0 && temp==1)
    {
        s[reverse]=1;
    }
    else
        if(reverse==4 && carry==0 && temp==0)



To

if(reverse==4 && carry==1 && temp==0)
{
    s[reverse]=1;
}
else if(reverse==4 && carry==0 && temp==1)
{
    s[reverse]=1;
}
else if(reverse==4 && carry==0 && temp==0)

所以如果没有跑掉页面的右侧就更清楚了。

然后看看评论 - 我完全不知道你为什么要做测试你是,并且不知道你想要实现的算法我不能从那个代码中解决它。



对不起,但这需要是在我可以提供帮助之前让它变得可读,并且解释得更好!

So that it is clearer without running off the right hand side of the page.
Then look at commenting - I have absolutely no idea why you are doing the tests you are, and without any idea of the algorithm you are trying to implement I can''t work it out from that code.

Sorry, but this need to be made readable, and explained a bit better before I can help!


有点长,但是应该有效:

Bit long, but should work:
#include <stdio.h>

void mul(int bd1[4], int bd2[4], int s[8])
{
  int i,j;

  for (i=0;i<8;i++)
    s[i] = 0;

  for (i=0; i<4; i++)
  {
    int c = 0;
    if ( bd2[3-i] )
    {
      for (j=0; j<4; j++)
      {
        if ( s[i+3-j] == 1)
        {
          if (bd1[3-j] == 1)
          {
            if (c==1)
            {
              s[i+3-j] = 1;
              c = 1;
            }
            else
            { //c == 0
              s[i+3-j] = 0;
              c = 1;
            }
          }
          else
          { // bd1[3-j] == 0
            if (c==1)
            {
              s[i+3-j] = 0;
              c = 1;
            }
            else
            { //c == 0
              s[i+3-j] = 1;
              c = 0;
            }
          }
        }
        else
        {// s[i+3-j] == 0
          if (bd1[3-j] == 1)
          {
            if (c==1)
            {
              s[i+3-j] = 0;
              c = 1;
            }
            else
            { //c == 0
              s[i+3-j] = 1;
              c = 0;
            }
          }
          else
          { // bd1[3-j] == 0
            if (c==1)
            {
              s[i+3-j] = 1;
              c = 0;
            }
            else
            { //c == 0
              s[i+3-j] = 0;
              c = 0;
            }
          }
        }
      }
    }
  }
}

int main()
{
    int i;
  int bd1[]={1,0,0,0};
  int bd2[]={1,1,1,0};
  int s[8];
  mul(bd1,bd2,s);


  for(i=0;i<8;i++)
    printf("%d", s[i]);
  printf("\n");
  return 0;
}


这篇关于C ++中的长手乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆