c++ primer plus编程练习题参考第九章

1.下面是一个头文件。
/*******************/
根据这个头文件,创建一个多文件程序。其中的一个为golf。cpp,他提供了与头文件中的原型匹配的函数定义;另一个文件应该包含main(),并演示原型化函数的所有特性。例如,包含一个让用户输入的循环,并使用输入的数据来填充一个由golf结构组成的数组,数组被填满或用户用高尔夫选手的姓名设置为空字符串时,循环将结束。main()汉化只使用头文件中原型化的函数来访问golf结构。

//golf.h
const int Len = 40;
struct golf1
{
    
    
	char fullname[Len];
	int handicap;
};
void setgolf1(golf1 &g, const char *name, int hc);
int setgolf1(golf1 &g);
void handicap(golf1 &g, int hc);
void showgolf1(const golf1 &g);

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

void setgolf1(golf1 & g, const char * name, int hc)
{
    
    
	strcpy_s(g.fullname, name);
	g.handicap = hc;
}

int setgolf1(golf1 & g)
{
    
    
	cout << "Please enter golf1 ";
	cin.getline(g.fullname, Len);
	if (strcmp(g.fullname, "") == 0)
	{
    
    
		return 0;
	}

	cout << "Please enter hanicap golf1 player: ";
	cin >> g.handicap;
	cin.get();

	return 1;
}

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

void showgolf1(const golf1 & g)
{
    
    
	cout << "fullname: " << g.fullname << endl;
	cout << "handicap: " << g.handicap << endl;
}

//运行文件
#include "glof1.h"
#include <iostream>

using namespace std;
int main()
{
    
    
	golf1 ann;
	setgolf1(ann, "ann", 20);
	showgolf1(ann);
	golf1 g[3];
	int i = 0;
	while ((i < 3) && (setgolf1(g[i])))
	{
    
    
		showgolf1(g[i]);
		cout << "next golf: "<< endl;
		i++;
	}
	golf1 leo;
	setgolf1(leo, "leo", 20);
	handicap(leo, 1);
	showgolf1(leo);
	system("pause");
	return 0;
}

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

#include <iostream>
#include <string>
using namespace std;
void strcount(const string str);
int main()
{
    
    
	string input;
	cout << "enter a line" << endl;
	getline(cin, input);
	while (cin)
	{
    
    
		strcount(input);
		cout << "enter next line(empty line to quit)" << endl;
		getline(cin, input);
		if (input == "")
			break;
	}
	cout << "Bye" << endl;
	system("pause");
	return 0;
}
void strcount(const string str)
{
    
    
	static int total = 0;
	int count = 0;
	cout << "\"" << str << "\"contains";
	while (str[count])
		count++;
	total += count;
	cout << count << "characters" << endl;
	cout << total << "characters total" << endl;
}

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

#include <iostream>
#include <new>
#include <cstring>
const int buf = 256;
using namespace std;
struct chaff
{
    
    
	char dross[20];
	int slag;
};
int main()
{
    
    
	char buff[buf];
	chaff *p1 = new(buff)chaff[2];
	char *p = new char[buf];
	chaff *p2 = new(p)chaff[2];
	char dross[20];
	int slag = 0;
	for (int i = 0; i < 2; i++)
	{
    
    
		cout << "enter dross :" << i + 1 << "chaff: " << endl;
		cin.getline(dross, 20);
		cout << "enter slag :" << i + 1 << "chaff:" << endl;
		cin >> slag;
		cin.ignore();
		strcpy_s(p1[i].dross, dross);
		strcpy_s(p2[i].dross, dross);
		p1[i], slag = p2[i].slag = slag;
	}
	for (int i = 0; i < 2; i++)
	{
    
    
		cout << "chaff :" << i + 1 << endl;
		cout << "p1_dross: " << p1[i].dross <<"p1_slag: "<<p1[i].slag<< endl;
		cout << "p1_dross: " << p2[i].dross << "p2_slag: " << p2[i].slag << endl;
	}
	delete[]p;
	system("pause");
	return 0;
}

4.请基于下面这个名称控件编写一个由3个文件组成的程序
/*******************************/
第一个文件是一个头文件,其中包含名称空间;第二个文件是一个源代码文件,它对这个名称空间进行扩展,以提供这三个函数的定义;第三个文件声明两个sales对象,并使用setsales()交互式版本为一个结构提供值,然后使用setsales()的非交互式版本为另一个结构提供值。另外它还使用showSales()来显示这2个结构的内容。

//namesp.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);
}

猜你喜欢

转载自blog.csdn.net/m0_51559565/article/details/123999660