使用条件语句在哨兵结束循环后显示最大值

时间:2015-08-18 04:38:34

标签: c++ sentinel

我需要创建一个程序,允许用户输入一系列 以0为整数的整数。最后,将显示最大的整数。

我最初在if语句下面有语句largest = value,但是把它改成了一个条件语句,这对我来说更有意义,因为我之前的方式只是在输入sentinel之前给了我最后一个值。它似乎给了我“正确”的输出,但是我用条件语句设置它的方式是正确的吗?如果没有,我该如何解决?

原始输出

This program finds the largest integer in a list.
Enter 0 to signal the end of the input.

Please enter an integer: 21

Please enter an integer: 15

Please enter an integer: 9

Please enter an integer: 6

Please enter an integer: 3

Please enter an integer: 0

The largest value is 3.

Press any key to continue . . .

当前输出

This program finds the largest integer in a list.
Enter 0 to signal the end of the input.

Please enter an integer: 15

Please enter an integer: 200

Please enter an integer: 3

Please enter an integer: 5

Please enter an integer: 21

Please enter an integer: 0

The largest value is 200.

Press any key to continue . . .

源代码:

#include "stdafx.h"
#include <iostream> 
using namespace std;

const int SENTINEL = 0;

/* Main Program */
int main()
{

    cout << "This program finds the largest integer in a list.";
    cout << "\nEnter " << SENTINEL << " to signal the end of the input." << endl;

    int largest = SENTINEL;

    while (true)
    {
        int value; 
        cout << "\nPlease enter an integer: ";  
        cin >> value;

        if (value == SENTINEL) 
            break;
        largest = (value > largest) ? value : largest; 
    }

    cout << "\nThe largest value is " << largest << "." << endl;

    cout << endl;

    system("PAUSE");

    return 0;
}

2 个答案:

答案 0 :(得分:2)

我认为你的方法是正确的。

但是,您可以简单地替换:

largest = (value > largest) ? value : largest; 

通过等效的if条件语句:

if (value > largest){
    largest = value;
}

另外,请注意,只需替换您的声明:

int largest = SENTINEL;

通过

int largest =  INT_MIN;

其中INT_MIN是最小可能整数。

答案 1 :(得分:2)

内联评论中的一些小建议,无论您从中提取哪些教育价值......

#include <iostream> 

const int SENTINEL = 0;

int main()
{
    // can continue input across lines, and rarely need to use endl...
    // (any input attempt from cin will flush cout first)
    std::cout << "This program finds the largest integer in a list.\n"
        "Enter " << SENTINEL << " to signal the end of the input.\n";

    int num_inputs = 0;
    int largest;
        // before, shouldn't have used sentinel here - if input all negative
        //   largest would be 0 in error...
        // now won't use unless we see actual inputs so no need to init

    int value;
    while (std::cout << "\nPlease enter an integer: " &&
           std::cin >> value)  // use cin so unparsable input breaks
    {
        if (value == SENTINEL) 
            break;
        if (++num_inputs == 1)
            largest = value;
        else if (value > largest)
            largest = value;
    }

    if (num_inputs > 0)
        std::cout << "\nThe largest value is " << largest << ".\n\n";
    system("PAUSE");  // for Windoze
}

使用num_inputs可以避免在没有非哨兵输入的情况下输出声称知道最大值的消息。

另一种维护largest的方法是:

else
    largest = std::max(value, largest);

...关于

while (std::cout << "\nPlease enter an integer: " &&
       std::cin >> value) 

...这确保无法作为整数解析的输入导致循环终止(因为std::cin设置为eoffail和/或{{1状态,其中任何一个导致bad运算符在布尔评估上下文中返回std::istream::operator bool() const。写入提示的诀窍(&#34;请输入...&#34;)in while条件只是在第一次输入尝试之前完成,然后每次重复循环而不必在false循环结束时重复相同的std::cout << ...源代码。它也稍微更准确,因为while的输出可能会失败并且终止循环并不是对此的不合理反应,尽管人们很少费心去测试std::cout的成功或cout输出操作。