C++ Primer Plus(第六版)第九章课后编程答案

  1.

//golf.h--for pe9-1.cpp

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<iostream>
#include"golf.h"
#include<cstring>
using namespace std;
void setgolf(golf &g, const char *name, int hc)
{
	strcpy_s(g.fullname, name);
	g.handicap = hc;
}
int setgolf(golf & g)
{
	    static int i = 0;      //静态变量
		cout << "#" << ++i << ":\n";
		cout << "Please enter the name: ";
		cin.getline(g.fullname, Len);
		int temp = 1;
		if (strcmp(g.fullname, " ") == 0)
		{
			temp = 0;
			return temp;
		}
		else 
		{
			cout << "Please enter the grade:";
			cin >> g.handicap;
			cin.get();
			return temp;
		}
}
void handicap(golf &g, int hc)
{
	g.handicap = hc;
}
void showgolf(const golf &g)
{
	cout << "The name of golf is: " << g.fullname << endl;
	cout << "The grade of golf is: " << g.handicap << endl;
}
//main.cpp
#include<iostream>
#include"golf.h"
using namespace std;
int main()
{
	golf ann;
	setgolf(ann, "Ann Birdfree", 24);
	showgolf(ann);
	golf andy[5];
	int count = 0;
	while ((count < 5) && (setgolf(andy[count])))
		  count++;
	for (int i = 0; i < count; i++)
	  showgolf(andy[i]);
	int change;
	for (int i = 0; i < count; i++)
	{
		cout << "#" << (i + 1) << ":\n";
		cout << "Please enter the change:";
		cin >> change;
		handicap(andy[i], change);
		showgolf(andy[i]);
	}
	return 0;
}

2.

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

void strcount(const string & str);
int main()
{
	string input;

	cout << "Enter a line:\n";
	getline(cin, input);
	while(input!="")     //测试时,使用enter来键入空行
	{
		strcount(input);
		cout << "Enter next line (empty line to quit):\n";
		getline(cin, input);
	}
	cout << "Bye\n";
	return 0;
}

void strcount(const string & str)
{
	using namespace std;
	static int total = 0;       //静态局部变量
	int count = 0;

	cout << "\"" << str << "\" contains ";
	while (str[count])
		count++;          //或使用str.size()成员函数来计算string类的长度
	total += count;
	cout << count << " characters\n";
	cout << total << " characters total\n";
}

3.

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

const int BUF = 512;
char buffer[BUF];
using namespace std;
struct chaff
{
	char dross[20];
	int slag;
};
void get(chaff &str,char * ch);
void show(const chaff &str);

int main()
{
	chaff * p1 = new (buffer) chaff[2];    //定位new运算符,buffer为静态内存,不需要delete释放内存
	//delete只能用于这样的指针:指向常规new运算符分配的堆内存
	for (int i = 0; i < 2; i++)
	{
		static int count = 0;
		cout << "#" << ++count << ":\n";
		cout << "Please enter the dross:";
		char ch[20];
		cin.getline(ch, 20);
		get(p1[i],ch);
	}
	for (int i = 0; i < 2; i++)
		show(p1[i]);
	return 0;
}

void get(chaff &str,char * ch)
{
    strcpy_s(str.dross, ch);
	cout << "Please enter the slag:";
	cin >> str.slag;
	cin.get();
}

void show(const chaff &str)
{
	static int count = 0;
	cout << "#" << ++count << ":\n";
	cout << "dross of the chaff is: " << str.dross << endl;
	cout << "slag of the chaff is: " << str.slag << endl;
}

4.

//namesp.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);
}
//namesp.cpp函数定义
#include<iostream>
#include"namesp.h"
namespace SALES
{
	void setSales(Sales & s, const double ar[], int n)
	{
		double sum = 0.0;
		for (int i = 0; i < n; i++)
		{
			s.sales[i] = ar[i];
			sum += s.sales[i];
		}
		s.average =sum/n;
		s.max = s.min = s.sales[0];
		for (int i = 0; i < n; i++)
		{
			if (s.max < s.sales[i])
				s.max = s.sales[i];
			if (s.min > s.sales[i])
				s.min = s.sales[i];
		}
	}
	void setSales(Sales &s)
	{
		std::cout << "Please enter sales:";
		double sum = 0.0;
		for (int i = 0; i < QUARTERS; i++)
		{
			std::cin >> s.sales[i];
			sum += s.sales[i];
		}
		std::cin.get();
		s.average = sum/QUARTERS;
		s.max = s.min = s.sales[0];
		for (int i = 0; i < QUARTERS; i++)
		{
			if (s.max < s.sales[i])
				s.max = s.sales[i];
			if (s.min > s.sales[i])
				s.min = s.sales[i];
		}
	}
	void showSales(const Sales & s)
	{
		for (int i = 0; i < QUARTERS; i++)
			std::cout << s.sales[i] << " "<<std::endl;
		std::cout<<"The average is:"<<s.average << std::endl;
		std::cout <<"The max is:"<< s.max << std::endl;
		std::cout <<"The min is:"<< s.min << std::endl;
	}
}
//main.cpp
#include<iostream>
#include"namesp.h"
const int ArSize=4;
int main(void)
{
	using std::cin;
	using std::cout;
	using std::endl;
	using SALES::Sales;
	using SALES::setSales;
	using SALES::showSales;
	Sales s1,s2;          //声明两个Sales对象
	
	double a[ArSize];         //交互式
	cout << "Please enter numbers:";
	for (int i = 0; i < ArSize; i++)
		cin >> a[i];
	setSales(s1, a, ArSize);
	showSales(s1);
	
	setSales(s2);      //非交互式
	showSales(s2);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41282726/article/details/84673831