程序中的布尔函数返回输入的范围无论如何都是无效的
Boolean function in program returns that an entered range is invalid no matter what
我正在开发一个程序,该程序可以确定范围内是否存在重复数字的数字。程序本身运行良好 - 但它的一个组成部分是确定用户输入的范围是否有效。我需要为此创建一个布尔函数,称为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 };
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 不计算一个范围内的完美数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 如何访问超出其块范围的对象?
- 程序中的布尔函数返回输入的范围无论如何都是无效的