如何在 C++ 的输出中排除 SENTINEL 值?
how to exclude the SENTINEL value from the output in c++?
如何在 C++ 中从输出中排除 SENTINEL 值? 对于学校项目。 SENTINEL 值扭曲输出并将其添加到计算中。我的教授对如何从我的计算中排除哨兵含糊不清,但我仍然对如何排除它感到困惑。 我的最大值输出一直等于 99999,这是 SENTINEL 值。 它还将 SENTINEL 值添加到所有计算中。
#include "stdafx.h"
#include <iostream>
#include <climits>
using namespace std;
int SENTINEL = 99999;
int main()
{
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0, sum = 0, num2 = 0, tot = 0;
int even = 0;
int odd = 0;
double total = 0;
while (num != SENTINEL)
{
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
if (num > max)
max = num;
if (num < min)
min = num;
if ((num % 2) != 0)
{
odd++;
}
else if (num % 2 == 0)
{
even++;
}
if (num < 99999) {
neg++;
}
if (num < 99) {
num2++;
}
total += num;
tot += num;
counter++;
}
total = total / counter;
cout << "The smallest Number of the list is: " << min << endl;
cout << "The largest Number of the list is: " << max << endl;
cout << "The sum of all the numbers is: " << tot << endl;
cout << "The average of all numbers is: " << total << endl;
cout << "There are " << neg << " numbers" << endl;
cout << "There are " << even << " even numbers" << endl;
cout << "There are " << odd << " odd numbers" << endl;
cout << "There are " << num2 << " numbers smaller than 99" << endl;
return 0;
}
您必须进行一些更改:
在- while 循环之前至少读取一次数字,以确保在数字为 99999 时不执行
while
。 - 将数字的读数移动到 while 循环的末尾,以便在进一步之前可以在开头进行检查 加工。
- 检查是否必须输入任何数字以避免在语句中除以零
total = total / counter;
- 仅当列表中有任何数字时,才必须使用
cout
输出各种语句。否则,它没有多大意义。
完成上述更改后,代码将如下所示:
#include <iostream>
#include <climits>
using namespace std;
int SENTINEL = 99999;
int main()
{
int min = INT_MAX, max = INT_MIN, num = 0, counter = 0, pos = 0, neg = 0, sum = 0, num2 = 0, tot = 0;
int even = 0;
int odd = 0;
double total = 0;
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
while (num != SENTINEL)
{
if (num > max)
max = num;
if (num < min)
min = num;
if ((num % 2) != 0)
{
odd++;
}
else if (num % 2 == 0)
{
even++;
}
if (num < 99999) {
neg++;
}
if (num < 99) {
num2++;
}
total += num;
tot += num;
counter++;
cout << "Enter in a number" << endl;
cout << "Enter 99999 to exit" << endl;
cin >> num;
}
if(counter != 0)
{
total = total / counter;
cout << "The smallest Number of the list is: " << min << endl;
cout << "The largest Number of the list is: " << max << endl;
cout << "The sum of all the numbers is: " << tot << endl;
cout << "The average of all numbers is: " << total << endl;
cout << "There are " << neg << " numbers" << endl;
cout << "There are " << even << " even numbers" << endl;
cout << "There are " << odd << " odd numbers" << endl;
cout << "There are " << num2 << " numbers smaller than 99" << endl;
}
else
{
cout << "No numbers were entered" << endl;
}
return 0;
}
示例输出:
./a.exe
Enter in a number
Enter 99999 to exit
99999
No numbers were entered
./a.exe
Enter in a number
Enter 99999 to exit
34
Enter in a number
Enter 99999 to exit
23
Enter in a number
Enter 99999 to exit
12
Enter in a number
Enter 99999 to exit
99999
The smallest Number of the list is: 12
The largest Number of the list is: 34
The sum of all the numbers is: 69
The average of all numbers is: 23
There are 3 numbers
There are 2 even numbers
There are 1 odd numbers
There are 3 numbers smaller than 99
相关文章:
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- 如何从整个磁盘中排除要装载的一个文件
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- 如何在 C++ 的输出中排除 SENTINEL 值?
- 针对代码::块设置的 SFML 故障排除
- 如何在 boost::spirit::qi 中将某些语义操作排除在 AST 之外
- C++:如何在从给定缓存中排除数字的同时生成随机数
- 遍历列表包含排除最后一个元素的内容
- C++排除通过派生类中的基类继承的类
- 在创建共享库时,是否可以明确地排除某些类
- 对"无效的抽象返回类型"进行故障排除
- 需要帮助对内存泄漏源进行故障排除 - C++
- 可视C++ - 从链接器>输入>其他依赖项中排除文件
- 从参数包中排除前 n 个参数
- 使用许多嵌套的#include语句对编译器错误进行故障排除
- 如何在运行时以选择性方式排除代码块
- 有没有办法排除不可调用函数的结果
- 我想使用 CMake 生成和编译 Visual Studio projet,我需要从构建中排除单元测试文件。有没有办法做到这一点?
- 使用C 中的原始操作员的异步线的相互排除
- 包括不排除静态库项目的用户路径的路径