C++ 在函数定义中使用 if/else 语句到 return 最小数字
C++ Using if/else statements in function definition to return smallest number
对于我的 C++ 家庭作业,我的目标是编写一个程序,输入三个整数并将它们传递给一个 return 是最小数字的函数。这只是我 C++ 的第 3 周,所以我知道的不多。
此外,我只能从 #include<iostream>
和 using namespace std
开始。我已经在这里待了几个小时,这对我来说并不容易。我已经尝试了很多不同的东西,但我只是遇到了错误...
这是我到目前为止真正理解的代码:
#include <iostream>
using namespace std;
int fsmallestNumber(int);
int main() {
int numberOne;
int numberTwo;
int numberThree;
int smallestNumber;
cout << "Enter in 3 numbers and I will find the smallest of all three" << endl;
cin >> numberOne >> numberTwo >> numberThree;
cout << "The smallest of all three numbers is " << smallestNumber << endl;
}
int fsmallestNumber(int sn){
}
我很困惑如何使用 if/else 语句找到最小的数字,以及如何将最小的数字 return 返回到函数中打印出来。
给你。
#include <iostream>
using namespace std;
int fsmallestNumber( int, int, int );
int main()
{
int numberOne;
int numberTwo;
int numberThree;
int smallestNumber;
cout << "Enter in 3 numbers and I will find the smallest of all three" << endl;
cin >> numberOne >> numberTwo >> numberThree;
smallestNumber = fsmallestNumber( numberOne, numberTwo, numberThree );
cout << "The smallest of all three numbers is " << smallestNumber << endl;
}
int fsmallestNumber( int x, int y, int z )
{
int smallest = x;
if ( y < smallest ) smallest = y;
if ( z < smallest ) smallest = z;
return smallest;
}
该函数必须接受三个参数。所以必须声明三个参数。
如果您需要包含 else 语句,那么函数可以这样写
int fsmallestNumber( int x, int y, int z )
{
int smallest;
if ( not ( y < x || z < x ) )
{
smallest = x;
}
else if ( not ( z < y ) )
{
smallest = y;
}
else
{
smallest = z;
}
return smallest;
}
注意C++标准库已经在header<algorithm>
中声明了合适的算法std::min
。所以你可以写
#include <algorithm>
//...
smallestNumber = std::min( { numberOne, numberTwo, numberThree } );
对于我的 C++ 家庭作业,我的目标是编写一个程序,输入三个整数并将它们传递给一个 return 是最小数字的函数。这只是我 C++ 的第 3 周,所以我知道的不多。
此外,我只能从 #include<iostream>
和 using namespace std
开始。我已经在这里待了几个小时,这对我来说并不容易。我已经尝试了很多不同的东西,但我只是遇到了错误...
这是我到目前为止真正理解的代码:
#include <iostream>
using namespace std;
int fsmallestNumber(int);
int main() {
int numberOne;
int numberTwo;
int numberThree;
int smallestNumber;
cout << "Enter in 3 numbers and I will find the smallest of all three" << endl;
cin >> numberOne >> numberTwo >> numberThree;
cout << "The smallest of all three numbers is " << smallestNumber << endl;
}
int fsmallestNumber(int sn){
}
我很困惑如何使用 if/else 语句找到最小的数字,以及如何将最小的数字 return 返回到函数中打印出来。
给你。
#include <iostream>
using namespace std;
int fsmallestNumber( int, int, int );
int main()
{
int numberOne;
int numberTwo;
int numberThree;
int smallestNumber;
cout << "Enter in 3 numbers and I will find the smallest of all three" << endl;
cin >> numberOne >> numberTwo >> numberThree;
smallestNumber = fsmallestNumber( numberOne, numberTwo, numberThree );
cout << "The smallest of all three numbers is " << smallestNumber << endl;
}
int fsmallestNumber( int x, int y, int z )
{
int smallest = x;
if ( y < smallest ) smallest = y;
if ( z < smallest ) smallest = z;
return smallest;
}
该函数必须接受三个参数。所以必须声明三个参数。
如果您需要包含 else 语句,那么函数可以这样写
int fsmallestNumber( int x, int y, int z )
{
int smallest;
if ( not ( y < x || z < x ) )
{
smallest = x;
}
else if ( not ( z < y ) )
{
smallest = y;
}
else
{
smallest = z;
}
return smallest;
}
注意C++标准库已经在header<algorithm>
中声明了合适的算法std::min
。所以你可以写
#include <algorithm>
//...
smallestNumber = std::min( { numberOne, numberTwo, numberThree } );