/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/28
From : C++ Primer Plus第五版第七章编程练习 第1题
Problem : 编写一个程序,不断要求用户输入两个数,直到其中一个为0。对于每两个数,程序将使用一个函数来计算它们的
调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数 = 2.0 * x * y / (x + y)
*******************************************************************************************************************/
#include<iostream>
using namespace std;
double harmonic_mean(double x,double y);
int main()
{
cout << "Enter two numbers (0 represent end): ";
double x,y;
while(cin >> x >> y && x*y!=0)
{
cout << "The harmonic mean of " << x << " and " << y << " is " << harmonic_mean(x,y) << endl;
cout << "Enter two numbers (0 represent end): ";
}
cout << "Bye! " <<endl;
system("pause");
return 0;
}
double harmonic_mean(double x,double y)
{
return 2.0*x*y/(x+y);
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/28
From : C++ Primer Plus第五版第七章编程练习 第2题
Problem : 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在
一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。请使用3个数组
处理函数来分别
*******************************************************************************************************************/
#include<iostream>
using namespace std;
const int SIZE = 10;
int input_scords(double scords[], int size);
void show(const double scords[],int size);
double mean(const double scords[],int size);
int main()
{
cout << "Enter " << SIZE << " scords: ";
double scords [10]={0};
int num;
num= input_scords(scords, SIZE);
show(scords,num);
cout << "The mean scords is " << mean(scords,num) <<endl;
system("pause");
return 0;
}
int input_scords(double scords[], int SIZE)
{
double temp;
int i=0;
for(i=0;i<SIZE;i++)
{
cin>>temp;
if(!cin) //bad input
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"Bad input:input process terminated.\n";
break;
}
else if(temp<0)
break;
scords[i]=temp;
}
return i;
}
void show(const double scords[],int size)
{
for (int i=0; i<size ;i++)
{
cout << scords[i] << " ";
}
cout << endl;
}
double mean(const double scords[],int size)
{
double sum=0;
for (int i=0; i<size ;i++)
sum+=scords[i];
double mean_scords=sum/size;
return mean_scords;
}
/*******************************************************************************************************************
Author : Cui mingyang
Blog : cx_12586
Time : 2017/10/28
From : C++ Primer Plus第五版第七章编程练习 第3题
Problem : 下面是一个结构声明:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a.编写一个函数,按值传递box结构,并显示每个成员的值
b.编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c.编写一个使用这两个函数的简单程序。
*******************************************************************************************************************/
#include<iostream>
using namespace std;