如何在 C++ 的输出中排除 SENTINEL 值?

how to exclude the SENTINEL value from the output in c++?

本文关键字:排除 SENTINEL 输出 C++      更新时间:2023-10-16

如何在 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;
}

您必须进行一些更改:

  1. while 循环之前至少读取一次数字,以确保在数字为 99999 时不执行while
  2. 将数字的读数移动到 while 循环的末尾,以便在进一步之前可以在开头进行检查 加工。
  3. 检查是否必须输入任何数字以避免在语句中除以零total = total / counter;
  4. 仅当列表中有任何数字时,才必须使用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