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

第九章编程练习答案

9.1根据以下头文件内容编写多文件程序,提示用户输入姓名的等级,存在结构中

//9.1根据以下头文件内容编写多文件程序,提示用户输入姓名的等级,存在结构中
//golf.h -- for pe9-1.cpp

const int Len = 40;
struct golf
{
	char fullname[Len];
	int handicap;
};

// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);

// interactive version:
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

// function resets handicap to new value
void handicap(golf & g, int hc);

// function displays contents of golf structure
void showgolf(const golf & g);

//golf.cpp
#include <iostream>
#include <cstring>
#include "golf.h"
using namespace std;

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

int setgolf (golf & g)
{
	cout << "输入姓名:";
	string strTmp;
	getline(cin, strTmp);
	if ("" == strTmp)
		return (0);
	strcpy(g.fullname, strTmp.c_str());
	cout << "输入等级:";
	int	n;
	cin >> n;
	cin.get();
	if (!n)
		return (0);
	g.handicap = n;
	return (1);
}

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

void showgolf(const golf & g)
{
	cout << g.fullname << '\t' << g.handicap;
}

//mian.cpp
#include <string>
#include <iostream>
#include "golf.h"
using namespace std;

int main () 
{     
	const unsigned	k_size = 4;
	golf	golfers[k_size];
	unsigned	numGolfers = 0;
	while (numGolfers < k_size && setgolf(golfers[numGolfers]))
		++numGolfers;
	for (unsigned i = 0; i < numGolfers; ++i) {
		showgolf(golfers[i]);
		cout << endl;
	}
} 

9.2修改程序清单9.9,使用string类型代替字符数组

//9.2修改程序清单9.9,使用string类型代替字符数组
#include <iostream>
#include <string>
using namespace std;

int main()
{	
	string input;
	unsigned	total = 0;
	cout << "Enter a line:\n";
	getline(cin, input);
	while (cin && "" != input)
	{
		cout << "\"" << input <<"\" contains ";
		cout << input.size() << " characters\n";
		total += input.size();
		cout << total << " characters total\n";
		cout << "Enter next line (empty line to quit):\n";
		getline(cin, input);
	}
	cout << "Bye" << endl;
}

9.3对结构体chaff,在函数中用new为含两个结构体的数组分配空间

//9.3对结构体chaff,在函数中用new为含两个结构体的数组分配空间
#include <iostream>
#include <cstring>
using namespace std;

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

int main () 
{     
	chaff* Chaff = new  chaff [2];
	strcpy(Chaff[0].dross, "foo");
	Chaff[0].slag = 2;
	strcpy(Chaff[1].dross, "bar");
	Chaff[1].slag = 8;
	for (unsigned i = 0; i < 2; ++i)
		cout << Chaff[i].dross << '\t' << Chaff[i].slag << endl;
	delete [] Chaff;
} 

9.4根据以下头文件内容,编写完成多文件程序

//9.4根据以下头文件内容,编写完成多文件程序
//sale.h
namespace SALES
{
	const int QUARTERS = 4;

	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};

	// copies the lesser of 4 or n items from the array ar
	// to the sales member of s and computes and stores the
	// average, maximum, and minimum values of the entered items;
	// remaining elements of sales, if any, set to 0
	void setSales(Sales & s, const double ar[], int n);

	// gathers sales for 4 quarters interactively, stores them
	// in the sales member of s and computes and stores the
	// average, maximum, and minimum values
	void setSales(Sales & s);

	// display all information in structure s
	void showSales(const Sales & s);
}

//sale.cpp
#include <iostream>
#include "Sales.h"
using namespace std;

namespace SALES
{
	static double calcAverage (double arr[], unsigned arrSize)
	{
		double	sum = 0;
		for (unsigned i = 0; i < arrSize; ++i)
			sum += arr[i];
		return (sum / arrSize);
	}

	static double calcMax (double arr[], unsigned arrSize)
	{
		unsigned	idxMax = 0;
		for (unsigned i = 0; i < arrSize; ++i)
			if (arr[i] > arr[idxMax])
				idxMax = i;
		return (arr[idxMax]);
	}

	static double calcMin (double arr[], unsigned arrSize)
	{
		unsigned	idxMin = 0;
		for (unsigned i = 0; i < arrSize; ++i)
			if (arr[i] < arr[idxMin]) 
				idxMin = i;
		return (arr[idxMin]);
	}

	void setSales (Sales & s, const double ar[], int n)
	{
		unsigned	times = n < QUARTERS ? (unsigned)n : QUARTERS;
		for (unsigned i = 0; i < times; ++i)
			s.sales[i] = ar[i];
		for (unsigned i = times; i < QUARTERS; ++i)
			s.sales[i] = 0;
		s.average = calcAverage(s.sales, times);
		s.max = calcMax(s.sales, times);
		s.min = calcMin(s.sales, times);
	}

	void setSales(Sales & s)
	{
		cout << "输入" << QUARTERS << "个销售记录:";
		unsigned	times = QUARTERS;
		double	arr[QUARTERS] = {0};
		for (unsigned i = 0; i < QUARTERS; ++i) {
			cin >> arr[i];
			if (!cin) {
				times = i;
				break;
			}
		}
		setSales(s, arr, QUARTERS);
		s.average = calcAverage(s.sales, times);
		s.max = calcMax(s.sales, times);
		s.min = calcMin(s.sales, times);
	}

	void showSales (const Sales & s)
	{
		cout << "sales: ";
		for (const auto& e : s.sales)
			cout << e << ' ';
		cout << endl;
		cout << "average: " << s.average << endl;
		cout << "max: " << s.max << endl;
		cout << "min: " << s.min << endl;
	}
}

//main.cpp
#include <iostream>
#include "sales.h"
using namespace std;

int main () 
{     
	using namespace SALES;
	Sales salesBook;
	double	salesLst[] = {12.2, 11.16, 10.61, 16.24, 11.53};
	setSales(salesBook, salesLst, sizeof(salesLst)/sizeof(salesLst[0]));
	showSales(salesBook);
	Sales salesPen;
	setSales(salesPen);
	showSales(salesPen);
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值