我正在尝试编写一个代码,该代码将二进制数输入作为字符串,并且只接受1或0,否则应显示错误消息。然后它应该逐位循环以将二进制数转换为字符串为十进制。我似乎无法做到正确我有事实,它只会接受1或0的正确。但是当它进入计算时会出现一些混乱,我似乎无法将其弄清楚。目前这是我认为最接近它的工作。任何人都可以给我一些暗示或帮助我解决我的错误吗?
#include <iostream>
#include <string>
using namespace std;
string a;
int input();
int main()
{
input();
int decimal, x= 0, length, total = 0;
length = a.length();
// atempting to make it put the digits through a formula backwords.
for (int i = length; i >= 0; i--)
{
// Trying to make it only add the 2^x if the number is 1
if (a[i] = '1')
{
//should make total equal to the old total plus 2^x if a[i] = 1
total = total + pow(x,2);
}
//trying to let the power start at 0 and go up each run of the loop
x++;
}
cout << endl << total;
int stop;
cin >> stop;
return 0;
}
int input()
{
int x, x2, count, repeat = 0;
while (repeat == 0)
{
cout << "Enter a string representing a binary number => ";
cin >> a;
count = a.length();
for (x = 0; x < count; x++)
{
if (a[x] != '0' && a[x] != '1')
{
cout << a << " is not a string representing a binary number>" << endl;
repeat = 0;
break;
}
else
repeat = 1;
}
}
return 0;
}
答案 0 :(得分:0)
pow
适合整数计算。在这种情况下,您可以使用shift operator。a[i] = '1'
将a[i]
的值设置为'1'
并返回'1'
,这始终为真。a[length]
,这应该毫无意义。固定代码:
int main()
{
input();
int decimal, x= 0, length, total = 0;
length = a.length();
// atempting to make it put the digits through a formula backwords.
for (int i = length - 1; i >= 0; i--)
{
// Trying to make it only add the 2^x if the number is 1
if (a[i] == '1')
{
//should make total equal to the old total plus 2^x if a[i] = 1
total = total + (1 << x);
}
//trying to let the power start at 0 and go up each run of the loop
x++;
}
cout << endl << total;
int stop;
cin >> stop;
return 0;
}
答案 1 :(得分:0)
我会用这种方法......
{{1}}
答案 2 :(得分:0)
我在您的代码中看到多个错误。
for
- 循环应该从i = length - 1
开始,而不是i = length
。a[i] = '1'
将a[i]
设置为'1'
,但不进行比较。pow(x,2)
表示x^2 http://latex.codecogs.com/gif.download?x%5E2而非2^x http://latex.codecogs.com/gif.download?2%5Ex。 pow
也不适用于整数运算。请改用2*2*...
或1<<e
。还有更短的方法来实现它。以下是我将如何做的示例:
std::size_t fromBinaryString(const std::string &str)
{
std::size_t result = 0;
for (std::size_t i = 0; i < str.size(); ++i)
{
// '0' - '0' == 0 and '1' - '0' == 1.
// If you don't want to assume that, you can use if or switch
result = (result << 1) + str[i] - '0';
}
return result;
}
答案 3 :(得分:0)
适用于最多32位的二进制字符串。长时间交换整数以获得64位。
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
string getBinaryString(int value, unsigned int length, bool reverse) {
string output = string(length, '0');
if (!reverse) {
for (unsigned int i = 0; i < length; i++) {
if ((value & (1 << i)) != 0) {
output[i] = '1';
}
}
}
else {
for (unsigned int i = 0; i < length; i++) {
if ((value & (1 << (length - i - 1))) != 0) {
output[i] = '1';
}
}
}
return output;
}
unsigned long getInteger(const string& input, size_t lsbindex, size_t msbindex) {
unsigned long val = 0;
unsigned int offset = 0;
if (lsbindex > msbindex) {
size_t length = lsbindex - msbindex;
for (size_t i = msbindex; i <= lsbindex; i++, offset++) {
if (input[i] == '1') {
val |= (1 << (length - offset));
}
}
}
else { //lsbindex < msbindex
for (size_t i = lsbindex; i <= msbindex; i++, offset++) {
if (input[i] == '1') {
val |= (1 << offset);
}
}
}
return val;
}
int main() {
int value = 23;
cout << value << ": " << getBinaryString(value, 5, false) << endl;
string str = "01011";
cout << str << ": " << getInteger(str, 1, 3) << endl;
}