C++ Primer Plus_第6版_第七章
7.13.1
用户不断输入两个数,知道其中一个为0
对于每两个数,程序使用一个函数计算他们的调和平均数
调和平均数=2.0xy/(x+y)
#include <iostream>
using namespace std;
/*
用户不断输入两个数,知道其中一个为0
对于每两个数,程序使用一个函数计算他们的调和平均数
调和平均数=2.0*x*y/(x+y)
*/
double avg(double x, double y);
int main()
{
double x = 0, y = 0;
while (1) {
cin >> x >> y;
if (x==0 || y == 0) {
break;
}
cout << avg(x, y) << endl;
}
return 0;
}
double avg(double x, double y) {
double ret = 0.0;
ret = 2.0*x*y/(x+y);
return ret;
}
7.13.2
用户输入最多10个高尔夫成绩,存储在一个数组中
允许提早结束输入
在一行上显示所有成绩,然后报告平均成绩
用3个数组处理函数分别进行输入、显示和计算平均成绩
#include <iostream>
using namespace std;
/*
用户输入最多10个高尔夫成绩,存储在一个数组中
允许提早结束输入
在一行上显示所有成绩,然后报告平均成绩
用3个数组处理函数分别进行输入、显示和计算平均成绩
*/
//输入
int input(double score[]) {
int i = 0;
cin >> score[i];
while (cin.get() != '\n' && i < 9) {
i++;
cin >> score[i];
}
return i+1;
}
//显示
void show(double score[], int size) {
for (int i = 0; i < size; i++) {
cout << score[i] << " ";
}
cout << endl;
}
//计算平均成绩
void avg(double score[], int size) {
double sum = 0.0;
double avg = 0.0;
for (int i = 0; i < size; i++) {
sum += score[i];
}
avg = sum / size;
cout << avg << endl;
}
int main()
{
double score[10] ={0.0};
int size = input(score);
show(score, size);
avg(score, size);
return 0;
}
7.13.3
结构体:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
1.编写函数,按值传递box结构,并显示每个成员的值
2.编写函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积
3.编写一个使用这两个函数的简单程序
#include <iostream>
using namespace std;
/*
结构体:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
1.编写函数,按值传递box结构,并显示每个成员的值
2.编写函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积
3.编写一个使用这两个函数的简单程序
*/
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show01(box b) {
cout << "制作者:" << b.maker << endl;
cout << "高:" << b.height << endl;
cout << "宽:" << b.width << endl;
cout << "长:" << b.length << endl;
b.volume = b.height * b.length * b.width;
cout << "体积:" << b.volume << endl;
}
void show02(box* b) {
cout << "制作者:" << b->maker << endl;
cout << "高:" << b->height << endl;
cout << "宽:" << b->width << endl;
cout << "长:" << b->length << endl;
b->volume = b->height * b->length * b->width;
cout << "体积:" << b->volume << endl;
}
int main()
{
box b;
cout << "输入制作者:";
cin >> b.maker;
cout << "输入高,宽,长:";
cin >> b.height >> b.width >> b.length;
show01(b);
cout << "-----------------" << endl;
show02(&b);
return 0;
}
7.13.4
从域号码中选择几个
从特选号码中选择1个
中头奖的概率为
域号码中正确选取的几率与特选号码中正确选取的几率的乘积
计算中头奖的概率
ps:本题本身要求为修改程序7.4,故代码风格和7.4较为相同
#include <iostream>
using namespace std;
/*
从域号码中选择几个
从特选号码中选择1个
中头奖的概率为
域号码中正确选取的几率
与
特选号码中正确选取的几率
的乘积
计算中头奖的概率
*/
long double probability01(unsigned numbers01, unsigned picks);
int main()
{
double total01, choices, total02;
cout << "分别输入域号码总数、需要选的个数和特选号码总数:\n";
while ((cin >> total01 >> choices >> total02) && choices <= total01) {
cout << "获得头奖的概率:";
cout << probability01(total01, choices) * total02 << endl;
cout << "继续输入,输入q退出:";
}
cout << "bye\n";
return 0;
}
long double probability01(unsigned numbers01, unsigned picks) {
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers01, p = picks; p > 0; n--, p--) {
result = result * n / p;
}
return result;
}
7.13.5
递归求阶乘
#include <iostream>
using namespace std;
//递归函数求阶乘
long long factorial(int num) {
if (num == 0 || num == 1)
return 1;
return num * factorial(num-1);
}
int main()
{
int n = 0;
//n<25目的:防止溢出
while ((cin >> n) && n <= 25)
cout << factorial(n) << endl;
return 0;
}
7.13.6
三个函数
Fill_array(double arr_name[], int arr_size);
当数组被填满或者用户输入了非数字,输入停止,返回数字的个数
Show_array(double arr_name[], int arr_size);
显示数组的内容
Reverse_array(double arr_name[], int arr_size);
反转
ps:反转一般参数为首尾位置,这里放上函数声明:
Reverse_array(int start_index, int end_index);
#include <iostream>
using namespace std;
/*
三个函数
Fill_array(double arr_name[], int arr_size)
当数组被填满或者用户输入了非数字,输入停止,返回数字的个数
Show_array(double arr_name[], int arr_size)
显示数组的内容
Reverse_array(double arr_name[], int arr_size)
反转
*/
const int MAX = 10; //数组大小最大为10
int Fill_array(double arr_name[], int arr_size) {
int i = 0;
while (i < MAX) {
if (!(cin >> arr_name[i])) {
i--;
return i+1;
}
i++;
}
return i;
}
void Show_array(double arr_name[], int arr_size) {
for (int i = 0; i < arr_size; i++) {
cout << arr_name[i] << endl;
}
}
void Reverse_array(double arr_name[], int arr_size) {
double temp = 0;
for (int i = 0; i < arr_size / 2; i++) {
temp = arr_name[i];
arr_name[i] = arr_name[arr_size - i - 1];
arr_name[arr_size - i - 1] = temp;
}
}
int main()
{
double arr[MAX] = {0.0};
int size = Fill_array(arr, MAX);
Show_array(arr, size);
Reverse_array(arr,size);
Show_array(arr,size);
return 0;
}
7.13.7
修改7.7
使之使用两个指针参数来表示区间
fill_array()不返回实际读取了多少个字
返回一个指向最后被填充位置的指针
其他函数可以将该指针作为第二个参数,用来标识数据的结尾
程序7.7
//程序7.7
#include <iostream>
using namespace std;
const int Max = 5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);
int main() {
double properties[Max];
int size = fill_array(properties, Max);
show_array(properties, size);
if (size > 0) {
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor)) {
cin.clear();
while (cin.get() != '\n') {
continue;
}
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
cin.get();
cin.get();
return 0;
}
int fill_array(double ar[], int limit) {
double temp;
int i;
for (i = 0; i < limit; i++) {
cout << "Enter value #" << (i+1) << ":";
cin >> temp;
if (!cin) {
cin.clear();
while (cin.get() != '\n') {
continue;
}
cout << "Bad input; input process terminated.\n";\
break;
}
else if (temp < 0) {
break;
}
ar[i] = temp;
}
return i;
}
void show_array(const double ar[], int n) {
for (int i = 0; i < n; i++) {
cout << "Property #" << (i+1) << ": $";
cout << ar[i] << endl;
}
}
void revalue(double r, double ar[], int n) {
for (int i = 0; i < n; i++) {
ar[i] *= r;
}
}
习题7.13.7
//习题7.13.7
#include <iostream>
using namespace std;
const int Max = 5;
double* fill_array(double* start, int limit)
{
double num;
int i;
for (i = 0; i < limit; i++)
{
cout << "#" << i + 1 << ":";
cin >> num;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "输入有误" << endl;
break;
}
else if (num < 0)
break;
*(start + i) = num;
}
return start + i;
}
void show_array(double* start, double* arr_end) {
for (double* i = start; i < arr_end; i++)
{
cout << *i << endl;
}
}
void revalue(double r, double* start, double* arr_end) {
for (double* i = start; i < arr_end; i++)
{
*i *= r;
}
}
int main()
{
double arr[Max];
double* arr_end = fill_array(arr, Max);
show_array(arr, arr_end);
cout << "输入折扣:";
double r;
cin >> r;
revalue(r, arr, arr_end);
show_array(arr, arr_end);
return 0;
}
7.13.8
修改7.15,不使用array对象的前提下编写下面两个版本
1.使用const char* 数组存储表示季度名称的字符串,
并使用double数组存储开支
2.使用const char* 数组存储表示季度名称的字符串,
并使用一个结构,该结构只有一个成员
一个用于存储开支的double数组
程序7.15
//程序7.15
#include <iostream>
#include <array>
#include <string>
using namespace std;
const int Season = 4;
const array<string, Season> Snames = {"Spring", "Summer", "Fall", "Winter"};
void fill_(array<double, Season>* pa);
void show(array<double, Season> da);
int main()
{
array<double, Season> expenses;
fill_(&expenses);
show(expenses);
return 0;
}
void fill_(array<double, Season>* pa)
{
for (int i = 0; i < Season; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(array<double, Season> da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Season; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
习题7.13.8
//习题7.13.8
#include <iostream>
using namespace std;
const int Season = 4;
const char* Sname[Season] = {"Spring", "Summer", "Fall", "Winter"};
void fill_(double* arr)
{
for (int i = 0; i < Season; i++)
{
cout << "输入" << Sname[i] << "的开支:";
cin >> arr[i];
}
}
void show(double *arr)
{
double total = 0.0;
cout << "下面展示结果:" << endl;
for (int i = 0; i < Season; i++)
{
cout << Sname[i] << "的开支:" << arr[i] << endl;
total += arr[i];
}
cout << "四季总开支:" << total << endl;
}
int main()
{
double arr[Season] = {0.0};
fill_(arr);
show(arr);
return 0;
}
7.13.9
题目要求在代码中
ps:这题是真的阴间,全英不说,就这个提前结束输入就很难打
#include <iostream>
#include <cstring>
using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
/*
getinfo()有两个参数:
第一个指针指向结构数组
第二个是结构数组的数量
函数用来存放学生的有关信息
在填充数组或者在学生姓名输入为空行时结束输入
函数返回实际的数组中元素个数
*/
int getinfo(student pa[], int n)
{
int i = 0;
char c = '\n';
char* ch = &c;
for (; i < n; i++)
{
cout << "输入第" << i+1 << "个学生的姓名:";
cin.getline(pa[i].fullname, SLEN);
/*当输入为换行的时候,字符串长度为0,
用于判断是否输入为换行*/
if (strlen(pa[i].fullname) == 0)
break;
cout << "输入第" << i+1 << "个学生的爱好:";
cin >> pa[i].hobby;
cout << "输入第" << i+1 << "个学生的opp等级:";
if (!(cin >> pa[i].ooplevel))
break;
cin.get();
}
return i;
}
/*
display1()接受一个结构体参数,
将其打印出来
*/
void display1(student st)
{
cout << "姓名:" << st.fullname << endl;
cout << "爱好:" << st.hobby << endl;
cout << "oop等级:" << st.ooplevel << endl;
}
/*
display2()接受一个结构体指针参数,
将其打印出来
*/
void display2(const student* ps)
{
cout << "姓名:" << ps->fullname << endl;
cout << "爱好:" << ps->hobby << endl;
cout << "oop等级:" << ps->ooplevel << endl;
}
/*
display3()接受一个结构体数组,一个结构体数量
将其打印出来
*/
void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "姓名:" << pa[i].fullname << endl;
cout << "爱好:" << pa[i].hobby << endl;
cout << "oop等级:" << pa[i].ooplevel << endl;
}
}
int main()
{
cout << "Enter class size: ";
int class_size;
cin >> class_size;
while(cin.get() != '\n')
continue;
student* ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++)
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[] ptr_stu;
cout << "Done\n";
return 0;
}
7.13.10
编写一个有关计算的函数:
calculate(double, double, double(*ptr_f)(double, double))
函数参数为两个double的数以及一个指向函数的指针
calculate()类型是double,返回被指向函数的计算结果
如:
double add(double x, double y)
{
return x+y;
}
double q = calculate(2.0, 5.0, add);
这个题也是真的恶心,函数+指针的难度档次直接就上去了
#include <iostream>
#include <string>
using namespace std;
/*
编写一个有关计算的函数:
calculate(double, double, double(*ptr_f)(double, double))
函数参数为两个double的数以及一个指向函数的指针
calculate()类型是double,返回被指向函数的计算结果
如:
double add(double x, double y)
{
return x+y;
}
double q = calculate(2.0, 5.0, add);
*/
double calculate(double a, double b, double(*ptr_f)(double, double))
{
return (*ptr_f)(a, b);
}
double add(double a, double b)
{
return a+b;
}
double sub(double a, double b)
{
return a-b;
}
double times(double a, double b)
{
return a*b;
}
int main()
{
double a, b;
double (*ptr_f[3]) (double, double) = {add, sub, times};
string s[3] = {"add", "sub", "times"};
cout << "输入非数字退出" << endl;
while (cin >> a >> b)
{
for (int i = 0; i < 3; i++)
{
cout << s[i] << ":" << a;
if (s[i] == "add")
cout << "+";
else if (s[i] == "sub")
cout << "-";
else
cout << "*";
cout << b << "=" << calculate(a, b, ptr_f[i]) << endl;
}
}
return 0;
}