C++ Primer Plus第九章编程练习

1、

// ball.h
#ifndef BALL_H_
#define BALL_H_
namespace ball
{
	const int Len = 10;
	struct golf
	{
		char fullname[Len];
		int handicap;
	};
	void SetGolf(golf & rg, const char * name, int hc);
	int SetGolf(golf & rg);
	void Handicap(golf & rg, int hc);
	void ShowGolf(const golf & rg);
}
#endif

//ball.cpp
namespace ball
{
	using std::cout;
	using std::cin;
	using std::endl;
	void SetGolf(golf & rg, const char * name, int hc)
	{
		int i = 0;
		while (*name++ && i < Len)  //没有读取字符串的结尾字符
		{
			rg.fullname[i] = *name;
			i++;
		}
		if (i = Len)
		{
			cout << "!!The name too long!!\n";
			rg.fullname[i - 1] = '0';
		}
		else
		{
			rg.fullname[i] = '0';
		}
		rg.handicap = hc;
	}
	int SetGolf(golf & rg)
	{
		cout << "Enter the fullname: ";
		cin.get(rg.fullname, Len);
		cin.clear();	//假若cin出错,必需重置cin,并能继续使用cin对象——使程序能执行
		while (cin.get() != '\n') // 设置delimit为换行符,清空输入队列
		{
			continue;
		}
		cout << "Enter the number of handicap: ";
		cin >> rg.handicap;
		cin.clear();
		while (cin.get() != '\n') //设置delimit为换行符,清空输入队列
		{
			continue;
		}
		if (strlen(rg.fullname) == 0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	void Handicap(golf & rg, int hc)
	{
		rg.handicap = hc;
	}
	void ShowGolf(const golf & rg)
	{
		cout << "--------------\n"
			<< rg.fullname << '\t' << rg.handicap << endl
			<< "--------------\n";
	}
}

//main()
int main(void)
{
	using std::cin;
	using std::cout;
	using std::endl;
	using ball::golf;
	golf silly[3];
	int i = 0;
	while (i < 3)
	{
		if (!ball::SetGolf(silly[i]))
		{
			break;
		}
		i++;
	}
	for (int j = 0; j < i; j++)
	{
		ball::ShowGolf(silly[j]);
	}
	system("pause");
	return 0;
}

2、

void strcount(const std::string & s);

int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	using std::string;
	/*char str[ARSIZE];*/
	string s;
	/*char next;*/
	cout << "Enter a line:\n";
	/*cin.get(str, ARSIZE);*/
	getline(cin, s);
	//while (cin)
	//{
	//	cin.get(next);
	//	while (next != '\n')
	//	{
	//		cin.get(next);
	//	}
	//	strcount(s);
	//	cout << "Next string (empty line to quit):\n";
	//	cin.get(str, ARSIZE);
	//}
	while (s.compare(""))
	{
		strcount(s);
		cout << "Next string (empty line to quit):\n";
		getline(cin, s);
	}
	cout << "bye!\n";
	system("pause");
	return 0;
}

void strcount(const std::string & s)
{
	static int total = 0;
	int count = 0;
	for (int i = 0; s[i] != '\0'; i++)
	{
		using std::isalpha;
		if (isalpha(s[i]))
		{
			count++;
			total++;
		}
	}
	std::cout << count << " charcacters\n";
	std::cout << total << " characters total\n";
}

3

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

int main(void)
{
	using std::strcpy;
	using std::cout;
	using std::endl;
	char buffer[100];
	chaff * p = new(buffer) chaff[2];
	strcpy_s(p[0].dross, "AAAAA");
	strcpy_s(p[1].dross, "bbbbbb");
	p[0].slag = 1;
	p[1].slag = 2;
	for (int i = 0; i < 2; i++)
	{
		cout.width(10);
		cout << p[i].dross;
		cout.width(10);
		cout << p[i].slag << endl;
	}
	system("pause");
	return 0;
}

4、

//.h
#ifndef SALE_H_
#define SALE_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);
}
#endif

//.cpp
namespace SALES
{
	void SetSales(Sales & s, const double ar[], int n)
	{
		double total = 0;
		double temp_min;
		double temp_max;
		for (int i = 0; i < QUARTERS; i++)
		{
			s.sales[i] = ar[i];
			total += ar[i];
			temp_min = (temp_min = ar[0]) > ar[i] ? ar[i] : temp_min;
			temp_max = (temp_max = ar[0]) < ar[i] ? ar[i] : temp_max;
		}
		s.average = total / QUARTERS;
		s.max = temp_max;
		s.min = temp_min;
	}
	void SetSales(Sales & s)
	{
		using std::cout;
		using std::cin;
		using std::endl;
		double temp_arr[QUARTERS];
		cout << "Enter the sales in quarter:\n";
		for (int i = 0; i < QUARTERS; i++)
		{
			cout << "#" << (i + 1) << " sale: ";
			/*cin >> s.sales[i];*/
			cin >> temp_arr[i];
		}
		cout << "done!\n";
		SetSales(s, temp_arr, QUARTERS);
	}
	void ShowSales(const Sales & s)
	{
		using std::cout;
		using std::endl;
		using std::left;
		cout << "-------------------------------------------------\n";
		cout << "The sales:";
		for (int i = 0; i < QUARTERS; i++)
		{

			cout << '\t' << s.sales[i];
		}
		cout << endl;
		cout << "average: " << s.average << '\t' << "max: " << s.max << '\t' << "min: " << s.min << endl;
	}
}

//main().cpp
int main(void)
{
	using SALES::Sales;
	using SALES::SetSales;
	using SALES::ShowSales;
	Sales s[2];
	double arr[4] = { 10.5, 20, 30.5, 40 };
	SetSales(s[0]);
	ShowSales(s[0]);
	SetSales(s[1], arr, 4);
	ShowSales(s[1]);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值