C++ Primer Plus 第六版 学习笔记 第七章 编程练习答案

// 编程练习 7.13.1.cpp : 定义控制台应用程序的入口点。
// 编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:调和平均数=2.0*x*y/(x+y)


#include "stdafx.h"
#include
using namespace std;
double thaverage(double x, double y);
int main()
{
double n1, n2;
double result;
n1 = 1; n2 = 1;
while (n1 != 0 && n2 != 0)
{
cout << "\nEnter first number: ";
cin >> n1;
cout << "\nEnter second number: ";
cin >> n2;
result = thaverage(n1, n2);
cout << "The thaverage is " << result << endl;
}
}


double thaverage(double x, double y)
{
double ave;
ave = 2.0 * x * y / (x + y);
return ave;

}


// 编程练习 7.13.2.cpp : 定义控制台应用程序的入口点。
// 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用三个数组处理函数来分别进行输入、显示和计算平均成绩。
#include "stdafx.h"
#include
using namespace std;
const int Max = 10;
// function prototypes
int input(double arr[], int limit);
void show(const double arr[], int n);
double average(double arr[], int n);
int main()
{
double golf[Max];
int size = input(golf, Max);
show(golf, size);
double a = average(golf, size);
cout << "The average result is " << a << ".\n";
return 0;
}


int input(double arr[], int limit)
{
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!temp) // bad input
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0)
break;
arr[i] = temp;
}
return i;
}
void show(const double arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "The result of #" << (i + 1) << ": ";
cout << arr[i] << endl;
}
}
double average(double arr[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
double ave = sum / n;
return ave;
}


// 编程练习 7.13.3.cpp : 定义控制台应用程序的入口点。
// 下面是一个结构声明:struct box { char maker[40]; float height; folat width; float length; float volume; };
// a 编写一个函数,按值传递box结构,并显示每个成员的值。
// b 编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
// c 编写一个使用这两个函数的简单程序。
#include "stdafx.h"
#include
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
using namespace std;
void show(box b);
void set(box * pb);
int main()
{
box tBox = { "Abc", 15.5, 20.5, 7.25, 50.0 };
box *p = &tBox;
cout << "b. " << endl;
show(tBox);
cout << "pb. " << endl;
set(p);
return 0;
}


void show(box b)
{
int i = 0;
cout << "Maker: ";
while (b.maker[i] != '\0')
{
cout << b.maker[i];
i++;
}
cout << endl;
cout << "Height: " << b.height << endl;
cout << "width: " << b.width << endl;
cout << "Length: " << b.length << endl;
cout << "Volume: " << b.volume << endl;
}
void set(box * pb)
{
int i = 0;
cout << "Maker: ";
while (pb->maker[i] != '\0')
{
cout << pb->maker[i];
i++;
}
cout << endl;
cout << "Height: " << pb->height << endl;
cout << "width: " << pb->width << endl;
cout << "Length: " << pb->length << endl;
pb->volume = pb->height * pb->length * pb->width;
cout << "Volume: " << pb->volume << endl;
}


// 编程练习 7.13.5.cpp : 定义控制台应用程序的入口点。
// 定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于3*2!,以此类推:而0!被定义为1.通用的计算公式是,如果n大于哦,则n!=n*(n-1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
#include "stdafx.h"
#include
using namespace std;
int factorial(int n);
int main()
{
cout << "Enter a number: ";
int num;
while ((cin >> num))
{
int result = factorial(num);
cout << "The factorial of " << num << ": " << result << endl;
cout << "Enter a number:";
}
}
int factorial(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}



// 编程练习 7.13.6.cpp : 定义控制台应用程序的入口点。
// 编写一个程序,它使用下列函数:Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。Show_array()将一个double数组的名称和长度作为参数,并显示数组的内容。Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序翻转。程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素以外的所有元素,然后显示数组。
#include "stdafx.h"
#include
using namespace std;
int Fill_array(double arr[], int limit);
void Show_array(const double arr[], int n);
void Reverse_array(double arr[], int n, int location);
const int MAX = 50;
int main()
{
double properties[MAX];
int size = Fill_array(properties, MAX);
Show_array(properties, size);
if (size > 0)
{
cout << "Enter the location where to reverse array:";
int loc;
while (!(cin >> loc))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input a number: ";
}
Reverse_array(properties, size, loc);
Show_array(properties, size);
}
cout << "Done.\n";
cin.get();
cin.get();
return 0;


}


int Fill_array(double arr[], 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;
arr[i] = temp;
}
return i;
}
void Show_array(const double arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Property #" << (i + 1) << ": ";
cout << arr[i] << endl;
}
}
void Reverse_array(double arr[], int n, int location)
{
double n1, n2;
for (int i = location; i < (n / 2); i++)
{
n1 = arr[i];
n2 = arr[n - i -1];
arr[i] = n2;
arr[n - i -1] = n1;
}
}


// 编程练习 7.13.7.cpp : 定义控制台应用程序的入口点。
// 修改程序清单7.7中的三个数组处理函数,使之使用两个指针参数来表示区间。fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。
#include "stdafx.h"
#include
using namespace std;
const int SIZEARRAY = 10;
double * Fill_array(double* arr);
void Show_array(double* arr,double* end);
void Reverse_array(double*arr, double* end );
int main()
{
double Array[SIZEARRAY];
double *arr = Array;
double* End = Fill_array(Array);
Show_array(Array, End);
Reverse_array(Array, End);
Show_array(Array, End);
cin.clear(); // 重置输入流
cin.sync(); // 清空q和换行,可转换为两个get()
cin.get(); // wait
}
double* Fill_array(double* arr)
{
int i = 0;
cout << "Enter #1, q to quit:";
while (i < SIZEARRAY && cin >> arr[i])
{
i++;
cout << "# " << i + 1 << ": ";
}
double * p = &arr[i - 1];
return p;
}


void Show_array(double* arr, double* end)
{
double* p = arr;
while (p != end)
{
cout << (*p)++ << " ";
p++;
}
cout << endl;
}


void Reverse_array(double* arr, double* end)
{
double* start_ = arr;
double* end_ = arr;
while (start_ < end_)
{
double temp = *start_;
*start_ = *end_;
*end_ = temp;
start_++;
end_--;
}
}


// 编程练习 7.13.8.cpp : 定义控制台应用程序的入口点。
// 在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。这种设计与使用array类的基本设计类似。
#include "stdafx.h"
#include
using namespace std;
const int Season = 4;
const char * Sname[Season] = { "Spring", "Summer", "Fall", "Winter" };
void fill(double *exp);
void show(double *exp);
int main()
{
double expenses[Season];
fill(expenses);
show(expenses);
cin.sync();
cin.get();
return 0;
}
void fill(double *exp)
{
for (int i = 0; i < Season; i++)
{
cout << "Enter " << Sname[i] << " expenses: ";
cin >> exp[i];
}
}
void show(double *exp)
{
double total = 0.0;
cout << endl << "EXPENSES" << endl;
for (int i = 0; i < Season; i++)
{
cout << "Enter " << Sname[i] << ": $ " << exp[i] << endl;
total += exp[i];
}
cout << "Total Expenses: $ " << total << endl;
}


// 编程练习 7.13.9.cpp : 定义控制台应用程序的入口点。
// 这个练习编写处理数组和结构的函数,完成程序。
#include "stdafx.h"
#include
using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of an array of student structures and an int representing the number of elements of the array. The function solicits and stores data about students. It terminates input upon filling the array or upon encountering a blank line for the student name. The function returns the actural number of array elements filled.
int getinfo(student pa[], int n);


// display1() takes a student structure as an argument and displays its contents
void display1(student st);


//display2() takes the address of student structures as an argument and displays the structure's contents
void display2(const student * ps);


// display3() takes the address of the first element of an array of student structures and the number of array elements as arguments and displays the contents of the structures
void display3(const student pa[], int n);


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;
}


int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Enter full name of " << i + 1 << ": ";
cin >> pa[i].fullname;
cout << "Enter hobby of " << i + 1 << ": ";
cin >> pa[i].hobby;
cout << "Enter ooplevel of " << i + 1 << ": ";
cin >> pa[i].ooplevel;
}
return i;
}


void display1(student st)
{
cout << "The student's full name is " << st.fullname << endl;
cout << "The student's hobby is " << st.hobby << endl;
cout << "The student's ooplevel is " << st.ooplevel << endl;
}


void display2(const student * ps)
{
cout << "The student's full name is " << ps->fullname << endl;
cout << "The student's hobby is " << ps->hobby << endl;
cout << "The student's ooplevel is " << ps->ooplevel << endl;
}


void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "The student's full name is " << pa[i].fullname << endl;
cout << "The student's hobby is " << pa[i].hobby << endl;
cout << "The student's ooplevel is " << pa[i].ooplevel << endl;
}
}


// 编程练习 7.13.10.cpp : 定义控制台应用程序的入口点。
// 设计一个名为calculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值。calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假设add()的函数定义如下:double add(double x, double y)   { return x + y; }   则下述代码中的函数调用将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9): double q = calculate(2.5, 10.4, add);请编写一个程序,它调用上述两个函数和至少另一个与add()类似的函数。该程序使用循环来让用户成对的输入数字。对于每对数字,程序都使用calculate()来调用add()和至少一个其他的函数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。提示:下面是声明这种指针数组的方式,其中包含三个指针: double (*pf[3])(double, double); 可以采用数组初始化语法,并将函数名作为地址来初始化这样的数组。
#include "stdafx.h"
#include
using namespace std;
double add(double x, double y);
double acc(double x, double y);
double mul(double x, double y);
double calculate(double x, double y, double(*p)(double, double));
int main()
{
double x, y;
double(*pf[3])(double, double) = { add, acc, mul };
cout << "Enter x y:";
cin >> x >> y;
for (int i = 0; i < 3; i++)
{
cout << calculate(x, y, pf[i]) << endl;
}
cin.sync();
cin.get();
return 0;
}


double add(double x, double y)
{
return x + y;
}
double acc(double x, double y)
{
return x - y;
}
double mul(double x, double y)
{
return x * y;
}
double calculate(double x, double y, double(*p)(double, double))
{
return (*p)(x, y);
}


你可能感兴趣的:(C,Primer,Plus,编程练习)