我一共创建了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我不懂是为什么?
感谢回复