[問題] C++ Question about "Function"

看板C_and_CPP (C/C++)作者 (MPower)時間16年前 (2009/02/22 15:45), 編輯推噓5(504)
留言9則, 7人參與, 最新討論串1/1
請問這題的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
用英文出的作業,還蠻屌的 http://tinyurl.com/bvezyh
02/22 16:01, 4F

02/22 16:13, , 5F
double max(double x, double y){return x>y?x:y;}
02/22 16:13, 5F

02/22 16:13, , 6F
double max(double x, double y, double z){return max(
02/22 16:13, 6F

02/22 16:13, , 7F
x, max(y, z));}
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
文章代碼(AID): #19eGAqGM (C_and_CPP)
文章代碼(AID): #19eGAqGM (C_and_CPP)