C++ primer plus 第九章作业习题答案

C++ primer plus 第九章作业习题答案


自己编写的答案,希望大家一起研究

//golf.h    
#ifndef GOLF_H_
#define GOLF_H_


const int Len = 40;
struct golf
{
	char fullname[Len];
	int handicap;
};

//non-interactive version:
//function sets golf structure to provided name,handicap
//using values passed as arguments to the function
void setgolf(golf &g, const char * name, int hc);

//interactive version:
//function solicits name and handicap from user
//and sets the members of g to the values entered
//returns 1 if name is entered,0 if name is empty string 
int setgolf(golf &g);

//function resets handicap to new value
void handicap(golf & g, int hc);

//function displays contents of golf structure
void showgolf(const golf &g);
#endif // !GOLF_H_
//golf.cpp
#include<iostream>
#include"golf.h"
#include<cstring>

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

}


int setgolf(golf &g)
{
	char name[Len];
	int hc;
	std::cout << "请输入用户的名称" << std::endl;
	if (std::cin.getline(name, Len) && strlen(name) != 0)
	{
		std::cout << "请输入用户的等级" << std::endl;
		std::cin >> hc;
		std::cin.get();
		setgolf(g, name, hc);
		return 1;
	}
	else
	{
		return 0;
	}
}


void handicap(golf & g, int hc)
{
	std::cout << "将用户" << g.fullname << "的等级设为"<<hc<< std::endl;
	g.handicap = hc;
}

void showgolf(const golf &g)
{
	std::cout << "当前用户的名称:" << g.fullname<<std::endl;
	std::cout << "当前用户的等级:" << g.handicap << std::endl;

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

int main()
{
	golf test;
	golf test2[10];
	int test2_n = 0;
	char name1[Len] = "Harry";
	int hc1 = 5;
	setgolf(test, name1, hc1);
	for (int i = 0; i <= 10; i++,test2_n++)
	{
		if (!setgolf(test2[i]))
			break;
	}
	for (int i = 0;i<test2_n ; i++)
	{
		showgolf(test2[i]);
	}
	std::cout << "\n\n\n\n\nother:\n";
	showgolf(test);
	handicap(test, 10);
	showgolf(test);
	system("pause");
	return 0;

}

9.2

// main.cpp 
#include <iostream>
#include<string>
using namespace std;
// constants


// function prototype
void strcount(const string str);

int main()
{

	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";
	// code to keep window open for MSVC++
	
	cin.clear();
	while (cin.get() != '\n')
		continue;
	cin.get();
	
	return 0;
}

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

	cout << "\"" << str << "\" contains ";

	total =total+ str.length();
	cout << str.length() << " characters\n";
	cout << total << " characters total\n";
}

9.3方法一

#include<iostream>
#include<new>

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

int main()
{
	using namespace std;
	chaff * test = new(buffer) chaff[2];
	for (int i = 0; i < 2; i++)
	{
		cout << "请输入dross值\n";
		cin.getline((test+i)->dross, 20);
		cout << "请输入slag值\n";
		cin >> (test + i)->slag;
		cin.get();
	}
	for (int i = 0; i < 2; i++)
	{
		cout << test[i].dross << ' ' << test[i].slag << endl;
	}
	system("pause");
	return 0;
}

方法2

#include<iostream>

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

int main()
{
	using namespace std;
	chaff * test = new chaff[2];
	for (int i = 0; i < 2; i++)
	{
		cout << "请输入dross值\n";
		cin.getline((test+i)->dross, 20);
		cout << "请输入slag值\n";
		cin >> (test + i)->slag;
		cin.get();
	}
	for (int i = 0; i < 2; i++)
	{
		cout << test[i].dross << ' ' << test[i].slag << endl;
	}
	system("pause");
	return 0;
}

9.4

//sale.h
#pragma once
#include<iostream>

namespace SALES
{
	const int QUARTERS = 4;
	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};
	//copies the lesser of 4 or n items from the array ar
	//to the sales member of s and computers and stores the
	//average,maximum,and minium values of the entered item;
	//remaining elements of sales,if any,set to 0
	void setSales(Sales &s, const double ar[], int n);
	//gathers sales for 4 quarters interactively,stores them
	//in the sales member of s and computes and stores the
	//average,maximum,and minimum values
	void setSales(Sales & s);
	//display all information in structure s
	void showSales(const Sales &s);
}
//sale.cpp
#include <iostream>
#include"sale.h"

namespace SALES
{
	void setSales(Sales &s, const double ar[], int n)
	{
		int num = 4;
		s.average = 0;
		s.max = 0;
		s.min = 99999999;
		if (n <= 0)
			std::cout << "error";
		else if (n < 4)
			num = n;
		for (int i = 0; i < num; i++)
		{
			s.sales[i] = ar[i];
			s.average = s.average + ar[i];
			if (ar[i] > s.max)
				s.max = ar[i];
			if (ar[i] < s.min)
				s.min = ar[i];
		}
		s.average /= num;
	}
	void setSales(Sales & s)
	{
		double ar[4];
		std::cout << "请输入四个季度的销售额\n";
		for (int i = 0; i < 4; i++)
		{
			std::cout << "q"<<i+1<<":";
			std::cin >> ar[i];
			std::cin.get();
			std::cout << std::endl;
		}
		setSales(s, ar, 4);
	}


	void showSales(const Sales &s)
	{
		std::cout << "四个季度的销售额:\n";
		for (int i = 0; i < 4; i++)
		{
			std::cout << "q" << i+1 << ":"
				<< s.sales[i] << "   ";
		}
		std::cout << std::endl;
		std::cout << "其中最大值:" << s.max << std::endl;
		std::cout << "其中最小值:" << s.min << std::endl; 
		std::cout << "其中最平均值:" << s.average << std::endl;
	}
}
//main.cpp
#include <iostream>
#include"sale.h"

int main()
{
	using namespace SALES;
	using namespace std;
	Sales test1;
	Sales test2;
	double dddd[4] = { 455,785,553,7750 };
	setSales(test1, dddd, 4);
	setSales(test2);
	cout << "test1:\n";
	showSales(test1);
	cout << "test1:\n";
	showSales(test2);
	cin.get();
}
发布了19 篇原创文章 · 获赞 0 · 访问量 207

猜你喜欢

转载自blog.csdn.net/qq_42632671/article/details/100081056