鱼C论坛

 找回密码
 立即注册
查看: 1381|回复: 3

[已解决]这个求32位二进制补码的代码为什么错误

[复制链接]
发表于 2023-10-29 16:08:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
using namespace std;
#include <string>
#include <vector>
string cheng(string x, int n)
{
        string res = "";
        for (int i = 1; i <= n; i++)
        {
                res = res + x;
        }
        return res;
}
int main()
{
        int n;
        int len;
        cin >> n;
        string res;
        string x = "0";
        vector<int> shuzi;
        int n2 = n;
        if (n2 < 0)
        {
                n = 1-n;
        }
        while (n != 1)
        {
                shuzi.push_back(n % 2);
                n = n / 2;
                if (n == 1)
                {
                        shuzi.push_back(1);
                }
        }
        len = shuzi.size();
        if (n2 > 0)
        {
                res = cheng(x, 32 - len);
                for (int i = shuzi.size() - 1; i >= 0; i--)
                {
                        res = res + to_string(shuzi[i]);
                }
                cout << res;
                return 0;
        }
        else if (n2 == 0)
        {
                cout << 000000000000000000000000000000;
                return 0;
        }
        else
        {
                res = cheng(x, 32 - len);
                for (int i = shuzi.size() - 1; i >= 0; i--)
                {
                        res = res + to_string(shuzi[i]);
                }
                for (int i = 0; i <= 32; i++)
                {
                        if (res[i] == '1')
                        {
                                res[i] = '0';
                        }
                        else
                        {
                                res[i] = '1';
                        }
                }
                cout << res;
                return 0;
        }
}
最佳答案
2023-10-29 16:09:43
这个代码有几个问题:

1. 对于负数的处理不正确。在计算机中,负数的二进制表示通常使用的是二进制补码,即首先取其绝对值的二进制表示,然后对每一位取反(0变为1,1变为0),最后加1。但在你的代码中,你只是简单地对每一位取反,没有加1。

2. 对于0的处理不正确。在你的代码中,你试图输出32个0,但是你使用的是整数000000000000000000000000000000,这在C++中会被解析为一个整数0,而不是一个包含32个0的字符串。你应该使用字符串\"00000000000000000000000000000000\"。

3. 对于二进制数的长度处理不正确。在你的代码中,你试图通过添加前导0来使二进制数的长度达到32位,但是你没有考虑到当输入的整数已经是32位时,你的代码会产生一个长度超过32位的字符串。

以下是修正后的代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string cheng(string x, int n)
{
    string res = "";
    for (int i = 1; i <= n; i++)
    {
        res = res + x;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    string res;
    string x = "0";
    vector<int> shuzi;
    int n2 = n;
    if (n2 < 0)
    {
        n = -n;
    }
    do
    {
        shuzi.push_back(n % 2);
        n = n / 2;
    } while (n != 0);

    int len = shuzi.size();
    res = cheng(x, 32 - len);
    for (int i = shuzi.size() - 1; i >= 0; i--)
    {
        res = res + to_string(shuzi[ i]);
    }

    if (n2 < 0)
    {
        for (int i = 0; i < 32; i++)
        {
            res[ i] = res[ i] == '1' ? '0' : '1';
        }
        for (int i = 31; i >= 0; i--)
        {
            if (res[ i] == '1')
            {
                res[ i] = '0';
            }
            else
            {
                res[ i] = '1';
                break;
            }
        }
    }

    cout << res;
    return 0;
}

这个修正后的代码首先将输入的整数转换为二进制表示,然后如果输入的整数是负数,就对二进制表示进行取反和加1操作,得到二进制补码。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-29 16:09:02 | 显示全部楼层
这段代码存在以下几个问题:

1. 在第39行,当n为负数时,将n赋值给n2,并将n变为1-n。这是为了处理负数的情况,但是在后续的计算中,使用的是n2而不是n,导致结果错误。

2. 在第43行至第48行,计算二进制表示时,没有考虑到n为负数的情况。对于负数,应该将其绝对值进行二进制转换,然后取反加1。在这段代码中,只考虑了n为正数和0的情况。

3. 在第61行至第68行,对于负数的二进制补码的计算有误。应该先计算补码,然后再取反。但是这段代码中,先计算了二进制表示,然后又对其进行取反操作。这样得到的结果是不正确的。

以下是修改后的代码:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string cheng(string x, int n)
{
    string res = "";
    for (int i = 1; i <= n; i++)
    {
        res = res + x;
    }
    return res;
}

int main()
{
    int n;
    int len;
    cin >> n;
    string res;
    string x = "0";
    vector<int> shuzi;
    int n2 = n;
    if (n2 < 0)
    {
        n = abs(n);
        n2 = 1 - n;
    }
    while (n != 1)
    {
        shuzi.push_back(n % 2);
        n = n / 2;
        if (n == 1)
        {
            shuzi.push_back(1);
        }
    }
    len = shuzi.size();
    if (n2 > 0)
    {
        res = cheng(x, 32 - len);
        for (int i = shuzi.size() - 1; i >= 0; i--)
        {
            res = res + to_string(shuzi[ i]);
        }
        cout << res;
        return 0;
    }
    else if (n2 == 0)
    {
        cout << "000000000000000000000000000000";
        return 0;
    }
    else
    {
        res = cheng(x, 32 - len);
        for (int i = shuzi.size() - 1; i >= 0; i--)
        {
            res = res + to_string(shuzi[ i]);
        }
        for (int i = 0; i < 32; i++)
        {
            if (res[ i] == '1')
            {
                res[ i] = '0';
            }
            else
            {
                res[ i] = '1';
            }
        }
        cout << res;
        return 0;
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-29 16:09:43 | 显示全部楼层    本楼为最佳答案   
这个代码有几个问题:

1. 对于负数的处理不正确。在计算机中,负数的二进制表示通常使用的是二进制补码,即首先取其绝对值的二进制表示,然后对每一位取反(0变为1,1变为0),最后加1。但在你的代码中,你只是简单地对每一位取反,没有加1。

2. 对于0的处理不正确。在你的代码中,你试图输出32个0,但是你使用的是整数000000000000000000000000000000,这在C++中会被解析为一个整数0,而不是一个包含32个0的字符串。你应该使用字符串\"00000000000000000000000000000000\"。

3. 对于二进制数的长度处理不正确。在你的代码中,你试图通过添加前导0来使二进制数的长度达到32位,但是你没有考虑到当输入的整数已经是32位时,你的代码会产生一个长度超过32位的字符串。

以下是修正后的代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string cheng(string x, int n)
{
    string res = "";
    for (int i = 1; i <= n; i++)
    {
        res = res + x;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    string res;
    string x = "0";
    vector<int> shuzi;
    int n2 = n;
    if (n2 < 0)
    {
        n = -n;
    }
    do
    {
        shuzi.push_back(n % 2);
        n = n / 2;
    } while (n != 0);

    int len = shuzi.size();
    res = cheng(x, 32 - len);
    for (int i = shuzi.size() - 1; i >= 0; i--)
    {
        res = res + to_string(shuzi[ i]);
    }

    if (n2 < 0)
    {
        for (int i = 0; i < 32; i++)
        {
            res[ i] = res[ i] == '1' ? '0' : '1';
        }
        for (int i = 31; i >= 0; i--)
        {
            if (res[ i] == '1')
            {
                res[ i] = '0';
            }
            else
            {
                res[ i] = '1';
                break;
            }
        }
    }

    cout << res;
    return 0;
}

这个修正后的代码首先将输入的整数转换为二进制表示,然后如果输入的整数是负数,就对二进制表示进行取反和加1操作,得到二进制补码。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-29 16:11:42 | 显示全部楼层
问题描述:

这段代码用于求一个32位整数的二进制补码,但是运行时出现错误。请问问题出在哪里?
#include <iostream>using namespace std;#include <string>#include <vector>string cheng(string x, int n){    string res = "";    for (int i = 1; i <= n; i++)    {        res = res + x;    }    return res;}int main(){    int n;    int len;    cin >> n;    string res;    string x = "0";    vector<int> shuzi;    int n2 = n;    if (n2 < 0)    {        n = 1-n;    }    while (n != 1)    {        shuzi.push_back(n % 2);        n = n / 2;        if (n == 1)        {            shuzi.push_back(1);        }    }    len = shuzi.size();    if (n2 > 0)    {        res = cheng(x, 32 - len);        for (int i = shuzi.size() - 1; i >= 0; i--)        {            res = res + to_string(shuzi[i]);        }        cout << res;        return 0;    }    else if (n2 == 0)    {        cout << 000000000000000000000000000000;        return 0;    }    else    {        res = cheng(x, 32 - len);        for (int i = shuzi.size() - 1; i >= 0; i--)        {            res = res + to_string(shuzi[i]);        }        for (int i = 0; i <= 32; i++)        {            if (res[i] == '1')            {                res[i] = '0';            }            else            {                res[i] = '1';            }        }        cout << res;        return 0;    }}
解决方案:

这段代码存在几个问题导致运行时出现错误:

1. 在使用using namespace std;时,应该将其放在头文件的外部,而不是在iostream头文件之后。例如,将using namespace std;移动到#include <string>之前。

2. 当输入为0时,输出的补码应该是全0而不是十进制的0。在输出0的补码时,需要将cout << 000000000000000000000000000000;修改为cout << "000000000000000000000000000000";,将其作为字符串进行输出。

3. 在对补码进行取反时,需要注意循环的结束条件。当前的代码中,循环条件为i <= 32,但数组的索引范围是0到31,所以应该修改为i < 32

下面是修改后的代码:
#include <iostream>#include <string>#include <vector>using namespace std;string cheng(string x, int n){    string res = "";    for (int i = 1; i <= n; i++)    {        res = res + x;    }    return res;}int main(){    int n;    int len;    cin >> n;    string res;    string x = "0";    vector<int> shuzi;    int n2 = n;    if (n2 < 0)    {        n = 1 - n;    }    while (n != 1)    {        shuzi.push_back(n % 2);        n = n / 2;        if (n == 1)        {            shuzi.push_back(1);        }    }    len = shuzi.size();    if (n2 > 0)    {        res = cheng(x, 32 - len);        for (int i = shuzi.size() - 1; i >= 0; i--)        {            res = res + to_string(shuzi[i]);        }        cout << res;        return 0;    }    else if (n2 == 0)    {        cout << "000000000000000000000000000000";        return 0;    }    else    {        res = cheng(x, 32 - len);        for (int i = shuzi.size() - 1; i >= 0; i--)        {            res = res + to_string(shuzi[i]);        }        for (int i = 0; i < 32; i++)        {            if (res[i] == '1')            {                res[i] = '0';            }            else            {                res[i] = '1';            }        }        cout << res;        return 0;    }}
请注意,以上修改只是修复了代码中的错误,并不保证逻辑的正确性。对于负数的二进制补码表示,可能需要考虑符号位的处理。具体实现还需根据需求进行调整。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 17:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表