C++ Primer Plus 第六版 第七章练习答案
-
普通声明;
-
二维数组声明;
-
Array对象声明;
-
递归;
-
函数指针;
#include <iostream>
using namespace std;
// practice 7.1
double average(double x, double y) {
return 2.0*x*y / (x + y);
}
void p7_1(void){
double x, y;
double avg = 0;
cout << "Enter two numbers(zero to end): ";
cin >> x >> y;
cin.get();
while (x != 0 && y != 0){
avg = average(x, y);
cout << "The average of " << x << " and " << y << " is " << avg << endl;
cout << "Enter the next two numbers: ";
cin >> x >> y;
cin.get();
}
}
// practice 7.2
int Input(double* grade, int number){
cout << "You can enter up to 10 grades( -1 to quit): " << endl;
for (int i = 0; i < number; i++)
if (!(cin >> grade[i]) || grade[i] == -1) {
grade[i] = 0;
return i;
}
}
void Print(const double* grade, const int number){
cout << "The grade is: " << endl;
for (int i = 0; i < number; i++)
cout << grade[i] << " ";
cout << endl;
}
void CalAvg(const double* grade, const int number){
double total = 0.0;
cout << "The average is :";
for (int i = 0; i < number; i++)
total += grade[i];
cout << total / number << endl;
}
const int ArrSize = 10;
void p7_2(void){
double grade[ArrSize] = { 0 };
int num = 0;
num = Input(grade, ArrSize);
Print(grade, num);
CalAvg(grade, num);
return;
}
// practice 7.3
struct box{
char maker[40];
float height;
float width;
float length;
float volume;
};
void Print_value(box vbox){
cout << "Box maker: " << vbox.maker << endl;
cout << "Box height: " << vbox.height << endl;
cout << "Box width: " << vbox.width << endl;
cout << "Box length: " << vbox.length << endl;
cout << "Box volume: " << vbox.volume << endl;
}
void CalVolume(box* pbox) {
pbox->volume = pbox->height * pbox->width * pbox->length;
}
void p7_3(void){
box mbox = { "Demo", 1.0, 1.0, 1.0, 2.0 };
Print_value(mbox);
CalVolume(&mbox);
Print_value(mbox);
}
// practice 7.4
long double probability(unsigned numbers, unsigned picks){
long double result = 1.0;
unsigned n = 0, p = 0;
for (n = numbers, p = picks; p > 0; n--, p--) //此处不能写成(unsigned n = numbers, unsigned p = picks; ...)
result = result * p / n; //若是单个的话就可以
return result;
}
const unsigned total_1 = 47, total_2 = 27;
const unsigned choices_1 = 5, choices_2 = 1;
void p7_4(void){
cout << "You have on chance in ";
cout << probability(total_1, choices_1) * probability(total_2, choices_2);
cout << " of winning.\n" << endl;
}
// practice 7.5
long long factorial(unsigned int number){
if (number == 0 || number == 1)
return 1;
else
return number * factorial(number - 1);
}
void p7_5(void){
long long result = 0;
unsigned int input;
cout << "Please enter the number: ";
cin >> input;
while (1){
result = factorial(input);
cout << "The result of " << input << "! is " << result << "." << endl;
cout << "Please enter the next number: ";
cin >> input;
}
}
// practice 7.6
int Fill_array(double* arr, const int number) {
cout << "Enter numbers(non-digital to quit): " << endl;
for (int i = 0; i < number; i++)
if (!(cin >> arr[i])) {
arr[i] = 0;
return i;
}
}
void Show_array(const double* arr, const int start, const int end) {
cout << "The array is :" << endl;
for (int i = start; i < end; i++)
cout << arr[i] << " ";
cout << endl;
}
void Reverse_array(double* arr, int length){
double tmp;
cout << "Reverse the array..." << endl;
for (int i = 0; i < length; i++) {
if (i <= length - 1 - i) {
tmp = arr[i];
arr[i] = arr[length - 1 - i];
arr[length - 1 - i] = tmp;
}
else
break;
}
}
const int Arrsize = 10;
void p7_6(void){
double* arr=new double[Arrsize];
int num = Fill_array(arr, Arrsize);
Show_array(arr, 0, num);
Reverse_array(arr, num);
Show_array(arr, 0, num);
Show_array(arr, 1, num-1);
}
// practice 7.7
double* fill_array(double* begin, double* end){
double temp = 0.0;
int i = 0;
double *tmp;
for (tmp = begin; tmp < end; tmp++){
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin){
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input preocess terminated.\n";
break;
}
else if (temp < 0)
break;
*tmp = temp;
i++;
}
return tmp;
}
void show_array(const double* begin, const double* end){
const double *tmp = nullptr;
unsigned int i = 0;
for (tmp = begin; tmp < end; tmp++){
cout << "Property #" << (i + 1) << ": $";
cout << *tmp << endl;
i++;
}
}
void revalue(double* begin, double* end, double r){
for (double * array = begin; array < end; array++)
*array *= r;
}
const int Max = 5;
void p7_7(void){
double properties[Max];
double* end = fill_array(properties, properties + Max);
show_array(properties, end);
if (end - properties > 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(properties, end, factor);
show_array(properties, end);
}
cout << "Done.\n";
return;
}
// practice 7.8
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
//==================a. ======================
void fill(double* pa){
for (int i = 0; i < Seasons; i++){
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pa[i];
}
}
void show(double* pa){
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++){
cout << Snames[i] << ": $" << pa[i] << endl;
total += pa[i];
}
cout << "Total Expenses: $" << total << endl;
}
//==================b. =====================
struct cost{
double expenses[Seasons];
};
void fill(cost* pCost){
for (int i = 0; i < Seasons; i++){
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pCost->expenses[i];
}
}
void show(cost* pCost){
double total = 0.0;
cout << "\nEXPENSE\n";
for (int i = 0; i < Seasons; i++){
cout << Snames[i] << ": $" << pCost->expenses[i] << endl;
total += pCost->expenses[i];
}
cout << "Total Expense: $" << total << endl;
}
void p7_8(void){
// a.
cout << "============= a =============" << endl;
double* pa = new double[Seasons];
fill(pa);
show(pa);
// b.
cout << endl << endl;
cout << "============= b =============" << endl;
struct cost* pCost = new cost;
fill(pCost);
show(pCost);
delete pa;
delete pCost;
}
// practice 7.9
const int SLEN = 30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n){
int number = 0;
for (int i = 0; i < n; i++){
cout << "Enter the infomation of student #" << i + 1 << endl;
cout << "Enter full name (blank line to quit): ";
cin.getline(pa[i].fullname, SLEN);
cout << "Enter hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Enter ooplevel: ";
cin >> pa[i].ooplevel;
while (cin.get() != '\n')
continue;
number++;
}
return number;
}
void display1(student st){
cout << "Display1: " << endl;
cout << "Full name: " << st.fullname << endl;
cout << "Hobby: " << st.hobby << endl;
cout << "Ooplevel: " << st.ooplevel << endl;
}
void display2(const student* st){
cout << "Display2: " << endl;
cout << "Full name: " << st->fullname << endl;
cout << "Hobby: " << st->hobby << endl;
cout << "Ooplevel: " << st->ooplevel << endl;
}
void display3(const student st[], int n){
cout << "Display3: " << endl;;
for (int i = 0; i < n; i++){
cout << "Infomation of student #" << i + 1 << ": " << endl;
cout << "Full name: " << st[i].fullname << endl;
cout << "Hobby: " << st[i].hobby << endl;
cout << "Ooplevel: " << st[i].ooplevel << endl;
}
}
void p7_9(void){
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]);
for (int i = 0; i < entered; i++)
display2(&ptr_stu[i]);
display3(ptr_stu, entered);
delete[] ptr_stu;
cout << "Done.\n";
return;
}
// practice 7.10
#include<string>
double add(double x, double y) {
return x + y;
}
double div(double x, double y) {
return x / y;
}
double calculate(double x, double y, double(*f)(double, double)){
return f(x, y);
}
double (*pf[2])(double, double) = { add,div };
string op[2] = { "add","div" };
void p7_10(void){
double x = 0, y = 0;
cout << "Enter two double number: ";
while (cin >> x >> y){
for (int i = 0; i < 2; i++)
cout << x << op[i] << y << " = " << calculate(x, y, pf[i]) << endl;
cout << "Enter next two double number: ";
}
}
int main(int argc, char **argv) {
cout << "=====================================\n"
<< "============ Chapter7: ============\n"
<< "=====================================\n\n";
cout << "==>> Practice 7_1:\n";
p7_1();
cout << endl;
cout << "==>> Practice 7_2:\n";
p7_2();
cout << endl;
cout << "==>> Practice 7_3:\n";
p7_3();
cout << endl;
cout << "==>> Practice 7_4:\n";
p7_4();
cout << endl;
cout << "==>> Practice 7_5:\n";
p7_5();
cout << endl;
cout << "==>> Practice 7_6:\n";
p7_6();
cout << endl;
cout << "==>> Practice 7_7:\n";
p7_7();
cout << endl;
cout << "==>> Practice 7_8:\n";
p7_8();
cout << endl;
cout << "==>> Practice 7_9:\n";
p7_9();
cout << endl;
cout << "==>> Practice 7_9:\n";
p7_10();
cout << endl;
}