将伪代码翻译成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 : " ;
}
这是愚蠢的伪代码。我们可以做得更好没有这些无用的bool…
int min = std::numeric_limits<int>::max();
for(int val; cin >> val)
{
if(val < min)
min = val;
}
cout << "Minimum: " << min << 'n';
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- 需要将一行代码从C++ Qt翻译成PyQt
- 为什么未命名的结构内联变量在每个翻译单元中没有相同的地址?
- 在没有 API 的情况下使用谷歌翻译文本
- 如何执行 cppcheck 交叉翻译单元 (CTU) 静态分析?
- 模板变量是否允许在多个翻译单元中并有效合并?
- 是否允许类在程序中的不同翻译单元之间具有不同的定义?
- 我怎样才能代替使用新的使用malloc翻译
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 如何将 'in' 关键字从 python 翻译成 c++?
- 为什么不同的翻译单元没有独立和沙盒的内存空间?
- 如何用c语言翻译openssl命令pbkdf2
- 为什么编译器不在同一翻译单元中警告 ODR 违规
- 静态变量在同一个翻译单元中被静态方法使用时是否保证被初始化?
- 如何翻译这个给定代码中的 ::* 运算符和member_fn?
- 来自不同翻译单元的函数会干扰吗?
- 在 opengl 中翻译相机时遇到问题
- 如何平均继续翻译向量
- 如何在C++中的不同翻译单元之间共享枚举的实例
- 在Visual Studio中更新Qt翻译(.ts文件)时出现未指定错误