我正在编写一个程序,该程序通过加倍(link to wikihow article)将二进制数转换为十进制数。
如果用户输入的内容不是1或0,则它不是二进制数,在这种情况下,我希望循环“中断”并说出类似的内容:
“糟糕!二进制数字只有1或0”。
如果不“那么”,则循环应继续。
那是我想编码的东西
for(int digits = 0; digits != digitsINbinNum; ++digits){
if(a condition that checks if user input is anything else than 1 or 0){
coût << ""Oops! Binary numbers have only 1 or 0" << endl;
break;
}else{
cin >> binArray[digits];/*<-----------Here's the part where I am trying to do that*/
}
}
有关更多信息,请参见下面给出的代码:
#include <iostream>
#include <iterator>
using namespace std;
int main(){
int digitsINbinNum;
cout << "If you don't mind. Please enter the number of digits in your binary number: ";
cin >> digitsINbinNum;
int binArray[digitsINbinNum];
cout << "Enter the binary number: ";
for(int digits = 0; digits != digitsINbinNum; ++digits){
cin >> binArray[digits];/*<-----------Here's the part where I am trying to do that*/
}
/*using the doubling method as found in wikihow.com*/
int total = 0;
for(int posiOFdigit = 0; posiOFdigit != sizeof(binNum[noOFdigits]); posiOFdigit++){
total = total * 2 + binNum[posiOFdigit];
}
/*Printing the number*/
cout << "Decimal form of ";
for(int n = 0; n != noOFdigits; n++){
cout << binNum[n];
}
cout << " is " << total;
return 0;
}
答案 0 :(得分:0)
可以通过问题中给定的link引用通过加倍方法将二进制数转换为十进制数的逻辑。
修改给定代码以使其尽可能接近问题的参考代码。
注意:由于ISO C ++禁止使用可变长度数组,因此我正在更改
int binArray[digits]
至
int *binArray = (int *)malloc(sizeof(int) * digitsINbinNum);
。
此修改使其成为整数指针,并在运行时获取所需大小的内存。
#include <iostream>
using namespace std;
int main(){
int digitsINbinNum,
/* variable to keep decimal conversion of given binary */
decimal_val = 0;
bool is_binary = true;
cout << "If you don't mind. Please enter the number of digits in your binary number: ";
cin >> digitsINbinNum;
/*
ISO C++ forbids variable length array,
making it int pointer and allocating dynamic memory
*/
int *binArray = (int *)malloc(sizeof(int) * digitsINbinNum);
if (binArray == NULL)
{
cout << "Memory allocation failure" << endl;
exit -1;
}
cout << "Enter the binary number: ";
for(int digits = 0; digits != digitsINbinNum; ++digits){
cin >> binArray[digits];
/*<-----------Here's the part where I am trying to do that*/
/* doubling method logic for conversion of given binary to decimal */
if ((binArray[digits] == 0) ||
(binArray[digits] == 1))
{
decimal_val = (decimal_val * 2) + binArray[digits];
}
else /* not a binary number */
{
is_binary = false;
cout << "Oops! Binary numbers have only 1 or 0" << endl;
break;
}
}
/* if conversion is successful: print result */
if (is_binary)
{
cout << "Decimal Value for given binary is: " << decimal_val << endl;
}
if (binArray)
{
free(binArray);
}
return 0;
}
答案 1 :(得分:-1)
您不需要为此的数组。这是一个简单的解决方案:
getOpenStatus = (restaurant: _Restaurant) => {
const closeHour = moment(restaurant.close_at, "HH:mm A").hours();
const closeMin = moment(restaurant.close_at, "HH:mm A").minutes();
const openHour = moment(restaurant.open_at, "HH:mm A").hours();
const openMin = moment(restaurant.open_at, "HH:mm A").minutes();
const closeMoment = moment({ hours: closeHour, minutes: closeMin });
const openMoment = moment({ hours: openHour, minutes: openMin });
if (closeMoment.isBefore(openMoment)) {
if (moment().isAfter(closeMoment)) closeMoment.add(1, "days");
else openMoment.subtract(1, "days");
}
return moment().isAfter(openMoment) && moment().isBefore(closeMoment);
}