由于我本学期在物理学校学习了数字系统课程,我决定尝试将我们在那里学到的知识应用到我自学的编程实践中。是的,出于某种原因,我们根本不这样做。无论如何,下面是我的代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int num = 0;
int power = 0; // used to calculate the power of the digit later
vector<int> binVec; // holds binary value
vector<int> decVec; // holds converted dec value
cout << "Input binary number for conversion to its decimal...n";
while (cin >> num)
{
binVec.push_back(num);
}
for (vector<int>::size_type i = 0; i<= binVec.size(); i++)
{
int temp;
temp = (binVec[i]*2)^power;
decVec.push_back(temp);
if (power = 0)
{
power = 2;
}
else
{
power = power * 2;
}
}
cout << "The decimal value is n";
for (vector<int>::size_type j = 0; j<= decVec.size(); j++)
{
cout << decVec[j];
}
return 0;
}
不用说,它将无法正常工作。一开始我犯了一些愚蠢的错误,但是大约半个小时,现在我正在用它破解我的头,我得到了奇怪的输出。例如,当我输入简单的 (10)bin 并期望一个 (2)dec 时,我得到一串以 2 开头的数字,就像 20006721 一样。此外,当程序运行时,我的编译器会发送一条错误消息。可能出了什么问题?
我知道我的代码很糟糕,而且没有得到很好的优化,所以任何反馈或责骂将不胜感激!
你的 if 语句有拼写错误:
if (power = 0)
您需要使用==
进行比较:
if (power == 0)
此外,在您的 for 循环中,您可以让它运行 1 次:
i <= binVec.size();
数组的索引从 0 到大小 - 1;因此,这样做<=
会导致访问向量范围之外的地址出现未定义的行为。将其更改为:
i < binVec.size();
你的算法有缺陷。例如,每个二进制数字输出一个十进制数字。但是,对于大多数二进制数,十进制数的长度会更小。
此外,"^"运算符是C++中的二进制 XOR 运算符,而不是幂运算符。
而不是你的主循环,我会建议这样的东西:
int decimalNumber = 0;
for (vector<int>::size_type i = 0; i < binVec.size(); i++)
{
// Note that I changed "less or equal" to "less than"
decimalNumber *= 2;
decimalNumber += (binVec[i]);
}
cout << decimalNumber;
使用 STL 的 accumulate() 算法的较短版本:
#include <iostream>
#include <numeric>
#include <iterator>
using namespace std;
int main()
{
cout << "Input binary number for conversion to its decimal..." << endl;
cout << "The decimal value is: "
<< accumulate(istream_iterator<bool>(cin), istream_iterator<bool>(), 0,
[](int a, int b) { return (a << 1) + b; })
<< endl;
}
已经指出了一些错误。 尚未提到您正在做一些相当奇怪的事情 - 即与^
运算符一起使用 XOR。我想知道您是否打算将其用作power
操作?
这是一个建议的转换 - 使用基本的字符串操作。
char* inputString = "101001010";
int ii, dec=0;
for(ii=0; ii<strlen(inputString); ii++) {
if(*(inputString+ii)=='1') {
dec = 2 * dec + 1;
}
else {
dec = 2 * dec;
}
}
printf("the conversion to digital is %dn", dec);
另一种选择 - 使用 ASCII 表中"0"和"1"彼此相邻的事实:
char* inputString = "101001010";
int ii, dec=0;
for(ii=0; ii<strlen(inputString); ii++) {
dec = 2 * dec + (int)(inputString[ii]-'0');
}
printf("the conversion to digital is %dn", dec);
你有几个简单的语法/语义错误,以及一个需要考虑的概念问题。 首先,小心分配/相等测试,你真的不需要从零开始幂,而是 1(因为 2^0 = 1),
这是您的代码的一部分,已修复,
int main()
{
int num = 0;
int bits=0; // you could use bitshift, rather than multiply
int power=1; // 2^0 = 1, so start power at 1, not 0
您循环将每个位位置转换为一个数字,但不要累加它们,请考虑 push_back(temp) 与 accum+=temp 的结果
int temp;
int accum=0;
bitpos=0; power=1;
for (vector<int>::size_type i=0; i<binVec.size(); i++)
{
temp = (binVec[i])*power;
decVec.push_back(temp);
accum += (binVec[i])<<bitpos;
bitpos++; power*=2;
}
由于您转换了每个十进制数字,因此您有一个十进制值列表,但这些值不是个位数,它们是基于向量中位置的 2 (2^n) 的幂。 您可能更喜欢存储在累加器(ulator)中的值。 当您查看结果时,这一点很清楚,在每个矢量元素之间打印一个逗号",",
cout << "The decimal values are ";
for (vector<int>::size_type j=0; j<decVec.size(); j++)
{
cout << decVec[j] << ",";
}
cout<<endl;
cout << "The decimal value is n" << accum << endl;
结果
./bin2dec
Input binary number for conversion to its decimal: 1 1 1 1 1 1 1 0
The decimal values are 1,2,4,8,16,32,64,0,
The decimal value is
127
这说明了您的最后一个概念项,即您的程序认为最左边的数字最不重要,这可能是您的意图,但相反。 这可以通过在最右侧的值启动电源/位字来解决,
int temp;
int accum=0;
//bitpos=0; power=1;
bitpos=(int)binVec.size()-1; power=2<<bitpos;
for (vector<int>::size_type i=0; i<binVec.size(); i++)
{
temp = (binVec[i])<<bitpos;
accum += (binVec[i])<<bitpos;
decVec.push_back(temp);
//bitpos++; power*=2;
bitpos--; power/=2;
}
这给出了更预期的结果,
./bin2dec
Input binary number for conversion to its decimal: 1 1 1 1 1 1 1 0
The decimal values are 128,64,32,16,8,4,2,0,
The decimal value is
254