#include "std_lib_facilities.h"
int main()
{
// Variable declaration
double smallest = 0;
double largest = 0;
double num = 0;
double stop_char = 0;
// User input and loop through it
cout << "Please, introduce a number ('|' to exit) \n";
while(cin >> num && stop_char != '|') {
cout << num << "\n";
if(num < smallest) {
smallest = num;
cout << "The smallest so far is: " << smallest << "\n";
} else if(num > largest) {
largest = num;
cout << "The largest so far is: " << largest << "\n";
}
}
cout << "You're leaving the program!";
}
我能够运行代码并跟踪迄今为止最大的数字,但由于某种原因无法跟踪最小的数字......
变量
stop_char
实际上没有被使用并且是多余的。
此逻辑表达式
stop_char != '|'
始终计算为 true
,因为变量 stop_char
最初设置为 0
并且在程序中未更改。
书中有如何编写循环的示例。
您可以按以下方式编写 for 循环
for ( double temp; cin >> temp; )
{
//...
}
或者像这样的 while 循环
double temp;
while ( cin >> temp )
{
//...
}
还有这样写
我们使用输入操作 cin >> temp 作为 for 的条件 声明。
上面显示的 while 语句中使用了相同的条件。
如果正确读取值,则条件
cin >> temp
的值为 true
。否则其值为 false
。
字符
'|'
与任何其他不能用作 double 类型对象的输入的字符一样,用于终止输入和相应的循环。
double
类型的值可以为负值和正值。所以这些初始零值
double largest = 0;
double num = 0;
在循环中没有意义。您需要在循环的第一次迭代中将它们设置为实际值。
程序可以通过以下方式查看例如:
#include "std_lib_facilities.h"
int main()
{
// Variable declaration
bool first_iteration = true;
double smallest = 0;
double largest = 0;
// User input and loop through it
cout << "Please, introduce a number ('|' to exit) \n";
double num;
while ( cin >> num ) {
cout << num << "\n";
if ( first_iteration || num < smallest )
{
first_iteration = false;
smallest = num;
cout << "The smallest so far is: " << smallest << "\n";
}
if ( first_iteration || largest < num ) {
first_iteration = false;
largest = num;
cout << "The largest so far is: " << largest << "\n";
}
}
cout << "You're leaving the program!";
}
0
是一个不好的初始值。
smallest
的良好初始值是可能的最大值。相反,
largest
的良好初始值是尽可能小的值。通过这样做,我们可以避免逻辑错误。例如,想象一下数据集
{ 3, 4, 5}
。最小值为
3
,但不小于
0
。
#include <iostream>
#include <limits>
int main() {
double num;
auto smallest = std::numeric_limits<decltype(num)>::max();
auto largest = std::numeric_limits<decltype(num)>::min();
std::cout << "Please type a number, '|' to exit: ";
while (std::cin >> num) {
if (num < smallest) {
smallest = num;
std::cout << "The smallest so far is: " << smallest << ".\n";
}
if (num > largest) {
largest = num;
std::cout << "The largest so far is: " << largest << ".\n";
}
std::cout << "Please type a number, '|' to exit: ";
}
std::cout << "Smallest: " << smallest << ".\nLargest: " << largest << ".\n";
}
输出:
~/tmp
❯ ./a.out
Please type a number, '|' to exit: 3
The smallest so far is: 3.
The largest so far is: 3.
Please type a number, '|' to exit: 4
The largest so far is: 4.
Please type a number, '|' to exit: 5
The largest so far is: 5.
Please type a number, '|' to exit: 2
The smallest so far is: 2.
Please type a number, '|' to exit: |
Smallest: 2.
Largest: 5.
正如(我认为)在其他地方指出的那样,如果您尝试输入 2.22507e-308
作为输入,此代码确实会遇到问题。这是我在屏幕上打印
std::numeric_limits<double>::min()
时看到的表示。最大值工作得很好,所以我的猜测是一个舍入问题,使最小值超出了实际范围。总的来说,我不认为这是一个非常重要的边缘情况。这里最大的胜利是我不必跟踪这是否是第一次迭代。这意味着减少 1 个变量,并减少 2N(其中 N 是数据集的大小)比较。对于编译器是否聪明并自己解决这个问题有一个潜在的争论,但如果根本没有必要,为什么还要麻烦呢?
最后一个警告,对于任何
'|'
是有效输入的类型,此代码都会崩溃,例如
char
。该代码依赖于
'|'
not 是所比较类型的有效值。但它适用于所有标准整数和浮点类型,您只需更改
num
的类型。