C++Primer Plus第6版第9章编程练习

文章提供了一系列C++代码示例,涉及结构体(如golf)的定义和使用,包括存储球员姓名和差点信息,以及设置、显示和处理差点的功能。程序还包括了对字符串输入的处理,使用getline()函数读取用户输入,以及使用string对象替代字符数组以简化字符串管理。此外,还展示了如何使用定位运算符在内存缓冲区中存储和显示结构体数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第1题

golf.h头文件

const int len = 40;
struct golf
{
	char fullname[len];
	int handicap;
};
void setgolf(golf&, const char* name, int hc);

int setgolf(golf& g);

void handicap(golf& g, int hc);

void showgolf(const golf& g);

main.cpp

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

const int SIZE = 3;

int main()
{
	using std::cout;
	using std::endl;
	golf andy;
	golf go[SIZE];

	setgolf(andy,"Ann Birdfree",24);
	showgolf(andy);

	for (int i = 0; i < SIZE; i++)
	{
		if (!setgolf(go[i]))
			break;
	}

	handicap(andy, 23);
	showgolf(andy);

	for (int i = 0; i < SIZE; i++)
	{
		if(go[i].fullname[0] != '\0')
			showgolf(go[i]);
	}

	return 0;
}

golf.cpp

#define _CRT_SECURE_NO_WARNINGS//定义宏不然vs认为strcpy函数是不安全的

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

void setgolf(golf& g, const char* name, int hc)
{
	using std::strcpy;

	strcpy(g.fullname, name);
	g.handicap = hc;
}

int setgolf(golf& g)
{
	using std::cout;
	using std::cin;

	cout << "fullname: ";
	cin.getline(g.fullname,len);
	if (g.fullname[0] == '\0')//为空返回0
		return false;
	cout << "handicap: ";
	cin >> g.handicap;
	cin.get();				 //筛选掉换行符

	return true;
}

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

void showgolf(const golf& g)
{
	using std::cout;
	using std::endl;

	cout << g.fullname << ", "
		 <<g.handicap << endl;
}

第二题

修改程序9.9:用string对象替代字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串""进行比较,以判断是否为空行.

#include<iostream>
#include<string>
//常数
const int ArSize = 10;

//功能原型
void strcount(const std::string str);

int main()
{
	using namespace std;
	string input;
	char next;

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

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

	cout << "\"" << str << "\"contains ";
	while (str[count])
		count++;
	total += count;
	cout << count << " characters\n";
	cout << total << " characters total\n";
}

第三题

struct chaff

{

        char dross[20];

        int slag;
}

编写一个程序,使用定位运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓冲区;另一种方法是使用new运算符来分配缓冲区。

#define _CRT_SECURE_NO_WARNINGS//vs使用strcpy会报错所以添加该宏

#include<iostream>
#include<cstring>
const int SIZE = 100;
static char vo[SIZE];

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

int main()
{
	using namespace std;
	
	charff* opp = new (vo)charff[2];

	strcpy(opp[0].dross, "hello");
	opp[0].slag = 2;
	strcpy(opp[1].dross, "order");
	opp[1].slag = 3;

	for (int i = 0; i < 2; i++)
		cout << opp[i].dross << ", "
			 << opp[i].slag << endl;

	return 0;
}

第四题

头文件sales.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);
}

sales.cpp

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

namespace SALES
{
	void setSales(Sales& s, const double ar[], int n)
	{
		int i, sum, count,temp;

		s.average = sum = count = 0;

		for (i = 0; i < QUARTERS && i < n; i++)
		{
			s.sales[i] = ar[i];
			count++, s.average += s.sales[i];	//有逗号运算符一定要看清
		}

		while (i < QUARTERS)					//设置0
		{
			s.sales[i++] = 0;
		}

		s.average /= count;
		s.min = s.max = s.sales[0];

		for (int k = 1; k < count; k++)
		{
			if (s.max < s.sales[k])
				s.max = s.sales[k];
			else if (s.min > s.sales[k])
				s.min = s.sales[k];
		}
	}

	void setSales(Sales& s)						//交互式
	{
		using std::cin;
		double ar[QUARTERS];					//也可以设置其他的数

		for (int i = 0; i < QUARTERS; i++)
		{
			cin >> ar[i];
		}
		setSales(s, ar, QUARTERS + 1);			//直接调用该函数进行平均值 最大 最小
	}

	void showSales(const Sales& s)
	{
		using std::cout;
		using std::endl;

		for (int i = 0; i < QUARTERS; i++)
		{
			cout << "sales " << i + 1
				 << ": " << s.sales[i] << endl;
		}

		cout << "average value: " << s.average
			 << "\nmax: " << s.max << "\nmin : " 
			 << s.min << endl;
	}
}

main.cpp

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

int main()
{
	using namespace SALES;

	Sales s;
	Sales ss;
	double ar[QUARTERS + 1]{ 2.1, 5.1,3.1,4.1,1.1};

	setSales(s, ar, QUARTERS + 1);
	showSales(s);
	std::cout << std::endl;
	setSales(ss);
	showSales(ss);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值