C++ PRIMER PLUS (第六版) 中文版 第七章编程练习答案

时间:2022-02-26 14:41:29

1. 

#include<iostream>

void main()
{
using namespace std;
double thpjs(int x, int y);
int x, y;
while (1)
{
cout << "输入两个数,结束请按零:";
cin >> x >> y;
if (x == 0 || y == 0)
break;
cout << "你输入的两个数的调和平均数是:" << thpjs(x, y) << endl;
}
cout << "拜拜!" << endl;
}


double thpjs(int x, int y)
{
return 2.0 * x * y / (x + y);

}

*****************************************************************************************************************************************************************************************

2.

#include<iostream>
#define s 10
void main()
{
using namespace std;
void input(double  *p, int size);
double average(const double *p, int size);
void display(const double *p, int size);
double golf[s];
input(golf, s);
display(golf, s);
cout << "平均成绩:" << average(golf, s) << endl;
}

void input(double *p, int size)
{
using namespace std;
int i = 0;
cout << "请输入10个高尔夫成绩,结束请输入负数。\n";
while (i < size)
{
cout << "第 " << i + 1 << "个:";
cin >> *(p + i);
if (*(p + i) < 0)
break;
i++;
}
}


void display(const double *p, int size)
{
using namespace std;
int i = 0;
cout << "所有的成绩记录如下:\n";
while (i < size)
{
if (*(p + i) < 0)
break;
cout << i + 1 << ": " << *(p + i) << endl;
i++;
}
}


double average(const double *p, int size)
{
double total = 0;
int i = 0;
while (i < size)
{
if (*(p + i) < 0)
break;
total += *(p + i);
i++;
}
return total / i;
}

*****************************************************************************************************************************************************************************************

3.

#include<iostream>
struct bo
{
char maker[15];
float height;
float width;
float length;
float volume;
};
void main()
{
using namespace std;
void volume(bo *p);
void display(bo p);


bo p = { "weihua", 6.6, 6.6, 6.6, 0 };
volume(&p);
display(p);
}


void volume(bo *p)
{
p->volume = p->height * p->width * p->length;
}


void display(bo p)
{
using namespace std;
cout << "以下是产品的所有信息:\n";
cout << p.maker << endl;
cout << "高:" << p.height << endl;
cout << "宽:" << p.width << endl;
cout << "长:" << p.length << endl;
cout << "体:" << p.volume << endl;
}


*****************************************************************************************************************************************************************************************


4.

#include<iostream>
void main()
{
using namespace std;
double pro_game1(int t, int c);
double pro_game2(int t, int c);
int total1, total2, choice1, choice2;
double r1, r2, r3;
while (1)
{
cout << "输入游戏1的卡片总数和要挑选的卡片数:";
while (!(cin >> total1 >> choice1 && choice1 <= total1))
{
cout << "输入有误,请重新输入!";
}
cout << "输入游戏2的卡片总数和要挑选的卡片数:";
while (!(cin >> total2 >> choice2 && choice2 <= total2))
{
cout << "输入有误,请重新输入!";
}
r1 = pro_game1(total1, choice1);
r2 = pro_game2(total2, choice2);
r3 = r1 * r2;
cout << "您赢得这场游戏的概率是:" <<r3 << endl;
}
}


double pro_game1(int t, int c)
{
int i;
double result = 1.0;
for (i = 0; i < c; i++)
{
result *= (t - i) / (c - i);
}
return 1.0/result;
}


double pro_game2(int t, int c)
{
return (1.0*c)/t;
}


*****************************************************************************************************************************************************************************************


6.

#include<iostream>
#define s 10
void main()
{
using namespace std;
int  fill_array(double *p);
void dis_arr(double *p, int n);
int reverse(double *p, double *q);
double ar[s];
int count;
if (!(count = fill_array(ar)))
{
cout << "empty!";
exit(0);
}
cout << "原来的数组:\n";
dis_arr(ar, count);
reverse(ar, ar + count);
cout << "\n翻转后的数组:\n";
dis_arr(ar, count);
reverse(ar, ar + count);
cout << "\n只反转中间的元素后的数组:\n";
reverse(ar + 1, ar + count - 1);
dis_arr(ar, count);
}


int  fill_array(double *p)
{
using namespace std;
int  i = 0 ;
while (i < s)
{
cout << "为数组输入一个数:";
if (!(cin >> *(p + i)))
{
cin.clear();
while (cin.get() != '\n')
continue;
break;
}
i++;
}
return i;
}


void dis_arr(double *p, int n)
{
using namespace std;
int i;
cout << "数组成员:\n";
for (i = 0; i < n; i++)
{
cout << *(p + i) << " ";
}
}


int reverse(double *p, double *q)
{
using namespace std;
double temp;
if (p == q)
{
cout << "数组为空!";
return 0;
}
q--;
while (p <= q)
{
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
return 1;
}

*****************************************************************************************************************************************************************************************

8.

#include<iostream>
const char season[4][10] = { "spring", "summer", "autumn", "winter" };
void main()
{
using namespace std;
int  fill_array(double *p, int n);
void dis_arr(const double *p, int n);
double expense[4];
fill_array(expense, 4);
dis_arr(expense, 4);
}


int  fill_array(double *p, int n)
{
using namespace std;
int  i = 0;
while (i < n)
{
cout << "输入:"<<season[i]<<" 的花费:";
if (!(cin >> *(p + i)))
{
cin.clear();
while (cin.get() != '\n')
continue;
break;
}
i++;
}
return i;
}
void dis_arr(const double *p, int n)
{
using namespace std;
int i;
double total = 0;
cout << "各季度的花费:\n";
for (i = 0; i < n; i++)
{
cout << season[i]<<" : "<<*(p + i) << " ";
total += *(p + i);
cout << endl;
}
cout << "总花费: " << total << endl;
}


****************************************************************************************************************************************************************************************


9.

#include<iostream>
using namespace std;
const int slen = 30;
struct student
{
char fullname[slen];
char hobby[slen];
int opplevel;
};
void main()
{
int getinfo(student p[], int n);
void display1(student st);
void display2(student *st);
void display3(student p[], int n);
cout << "输入班级人数:";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student * p_s = new student[class_size];
int entered = getinfo(p_s, class_size);
for (int i = 0; i < entered; i++)
{
display1(p_s[i]);
display2(&p_s[i]);
}
display3(p_s, entered);
delete[] p_s;
cout << "done!\n";
}


int getinfo(student p[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "\n输入第" << i + 1 << "个学生的信息:\n";
cout << "全名:";
if(!(cin >> (p + i)->fullname))
break;
cout << "\n爱好:";
cin >> (p + i)->hobby;
cout << "\nopp level:";
cin >> (p + i)->opplevel;
}
return i;
}


void display1(student st)
{
cout << st.fullname << "   " << st.hobby << "   " << st.opplevel << endl;
}


void display2(student *st)
{
cout << st->fullname << "   " << st->hobby << "   " << st->opplevel << endl;
}


void display3(student p[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout <<(p + i)->fullname << "  " <<(p + i)->hobby;
cout << "  " << (p+i)->opplevel;
}
}

*****************************************************************************************************************************************************************************************

10.

#include<iostream>
using namespace std;
char a[4][4] = { "加", "减", "乘", "除" };
void main()
{
double calculate(double x, double y, double(*p)(double a, double b));
double add(double x, double y);
double sub(double x, double y);
double mul(double x, double y);
double div(double x, double y);
int x, y;
double(*p[4])(double, double) = { add, sub, mul, div };
//double(*(*pc)[4])(double, double) = &p;
auto pc = &p;
while (1)
{
cout << "请输入两个数,结束请输入任意字母:";
if (!(cin>>x>>y))
{
cin.clear();
while (cin.get() != '\n')
continue;
break;
}
for (int i = 0; i < 4; i++)
{
cout << a[i] << " : " << calculate(x, y, *(*pc + i)) << endl;
}
}
}


double calculate(double x, double y, double(*p)(double a, double b))
{
return p(x, y);
}


double add(double x, double y)
{
return x + y;
}


double sub(double x, double y)
{
return x - y;
}


double mul(double x, double y)
{
return x * y;
}


double div(double x, double y)
{
return x / y;
}