如何反转程序的输出?

发布于 2024-12-11 10:16:09 字数 522 浏览 0 评论 0原文

我必须将 43.62 这样的十进制数转换为二进制数。所以我首先编写了一个将 43 转换为二进制的基本程序。但我注意到我的程序以相反的方式打印出二进制数,因此它打印 1 1 0 1 0 1 而不是 1 0 1 0 1 1。我该如何解决这个问题。

我的代码:

#include <iostream>

using namespace std;

int main()
{
    int number;
    int remainder;

    cout << "Enter a integer: ";
    cin >> number;

    while(number != 0)
    {
        remainder = number % 2;
        cout << remainder << " ";
        number /= 2;
    }

    int pause;
    cin >> pause;

    return 0;
}

I have to convert decimal numbers like 43.62 to binary. So i first wrote a basic program that converts 43 into binary. But I notice that my program prints out the binary number in reverse, so it prints 1 1 0 1 0 1 instead of 1 0 1 0 1 1. how can I fix this.

My Code:

#include <iostream>

using namespace std;

int main()
{
    int number;
    int remainder;

    cout << "Enter a integer: ";
    cin >> number;

    while(number != 0)
    {
        remainder = number % 2;
        cout << remainder << " ";
        number /= 2;
    }

    int pause;
    cin >> pause;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

神回复 2024-12-18 10:16:09

不要将每个数字发送到 cout,而是将它们发送到数组。然后以相反的顺序读出数组。或者将它们推入堆栈,然后将它们从堆栈中弹出。或者...

Instead of sending each digit to cout, send them to an array. Then read the array out in reverse order. Or push them onto a stack, and then pop them back off the stack. Or...

与君绝 2024-12-18 10:16:09

有点像用大锤来破解坚果,但这里有一个基于递归方法的解决方案:

#include <iostream>
using namespace std;

void OutputDigit(int number)
{
    if (number>0)
    {
        OutputDigit(number /= 2);
        cout << number % 2 << " ";
    }
}

int main()
{
    OutputDigit(43);
    return 0;
}

只需将 cout 移动一行即可获得与之前相同的输出!

Something of a sledgehammer to crack a nut, but here's a solution based on a recursive approach:

#include <iostream>
using namespace std;

void OutputDigit(int number)
{
    if (number>0)
    {
        OutputDigit(number /= 2);
        cout << number % 2 << " ";
    }
}

int main()
{
    OutputDigit(43);
    return 0;
}

You can get the same output as you had before by simply moving the cout one line up!

丢了幸福的猪 2024-12-18 10:16:09

查看 vector 并思考保存余数而不是立即打印它们有何用处。

请注意,您不必将内容放在向量的末尾。 vector::insert 让您指定一个位置...这有帮助吗?

或者,您创建的算法从最低有效数字开始。有没有办法从最高有效数字开始?如果我有数字 42 (0101010),则最高有效数字代表 32,前面的 0 代表 64。如果我用 42 减去 32 会发生什么?

Look at vector and think about how it could be useful to save the remainders instead of printing them right away.

Notice that you don't have to put things at the end of the vector. vector::insert lets you specify a position... could that be helpful?

Alternatively, the algorithm you created starts at the least significant digit. Is there a way to start from the most significant digit instead? If I have the number 42 (0101010), the most significant digit represents the 32s, and the 0 ahead of it represents the 64s. What happens if I subtract 32 from 42?

長街聽風 2024-12-18 10:16:09

存储结果然后向后打印会更容易。使用递归也是实现这一目标的另一种可能性。

It would be easier to store the results and then print them backwards. Using recursion is also another possibility to do just that.

我不咬妳我踢妳 2024-12-18 10:16:09

最重要的位在前:

const unsigned int BITS_PER_INT = CHAR_BIT * sizeof(int);
char bit_char = '0';
for (int i = BITS_PER_INT - 1;
     i > 0;
     --i)
{
    bit_char = (value & (1 << i)) ? '1' : '0';
    cout << bit_char << ' ';
}
cout << '\n';
cout.flush();

要先打印最低的有效位,请更改 for 循环的方向。

Most significant bit first:

const unsigned int BITS_PER_INT = CHAR_BIT * sizeof(int);
char bit_char = '0';
for (int i = BITS_PER_INT - 1;
     i > 0;
     --i)
{
    bit_char = (value & (1 << i)) ? '1' : '0';
    cout << bit_char << ' ';
}
cout << '\n';
cout.flush();

To print least significant bit first, change the direction of the for loop.

梨涡 2024-12-18 10:16:09

在 C++ 中,您还可以使用位集容器来执行此操作,

#include <bitset>

int i = 43;
std::bitset<sizeof(int)*CHAR_BIT> bin(i);

In C++, you can also use a bitset container to do this,

#include <bitset>

int i = 43;
std::bitset<sizeof(int)*CHAR_BIT> bin(i);
茶色山野 2024-12-18 10:16:09

只需使用字符串函数

string s ;

while(number != 0)
{
    remainder = number % 2;
    string c = remainder ? "1": "0";
    s.insert(s.begin(),c.begin(),c.end());
    number /= 2;
}

Just use string functions

string s ;

while(number != 0)
{
    remainder = number % 2;
    string c = remainder ? "1": "0";
    s.insert(s.begin(),c.begin(),c.end());
    number /= 2;
}
素罗衫 2024-12-18 10:16:09

当您通过保留余数进行此类转换时,结果将始终被恢复。按照建议使用 bitwise &

unsigned char bit = 0x80; // start from most significant bit
int  number = 43;
while(bit)
{
    if( bit & number ) // check if bit is on or off in your number
    {
       cout << "1";
    }
    else
    {
       cout << "0";
    }
    bit = bit >>1; // move to next bit
}

此示例将开始遍历数字的所有 8 位,检查该位是打开还是关闭,并相应地打印出来。

When you do such conversion by holding on to the remainder, the result will always be reverted. As suggested use bitwise &:

unsigned char bit = 0x80; // start from most significant bit
int  number = 43;
while(bit)
{
    if( bit & number ) // check if bit is on or off in your number
    {
       cout << "1";
    }
    else
    {
       cout << "0";
    }
    bit = bit >>1; // move to next bit
}

This example will start going through all your 8 bits of the number and check if the bit is on or off and print that accordingly.

氛圍 2024-12-18 10:16:09

最佳选择 - 使用 C++ stringstream 格式化 I/O

// Add the following headers
    #include <sstream>
    #include <algorithm>

    // your function

    stringstream ss;

    // Use ss in your code instead of cout


    string myString = ss.str();
    std::reverse(myString.begin(),myString.end());
    cout << myString;

Best option - Use C++ stringstream for formatting I/O

// Add the following headers
    #include <sstream>
    #include <algorithm>

    // your function

    stringstream ss;

    // Use ss in your code instead of cout


    string myString = ss.str();
    std::reverse(myString.begin(),myString.end());
    cout << myString;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文