input = 5 5 2
output = 101 101
#include
using namespace std;
#include
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我不懂是为什么?
感谢回复
参考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
参考GPT和自己的思路:
首先,你的代码中有一个错误。在B部分的循环中,你错把n用于了计算余数的操作,应该使用z。修改后的代码如下:
//B:
int b[l2.size()];
int z = tmpl2;
for(i = 0; z > 0; i++){
b[i] = z % B;
z = z / B;
}
cout << "Base be of the given numberB = ";
for(i = i-1; i >= 0; i--){
cout << "\n" << b[i];
}
另外,根据你的问题描述,你想要将l1和l2 转换为二进制后相加。所以在A和B部分的循环中,你需要计算出l1和l2的二进制表示,并将它们相加,最终得到的结果也需要转换为二进制。以下是修改后的代码:
//A:
int a[l1.size()], n = tmpl1, i = 0;
while (n > 0) {
a[i++] = n % B;
n /= B;
}
int lenA = i;
//B:
int b[l2.size()], z = tmpl2;
i = 0;
while (z > 0) {
b[i++] = z % B;
z /= B;
}
int lenB = i;
//计算相加结果
i = 0;
int carry = 0, sum[l1.size()];
while (i < lenA || i < lenB) {
int tmp = (i < lenA ? a[i] : 0) + (i < lenB ? b[i] : 0) + carry;
sum[i] = tmp % B;
carry = tmp / B;
i++;
}
if (carry > 0) {
sum[i++] = carry;
}
int lenSum = i;
//输出结果
cout << "Result in base " << B << " = ";
for (i = lenSum - 1; i >= 0; i--) {
cout << sum[i];
}
最后,完整代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main(){
string userInputString1 = "";
string userInputString2 = "";
string userInputB = "";
cout << "Please input l1, l2 and B:" << endl;
cin >> userInputString1 >> userInputString2 >> userInputB;
int B = stoi(userInputB);
//A:
int a[100], n = stoi(userInputString1), i = 0;
while (n > 0) {
a[i++] = n % B;
n /= B;
}
int lenA = i;
//B:
int b[100], z = stoi(userInputString2);
i = 0;
while (z > 0) {
b[i++] = z % B;
z /= B;
}
int lenB = i;
//计算相加结果
i = 0;
int carry = 0, sum[100];
while (i < lenA || i < lenB) {
int tmp = (i < lenA ? a[i] : 0) + (i < lenB ? b[i] : 0) + carry;
sum[i] = tmp % B;
carry = tmp / B;
i++;
}
if (carry > 0) {
sum[i++] = carry;
}
int lenSum = i;
//输出结果
cout << "Result in base " << B << " = ";
for (i = lenSum - 1; i >= 0; i--) {
cout << sum[i];
}
cout << endl;
}