c++ primer plus第九章习题答案

直接上源码:


第一题

golf.h

//*****************************************************************//
//*******************************golf.h****************************//
// golf.h -- for pe9-1.cpp
#ifndef GOLF_H_
#define GOLF_H_


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:
// fuction 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);

#endif
//*****************************************************************//
golf.cpp

<pre name="code" class="cpp">#include <iostream>
#include <string>
#include "golf.h"

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

int setgolf(golf & g)
{
	using namespace std;

	cout << "Enter fullname less than 40 words:" << endl;
	cin.getline(g.fullname,40);

	cout << "Enter handicap:" << endl;
	if(g.fullname[0] == '\0')
		return 0;
	cin >> g.handicap;
	cin.get();

	return 1;
}

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

void showgolf(const golf & g)
{
	using namespace std;
	cout << "fullname: " << g.fullname << endl;
	cout << "handicap: " << g.handicap << endl;
}


 

main.cpp

#include <iostream>
#include "golf.h"

int main()
{
	golf ann[10];
	int count = 0;
	int ret = 1;
	do{
		if(count%2 == 0)
		{
			ret = setgolf(ann[count]);
		}
		else
		{
			using namespace std;
			char temp[40];
			int num;
			cout << "Enter fullname less than 40 words:" << endl;
			cin.getline(temp,40);
			if(temp[0] == '\0')
			{
				break;
			}
			else
			{
				//
			}
			cout << "Enter handicap:" << endl;
			cin >> num;
			cin.get();
			setgolf(ann[count], temp, num);
		}
		count ++;
	}while((ret == 1)&&(count < 10));

	showgolf(ann[0]);
	handicap(ann[0], 6);
	showgolf(ann[0]);

	return 0;
}


第二题

// static.cpp -- using a static local variable
#include <iostream>
#include <string>
using namespace std;
// constants
const int ArSize = 10;
// function prototype
void strcount(const string str);
int main()
{
	string input;
	char next;

	cout << "Enter a line:\n";
	while(getline(cin, input))// getline is use to input a string
	{
		if(input == "")		// string didn't fit!
		{
			break;			// no inputs break
		}
		else				// this else is not necessary but should be here to consolidate the reliability
		{
			//
		}
		strcount(input);
		cout << "Enter next line (empty line to quit):\n";
	}
	cout << "Bye\n";
	return 0;
}

void strcount(const string str)
{
	int count = 0;				// automatic local variable

	cout << "\"" << str << "\" contains ";
	count = str.length();		// length of string str, string is a class
	cout << count << " characters\n";
	cout << count << " characters total\n";
}

第三题

#include <iostream>			// have fun buddy and never give up
#include <cstring>
#include <new>
using namespace std;

#define StartAddr1 0
#define StartAddr2 6		// change 6 to 5, see what happened, change to 1, then what happened?

struct chaff				// this struct takes up 24 bytes, see "memory alignment"
{
	char dross[20];
	int slag;
};
int buffer[12];				// 48 bytes in total, that's enough
void display(chaff * obj);
int main()
{
	chaff  *pt[2];
	pt[0] = new (&buffer[StartAddr1]) chaff;// set starting address
	pt[1] = new (&buffer[StartAddr2]) chaff;
	strcpy(pt[0] -> dross, "abcdefg");		// pointer uses "->" while non-pointer uses "."
	pt[0] -> slag = 10;
	strcpy(pt[1] -> dross, "hijklmn");
	pt[1] -> slag = 20;
	cout << "the address of buffer1: " << &buffer[StartAddr1] << endl;// test buffer's address
	cout << "the address of buffer2: " << &buffer[StartAddr2] << endl;
	cout << endl;
	for(int i = 0; i < 2; i++)
	{
		display(pt[i]);
	}
	return 0;
}

void display(chaff * obj)
{
	cout << "dross is " ;
	cout << obj -> dross << endl;
	cout << "slag is " ;
	cout << obj -> slag << endl;
	cout << "the address of object: " << (void *)obj << endl;			// test pointer's address
	cout << "the address of the 5th char is: " << (void *)&(obj->dross[4]) << endl;// this one makes sense when you change the value of StartAddr 
	cout << "the address of the last int (slag) is: " << &obj->slag << endl;// so as above
	cout << endl;

	return 0;
}

第四题

sales.h

//***********************************************//
//*******************sales.h*********************//
#ifndef SALES_H_
#define SALES_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);
}


#endif SALES_H_
//***********************************************//

sales.cpp

#include <iostream>
#include <stdlib.h>
#include "sales.h"

namespace SALES
{
	void setSales(Sales & s, const double ar[], int n)
	{
		using std::cout;
		if(n > 4 || n < 0)
		{
			cout << "error! n is out of range.\n";
			_exit(-1);			// force to quit the whole program
		}						// exit(0),exit(non-0),_exit(0),_exit(non-0) is different from each other.
		else					// _exit(): loss the data in the buffer, exit(): hold the data in the buffer
		{						// exit(0): quit normally, exit(non-0): quit abnormally
			double total = 0;
			for(int i = 0; i < n; i++)
			{
				s.sales[i] = ar[i];
				total = total + s.sales[i];
			}
			s.average = total / n;
			s.max = s.sales[0];	// initialize max and min, compare with each one
			s.min = s.sales[0];
			for(int i = 0; i < n; i++)
			{	
				if(s.sales[i] > s.max)
					s.max = s.sales[i];
				else if(s.sales[i] < s.min)
					s.min = s.sales[i];
				else
				{
					//
				}
			}
			for(int i = 0; i < QUARTERS; i++)
				s.sales[i] = 0;
		}
	}

	void setSales(Sales & s)
	{
		using std::cout;
		using std::cin;

		int n;
		double temp[QUARTERS];
		cout << "Set the sales number no more than 4: ";
		cin >> n;
		if(n > 4 || n < 0)
		{
			cout << "error! n is out of range.\n";
			_exit(-1);
		}
		else
		{
			for(int i = 0; i < n; i++)
			{
				cout << "input the #" << i+1 << " sale: ";
				cin >> temp[i];
			}
			setSales(s, temp, n);		// call subfuction "setSales" directly as it shares the same algorithm as this one
			/*double total = 0;			// these also can work, the same as setSales(s, temp, n)
			for(int i = 0; i < n; i++)
			{
				s.sales[i] = temp[i];
				total = total + s.sales[i];
			}
			s.average = total / n;
			s.max = s.sales[0];
			s.min = s.sales[0];
			for(int i = 0; i < n; i++)
			{
				if(s.sales[i] > s.max)
					s.max = s.sales[i];
				else if(s.sales[i] < s.min)
					s.min = s.sales[i];
				else
				{
					//
				}
			}
			for(int i = 0; i < QUARTERS; i++)
				s.sales[i] = 0;*/
		}
	}

	void showSales(const Sales & s)
	{
		using std::cout;
		using std::endl;
		cout << endl;
		cout << "average of sale is " << s.average << endl;
		cout << "max value of sale is " << s.max << endl;
		cout << "min value of sale is " << s.min << endl;
	}
}

main.cpp

#include "sales.h"
using namespace SALES;

int main()
{
	Sales obj1, obj2;
	double a[] = {3,4,5,6};
	setSales(obj1, a, 4);
	setSales(obj2);
	showSales(obj1);
	showSales(obj2);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值