c++ primer plus(第6版)中文版 第七章编程练习答案

时间:2020-11-29 14:40:47

第七章编程练习答案

7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数

//7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数
#include <iostream>
using namespace std;

double average (unsigned x, unsigned y)
{
return (2.0 * x * y / (x + y));
}

int main ()
{
while (true) {
unsigned x, y;
cout << "输入一对正整数,计算其调和平均数(0退出):";
cin >> x >> y;
if (0 == x || 0 == y)
break;
cout << "--> " << average(x, y) << endl;
}
}

7.2要求用户输入10个高尔夫成绩到数组里(可提前输入),计算平均成绩,用3个函数分别输入,显示和计算。

//7.2要求用户输入10个高尔夫成绩到数组里(可提前输入),计算平均成绩,用3个函数分别输入,显示和计算。
#include <iostream>
using namespace std;

unsigned input (double scores[], unsigned size)
{
double tmp;
unsigned num = 0;
while (num < size && cin >> tmp && -1 != tmp) {
scores[num++] = tmp;
}
return num;
}

void show (double scores[], unsigned size)
{
for (unsigned i = 0; i < size; ++i)
cout << scores[i] << ' ';
}

double average (double scores[], unsigned size)
{
double sum = 0;
for (unsigned i = 0; i < size; ++i)
sum += scores[i];
return (sum / size);
}

int main (void)
{
const unsigned size = 10;
double scores[size];
cout << "输入" << size << "个高尔夫成绩,-1可提前结束:";
unsigned num = input(scores, size);
cout << "高尔夫成绩如下:\n";
show(scores,num);
cout << endl;
cout << "高尔夫成绩均值为:" << average(scores, num) << endl;
}

7.3如下结构体Box,编写一个主函数和两个函数:
//a.按值传递Box结构,并依次显示值
//b.传递Box的地址,将volume设计为其他三维的乘积

//7.3如下结构体Box,编写一个主函数和两个函数:
//a.按值传递Box结构,并依次显示值
//b.传递Box的地址,将volume设计为其他三维的乘积
#include <iostream>
using namespace std;

struct Box
{
char Maker[40];
float height;
float width;
float length;
float volume;
};

void show (Box box) {
cout << box.Maker << ' ' << box.height << ' ' << box.width << ' ' << box.length;
}

float volume (Box* box)
{
return (box->volume = box->height * box->width * box->length);
}

int main (void)
{
Box box = {"BigBro", 12, 8, 6, 0};
show(box);
cout << endl;
cout << "体积为:" << volume(&box) << endl;
}

7.4编写彩票程序,中头奖为(1~47)选5个和(1~27)选1个,计算头奖几率

//7.4编写彩票程序,中头奖为(1~47)选5个和(1~27)选1个,计算头奖几率
#include <iostream>
using namespace std;

long double probability (unsigned numbers, unsigned picks)
{
long double pro = 1.0;
while (picks > 0) {
pro *= 1.0 * numbers / picks;
--numbers;
--picks;
}
return (pro);
}

int main ()
{
long double pro = probability(47, 5) * probability(27, 1);
cout << "你有" << (int)pro << "分之一的几率中奖" << endl;
}

7.5编写程序利用函数递归计算阶乘

//7.5编写程序利用函数递归计算阶乘
#include <iostream>
using namespace std;

long double jiecheng(int n)
{
if(n==1) return n;
else return n*jiecheng(n-1);
}

int main()
{
int n=0;
cout << "input n=" ;
cin >> n;
cout << n << "!=" << jiecheng(n) << endl;
}

7.6编写一个程序包含3个函数,插入,显示和逆序

//7.6编写一个程序包含3个函数,插入,显示和逆序
#include <iostream>
using namespace std;

void Fill_array(double arr[],int arrSize)
{
int i=0;
for(i=0;i<arrSize;i++)
{
cout<<"Please enter the "<<i+1<<" double values:";
if(!(cin>>arr[i]))
break;
}
cout<<"you had input "<<i<<" values!"<<endl;
}

void const Show_array(const double arr[],int arrSize)
{
cout<<"the array is:"<<endl;
for(int i=0;i<arrSize;i++)
cout<<arr[i]<<" ";
}

void Reverse_array(double arr[],int arrSize)
{
cout<<"now Reverse the array!"<<endl;
for(int i=0;i<arrSize/2;i++)
{
swap(arr[i],arr[arrSize-1-i]);
}
Show_array(arr,arrSize);
}
int main()
{
double arr[10] {0};
Fill_array(arr,10);
Show_array(arr,10);
Reverse_array(arr,10);
cout << endl;
}

7.7对7.6,使用指针表示区间,编写程序

//7.7对7.6,使用指针表示区间,编写程序
#include <iostream>
using namespace std;

void Fill_array(double arr[],int arrSize)
{
int i=0;
for(i=0;i<arrSize;i++)
{
cout<<"Please enter the "<<i+1<<" double values:";
if(!(cin>>arr[i]))
break;
}
cout<<"you had input "<<i<<" values!"<<endl;
}

void const Show_array(double* begin,double* end)
{
cout<<"the array is:"<<endl;
while(begin!=end)
cout<<*begin++<<" ";
}

void Reverse_array(double* begin,double* end)
{
cout<<"now Reverse the array!"<<endl;
while(end-begin>1) //两地址相减,差值位一个指向的单位
{
swap(*begin,*end);
++begin;
--end;
}
}
int main()
{
double arr[10] {0};
Fill_array(arr,10);
Show_array(arr,arr+10);
Reverse_array(arr,arr+9);
Show_array(arr,arr+10);
cout << endl;
}

7.8.a对程序清单7.15,使用const char*储存季度名,用double数组记录开支

//7.8.a对程序清单7.15,使用const char*储存季度名,用double数组记录开支
#include <iostream>
#include <string>
using namespace std;
const int Seasons = 4;
const char* Snames[] = {"Spring", "Summer", "Fall", "Winter"};

void fill(double* pa)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pa[i];
}
}

void show(double da[])
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}

int main()
{
double expenses[Seasons];
fill(expenses);
show(expenses);
return 0;
}

7.8.b对程序清单7.15,使用const char*储存季度名,包含一个double数组成员的结构体记录开支

//7.8.b对程序清单7.15,使用const char*储存季度名,包含一个double数组成员的结构体记录开支
#include <iostream>
#include <string>

const int Seasons = 4;
const char* Snames[] = {"Spring", "Summer", "Fall", "Winter"};

struct TData
{
double arr[Seasons];
};

void fill(TData* pData)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pData->arr[i];
}
}

void show(TData data)
{
using namespace std;
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << data.arr[i] << endl;
total += data.arr[i];
}
cout << "Total Expenses: $" << total << endl;
}

int main()
{
TData expenses;
fill(&expenses);
show(expenses);
}

7.9根据已有代码和要求完成程序

//7.9根据已有代码和要求完成程序
#include <iostream>
#include <cctype>
#include <cstring>
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 actual number of array elements
// filled.
int getinfo(student pa[], int n)
{
int num_array_elem = n;
char tmp[SLEN];
for (int i = 0; i < n; ++i) {
cout << "输入姓名:";
cin.getline(tmp, SLEN);
bool blank_line = true;
for (unsigned j = 0; j < strlen(tmp); ++j) {
if (!isspace(tmp[j])) {
blank_line = false;
break;
}
}
if (blank_line) {
num_array_elem = i;
break;
}
strcpy(pa[i].fullname, tmp);
cout << "输入兴趣:";
cin.getline(pa[i].hobby, SLEN);
cout << "输入面向对象程序设计能力的级别:";
cin >> pa[i].ooplevel;
cin.get();
}
return (num_array_elem);
}
// display1() takes a student structure as an argument
// and displays its contents
void display1(student st)
{
cout << st.fullname << '\t' << st.hobby << '\t' << st.ooplevel << endl;
}
// display2() takes the address of student structure as an
// argument and displays the structure’s contents
void display2(const student * ps)
{
cout << ps->fullname << '\t' << ps->hobby << '\t' << ps->ooplevel << endl;
}
// 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)
{
for (int i = 0; i < n; ++i) {
cout << pa[i].fullname << '\t' << pa[i].hobby << '\t' << pa[i].ooplevel << endl;
}
}
int main()
{
cout << "输入班级人数:";
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 << "完毕\n";
return 0;
}

7.10使用指向函数的指针,并定义一个数组储存2个函数地址,对输入的2个数进行+-运算

//7.10使用指向函数的指针,并定义一个数组储存2个函数地址,对输入的2个数进行+-运算
#include <iostream>
using namespace std;
typedef double (*TPfun) (double x, double y);

void calculate (double x, double y, TPfun fun[], unsigned num_of_funs)
{
for (unsigned i = 0; i < num_of_funs; ++i)
cout << fun[i](x, y) << endl;
}

double add (double x, double y)
{
cout << "加法操作结果:";
return (x + y);
}

double sub (double x, double y)
{
cout << "减法操作结果:";
return (x - y);
}

int main ()
{
TPfun fun[] = {add, sub};
while (true) {
cout << "输入两个数:";
double x, y;
cin >> x >> y;
if (!cin)
break;
calculate(x, y, fun, sizeof(fun)/sizeof(fun[0]));
}
}