C++程序设计(第3版)谭浩强 第6章 习题

1.输入3个整数,按由小到大的顺序输出。

【解】

#include 
using namespace std;
int main()
{
	void swap(int *p1, int *p2);
	int n1, n2, n3;
	int *p1, *p2, *p3;
	cout << "input three integers n1,n2,n3:";
	cin >> n1 >> n2 >> n3;
	p1 = &n1;
	p2 = &n2;
	p3 = &n3;
	if (n1 > n2) swap(p1, p2);
	if (n1 > n3) swap(p1, p3);
	if (n2 > n3) swap(p2, p3);
	cout << "Now,the order is:" << n1 << " " << n2 << " " << n3 << endl;
	return 0;
}
void swap(int *p1, int *p2)
{
	int p;
	p = *p1; *p1 = *p2; *p2 = p;
}

运行结果:

//input three integers n1, n2, n3:54 –12 2↙
//Now, the order is : –12 2 54

2.输入3个字符串,按由小到大的顺序输出。

【解】

//(1)用字符数组方法
#include 
#include 
using namespace std;
int main()
{
	void swap(char *, char *);
	char str1[20], str2[20], str3[20];
	cout << "input three line:" << endl;
	gets(str1);
	gets(str2);
	gets(str3);
	if (strcmp(str1, str2) > 0)  swap(str1, str2);
	if (strcmp(str1, str3) > 0)  swap(str1, str3);
	if (strcmp(str2, str3) > 0)  swap(str2, str3);
	cout << endl << "Now,the order is:" << endl;
	cout << str1 << endl << str2 << endl << str3 << endl;
	return 0;
}
void swap(char *p1, char *p2)          //交换两个字符串
{
	char p[20];
	strcpy(p, p1); strcpy(p1, p2); strcpy(p2, p);
}

//(2)用string方法,程序中使用了指针和引用
#include 
#include 
using namespace std;
int main()
{
	void change(string &, string &);
	string str1 = "                                         ",
		str2 = "                                           ",
		str3 = "                                           ";
	char *p1 = &str1[0], *p2 = &str2[0], *p3 = &str3[0];
	cout << "input three line:" << endl;
	gets(p1);
	gets(p2);
	gets(p3);
	if (str1 > str2)change(str1, str2);
	if (str1 > str3)change(str1, str3);
	if (str2 > str3)change(str2, str3);
	cout << endl << "Now,the order is:" << endl;
	cout << str1 << endl << str2 << endl << str3 << endl;
	return 0;
}
void change(string &st1, string &st2)          //交换两个字符串。形参为引用变量
{
	string st;
	st = st1; st1 = st2; st2 = st;
}

运行结果:

//input three line :
//I study very hard.↙
//C language is very interesting.↙
//He is a professor.↙ 
//
//Now, the order is :
//C language is very interesting.
//He is a professor.
//I study very hard.

3.输入10个整数,将其中最小的数与第1个数对换,把最大的数与最后一个数对换。写3个函数: ①输入10个数; ②进行处理; ③输出10个数。

【解】

#include 
using namespace std;
int main()
{
	void input(int *number);
	void max_min_value(int *number);
	void output(int *number);
	int number[10];
	input(number);						//调用输入10个数的函数 
	max_min_value(number);   			//调用交换函数 
	output(number);                      	//调用输出函数 
	return 0;
}
void input(int *number)          		//输入10个数的函数, 形参number为指针变量 
{
	int i;
	cout << "input 10 numbers:";
	for (i = 0; i < 10; i++)
		cin >> number[i];
}
void max_min_value(int *number)      	//交换函数 
{
	int *max, *min, *p, temp;
	max = min = number;							//使max和min都指向number[0]
	for (p = number + 1; p < number + 10; p++)
		if (*p > *max) max = p;            			//将大数地址赋给 max  
		else if (*p < *min) min = p;      				//将小数地址赋给 min  
	temp = number[0]; number[0] = *min; *min = temp;		//将最小数与第一个数交换 
	if (max == number)   max = min; //如果第一个数刚好是最大数, 则使max指向该最大数
	temp = number[9]; number[9] = *max; *max = temp;		//将最大数与最后一个数交换 
}
void output(int *number)              			//输出函数 
{
	int *p;
	cout << "now,they are:    ";
	for (p = number; p < number + 10; p++)
		cout << *p << " ";
	cout << endl;
	return;
}

运行结果:

//input 10 numbers:84 72 45 67 81 19 96 64 42 83↙
//Now, they are : 19 72 45 67 81 84 83 64 42 96

4.有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见图6.1。写一函数实现以上功能,在主函数中输入n个整数,并输出调整后的n个数。

【解】

#include 
using namespace std;
int main()
{
	void move(int *, int, int);
	int number[20], n, m, i;
	cout << "how many numbers?";		//询问共有多少个数 
	cin >> n;
	cout << "input " << n << " numbers:" << endl;    	//要求输入n个数 
	for (i = 0; i < n; i++)
		cin >> number[i];
	cout << "how many places do you want move?";		//询问后移多少个位置 
	cin >> m;
	move(number, n, m);        		//调用move 函数 
	cout << "Now,they are:" << endl;
	for (i = 0; i < n; i++)
		cout << number[i] << " ";
	cout << endl;
	return 0;
}
void move(int *array, int n, int m)    	//使循环后移一次的函数
{
	int *p, array_end;
	array_end = *(array + n - 1);
	for (p = array + n - 1; p > array; p--)
		*p = *(p - 1);
	*array = array_end;
	m--;
	if (m > 0) move(array, n, m);	//递归调用, 当循环次数m减至为0时, 停止调用 
}

运行结果:

//how many numbers ? 9↙
//input 8 numbers :
//12 43 –34 65 67 8 2 7 11↙
//how many places do you want move ? 3↙
//Now, they are :
//2 7 11 12 43 –34 65 67 8

5.有n个人围成一圈,顺序排号。从第1个人开始报数(从1~3报数),凡报到3的人退出圈子,问最后留下的人原来排在第几号。

【解】

#include 
using namespace std;
int main()
{
	int i, k, m, n, num[50], *p;
	cout << "input number of person: n=";
	cin >> n;
	p = num;
	for (i = 0; i < n; i++)
		*(p + i) = i + 1;         	//以1至n为序给每个人编号 
	i = 0;                  	//i为每次循环时计数变量 
	k = 0;                  	//k为按1,2,3报数时的计数变量 
	m = 0;                	//m为退出人数 
	while (m < n - 1)       	//当退出人数比n–1少时(即未退出人数大于1时)执行循环体
	{
		if (*(p + i) != 0)  k++;
		if (k == 3)           	//将退出的人的编号置为0 
		{
			*(p + i) = 0;
			k = 0;
			m++;
		}
		i++;
		if (i == n) i = 0;		//报数到尾后, i恢复为0 
	}
	while (*p == 0) p++;
	cout << "The last one is NO." << *p << endl;
	return 0;
}

运行结果:

//input number of person : n = 8↙
//The last one is NO.7    (最后留在圈子内的是7号)

6.写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。

【解】

#include 
using namespace std;
int main()
{
	int length(char *p);
	int len;
	char str[20];
	cout << "input string:";
	cin >> str;
	len = length(str);
	cout << "The length of string is " << len << "." << endl;
	return 0;
}
int length(char *p)         		//求字符串长度的函数
{
	int n;
	n = 0;
	while (*p != '\0')
	{
		n++;
		p++;
	}
	return(n);
}

运行结果:

//input string : Beijing↙
//The length of string is 7.

7.有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。

【解】

#include 
using namespace std;
int main()
{
	void copystr(char *, char *, int);
	int m;
	char str1[20], str2[20];
	cout << "input string:";
	gets(str1);
	cout << "which character do you want begin to copy?";
	cin >> m;
	if (strlen(str1) < m)
		cout << "input error!" << endl;
	else
	{
		copystr(str1, str2, m);
		cout << "result:" << str2 << endl;
	}
	return 0;
}
void copystr(char *p1, char *p2, int m)			//字符串部分复制函数
{
	int n;
	n = 0;
	while (n < m - 1)
	{
		n++;
		p1++;
	}
	while (*p1 != '\0')
	{
		*p2 = *p1;
		p1++;
		p2++;
	}
	*p2 = '\0';
}

运行结果:

//input string : countryside↙
//which character do you want begin to copy ? 8↙
//result : side

8.输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少。

【解】

#include 
using namespace std;
int main()
{
	int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
	char *p, s[20];
	cout << "input string:";
	while ((s[i] = getchar()) != '\n') i++;
	p = &s[0];
	while (*p != '\n')
	{
		if (('A' <= *p) && (*p <= 'Z'))
			++upper;
		else if (('a' <= *p) && (*p <= 'z'))
			++lower;
		else if (*p == ' ')
			++space;
		else if ((*p <= '9') && (*p >= '0'))
			++digit;
		else
			++other;
		p++;
	}
	cout << "upper case:" << upper << endl << "lower case:" << lower << endl;
	cout << "space:" << space << endl << "digit:" << digit << endl << "other:" << other << endl;
	return 0;
}

运行结果:

//input string : Today is 2012 / 1 / 1↙
//upper case:1
//lower case:6
//space : 2
//digit : 6
//other : 2

9.写一函数,将一个3×3的整型矩阵转置。

【解】

#include 
using namespace std;
int main()
{
	void move(int *);
	int a[3][3], *p, i;
	cout << "input matrix:" << endl;
	for (i = 0; i < 3; i++)
		cin >> a[i][0] >> a[i][1] >> a[i][2];      //输入一行的3个元素
	p = &a[0][0];
	move(p);
	cout << "Now,matrix:" << endl;
	for (i = 0; i < 3; i++)
		cout << a[i][0] << " " << a[i][1] << " " << a[i][2] << endl;
	cout << endl;
	return 0;
}
void move(int *pointer)
{
	int i, j, t;
	for (i = 0; i < 3; i++)
		for (j = i; j < 3; j++)
		{
			t = *(pointer + 3 * i + j);
			*(pointer + 3 * i + j) = *(pointer + 3 * j + i);
			*(pointer + 3 * j + i) = t;
		}
}

运行结果:

//input matrix :
//1 2 3↙
//4 5 6↙
//7 8 9↙
//Now, matrix :
//1 4 7
//2 5 8
//3 6 9

10.将一个5×5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(按从左到右、从上到下顺序依次从小到大存放),写一函数实现。用main函数调用。

【解】

#include 
using namespace std;
int main()
{
	void change(int *p);
	int a[5][5], *p, i, j;
	cout << "input matrix:" << endl;
	for (i = 0; i < 5; i++)                     	//输入矩阵
		for (j = 0; j < 5; j++)
			cin >> a[i][j];
	p = &a[0][0];                      	//使p指向0行0列元素 
	change(p);                     	//调用函数, 实现交换
	cout << "Now,matrix:" << endl;
	for (i = 0; i < 5; i++)                     	//输出已交换的矩阵
	{
		for (j = 0; j < 5; j++)
			cout << a[i][j] << " ";
		cout << endl;
	}
	return 0;
}
void change(int *p)             	//交换函数
{
	int i, j, temp;
	int *pmax, *pmin;
	pmax = p;
	pmin = p;
	for (i = 0; i < 5; i++) 	//找最大值和最小值的地址, 并赋给pmax,pmin
		for (j = i; j < 5; j++)
		{
			if (*pmax < *(p + 5 * i + j)) pmax = p + 5 * i + j;
			if (*pmin > *(p + 5 * i + j)) pmin = p + 5 * i + j;
		}
	temp = *(p + 12);    	//将最大值与中心元素互换
	*(p + 12) = *pmax;
	*pmax = temp;
	temp = *p;   	 //将最小值与左上角元素互换
	*p = *pmin;
	*pmin = temp;
	pmin = p + 1;
	//将a[0][1]的地址赋给pmin, 从该位置开始找最小的元素
	for (i = 0; i < 5; i++) 	//找第二最小值的地址赋给 pmin 
		for (j = 0; j < 5; j++)
			if (((p + 5 * i + j) != p) && (*pmin > *(p + 5 * i + j))) pmin = p + 5 * i + j;
	temp = *pmin;     	//将第二最小值与右上角元素互换 
	*pmin = *(p + 4);
	*(p + 4) = temp;
	pmin = p + 1;
	for (i = 0; i < 5; i++) 	//找第三最小值的地址赋给pmin 
		for (j = 0; j < 5; j++)
			if (((p + 5 * i + j) != (p + 4)) && ((p + 5 * i + j) != p) && (*pmin > *(p + 5 * i + j))) pmin = p + 5 * i + j;
	temp = *pmin;   	//将第三最小值与左下角元素互换
	*pmin = *(p + 20);
	*(p + 20) = temp;

	pmin = p + 1;
	for (i = 0; i < 5; i++)    	//找第四最小值的地址赋给pmin 
		for (j = 0; j < 5; j++)
			if (((p + 5 * i + j) != p) && ((p + 5 * i + j) != (p + 4)) && ((p + 5 * i + j) != (p + 20)) && *pmin > *(p + 5 * i + j))
			pmin = p + 5 * i + j;
	temp = *pmin;    	//将第四最小值与右下角元素互换
	*pmin = *(p + 24);
	*(p + 24) = temp;
}

运行结果:

//input matrix :
//35 34 33 32 31↙
//30 29 28 27 26↙
//25 24 23 22 21↙
//20 19 18 17 16↙
//15 14 13 12 11↙
//Now, matrix:
//11 34 33 32 12
//30 29 28 27 26
//25 24 35 22 21
//20 19 18 17 16
//13 23 15 31 14

11.在主函数中输入10个等长的字符串。用另一函数对它们排序。然后在主函数输出这10个已排好序的字符串。

【解】

#include 
using namespace std;
int main()
{
	void sort(char s[][6]);
	int i;
	char str[10][6];
	cout << "input 10 strings:" << endl;
	for (i = 0; i < 10; i++)
		cin >> str[i];
	sort(str);
	cout << "Now,the sequence is:" << endl;
	for (i = 0; i < 10; i++)
		cout << str[i] << endl;
	return 0;
}
void sort(char s[][6])
{
	int i, j;
	char *p, temp[10];
	p = temp;
	for (i = 0; i < 9; i++)
		for (j = 0; j < 9 - i; j++)
			if (strcmp(s[j], s[j + 1]) > 0)
			{
				strcpy(p, s[j]);
				strcpy(s[j], s[+j + 1]);
				strcpy(s[j + 1], p);
			}
}

//(2)用指向一维数组的指针作函数参数
#include 
using namespace std;
int main()
{
	void sort(char(*p)[6]);
	int i;
	char str[10][6];
	char(*p)[6];             	// p是指向由6个元素组成的一维数组的指针
	cout << "input 10 strings:" << endl;
	for (i = 0; i < 10; i++)
		cin >> str[i];
	p = str;                   	//p指向二维数组str的0行元素
	sort(p);
	cout << "Now,the sequence is:" << endl;
	for (i = 0; i < 10; i++)
		cout << str[i] << endl;
	return 0;
}
void sort(char(*s)[6])  		//形参q是指向由6个元素组成的一维数组的指针
{
	int i, j;
	char temp[6], *t = temp;
	for (i = 0; i < 9; i++)
		for (j = 0; j < 9 - i; j++)
			if (strcmp(s[j], s[j + 1]) > 0)
			{
				strcpy(t, s[j]);  		//以下3行的作用是将s[j]指向的一维数组的内容
				strcpy(s[j], s[+j + 1]);     	//与s[j+1]指向的一维数组的内容互换
				strcpy(s[j + 1], t);
			}
}

//(3)用string数组
#include 
#include 
using namespace std;
int main()
{
	void sort(string *);
	int i;
	string str[10], *p = str;   		//str为string型数组, p为指向string型变量的指针
	cout << "input 10 strings:" << endl;
	for (i = 0; i < 10; i++)
		cin >> str[i];
	sort(p);
	cout << "Now,the sequence is:" << endl;
	for (i = 0; i < 10; i++)
		cout << str[i] << endl;
	return 0;
}

void sort(string *s)         	//形参为指向string型变量的指针
{
	int i, j;
	string temp;
	for (i = 0; i < 9; i++)
		for (j = 0; j < 9 - i; j++)
			if (s[j] > s[j + 1])
			{
				temp = s[j];
				s[j] = s[+j + 1];
				s[j + 1] = temp;
			}
}

运行结果:

//input 10 strings:
//China↙
//Japan↙
//Korea↙
//Egypt↙
//Nepal↙
//Burma↙
//Ghana↙
//Sudan↙
//Italy↙
//Libya↙
//Now, the sequence is :
//Burma
//China
//Egypt
//Ghana
//Italy
//Japan
//Korea
//Libya
//Nepal
//Sudan

12.用指针数组处理第11题,字符串不等长。

【解】

#include 
using namespace std;
int main()
{
	void sort(char *[]);
	int i;
	char *p[10], str[10][20]; 		//p为指针数组, 有10个元素
	for (i = 0; i < 10; i++)
		p[i] = str[i];    			//将第i个字符串的首地址赋予指针数组p的第i个元素
	cout << "input 10 strings:" << endl;
	for (i = 0; i < 10; i++)
		cin >> p[i];
	sort(p);
	cout << "Now,the sequence is:" << endl;
	for (i = 0; i < 10; i++)
		cout << p[i] << endl;
	return 0;
}
void sort(char *s[])
{
	int i, j;
	char *temp;
	for (i = 0; i < 9; i++)
		for (j = 0; j < 9 - i; j++)
			if (strcmp(*(s + j), *(s + j + 1)) > 0)
			{
				temp = *(s + j);
				*(s + j) = *(s + j + 1);
				*(s + j + 1) = temp;
			}
}

13.写一个用矩形法求定积分的通用函数,分别求 ∫10sinxdx,∫1-1cosxdx,∫20exdx(说明: sin,cos,exp已在系统的数学函数库中,程序开头要用#include)。

【解】

#include 
#include 
using namespace std;
int main()
{
	float integral(float(*p)(float), float a, float b, int n);		//对integral函数作声明
	float fsin(float);                                  	//对fsin函数作声明 
	float fcos(float);                                 	//对fcos函数作声明 
	float fexp(float);                                 	//对fexp函数作声明 
	float a1, b1, a2, b2, a3, b3, c, (*p)(float);
	int n = 20;
	cout << "input a1,b1:";     						//输入求sin(x)定积分的下限和上限 
	cin >> a1 >> b1;
	cout << "input a2,b2:";     						//输入求cos(x)定积分的下限和上限 
	cin >> a2 >> b2;
	cout << "input a3,b3:";    						//输入求ex定积分的下限和上限 
	cin >> a3 >> b3;
	p = fsin;
	c = integral(p, a1, b1, n);   						//求出sin(x)的定积分 
	cout << "The integral of sin(x) is :" << c << endl;
	p = fcos;
	c = integral(p, a2, b2, n);      						//求出cos(x)的定积分 
	cout << "The integral of cos(x) is :" << c << endl;;
	p = fexp;
	c = integral(p, a3, b3, n);     						//求出ex的定积分 
	cout << "The integral of exp(x) is :" << c << endl;
	return 0;
}
float integral(float(*p)(float), float a, float b, int n)  		//用矩形法求定积分的通用函数
{
	int i;
	float x, h, s;
	h = (b - a) / n;
	x = a;
	s = 0;
	for (i = 1; i <= n; i++)
	{
		x = x + h;
		s = s + (*p)(x)*h;
	}
	return(s);
}
float fsin(float x)          						//计算sin(x)的函数 
{
	return sin(x);
}

float fcos(float x)        							//计算cos(x)的函数 
{
	return cos(x);
}

float fexp(float x)       							//计算ex的函数 
{
	return exp(x);
}

运行结果:

//input a1, b1:0 1↙
//input a2, b2 : –1 1↙
//input a3, b3 : 0 2↙
//The integral of sin(x) is : 0.480639
//The integral of cos(x) is : 1.68154
//The integral of exp(x) is : 6.71383

14.将n个数按输入时顺序的逆序排列,用函数实现。

【解】

#include 
using namespace std;
int main()
{
	void sort(char *p, int m);
	int i, n;
	char *p, num[20];
	cout << "input n:";
	cin >> n;
	cout << "please input these numbers:" << endl;
	for (i = 0; i < n; i++)
		cin >> num[i];
	p = &num[0];
	sort(p, n);
	cout << "Now,the sequence is:" << endl;
	for (i = 0; i < n; i++)
		cout << num[i] << " ";
	cout << endl;
	return 0;
}
void sort(char *p, int m)    			//将n个数逆序排列函数 
{
	int i;
	char temp, *p1, *p2;
	for (i = 0; i < m / 2; i++)
	{
		p1 = p + i;
		p2 = p + (m - 1 - i);
		temp = *p1;
		*p1 = *p2;
		*p2 = temp;
	}
}

运行结果:

//input n : 10↙
//please input these numbers : 
//10 9 8 7 6 5 4 3 2 1↙
//Now, the sequence is :
//1 2 3 4 5 6 7 8 9 10

15.有一个班4个学生,5门课。①求第1门课的平均分;②找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩和平均成绩;③找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。

【解】

#include 
using namespace std;
int main()
{
	void avsco(float *, float *);
	void avcour1(char(*)[10], float *);
	void fail2(char course[5][10], int num[], float *psco, float aver[4]);
	void good(char course[5][10], int num[4], float *psco, float aver[4]);
	int i, j, *pnum, num[4];
	float score[4][5], aver[4], *pscore, *paver;
	char course[5][10], (*pcourse)[10];
	cout << "input course:" << endl;
	pcourse = course;
	for (i = 0; i < 5; i++)
		cin >> course[i];
	cout << "input NO. and scores:" << endl;
	cout << "NO.";
	for (i = 0; i < 5; i++)
		cout << "," << course[i];
	cout << endl;
	pscore = &score[0][0];
	pnum = &num[0];
	for (i = 0; i < 4; i++)
	{
		cin >> *(pnum + i);
		for (j = 0; j < 5; j++)
			cin >> *(pscore + 5 * i + j);
	}
	paver = &aver[0];
	cout << endl << endl;
	avsco(pscore, paver);           			//求出每个学生的平均成绩 
	avcour1(pcourse, pscore);      				//求出第一门课的平均成绩 
	cout << endl << endl;
	fail2(pcourse, pnum, pscore, paver); 			//找出两门课不及格的学生 
	cout << endl << endl;
	good(pcourse, pnum, pscore, paver); 			//找出成绩好的学生 
	return 0;
}
void avsco(float *pscore, float *paver) 			//求每个学生的平均成绩的函数
{
	int i, j;
	float sum, average;
	for (i = 0; i < 4; i++)
	{
		sum = 0.0;
		for (j = 0; j < 5; j++)
			sum = sum + (*(pscore + 5 * i + j));   			//累计每个学生的各科成绩
		average = sum / 5;                			//计算平均成绩
		*(paver + i) = average;
	}
}
void avcour1(char(*pcourse)[10], float *pscore) 	//求第一课程的平均成绩的函数 
{
	int i;
	float sum, average1;
	sum = 0.0;
	for (i = 0; i < 4; i++)
		sum = sum + (*(pscore + 5 * i));          		//累计每个学生的得分
	average1 = sum / 4;                      	//计算平均成绩
	cout << "course 1: " << *pcourse << ",average score:" << average1 << endl;
}
void fail2(char course[5][10], int num[], float *pscore, float aver[4])
//找两门以上课程不及格的学生的函数 
{
	int i, j, k, labe1;
	cout << "   ==========Student who failed in two courses =======  " << endl;
	cout << "NO.   ";
	for (i = 0; i < 5; i++)
		cout << course[i] << "  ";
	cout << "  average" << endl;
	for (i = 0; i < 4; i++)
	{
		labe1 = 0;
		for (j = 0; j < 5; j++)
			if (*(pscore + 5 * i + j) < 60.0) labe1++;
		if (labe1 >= 2)
		{
			cout << num[i] << "      ";
			for (k = 0; k < 5; k++)
				cout << *(pscore + 5 * i + k) << "      ";
			cout << "    " << aver[i] << endl;
		}
	}
}
void good(char course[5][10], int num[4], float *pscore, float aver[4])
//找成绩优秀的学生(全部课程成绩在85分以上或平均成绩在90分以上)的函数 
{
	int i, j, k, n;
	cout << "    ======Students whose score is good======" << endl;
	cout << "NO.   ";
	for (i = 0; i < 5; i++)
		cout << course[i] << "  ";
	cout << "  average" << endl;
	for (i = 0; i < 4; i++)
	{
		n = 0;
		for (j = 0; j < 5; j++)
			if (*(pscore + 5 * i + j) > 85.0) n++;
		if ((n == 5) || (aver[i] >= 90))
		{
			cout << num[i] << "      ";
			for (k = 0; k < 5; k++)
				cout << *(pscore + 5 * i + k) << "      ";
			cout << "    " << aver[i] << endl;
		}
	}
}

运行结果:

//input course : (提示输入课程名称)
//English↙
//Computer↙
//Math↙
//Physics↙
//Chemistry↙
//input NO. and scores:             (提示输入学号和各门课成绩)
//NO., English, Computer, Math, Physics, Chemistry, average  (提示按此顺序输入)
//101  34  56  88  99  89↙
//102  77  88  99  67  78↙
//103  99  90  87  86  89↙
//104  78  89  99  56  77↙
//
//course 1: English, average score : 72     (第一门课英语的平均成绩为72)
//
//==========Students who failed in two courses====== = (有两门课不及格者)
//NO.English    Computer    Math    Physics   Chemistry   average
//101      34         56        88       99         89       73.2
//
//======Student whose score is good======       (成绩优良者)
//NO.English    Computer    Math    Physics   Chemistry   average
//103      99         90        87       86         89        90

16.输入一个字符串,内有数字和非数字字符,如将其中连续的数字作为一个整数,依次存放到一数组a中。例如,123放在a[0],456放在a[1]……统计共有多少个整数,并输出这些数。

【解】

#include 
using namespace std;
int main()
{
	char str[50], *pstr;
	int i, j, k, m, e10, digit, ndigit, a[10], *pa;
	cout << "input a string:" << endl;
	gets(str);
	cout << endl;
	pstr = &str[0];  	//字符指针pstr指向数组str首元素 
	pa = &a[0];        	//指针pa指向a数组首元素
	ndigit = 0;        	//ndigit代表有多少个整数 
	i = 0;            	//i代表字符串中的第几个字符
	j = 0;             	//j代表连续数字的位数 
	while (*(pstr + i) != '\0')
	{
		if ((*(pstr + i) >= '0') && (*(pstr + i) <= '9'))
			j++;
		else
		{
			if (j > 0)
			{
				digit = *(pstr + i - 1) - 48;    	//将个数位赋予digit
				k = 1;
				while (k < j) 	//将含有两位以上数的其他位的数值累计于digit
				{
					e10 = 1;
					for (m = 1; m <= k; m++)
						e10 = e10 * 10;        	//e10代表该位数所应乘的因子
					digit = digit + (*(pstr + i - 1 - k) - 48)*e10; 	//将该位数累加于digit
					k++;                      	//位数k自增
				}
				*pa = digit;                 	//将数值放在数组a中
				ndigit++;
				pa++;                    	//指针pa指向a数组下一元素
				j = 0;
			}
		}
		i++;
	}
	if (j > 0)                        	//以数字结尾字符串的最后一个数据
	{
		digit = *(pstr + i - 1) - 48;        	//将个数位赋予digit
		k = 1;
		while (k < j)     	//将含有两位以上数的其他位的数值累加于digit
		{
			e10 = 1;
			for (m = 1; m <= k; m++)
				e10 = e10 * 10;        	//e10代表位数所应乘的因子
			digit = digit + (*(pstr + i - 1 - k) - 48)*e10; 	//将该位数的数值累加于digit
			k++;  	//位数k自增
		}
		*pa = digit;               	//将数值放到数组a中
		ndigit++;
		j = 0;
	}
	printf("There are %d numbers in this line. They are:\n", ndigit);
	j = 0;
	pa = &a[0];
	for (j = 0; j < ndigit; j++)       	//打印数据
		cout << *(pa + j) << endl;
	cout << endl;
	return 0;
}

运行结果:

//input a string :
//a123x456 7689 + 89 = 321 / ab23↙
//There are 6 numbers in this line.They are :
//123 456 7689 89 321 23

17.写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为int strcmp(char [^p1,char [^p2);设p1指向字符串s1,p2指向字符串s2。要求当s1=s2时,返回值为0,若s1≠s2,返回它们二者第1个不同字符的ASCII码差值(如"BOY"与"BAD",第2个字母不同,"O"与"A"之差为79-65=14)。如果s1>s2,则输出正值,如s1

【解】

#include 
using namespace std;
int main()
{
	int strcmp(char *p1, char *p2);
	int m;
	char str1[20], str2[20], *p1, *p2;
	cout << "input two strings:" << endl;
	cin >> str1;
	cin >> str2;
	p1 = &str1[0];
	p2 = &str2[0];
	m = strcmp(p1, p2);
	cout << "result:" << m << endl;
	return 0;
}
int strcmp(char *p1, char *p2)	//自己定义字符串比较函数 
{
	int i;
	i = 0;
	while (*(p1 + i) == *(p2 + i))
		if (*(p1 + i++) == '\0') return(0);	//全部字符相同时返回结果0 
	return(*(p1 + i) - (*(p2 + i)));	//不相同时返回结果为第一对不相同字符的ASCII码的差值 
}

运行结果:

//① input two strings :
//CHINA↙
//Chen↙
//result :  - 32
//② input two strings :
//hello!↙
//hello!↙
//result : 0
//③ input two strings :
//dog↙
//cat↙
//result : 1

18.编写一程序,输入月份号,输出该月的英文月名。例如,输入3,则输出March,要求用指针数组处理。

【解】

#include 
using namespace std;
int main()
{
	char *month_name[13] = { "illegal month","January","February","March","April","May","June","July",
	  "August","September","October", "November","December" };
	int n;
	cout << "input month:" << endl;
	cin >> n;
	if ((n <= 12) && (n >= 1))
		cout << "It is " << *(month_name + n) << endl;
	else
		cout << "It is wrong" << endl;
	return 0;
}

运行结果:

//① input month : 2↙
//It is February.
//② input month : 8↙
//It is August.
//③ input month : 13↙
//It is wrong.

19.用指向指针的指针的方法对5个字符串排序并输出。

【解】

#include 
using namespace std;
int main()
{
	void sort(char **p);
	const int m = 20;   				//定义字符串的最大长度
	int i;
	char **p, *pstr[5], str[5][m];
	for (i = 0; i < 5; i++)
		pstr[i] = str[i];   //将第i个字符串的首地址赋予指针数组 pstr 的第i个元素
	cout << "input 5 strings:" << endl;
	for (i = 0; i < 5; i++)
		cin >> pstr[i];
	p = pstr;
	sort(p);
	cout << "strings sorted:" << endl;
	for (i = 0; i < 5; i++)
		cout << pstr[i] << endl;
	return 0;
}
void sort(char **p)					//冒泡法对5个字符串排序函数
{
	int i, j;
	char *temp;
	for (i = 0; i < 5; i++)
	{
		for (j = i + 1; j < 5; j++)
		{
			if (strcmp(*(p + i), *(p + j)) > 0) 		//比较后交换字符串地址
			{
				temp = *(p + i);
				*(p + i) = *(p + j);
				*(p + j) = temp;
			}
		}
	}
}

运行结果:

//input 5 strings:
//China↙
//America↙
//India↙
//Philippines↙
//Canada↙
//strings sorted :
//America
//Canada
//China
//India
//Philippines

20.用指向指针的指针的方法对n个整数排序并输出。要求将排序单独写成一个函数。整数和n在主函数中输入。最后在主函数中输出。

【解】

#include 
using namespace std;
int main()
{
	void sort(int **p, int n);
	int i, n, data[10], **p, *pstr[10];
	cout << "input n:";
	cin >> n;
	for (i = 0; i < n; i++)
		pstr[i] = &data[i];   //将第i个整数的地址赋予指针数组 pstr 的第i个元素
	cout << "input " << n << " integer numbers:" << endl;
	for (i = 0; i < n; i++)
		cin >> *pstr[i];
	p = pstr;
	sort(p, n);
	cout << "Now,the sequence is:" << endl;
	for (i = 0; i < n; i++)
		cout << *pstr[i] << "  ";
	cout << endl;
	return 0;
}
void sort(int **p, int n)
{
	int i, j, *temp;
	for (i = 0; i < n - 1; i++)
	{
		for (j = i + 1; j < n; j++)
		{
			if (**(p + i) > **(p + j)) 				//比较后交换整数地址
			{
				temp = *(p + i);
				*(p + i) = *(p + j);
				*(p + j) = temp;
			}
		}
	}
}

运行结果:

//input n : 7↙
//input 7 integer numbers :
//34 98 56 12 22 65 1↙
//Now, the sequence is :
//1 12 22 34 56 65 98

你可能感兴趣的:(C++程序设计(第3版)谭浩强,课后习题答案,c++)