程序中的布尔函数返回输入的范围无论如何都是无效的

Boolean function in program returns that an entered range is invalid no matter what

本文关键字:无论如何 范围 无效 返回 布尔 函数 程序 输入      更新时间:2023-10-16

我正在开发一个程序,该程序可以确定范围内是否存在重复数字的数字。程序本身运行良好 - 但它的一个组成部分是确定用户输入的范围是否有效。我需要为此创建一个布尔函数,称为range_is_valid。两个输入是 a 和 b,如果 b

#include <iostream>
#include <vector>
#include <stdbool.h>
using namespace std;

//function for finding repeating digits
bool has_repeating_digits(int number){
bool hasDigit[10] = {false, false, false, false, false, false, false, false, false, false};
int count = 0;
while (number > 0) {
int digit = number % 10;
if (hasDigit[digit]){
return true; // We already have this digit0
}
hasDigit[digit] = true; // Flag the digit we just found
number /= 10; // Divide by 10, to move onto the next digit
}
return false; // If we get here, there is no repeat digit
}  
//function for counting valid numbers
int count_valid_nums(int a, int b, int count){
int validnums = ((b - a) + 1) - count; //the total range minus the count of invalid nums leaves just valid nums
return validnums; 
}
//function for determining if inputted range is valid
bool range_is_valid(int a, int b){
if ((a > b) || (a < 0) || (b < 0) || (a > 10000) || (b > 10000)) {
return false; //if any of those are true then invalid range
}
}
int main(){
//variables
int a, b, i, count= 0; // a & b inputs, i for iterating
//entering number range
cout << "Enter numbers 0 < a <= b <= 10000: " << endl;
cin >> a;
cin >> b;
//Check if the range is valid
range_is_valid(a,b);
if (range_is_valid(a, b) == false){
cout << "The range is invalid" << endl;
}
//making a vector to contain numbers between a and b
vector<int> listofnums((b-a)+1); 
int initialvalue = a;
while (i <= (b-a)) {  
listofnums[i] = initialvalue;                                 

//call the has repeating digits function
has_repeating_digits(listofnums[i]);
if (has_repeating_digits(listofnums[i])){ //counting how many numbers in vector have repeat digits
count++;
}   
initialvalue++; 
i++;
}
//call the count valid numbers function
cout << "There are " << count_valid_nums (a, b, count) << " valid numbers between " << a << " and " << b << endl;
return 0;
}

当你有这个

if (condition) return true;
else return false;

你也可以写

return condition;

range_is_valid你有

if (condition) return false;
// missing return !!!!

condition == false时不返回任何内容。你可以把它写成

return !(condition);

这很简单。似乎这两个数字都应该是正数,并且不大于 10000。

例如,该函数可以通过以下方式查找

bool range_is_valid(int a, int b)
{
return a <= b && a > 0 && b <= 10'000; 
}

但是,这不是一个好方法,因为该函数使用幻数10000

最好传递这样的数字作为函数的参数。

考虑到这一点,函数可以如下所示

#include <utility>
//...
bool range_is_valid( const std::pair<int, int> &data, const std::pair<int, int> &range )
{
return data.first <= data.second && 
data.first > range.first  &&
data.second <= range.second;   
}

这是一个演示程序。

#include <iostream>
#include <iomanip>
#include <utility>
bool range_is_valid( const std::pair<int, int> &data, const std::pair<int, int> &range )
{
return data.first <= data.second && 
data.first > range.first  &&
data.second <= range.second;   
}
int main() 
{
const std::pair<int, int> range( 0, 10'000 );
int a, b;
size_t attempts = 3;
while ( attempts-- != 0 )
{
std::cout << "Enter numbers " << range.first << 
" < a <= b <= " << range.second << 'n';
std::cin >> a >> b;
std::cout << std::boolalpha << range_is_valid( { a, b }, range ) << 'n';
}
return 0;
}

它的输出可能看起来像

Enter numbers 0 < a <= b <= 10000
0 10000
false
Enter numbers 0 < a <= b <= 10000
1 10001
false
Enter numbers 0 < a <= b <= 10000
1 10000
true

始终尝试编写更通用的函数。

请注意,本声明

bool hasDigit[10] = {false, false, false, false, false, false, false, false, false, false};

看起来很傻。

你可以写

bool hasDigit[10] = {};

bool hasDigit[10] = { false };