简体   繁体   English

C ++删除二进制数组中的前导零

[英]C++ removing leading zeros in a binary array

I am making a program that adds two binary numbers (up to 31 digits) together and outputs the sum in binary. 我正在制作一个程序,将两个二进制数(最多31位)加在一起,然后以二进制形式输出总和。

I have every thing working great but I need to remove the leading zeros off the solution. 我的一切工作都很好,但是我需要删除解决方案中的前导零。

This is what my output is: 这是我的输出是:

char c[32];
int carry = 0;
if(carry == '1')
{
    cout << carry;
}

for(i = 0; i < 32; i++)
{
    cout << c[i]; 
}

I tried this but it didn't work: 我尝试了这个,但它不起作用:

char c[32];
int carry = 0;
bool flag = false;

if(carry == '1')
{
    cout << carry;
}

for(i=0; i<32; i++)
{
    if(c[i] != 0)
    {
        flag = true;

        if(flag)
        {
            for(i = 0; i < 32; i++)
            {
                cout << c[i]; 
            }
        }
    }
}

Any ideas or suggestions would be appreciated. 任何想法或建议将不胜感激。

EDIT: Thank you everyone for your input, I got it to work! 编辑:谢谢大家的输入,我开始工作了!

You should not have that inner loop (inside if(flag) ). 您不应该具有该内部循环(在if(flag)内部)。 It interferes with the i processing of the outer loop. 它会干扰外循环的i处理。

All you want to do at that point is to output the character if the flag is set. 此时,您要做的就是输出设置了标志的字符。

And on top of that, the printing of the bits should be outside the detection of the first bit. 最重要的是,这些位的打印应该在第一个位的检测范围之外。

The following pseudo-code shows how I'd approach this: 以下伪代码显示了如何实现此目的:

set printing to false
if carry is 1:
    output '1:'

for each bit position i:
    if c[i] is 1:
        set printing to true
    if printing:
        output c[i]

if not printing:
    output 0

The first block of code may need to be changed to accurately output the number with carry. 可能需要更改第一段代码以准确输出带有进位的数字。 For example, if you ended up with the value 2 and a carry, you would want either of: 例如,如果最后得到的值是2和一个进位,那么您将需要:

1:10                              (or some other separator)
100000000000000000000000000000010 (33 digits)

Simply outputting 110 with no indication that the leftmost bit was a carry could either be: 仅输出110而没有指示最左边的位是进位,可能是:

  • 2 with carry; 2随身携带; or 要么
  • 6 without carry 6不带

The last block ensures you have some output for the value 0 which would otherwise print nothing since there were no 1 bits. 最后一块确保您有一些值为0的输出,否则将没有任何输出,因为没有1位。

I'll leave it up to you whether you should output a separator between carry and value (and leave that line commented out) or use carry to force printing to true initially. 无论您是在进位和值之间输出分隔符(然后将该行保留为注释)还是使用进位来强制将printing初始设置为true,我都会自己决定。 The two options would be respectively: 这两个选项分别是:

if carry is 1:
    output '1 '

and: 和:

if carry is 1:
    output 1
    set printing to true

And, since you've done the conversion to C++ in a comment, that should be okay. 而且,由于您已经在注释中完成了到C ++的转换,因此应该可以。 You state that it doesn't work, but I typed in your code and it worked fine, outputting 10 : 您声明它不起作用,但是我输入了您的代码,它工作正常,输出10

#include <iostream>

int main(void)
{
    int i;
    int carry = 0;
    int c[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
    bool print = false;

    // This is the code you gave in the comment, slightly modified.
    // vvvvvv
    if(carry == 1) {
        std::cout << carry << ":";
    }

    for (i = 0; i < 32; i++) {
        if (c[i] == 1) {
            print = true;
        }

        if (print) {
            std::cout << c[i];
        }
    }
    // ^^^^^^

    std::cout << std::endl;

    return 0;
}
const char * begin = std::find(c, c+32, '1');
size_t len = c - begin + 32;
std::cout.write(begin, len);

Use two fors over the same index. 在同一个索引上使用两个fors。 The first for iterates while == 0, the second one prints starting from where the first one left off. 第一个在== 0时进行迭代,第二个从第一个中断处开始打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM