1135人加入学习
(5人评价)
C++编程系列 第二季函数和类

制作于2018.4.2

价格 免费
课程还未发布,不允许加入和购买

float Harmean(float a, float b);

int main()
{
    float x = 1.f, y = 1.f, harm;
    while (x != 0 && y != 0)
    {
        cout << "enter two number,first:";
        cin >> x;
        cout << "enter two number,second:";
        cin >> y;
        harm = Harmean(x, y);
        if (x != 0 && y != 0)
            cout << x << "和" << y << "的调和平均数为" << harm << endl;
    };
}

float Harmean(float a, float b)
{
    float har = (2 * a * b) / (a + b);
    return har;
}

[展开全文]

练习:求两个数的调和平均数

要求:

1.不断要求用户输入两个数,知道其中一个数的值为0;

2.对于每两个数,程序将使用一个函数来计算他们的调和平均数。

3.函数将计算结果放会给主函数,在主函数中输出输出输入的数字和他们的调和平均数。

4.调和平均数是指倒数平均值的倒数。

#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
float pj(float i, float j);
int main()
{
	float a, b, temp;
	do
	{
		cout << "	请输入a的值" << endl;
		cin >> a;
		cout << "	请输入b的值" << endl;
		cin >> b;
		temp = pj(a, b);
		cout << "a= " << a << endl << "b= " << b << endl << "调和平均值= " << temp << endl;
	} while (a != 0 && b != 0);
}
float pj(float i, float j)
{
	float sum = 1 / ((1 / i + 1 / j)/2);
	return sum;
}

 

[展开全文]

long fact(int i)

{

long temp;

if (i==0)

{

temp=1;

}

else

{

temp=1*fact(i-1);

}

return temp;

}

[展开全文]

授课教师

SIKI学院老师

课程特色

下载资料(1)
视频(43)

学员动态