[問題] C++ Question about "Function"
請問這題的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
推
02/22 15:47, , 1F
02/22 15:47, 1F
→
02/22 15:50, , 2F
02/22 15:50, 2F
推
02/22 16:00, , 3F
02/22 16:00, 3F
推
02/22 16:01, , 4F
02/22 16:01, 4F
推
02/22 16:13, , 5F
02/22 16:13, 5F
→
02/22 16:13, , 6F
02/22 16:13, 6F
→
02/22 16:13, , 7F
02/22 16:13, 7F
推
02/22 16:25, , 8F
02/22 16:25, 8F
→
02/23 04:22, , 9F
02/23 04:22, 9F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章