小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. C/C++
  5. 正文

C++ Primer Plus(第五版)第9章编程题答案

2018年11月14日 1635点热度 0人点赞 1条评论

1. 下面是一个头文件:

(……省略……)

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

golf.cpp

#include<iostream>
#include <string>
#include <cstring>
#include"golf.h"
using namespace std;
void setgolf(golf & g, const char *name, int hc)
{
	strcpy(g.fullname, name);
	g.handicap = hc;
}
int setgolf(golf & g)
{
	cout << "Please enter the full name of golf player: ";
	cin.getline(g.fullname, Len);
	if (strcmp(g.fullname, "") == 0)
	{
		return 0;
	}

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

	return 1;
}

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

void showgolf(const golf & g)
{
	cout << "Here is the golf contents:\n";
	cout << "Name: " << g.fullname << endl;
	cout << "Handicap: " << g.handicap << endl;
}

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

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int ArSize = 10;
void strcount(const string & str);

int main() {

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


void strcount(const string & str){

	using namespace std;
	static int total = 0;
	int count = 0;
	cout << "\"" << str << "\" contains ";
	count = str.length();
	total += count;
	cout << count << " characters\n";
	cout << total << " characters total\n";
}

3. 下面是一个结构声明:

struct chaff

{

char dross[20];

int slag;

};

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

#include<iostream>
#include<new>
#include<cstring>
using namespace std;

struct chaff
{
	char dross[20];
	int slag;
};
void show_struct(chaff & p);
char buffer2[500];
int main()
{
	chaff *p = new(buffer2) chaff[2];
	strcpy(p[0].dross, "Yushuai Zhang");
	strcpy(p[1].dross, "Shandong Province");
	p[0].slag = 23;
	p[1].slag = 100;
	for (int i = 0; i < 2; i++)
	{
		show_struct(p[i]);
	}

	system("pause");
	return 0;
}

void show_struct(chaff & p)
{
	cout << "Dross: " << p.dross << endl;
	cout << "SLAG: " << p.slag << endl;
	cout << endl;
}

4. 请基于下面这个名称空间编写一个由3个文件组成的程序:

(……省略……)

第一个文件是一个头文件,其中包含名称空间;第二个文件是一个源代码文件,它对这个名称空间进行扩展,以提供这三个函数的定义;第三个文件声明两个Sales对象,并使用setSales()的交互式版本为一个结构提供值,然后使用setSales()的非交互式版本为另一个结构提供值。另外它还使用showSales()来显示这两个结构的内容。

exercise4.h

#pragma once
#ifndef SALES_H_
#define SALES_H_
namespace SALES {
	const int QUARTERS = 4;
	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};
	void setSales(Sales &s);
	void showSales(const Sales &s);
}
#endif

exercise4_fun.cpp

#include<iostream>
#include "exercise4.h"
namespace SALES
{
	void setSales(Sales & s)
	{
		using std::cin;
		using std::cout;
		using std::endl;

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

		// get average, max, min
		double sum = 0.0;
		double max = s.sales[0], min = s.sales[0];
		for (int i = 0; i < QUARTERS; ++i)
		{
			double cur = s.sales[i];
			if (cur > max)
				max = cur;
			if (cur < min)
				min = cur;
			sum += cur;
		}
		s.average = sum / (float)QUARTERS;
		s.max = max;
		s.min = min;
	}

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

		// show sales
		cout << "SALES: ";
		for (int i = 0; i < QUARTERS; ++i)
			cout << s.sales[i] << " ";
		cout << endl;

		// show average
		cout << "AVERAGE: " << s.average << endl;

		// show max and min
		cout << "MAX: " << s.max << endl;
		cout << "MIN: " << s.min << endl;
	}
}

exercise4_main.cpp

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

int main(void)
{
	using namespace SALES;
	Sales s1;
	setSales(s1);
	showSales(s1);
	system("pause");
	return 0;
}

备注:

操作系统:Windows 10 x64(1803)

编译器:Microsoft Visual Studio 2015

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: C++学习笔记
最后更新:2018年11月14日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程小奥看房之鸿荣源珈誉府论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
再忆陈毅中学 新青年报[New Youth]第五期发布! 已修:数据结构【浙江大学】[2018-06-22] 生活点滴0912-0919:高中学习生活的第一周 基于WebRTC的单通道语音增强系统发布第二个版本 请你们不要太嚣张!
标签聚合
Python linux Java leetcode 高中 python学习 学习 算法 鸟哥的linux私房菜 生活
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 8 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号