c++ primer plus chapter9 习题

在这里插入图片描述

//golf.h
#ifndef GOLF_H_
#define GOLF_H_

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

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

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

int setgolf(golf& g)
{
	using namespace std;
	cout << "Please enter user's name: ";
	cin.getline( g.fullname, Len);
	if (strcmp(g.fullname, "") == 0)
		return 0;
	else {
		cout << "Please enter user's handicap: ";
		cin >> g.handicap;
		cin.get();
		return 1;
	}
}

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

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

using namespace std;

int main()
{
	golf g[3];
	int i;
	for (i = 0; i < 3; i++) {
		if (setgolf(g[i]) == 0)
			break;
	}
	for (int j = 0; j < i; j++) {
		showgolf(g[j]);
	}
	cout << "\nReset the information of all players\n";
	for (int j = 0; j < i; j++) {
		handicap(g[j], 90);
		showgolf(g[j]);
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

void strcount(const string str);

int main()
{
	string input;

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

void strcount(const string str)
{
	static int total = 0;
	int count = 0;
	count = str.size();
	total += count;
	cout << "\"" << str << "\" contains";
	cout << count << " characters\n";
	cout << total << " characters total\n";
}

在这里插入图片描述

#include<iostream>
#include<cstring>
#include<new>

using namespace std;

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

int main()
{
	char buffer[50] = "";
	chaff* pt = new chaff[2];
	chaff* dt = new(buffer) chaff[2];
	for (int i = 0; i < 2; i++) {
		strcpy_s(pt[i].dross, "i love you!");
		pt[i].slag = 3;
		strcpy_s(dt[i].dross, "you love me!");
		dt[i].slag = 10;
	}
	for (int i = 0; i < 2; i++) {
		cout << pt[i].dross << "   " << pt[i].slag << endl;
		cout << dt[i].dross << "   " << dt[i].slag << endl;
	}
	delete[]pt;
	return 0;
}

在这里插入图片描述

//sale.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
//sale.cpp
#include<iostream>
#include "sale.h"

namespace SALES{
	void setSales(Sales& s, const double ar[], int n)
	{
		double sum = 0.0;
		if (n >= QUARTERS) {
			for (int i = 0; i < QUARTERS; i++) {
				s.sales[i] = ar[i];
				sum += ar[i];
			}
			s.average = sum / QUARTERS;
		}
		else {
			int i;
			for (i = 0; i < n; i++) {
				s.sales[i] = ar[i];
				sum += ar[i];
			}
			while (i < QUARTERS)
				s.sales[i++] = 0;
			s.average = sum / n;
		}
		s.min = s.sales[0], s.max = s.sales[0];
		for (int i = 1; i < QUARTERS; i++) {
			if (s.sales[i] < s.min)
				s.min = s.sales[i];
			if (s.sales[i] > s.max)
				s.max = s.sales[i];
		}
	}
	void setSales(Sales& s)
	{
		using namespace std;
		double sum = 0.0;
		cout << "Enter 4 double numbers: ";
		for (int i = 0; i < QUARTERS; i++) {
			cin >> s.sales[i];
			sum += s.sales[i];
		}
		s.average = sum / QUARTERS;
		s.min = s.sales[0], s.max = s.sales[0];
		for (int i = 1; i < QUARTERS; i++) {
			if (s.sales[i] < s.min)
				s.min = s.sales[i];
			if (s.sales[i] > s.max)
				s.max = s.sales[i];
		}
	}
	void showSales(const Sales& s)
	{
		using namespace std;
		for (int i = 0; i < QUARTERS; i++) {
			cout << s.sales[i] << "   ";
		}
		cout << endl;
		cout << "Average: " << s.average << endl;
		cout << "Max: " << s.max << endl;
		cout << "Min: " << s.min << endl;
	}
}

//main.cpp
#include<iostream>
#include "sale.h"

using namespace std;

int main()
{
	using namespace SALES;
	Sales s[2];
	double arr[6] = { 25, 36.2, 45.3, 12.3, 75.2, 12.0 };
	setSales(s[0]);
	setSales(s[1], arr, 6);
	for (int i = 0; i < 2; i++) {
		showSales(s[i]);
		cout << "---------------------\n";
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定谔的喵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值