繁体   English   中英

将伪代码转换为C ++

[英]Translate pseudo code into C++

我得到了伪代码来转换为C ++:

    Set a Boolean variable “first” to true.
    While another value has been read successfully
          If first is true
          Set the minimum to the value.
          Set first to false.
          Else if the value is less than the minimum
          Set the minimum to the value.
    Print the minimum

这是我的代码:

bool first = true;
bool read_value = true;
int value = 0;
int minimum = 0;
cout << "Enter an integer : " ;
cin >> value;
while(cin.hasNextInt())   // error here
{
    cout << "Enter another integer : " ;
    cin >> minimum;
    if( first == true){
        minimum = value;
        first = false;
    }else if ( value < minimum){
        minimum = value;
    }
}
cout << minimum;

system("PAUSE");
return 0;

hasNextInt那里有错误。 而且我真的不知道伪代码想要什么。 有人可以向我解释吗?

提前致谢。

标准C ++库中没有hasNextInt()函数(这就是为什么您不能编译的原因)。 但是,Java中只有一个!

这更接近您想要的代码:

cout << "Enter an integer : " ;
while( cin >> value )
{
    if( first == true){
        minimum = value;
     first = false;
    }else if ( value < minimum){
        minimum = value;
    }
    cout << "Enter another integer : " ;
}

那是愚蠢的伪代码。 没有一些毫无价值的傻瓜,我们可以做得更好。

int min = std::numeric_limits<int>::max();

for(int val; cin >> val)
{
    if(val < min)
        min = val;
}

cout << "Minimum: " << min << '\n';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM