C++ Primer Plus 第九章答案 内存模型和名称空间

9.5复习题

//1
homer将自动成为自动变量
在一个文件中将secret定义为外部变量,并在第二个文件中使用extern声明它
在外部定义前加上关键字static,将topsecret定义为一个有内部链接的静态变量,也可以在一个未命名的名称空间中定义
在函数内beencalled的声明前加关键字static,将beencalled定义为一个无链接性的静态变量

//2
using声明使特定的标识符可用,using编译指令使整个名称空间可用

//3
#include<iostream>
int main()
{
	double x;
	std::cout << "Enter value: ";
	while (!(std::cin >> x))
	{
		std::cout << "Bad intput.Please enter anumber: ";
		std::cin.clear();
		while (std::cin.get() != '\n')
			continue;
	}
	std::cout >> "Value = " << x << std::endl;
	return 0;
}


//4
#include<iostream>

int main()
{
	using std::cin;                                            //此处using声明放在main函数里面
	using std::cout;
	using std::endl;
	double x;
	cout << "Enter value: ";
	while (!(cin >> x))
	{
		cout << "Bad intput.Please enter anumber: ";
		cin.clear();
		while (cin.get() != '\n')
			continue;
	}
	cout >> "Value = " << x << endl;
	return 0;
}


//5
分别在两个文件中使用static定义同样的函数,因此这个函数只能在此文件中使用
可以在每个文件中包含单独的静态函数定义,或者每个文件都在未命名的名称空间中定义一个合适的average函数

//6
依次输出:
10
4
0
Other: 10,1
another(): 10,-4

//7
1
4, 1, 2
2
2
4, 1, 2
2

9.6编程练习(编译运行均已通过)

practice 1

1. 下面是一个头文件:

(……省略……)

注意到setgolf()被重载,可以这样使用其第一个版本:

golf ann;

setgolf(ann, "Ann Birdfree", 24);

上述函数调用提供了存储在ann结构中的信息。可以这样使用其第二个版本:

golf andy;

setgolf(andy);

上述函数将提示用户输入姓名和等级,并将它们存储在andy结构中。这个函数可以(但是不一定必须)在内部使用第一个版本。

根据这个头文件,创建一个多文件程序。其中的一个文件名为golf.cpp,它提供了与头文件中的原型匹配的函数定义;另一个文件应包含main(),并演示原型化函数的所有特性。例如,包含一个让用户输入的循环,并使用输入的数据来填充一个由golf结构组成的数组,数组被填满或用户将高尔夫选手的姓名设置为空字符串时,循环将结束。main()函数只使用头文件中原型化的函数来访问golf结构。

//practice 1



//golf.h
#pragma once        //这是一个比较常用的指令,只要在头文件的最开始加入这条指令就能够保证头文件被编译一次 
const int Len = 40;
struct golf
{
	char fullname[Len];
	int handicap;
};

void setgolf(golf& g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);



//golf.cpp
#include"golf.h"
#include<iostream>
#include<cstring>

using std::cout;
using std::cin;
using std::endl;

void setgolf(golf& g, const char* name, int hc)
{
	strcpy_s(g.fullname, name);
	g.handicap = hc;
}

int setgolf(golf& g)
{
	char name[Len];
	int hc = 0;              //定义局部变量时,系统不会自动初始化,定义全局变量会初始化为0,char为'\0',pointer为NULL
	cout << "Please enter the fullname of struct golf: ";
	cin.getline(name, Len);
	if (name[0] == '\0')
	{
		cout << "Done!" << endl;
		return 0;
	}
	else
	{
		cout << "Next enter the handicap of struct golf: ";
		cin >> hc;
		setgolf(g, name, hc);
		cin.get();     //读取换行符,否则遇见下次getline会读取换行符认为是一个空行
		return 1;
	}
}

void handicap(golf& g, int hc)
{
	g.handicap = hc;
}

void showgolf(const golf& g)
{
	cout << "The fullname is: " << g.fullname << endl;
	cout << "The handicap is: " << g.handicap << endl;
}



//main.cpp
#include"golf.h"
#include<iostream>

int main()
{
	golf ann;
	setgolf(ann, "Ann Birdfree", 24);
	showgolf(ann);
	
	golf andy;
	while (1)
	{
		if (setgolf(andy))
			showgolf(andy);
		else
			break;
	}

	handicap(andy, 1);
	showgolf(andy);
	return 0;
}


practice 2

2. 修改程序清单9.9:用string对象代替字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串“”进行比较,以判断是否为空行。

#include<iostream>
#include<string>
using namespace std;

void strcount(const string str);

int main()
{
	string str;
	cout << "Enter a line: ";
	getline(cin, str);
	while (cin)
	{
		strcount(str);
		cout << "Enter next line(empty line to quit): ";
		getline(cin, str);
		if (str == "")
			break;
	}
	cout << "Bye!" << endl;
	return 0;
}

void strcount(const string str)
{
	static int total = 0;
	int count = 0;
	cout << "\"" << str << "\" contains ";
	for (int i = 0; str[i] != '\0'; i++)
		count++;
	total += count;
	cout << count << " characters\n";
	cout << total << " characters total\n";
}

practice 3

3. 下面是一个结构声明:

struct chaff

{

       char dross[20];

        int slag;

};

编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓冲区;另一种方法是使用常规new运算符来分配缓冲区。

#include<iostream>
#include<cstring>
#include<new>

struct chaff
{
	char dross[20];
	int slag;
};

using namespace std;
void show(const chaff*, int n);

int main()
{
	char* pc = new char[512];
	chaff* pchaff = new (pc)chaff[2];
	int i = 0;
	for (; i < 2; i++)
	{
		cout << "Enter to give chaff a value: " << endl;
		cout << "Enter the dross: ";
		char str[20];
		cin.getline(str, 20);
		strcpy_s(pchaff[i].dross, str);
		cout << "Enter the slag: ";
		cin >> pchaff[i].slag;
		cin.get();
	}
	show(pchaff, i);
	return 0;
}

void show(const chaff* ch, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << "The dorss of chaff: " << ch[i].dross << endl;
		cout << "The slag of chaff: " << ch[i].slag << endl;

	}
}

practice 4

4. 请基于下面这个名称空间编写一个由3个文件组成的程序:

(……省略……)

第一个文件是一个头文件,其中包含名称空间;第二个文件是一个源代码文件,它对这个名称空间进行扩展,以提供这三个函数的定义;第三个文件声明两个Sales对象,并使用setSales()的交互式版本为一个结构提供值,然后使用setSales()的非交互式版本为另一个结构提供值。另外它还使用showSales()来显示这两个结构的内容。
 

#pragma once
//tou.h

namespace SALES
{
	const int QUARTERS = 4;
	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};
	void setSales(Sales& s, const double ar[], int n);
	void setSales(Sales& s);
	void showSales(const Sales& s);
}




//tou.cpp
#include"tou.h"
#include<iostream>

using namespace std;
namespace SALES
{
	void setSales(Sales& s, const double ar[], int n)
	{
		double total = 0;
		double max = ar[0];
		double min = ar[0];
		if (n < QUARTERS)
		{
			for (int i = 0; i < n; i++)
				s.sales[i] = ar[i];
			for (int j = n; j < 4; j++)
				s.sales[j] = 0;
		}
		else
			for (int i = 0; i < QUARTERS; i++)
				s.sales[i] = ar[i];
		for (int i = 0; i < QUARTERS; i++)
		{
			total += ar[i];
			if (ar[i] > max)
				max = ar[i];
			if (ar[i] < min)
				min = ar[i];
		}
		s.average = total / QUARTERS;
		s.max = max;
		s.min = min;
	}

	void setSales(Sales& s)
	{
		cout << "Enter the sale of quarters: ";
		for (int i = 0; i < QUARTERS; i++)
			cin >> s.sales[i];

		double total = 0;
		double max = s.sales[0];
		double min = s.sales[0];
		for (int i = 0; i < QUARTERS; i++)
		{
			s.sales[i] = s.sales[i];
			total += s.sales[i];
			if (s.sales[i] > max)
				max = s.sales[i];
			if (s.sales[i] < min)
				min = s.sales[i];
		}
		s.average = total / QUARTERS;
		s.max = max;
		s.min = min;
		cout << endl;
	}

	void showSales(const Sales& s)
	{
		cout << "The sales of quarters are: ";
		for (int i = 0; i < QUARTERS; i++)
			cout << s.sales[i] << endl;
		cout << "The average of sales is: " << s.average << endl;
		cout << "The maximum of sales is: " << s.max << endl;
		cout << "The minimun of sales is: " << s.min << endl;
		cout << endl;
	}
}





//main.cpp
#include"tou.h"
#include <iostream>

using namespace std;
using namespace SALES;
int main()
{
	Sales s1, s2;
	double array[] = { 3333.3,2535.1,9532.5,1001.0,5231.0,5555.0 };
	setSales(s1, array, 4);
	showSales(s1);

	setSales(s2);
	showSales(s2);
	cin.get();//cin不读取空白字符,因此setSales中cin留下的换行符需要多一个cin.get()读取
	cin.get();

	setSales(s1, array, 3);
	showSales(s1);
	cin.get();

	setSales(s1, array, 2);
	showSales(s1);
	cin.get();

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值