1. 编写一个程序,不断要求用户输入两 个数,直到其中的一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给 main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下 : 调和平均数 = 2.0xy/(x+y)
#include <iostream>
using namespace std;
double HarmonicMean(double, double);
int main()
{
double Num1, Num2;
cout << "Enter Tow Numbers: ";
while(cin)
{
cin >> Num1 >> Num2;
if (Num1 == 0 || Num2 == 0)
break;
else
cout << "The Harmonic Mean of " << Num1 << " and " << Num2 << " is " << HarmonicMean(Num1, Num2) << endl;
cout << "Enter another two numbers: ";
}
return 0;
}
double HarmonicMean(double Num1, double Num2)
{
return 2.0 * Num1 * Num2 / (Num1 + Num2);
}
代码运行结果如下:
2. 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用 3 个数组处理函数来分别进入输入、显示、计算平均成绩。
已经又有两天没有写代码了,本来没啥事,突然要给别人擦PG,搞19年的80兆竣工资料,两天终于差不多算是搞完了
题目提到 “使用 3 个数组处理函数”,不知道是翻译有问题,还是咱的,看不明白,我想应该是使用三个调用函数来解决问题,先把代码码上
#include <iostream>
using namespace std;
const int ArSize = 3; // 写代码时,这里把 int 给忘了,这里测试用的 3
void Fill_Arr(double arr[], int);// 写代码时,这里三把 int 给忘了
void Show_Arr(double [], int ArSize);// 写了三种参数写法
double Average(double *, int ArSize);
int main()
{
double arr[ ArSize ];
cout << "请输入此次所有高尔夫的成绩: ";
Fill_Arr(arr, ArSize);
cout << "此次高尔夫的所有成绩: ";
Show_Arr(arr, ArSize);
cout << endl;
cout << "此次高尔夫的平均成绩: " << Average(arr, ArSize);
cout << endl;
// 下面的代码写法无法计算出平均数
// cout << "此次高尔夫的平均成绩: ";
// Average(arr, ArSize);
return 0;
}
void Fill_Arr(double arr[], int ArSize)
{
for (int i =0; i < ArSize; i++)
cin >> arr[ i ];
}
void Show_Arr(double arr[], int ArSize)
{
for (int i = 0; i < ArSize; i ++)
cout << arr[ i ] << ", " ;
}
double Average(double arr[], int ArSize)
{
double Sum = 0, average = 0;
for (int i = 0; i < ArSize; i ++)
Sum += arr[ i ];
average = Sum / ArSize;
return average;
}
下面的运行结果是使用注释的代码:
两行改成一行后
这上面代码只适合题目的输入多少个成绩,一定要输入足量,当不足量的时候程序就会出问题,题目要求 “程序允许用户提早结束输入”,于是我把 Fill_arr() 函数改成`在这里插入代码片
void Fill_Arr(double arr[], int ArSize)
{
float temp;
int i = 0;
cin >> temp;
while (i < ArSize && temp >= 0)
{
arr[ i ] = temp;
i ++;
cin >> temp;
}
}
结果 Show_Arr() 仍然显示多次输入的数值,
于是我想这里是不是可以把 Fill_Arr() 时输入正确数的个数 当成一个变量,然后返回这个变量,在 Show_Arr() 时再调用这个变量,试下,代码修改如下:
#include <iostream>
using namespace std;
const int ArSize = 5; // 数量 3 太少了,改成 5
int Fill_Arr(double arr[], int);
void Show_Arr(double [], int );
double Average(double *, int ArSize);
int main()
{
double arr[ ArSize ];
cout << "请输入此次所有高尔夫的成绩: ";
int count = Fill_Arr(arr, ArSize); // 11 12 14 15 -3
cout << "此次高尔夫的所有成绩: ";
Show_Arr(arr, count);
cout << endl;
cout << "此次高尔夫的平均成绩: " << Average(arr, ArSize); // 标记下,有下文
cout << endl;
return 0;
}
int Fill_Arr(double arr[], int ArSize)
{
float temp;
int count = 0;
cin >> temp;
while (count < ArSize && temp >= 0)
{
arr[ count ] = temp;
cin >> temp;
count ++;
}
return count;
}
void Show_Arr(double arr[], int count)
{
for (int i = 0; i < count; i ++)
cout << arr[ i ] << ", " ;
}
double Average(double arr[], int count)
{
double Sum = 0, average = 0;
for (int i = 0; i < count; i ++)
Sum += arr[ i ];
average = Sum / count;
return average;
}
运行结果
显示的成绩问题已经解决,但是平均成绩却不对了,用 F10 大法
第二个函数调用完成后 count 的值为 4,这个没错,虽然输入了 5 个数,但有效位却只有 4 个,很好,继续
怎么第三次调用函数的时候 count 变成了 5 ,怎么会自增了?第三个求平均值的函数,我传入的参数是 count ,见上图,这 TMD 的就怪了,然后我再进一步看代码,终于被我发现了,(代码区有个注释为 标记有下文处)这里传入的参数竟然是 ArSize,也就是说这里传入的还是代码开头声明的 5,虽然下面的函数中写的是 count,但实际上传入的却是 5,而 数组 arr[5]是一个随机数,在这台机子上显示是 -9.25596e+061,难怪最后平均值是这么大一个负数,现在明白了,下面的函数参数主要取决于,main()函数调用时候传入的参数,而并不一定是我们看到传入的参数名,将第三个调用函数的参数 ArSize 改成 count后,再次运行代码:
这次终于比较完美了。继续下一题。
3. 下面是一个结构声明
struct box
{
char maker[ 40 ];
float height;
float width;
float length;
float volume;
};
a. 缩写一个函数,按值传递 box 结构,并显示每个成员的值。
#include <iostream>
using namespace std;
struct box
{
char maker[ 40 ];
float height;
float width;
float length;
float volume;
};
void Show_Box(box Box, char, float, float, float, float);
int main()
{
float h = 3.14, w = 3.44, l = 3.22, v = 3.40;
Show_Box(Box, "Dong zhao hui", h, w, l, v);
return 0;
}
void Show_Box(box Box, char, float, float, float, float)
{
cout << Box.maker << ", " << Box. height << ", " << Box.width << Box.length << ", " << Box.volume << endl;
}
上面是我试着这样的传参行不行,不知道是C++ 不允许这样传参还是我写的代码有问题,不能通过,是不是没学会走就要跑了,老老实实的按规矩写了一段代码:
#include <iostream>
using namespace std;
struct box
{
char maker[ 40 ];
float height;
float width;
float length;
float volume;
};
void Show_Box(box Box);
int main()
{
box Box ={
"Dong zhao hui", 12, 11, 13, 15};
Show_Box(Box);
return 0;
}
void Show_Box(box Box)
{
cout << Box.maker << ", " << Box. height << ", " << Box.width << ", " << Box.length << ", " << Box.volume << endl;
}
第二段代码运行如下:
第一段代码明天再来研究下
b. 编写一个函数,传递 box 结构的地址,并将 volume 成员设置为其他三维长度的乘积。
本来昨天晚上已经准备写代码了,刚刚坐下朋友电话来说他电动车没电了,让我帮忙骑个车把他女儿送回去,最后在他家玩到11点多才回,现在继续…
// 第一次代码不正确
#include <iostream>
using namespace std;
struct box
{
char maker[ 40 ];
float height;
float width;
float length;
float volume;
};
box Box ;
box * pt = &Box;
void Show_Box(box &pt); // 这里应该参数应该是 &Box
int main()
{
Show_Box(box &Box);
return 0;
}
void Show_Box(box &pt) // 和上面一样,这里应该参数应该是 &Box
{
pt -> maker = "Qcriser"; // 这里 char 数组不能这样初始化赋值,要用到 strcpy();
pt -> height = 11;
pt -> width = 12;
pt -> length = 14;
pt -> volume = (pt -> height) * (pt -> width) * (pt -> length);
cout << pt -> height << ", " << pt -> width << ", " << pt -> length << ", " << pt -> volume;
}
上面是初次写的代码,由于不知道如何传递结构的地址,还去翻了下书中以前的资料,不过代码没有通过,在代码区简单的注释了下,下面是正确的代码:
#include <iostream>
#include <string>
using namespace std;
struct box
{
char maker[ 40 ];
float height;
float width;
float length;
float volume;
};
void Show_Box(box