C++Primer Plus第九章编程练习答案参考

这一章感觉看题目意思就老费劲了,要理解半天
1.

//头文件golf.h
#ifndef GOLF_H
#define GOLF_H

const int Len = 40;
struct golf
{
	char fullname[40];
	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);

#endif

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

void setgolf(golf &g, const char *name, int hc)
{
	strcpy(g.fullname, name);
	handicap(g, hc);
}
int setgolf(golf &g)
{
	using std::cout;
	using std::cin;
	cout << "Enter the fullname and handicap:\n";
	if(cin.getline(g.fullname, Len)&&g.fullname!="")
	{
		cin >> g.handicap;
		cin.get();
		return 1;
	}
	else
		return 0;
}

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

void showgolf(const golf &g)
{
	using std::cout;
	cout << "Fullname[" << g.fullname << "]\t"
	<< "Handicap: " << g.handicap << "\n";
}
//main.cpp
#include<iostream>
#include"golf.h"

const int Size=3;
int main()
{
	using std::cout;
	using std::cin;
	
	golf anny[Size];
	golf ann[Size];
	char* name = new char[Len];
	int hc, i=0;
	for(i=0; i<Size; i++)
	{
		cout << "Enter the fullname and handicap:\n";
		cin.getline(name, Len);
		cin >> hc;
		cin.get();
		if(name=="")
			break;
		else
			setgolf(anny[i], name, hc);
	}
	delete[] name;
	cout << "________________________\n";
	i=0;
	while(i<Size&&setgolf(ann[i])){++i;}

		
	cout << "All the players:";
	for(i=0; i<Size; i++)
	{
		showgolf(anny[i]);
	}
	for(i=0; i<Size; i++)
	{
		showgolf(ann[i]);
	}
}
#include<iostream>
#include<string>
using namespace std;

void strcount(const string str);

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

void strcount(const string str)
{
	using std::cout;
	static int total=0;
	int count=str.size();
	for(int i=0; i<str.size(); i++)
	{
		if(str[i]==' ')
		{
			--count;
		}
	}
	total += count;
	cout << "\"" << str << "\" contains "
	<< count << " characters\n"
	<< total << " characters total\n";
}

3.这一题感觉自己有点乱来,就按照自己的想法写的嘿嘿

#include<iostream>
#include<cstring>

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

int main()
{
	using std::cout;
	using std::cin;
	
	chaff cha[2];
	chaff* chaf = new chaff[2];
	chaff* c = new (cha) chaff;
	chaff* ch = new (chaf) chaff;
	char dro[20];
	int s;
	for(int i=0; i<2; i++)
	{
		cout << "Enter the dross and slag:\n";
		cin.getline(dro, 20);
		cin >> s;
		cin.get();
		strcpy(c[i].dross, dro);
		c[i].slag = s;
		strcpy(ch[i].dross, dro);
		ch[i].slag = s;
	}
	for(int i=0; i<2; i++)
		cout << c[i].dross << "\t" << c[i].slag << "\n";
	cout << "-----------------------------------------------\n";
	for(int i=0; i<2; i++)
		cout << ch[i].dross << "\t" << ch[i].slag << "\n";
}
//头文件namespaces.h
#include<iostream>
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);
}
//definition.cpp
#include"namespaces.h"

namespace SALES
{
	void setSales(Sales &s, const double ar[], int n)
	{
		int times = QUARTERS>n ? n:QUARTERS;
		double arMax=ar[0], arMin=ar[0];
		double total=0.0;
		for(int i=0; i<times; i++)
		{
			s.sales[i] = ar[i];
			total += s.sales[i];
			if(arMax<ar[i])
				arMax = ar[i];
			if(arMin>ar[i])
				arMin = ar[i];
		}
		if(n<QUARTERS)
		{
			for(int i=n; i<QUARTERS; i++)
				s.sales[i] = 0.0;
		}
		s.max = arMax;
		s.min = arMin;
		s.average = total/times;
	}
	
	void setSales(Sales &s)
	{
		int size;
		double total=0.0;
		std::cout << "Enter the array size(size<5): ";
		std::cin >> size;
		std::cout << "Enter " << size << " array:\n";
		for(int i=0; i<size; i++)
		{
			std::cin >> s.sales[i];
			total += s.sales[i];
		}
		s.max = s.sales[0];
		s.min = s.sales[0];
		for(int i=1; i<size; i++)
		{
			if(s.max<s.sales[i])
				s.max = s.sales[i];
			if(s.min>s.sales[i])
				s.min = s.sales[i];
		}
		if(size<QUARTERS)
		{
			for(int i=size; i<QUARTERS; i++)
				s.sales[i] = 0.0;
		}
		s.average = total/size;
	}
	
	void showSales(const Sales &s)
	{
		using std::cout;
		using std::endl;
		for(int i=0; i<QUARTERS; i++)
			cout << "[" << i+1 << "] " << s.sales[i] << endl;
		cout << "MAX: " << s.max << endl
		<< "MIN: " << s.min << endl
		<< "AVERAGE: " << s.average << endl;
	}
}
//main.cpp
#include"namespaces.h"

int main()
{
	using namespace SALES;
	using namespace std;
	
	Sales s1;
	Sales s2;
	int size;
	cout << "Enter the array size: ";
	cin >> size;
	double* ar = new double[size];
	cout << "Enter " << size << " double array:\n";
	for(int i=0; i<size; i++)
		cin >> ar[i];

	setSales(s1,ar,size);
	showSales(s1);
	
	setSales(s2);
	showSales(s2);
	
	return 0;
}

以上,如有问题欢迎各位指出来鸭~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值