在C++中将二进制转换为十进制而不使用条件语句
Converting binary to decimal in C++ without using conditionals
我目前正在处理一项任务,该任务要求我用C++编写一个程序,将用户输入的二进制数转换为十进制数。听起来不太复杂吧?我们不能使用任何形式的条件语句,不能使用字符串,不能使用二进制到十进制函数(记不清它在哪个库中,但我知道它存在(。基本上,我们不能用课堂上没有学到的东西。
我想用我们在课堂上学到的东西开场会更容易:
- 正在导入库
- 为变量赋值
- 基本数学运算符
我相信一个好的起点是模块划分,我只是真的不知道如果不使用条件句,我该如何获得正确的答案。在过去的48小时里,我一直坐在这里,绞尽脑汁,但我仍然没有那一刻。
感谢您的提前帮助!
编辑:就其价值而言,我们被告知假设这些值是无符号的,并且最大大小为4位。
如果允许您使用循环,它将非常简单:
#include <iostream>
#include <string>
int main()
{
std::string str("1101");
int result = 0;
for(int i=str.size()-1, pos=0; i>=0; --i, ++pos)
result += str[i] == '0'? 0 : 1 << pos;
std::cout << result << std::endl;
}
但是,在没有循环或递归的情况下做到这一点吗?谁说你必须准确!
#include <iostream>
#include <string>
#include <cmath>
long long not_very_accurate(long long input)
{
long long power = ceil(log10(input+1));
return std::pow(2, power);
}
int main()
{
long long input = 1011010110;
std::cout << not_very_accurate(input) << std::endl; //almost the same :)
}
最大误差为实际结果的2倍。1
使用越多,结果越好。
作为提示,以下是对两个位的操作方法:
int main()
{
int binary;
cin >> binary;
int decimal = 2*(binary/10) + binary%10;
cout << decimal << 'n';
}
示例:
如果二进制==11,则
2*(11/10(+(11%10(==3
由于您在下面的注释中说过输入限制为4位,因此只需使用几个扩展表达式即可处理4或5个不同的输入长度。
这里的指导技巧是,像str[0] == '1'
这样的布尔表达式将隐式转换为零或一。然后可以对这些部分进行"数学运算"。
既然这显然是家庭作业,我不会帮你完成的。如果由于类中尚未引入数组而需要删除table
数组成员变量,则由您决定。但是,如果您使用已经提供的示例,它是可以做到的。
最后,StackOverflow并不是作为你的个人机械土耳其人来为你做作业的。在未来,你需要展示你已经尝试过的东西,并阐明你需要帮助的地方。
int DecimalFromBinary(char* str)
{
int result = 0;
int len = (str[0] != ' ') +
((str[0] != ' ') && (str[1] != ' ')) +
((str[0] != ' ') && (str[1] != ' ') && (str[2] != ' ')) +
((str[0] != ' ') && (str[1] != ' ') && (str[2] != ' ') && (str[3] != ' '));
int table[5] = { 1,8,4,2,1 };
result += (str[0] == '1') * 8;
result += ((len > 1) && (str[1] == '1')) * 4;
result += ((len > 2) && (str[2] == '1')) * 2;
result += ((len > 3) && (str[3] == '1')) * 1;
result /= table[len];
// printf("%dn", result);
return result;
}
// Prompts the user for a binary number and then returns the decimal equivalent without making use of conditionals/loops
int first_bit;
int second_bit;
int third_bit;
int fourth_bit;
int decimal;
cout << "What is the first digit in your 4-bit binary number?" << endl;
cin >> first_bit;
cout << "What is the second digit in your 4-bit binary number?" << endl;
cin >> second_bit;
cout << "What is the third digit in your 4-bit binary number?" << endl;
cin >> third_bit;
cout << "What is the fourth digit in your 4-bit binary number?" << endl;
cin >> fourth_bit;
cout << "Your 4-bit binary number is " << first_bit << second_bit << third_bit << fourth_bit << endl;
decimal = ((first_bit) * pow(2, 3)) + ((second_bit) * pow(2, 2)) + ((third_bit) * pow(2, 1)) + ((fourth_bit) * pow(2, 0));
cout << "The decimal equivalent of " << first_bit << second_bit << third_bit << fourth_bit << " is " << decimal << endl;
嘿,伙计们,终于为我的问题找到了答案。这可能不是接收4位二进制数的用户输入的理想方法,但这是我能想到的最简单的过程,不涉及使用循环或条件,每次都能得到正确的答案。谢谢你的帮助!
我是一个初学者,这就是我的想法。。。
请让我知道这个代码是否可以改进
#include <iostream>
#include<math.h>
using namespace std;
int main(){
int psuedo, bin,q, rem;
cout<<"Enter a binary number: ";
cin>> bin;
int num=0;
psuedo = bin;
while(q!=0){
q=psuedo/10; // psuedo helps us to count number of digits used in binary
num++; // num represents number of digits in binary
psuedo=q;
}
int dec=0;
for(int i =0; i<num; i++){
rem = bin%10;
dec = (rem*pow(2,i))+dec; // conversion to decimal number from binary
q = bin/10;
bin = q;
}
cout<<"The decimal number is: "<<dec<<endl;
return 0;
}
示例:
输入仓=0101
输出dec=5
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ 中的"if"语句不会从左到右计算条件
- 优化/减少 if 语句中的条件表达式
- CPP 带有条件语句的内在事物
- 有没有办法在循环中的条件至少满足一次时运行语句?
- 如果验证容器的大小并在同一条件语句下访问元素,这是很好的做法吗?
- 如果语句错误地读取字符串,则始终在第一个条件返回
- 条件不变的 if 语句会减慢我的C++代码吗?
- C++ - 这个条件语句写得正确吗?还是有更有效的写法?
- C++17 if 语句,带有初始值设定项但没有条件
- if语句中满足的条件的索引
- C++:如何在 if 语句条件中使用指针
- 在 CPP 中将函数调用定时为 if 语句条件
- c++ 读取左 IF 语句条件和右语句条件
- C++忽略if语句条件
- 考虑到模板参数,正在更改if语句条件
- IF语句条件内的循环