C++编程基础二 07-习题2

 1 // C++函数和类 07-习题2.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <string>
 6 #include <limits>
 7 #include <array>
 8 #include <math.h>
 9 #include <iostream>
10 using namespace std;
11 float harmonicMean(float a, float b);
12 //完成程序:求两个数的调和平均数
13 //要求:
14 //1.不断要求用户输入2个数,直到其中一个数的值为0。
15 //2.对于每两个数,程序将使用一个函数来计算它们的调和平均数。
16 //3.函数将计算结果返回给主函数,在主函数中输出输入的数字和它们的调和平均值。
17 //调和平均数是指倒数平均值的倒数。  
18 
19 
20 int main()
21 {
22     float num1, num2;
23     cout << "请输入两个数的值:" << endl;
24     while (cin >> num1 >> num2 && num1 != 0 && num2 != 0)
25     {
26         cout << num1 << "" << num2 << "的调和平均数为: ";
27         cout << harmonicMean(num1, num2) << endl;
28         cout << "请输入两个数的值:" << endl;
29     }
30     return 0;
31 }
32 
33 float harmonicMean(float a, float b)
34 {
35     return 2 * a*b / (a + b);
36 }

猜你喜欢

转载自www.cnblogs.com/uimodel/p/9348585.html