yuqing31 2023-03-18 12:11 采纳率: 33.3%
浏览 52
已结题

关于c++里将十进制转换为二进制的问题

我一共创建了3个变量,分别是l1,l2,B,l1和l2是我想要将其转换成2进制的数字,B是基数,所以这里的B是2
我想要实现:输入l1,l2和基数B,输出为l1和l2计算后的结果。
                               input = 5 5 2
                               output = 101 101
用代码块功能插入代码,
#include <iostream>
using namespace std;
#include <vector>
int main(){
    string userInputString1 = "";      //用户输入l1
    string userInputString2 = "";      //用户输入l2
    string userInputB = "";                //用户输入基数

    cout << "Please input l1, l2 and B:" << endl;
    cin >> userInputString1 >> userInputString2 >> userInputB;
    vector<char> l1;      //这个是第二题需要用到的步骤和本问题无关
    vector<char> l2;
    int B = stoi(userInputB);   //转换为int类型
    int tmpl1 = stoi(userInputString1);
    int tmpl2 = stoi(userInputString2);
    copy(userInputString1.c_str(), userInputString1.c_str() + userInputString1.length(), back_inserter(l1));
    copy(userInputString2.c_str(), userInputString2.c_str() + userInputString2.length(), back_inserter(l2));

    //A:
    int a[l1.size()], n, i;     
    n = tmpl1;
    for(i = 0; n > 0; i++){
        a[i] = n % B;       // 余数
        n = n / B;
    }
    cout << "Base be of the given numberA = ";
    for(i = i-1; i >= 0;i--){
        cout << a[i];
    }
    
    //B:
    int b[l2.size()];
    int z = tmpl2;
    for(i = 0; z > 0; i++){
        b[i] = z % B;
        n = n / B;
    }
    cout << "Base be of the given numberB = ";
    for(i = i-1; i >= 0; i--){
        cout << "\n" << b[i];
    }
}
运行结果及详细报错内容

我得到的输出:

Base be of the given numberA = 101

没有输出我的numberB我不懂是为什么?
感谢回复

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
  • 写回答

3条回答 默认 最新

  • 追cium 2023-03-18 12:15
    关注

    参考GPT和自己的思路:

    首先,需要修改一下代码中B的类型,从int改为char,因为在后面转换为二进制的过程中需要用到字符型。

    接下来,第二个for循环的赋值语句中,是应该把z除以B而不是n除以B,因为n在之前的循环中已经被赋值为0了。

    修改后的代码如下:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        string userInputString1 = "";    //用户输入l1
        string userInputString2 = "";    //用户输入l2
        string userInputB = "";          //用户输入基数
    
        cout << "Please input l1, l2 and B:" << endl;
        cin >> userInputString1 >> userInputString2 >> userInputB;
        vector<char> l1;  //这个是第二题需要用到的步骤和本问题无关
        vector<char> l2;
        char B = userInputB[0];  //转换为char类型
        int tmpl1 = stoi(userInputString1);
        int tmpl2 = stoi(userInputString2);
        copy(userInputString1.c_str(), userInputString1.c_str() + userInputString1.length(), back_inserter(l1));
        copy(userInputString2.c_str(), userInputString2.c_str() + userInputString2.length(), back_inserter(l2));
    
        //A:
        int a[l1.size()], n, i;
        n = tmpl1;
        for (i = 0; n > 0; i++)
        {
            a[i] = n % B;   // 余数
            n = n / B;
        }
        cout << "Base be of the given numberA = ";
        for (i = i - 1; i >= 0; i--)
        {
            cout << a[i];
        }
    
        //B:
        int b[l2.size()];
        int z = tmpl2;
        for (i = 0; z > 0; i++)
        {
            b[i] = z % B;
            z = z / B;  //应该是z而不是n
        }
        cout << endl << "Base be of the given numberB = ";
        for (i = i - 1; i >= 0; i--)
        {
            cout << b[i];
        }
    
        return 0;
    }
    

    输出结果:

    Please input l1, l2 and B:
    5 5 2
    Base be of the given numberA = 101
    Base be of the given numberB = 101
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月26日
  • 已采纳回答 3月18日
  • 创建了问题 3月18日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装