C++ Primer Plus 自学第九章结尾编程4题

第一题

要求:一个头文件,两个源文件,填充数组结构,输入,展示。

头文件一:

//golf.h -- for pe9-1.cpp

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

 源文件一:

//9.1
#include<iostream>
#include "golf.h"
int main()
{
	using namespace std;
	cout << "Enter the golf name and the handicap:\n";
	char name[Len];
	golf Golfa;
	int i;
	for (i = 0; i < Len; i++)
	{
		name[i] = cin.get();
		if (name[i]== '\n')
		{
			name[i] = '\0';
			cout << "the enter end;\n";
			break;
		}
	}
    setgolf(Golfa,name, i+1);
	cout << "enter the golf level:\n";
	setgolf(Golfa);
	cout << "do you want to reset handicap to new value?(Y/N)\n";
	char a;
	cin >> a;
	switch (a)
	{
	case 'Y':
	case 'y':
		cout << "enter the value:\n";
		int i;
		cin >> i;
		handicap(Golfa, i);
		break;
	case 'n':
	case 'N':
	default:
		cout << "you don't \n";
		break;
	};
	showgolf(Golfa);
	cout << "Done.\n";
	return 0;
}

源文件二:

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

void setgolf(golf& g, const char* name, int hc)
{
	for (int i = 0; i < hc; i++)
	{
		g.fullname[i] = name[i];
	}
}

int setgolf(golf& g)
{
	using std::cin;
	cin >> g.handicap;
	return 0;
}

void handicap(golf& g, int hc)
{
	using std::cin;
	g.handicap=hc;
}

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

第二题

要求:用string修改程序。

//9.2
#include<iostream>
#include<string>
#include<cstring>
//const int Arsize = 10;
using std::string;
void strcount(const string  str);

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

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

void strcount(const string str)
{
	using namespace std;
	static int total = 0;
	int count = 0;

	cout << "\"" << str << "\" constains ";
	count=size(str);
	total += count;
	cout << " count: " << count << " total: " << total << endl;
}

第三题

要求:用new运算符来分配缓冲区

//9.3
#include<iostream>
#include<cstring>
#pragma warning(disable : 4996)
struct chaff
{
	char dross[20];
	int slag;
};

int main()
{
	using namespace std;
	chaff *a = new chaff [2];
	char m[20] = "hello world!";
	strcpy(a[0].dross,m);
	strcpy(a[1].dross,m);
	a[0].slag =a[1].slag= 20;
	for (int k = 0; k < 2; k++)
		cout << a[k].dross << " and "<< a[k].slag << endl;
	return 0;
}

第四题

要求:用名称空间来创建结构,函数,计算函数,输入函数,展示函数。

头文件:

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);
}

源文件一

//9.4
#include<iostream>
#include"nampsS.h"
//#pragma warning(disable: 0167)
using namespace SALES;
int main()
{
	using namespace std;
	double ar[4] = { 321,345,456.4,357 };
	Sales Spring, Summer, Fule, Winter;
	setsales(Spring, ar, 4);
	showSales(Spring);
	setSales(Summer);
	setSales(Fule);
	setSales(Winter);

	return 0;
}

源文件二

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

namespace SALES
{
	void setsales(Sales& s,const double ar[], int n)
	{
		int i;
		for (i = 0; i < n; i++)
			s.sales[i] = ar[i];
		double max=0, min=0, total=0, average;
		for (i = 0; i < n - 1; i++)
		{
			max = s.sales[i] < s.sales[i + 1] ? s.sales[i + 1] : s.sales[i];
			min = s.sales[i] > s.sales[i + 1] ? s.sales[i + 1] : s.sales[i];
			total += s.sales[i];
		}
		total += s.sales[i];
		average = total / n;
		s.average = average;
		s.max = max;
		s.min = min;
	};

	void setSales(Sales& s)
	{
		using SALES::setSales;
		using std::cout;
		using std::cin;
		using std::endl;
		static int count = 2;
		cout << "The Sales #" << count << ": \n";
		cout << "please enter the value of sales:\n";
		double sales_s[QUARTERS]{};
		for (int i = 0; i < QUARTERS; i++)
			cin >> sales_s[i];
		setsales(s,sales_s, 4);
		count++;
		showSales(s);
	}

	void showSales(const Sales& s)
	{
		using std::cout;
		using std::endl;
		for (int i = 0; i < QUARTERS; i++)
			cout << "Quarters #" << i + 1 << " : " << s.sales[i] << endl;
		cout << "Max: " << s.max << " Min: " << s.min << endl;
		cout << "Average: " << s.average << endl;
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值