作者hankhank5345 (MPower)
看板C_and_CPP
标题[问题] C++ Question about "Function"
时间Sun Feb 22 15:45:55 2009
请问这题的Function要怎麽写!!!感谢
题目: Write an overloaded function max that takes either two or three
parameters of type double and returns the largest of them.
#include <iostream>
using namespace std;
double max(double x, double y);
//PRECONDITION: x and y hold double values
//POSTCONDITION: the greater of x and y is returned
double max(double x, double y, double z);
//PRECONDITION: x, y and z hold double values
//POSTCONDITION: the greatest of x, y, and z is returned
int main()
{
double x, y, z;
//
// Test the two-argument max function
//
cout << endl << "** Welcome to the max program ** " << endl;
cout << "Please enter two numbers" << endl;
cout << ">> ";
cin >> x >> y;
cout << "The max of " << x << " and " << y << " is "
<< max(x,y) << endl << endl;
//
// Test the three-argument max function
//
cout << "Please enter three numbers" << endl;
cout << ">> ";
cin >> x >> y >> z;
cout << "The max of " << x << ", " << y << ", and " << z
<< " is " << max(x,y,z) << endl << endl;
}
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
提示:
You can write the 3-parameter max using an if; e.g.,
if x is greater than y and greater than z, it's the largest.
But an easier way is to make calls to the 2-parameter max --
the max of x, y, and z is the max of two things:
(1) x and (2) the max of y and z.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 71.199.79.106
1F:推 TroyLee:用心写搂 02/22 15:47
2F:→ tinlans:.............. 02/22 15:50
3F:推 walm20:给我钱 就教你写 02/22 16:00
5F:推 legendmtg:double max(double x, double y){return x>y?x:y;} 02/22 16:13
6F:→ legendmtg:double max(double x, double y, double z){return max( 02/22 16:13
7F:→ legendmtg:x, max(y, z));} 02/22 16:13
8F:推 varg:起码还知道这种东西要用分身问 02/22 16:25
9F:→ hankhank5345:谢谢我会写了~~感谢各位!! 02/23 04:22