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

第七章编程练习答案

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

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

时间: 2024-08-24 13:50:10

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

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

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

C Primer Plus (第五版) 第七章 编程练习

第七章 C控制语句:分支和跳转 编程练习: 1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include <stdio.h> int main(void) { int a=0, b=0, c=0; char ch; while ((ch = getchar()) != '#') { if (ch != ' ' && ch != '\n') c++; else if (ch != ' ') b++; els

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

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

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

第十章编程练习答案 10.1为复习题5类提供定义,并演示 //10.1为复习题5类提供定义,并演示 #include <iostream> using namespace std; class BankAccount { string m_name; string m_num; unsigned m_balance; public: BankAccount (string name, string num, unsigned balance) { m_name = name; m_num =

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

第八章编程练习答案 8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动) //8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动) #include <iostream> using namespace std; void show (const char* str, int time=1) { unsigned n=(time>0)?time:-time; for (unsigned i

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

第十二章编程练习答案 12.1根据以下类声明,完成类,并编小程序使用它 //12.1根据以下类声明,完成类,并编小程序使用它 #include <iostream> #include <cstring> using namespace std; class Cow{ char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm, const char * ho, double wt);

C语言程序设计:现代方法(第2版)第三章全部习题答案

前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解答,并参考GitHub上相关的分享和Chegg.com相关资料.因为并没有权威的答案来源,所以可能会存在错误的地方,如有错误还希望大家能够帮助指出. 第三章练习题和编程题答案 练习题 3.1节 1.下面的printf函数调用产生的输出分别是什么? (a)  printf("6d,%4d",

《C Primer Plus(第6版)(中文版)》普拉达(作者)epub+mobi+azw3

内容简介 <C Primer Plus(第6版)中文版>详细讲解了C语言的基本概念和编程技巧. <C Primer Plus(第6版)中文版>共17章.第1.2章介绍了C语言编程的预备知识.第3~15章详细讲解了C语言的相关知识,包括数据类型.格式化输入/输出.运算符.表达式.语句.循环.字符输入和输出.函数.数组和指针.字符和字符串函数.内存管理.文件输入输出.结构.位操作等.第16章.17章介绍C预处理器.C库和高级数据表示.本书以完整的程序为例,讲解C语言的知识要点和注意事项

C Primer Plus(第五版)中文版.pdf

下载地址:网盘下载 内容简介 编辑 本书全面讲述了C语言编程的相关概念和知识. 全书共17章.第1.2章学习C语言编程所需的预备知识.第3到15章介绍了C语言的相关知识,包括数据类型.格式化输入输出.运算符.表达式.流程控制语句.函数.数组和指针.字符串操作.内存管理.位操作等等,知识内容都针对C99标准:另外,第10章强化了对指针的讨论,第12章引入了动态内存分配的概念,这些内容更加适合读者的需求.第16章和第17章讨论了C预处理器和C库函数.高级数据表示(数据结构)方面的内容.附录给出了各章