当前位置: 首页 > news >正文

(一一四)第九章编程练习

1.下面是一个头文件:

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

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:

//  functionsolicits name and handicap from user

//  and sets the member of g to the values enterd

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

注意到setgolf()被重载,可以使用其第一个版本:

golf ann;

setgolf(ann, "Ann Birdfree", 24);

上述函数调用提供了存储在ann结构中的信息。可以这样使用其第二个版本;

golf andy;

setgolf(andy);

上述函数将提示用户输出姓名和等级,并将他们存储在andy结构中。这个函数可以(但不一定必须)在内部使用第一个版本

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

 

答:

//golf.h
#pragma once
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 member of g to the values enterd
//  returns 1 if name is entered, 0 if name is empty string
//函数请求结构的两个成员的数据,如果有,则返回1,如果是空的,返回0
int setgolf(golf & g);

//function resets handicap to new value
//函数更改结构的handicap成员的值为传递的参数
void handicap(golf & g, int hc);

//function displays contents of golf structure
//函数显示结构成员的内容
void showgolf(const golf & g);

//1.cpp
#include<iostream>
#include"golf.h"

int main()
{
	using namespace std;
	golf mm[10];
	cout << "请输入姓名和一个人的等级。若输入在姓名输入空字符串时(在输入名字的时候直接按回车键),则结束输入" << endl;
	cout << "1# 姓名:";
	cin.getline(mm[0].fullname, 39);
	cout << "等级:";
	cin >> mm[0].handicap;
	int i;
	cin.sync();
	for (i = 1;setgolf(mm[i - 1]);i++)
	{
		cout << i+1<<"# 姓名:";
		cin.getline(mm[i].fullname, 39);
		cout << "等级:";
		cin >> mm[i].handicap;
		cin.sync();
	}
	
	cout << "输入结束。" << endl << endl;
	if (!setgolf(mm[0]))
	{
		cout << "由于你并没有输入数据,程序结束。" << endl;
		system("pause");
		return 0;
	}
	cout << "现在显示输入结果:" << endl;
	for (i = 0;i < 10 && setgolf(mm[i]);i++)
	{
		showgolf(mm[i]);
	}

	cout << endl;
	cout << "现在将1#的等级改为200" << endl;
	handicap(mm[0], 200);
	showgolf(mm[0]);

	system("pause");
	return 0;
}

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

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

int setgolf(golf & g)
{
	if (g.fullname[0] == '\0')return 0;
	else return 1;
}

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

void showgolf(const golf & g)
{
	std::cout << "名字为:" << g.fullname << ", 等级为:" << g.handicap << std::endl;
}



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

 

答:


#include<iostream>
#include<string>

void strcount(const std::string str);

int main()
{
	using namespace std;
	string input;
	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";
	system("pause");
	return 0;
}
void strcount(const std::string str)
{
	using namespace std;
	static int total = 0;
	int count = str.length();//string类名.length()返回的是字符串长度。也可以用str.size(),效果是一样的
	total += count;
	cout << count << " characters\n";
	cout << total << " characters total\n";
}



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

struct chaff

{

char dross[20];

int flag;

};

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

 

答:


#include<iostream>

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

int main()
{
	using namespace std;

	chaff pp[5];
	chaff*x = new(pp)chaff[2];	//定位new运算符,把两个这样的结构放在pp的位置
	char *mm=new char[20];
	for (int i = 0;i < 2;i++)
	{
		cout << i + 1 << "# dross: ";
		cin.getline(mm, 19);
		strcpy_s(x[i].dross,mm);	//将输入的复制进去
		cout << "flag:  ";
		cin >> x[i].flag;
		cin.sync();
	}

	for (int i = 0;i < 2;i++)
	{
		cout << "dross:" << x[i].dross;
		cout << ", flag: " << x[i].flag << endl;
	}

	system("pause");
	return 0;
}



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

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 computes and stores the

// average, maximum, and minimum values of the entered items;

// 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 structures s

void showSales(const Sales & s);

}

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

 

答:

//1.h
#pragma once

namespace SALES
{
	const int QUARTERS = 4;
	struct Sales
	{
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	};
	//从ar数组里复制n或者4(取较小的)个成员到s结构中的sales成员。然后计算最大最小和平均。然后把sales成员剩下的成员,设置为0
	// copies the lesser of 4 or n items from the array ar
	// to the sales member of s and computes and stores the
	// average, maximum, and minimum values of the entered items;
	// remaining elements of sales, if any, set to 0
	void setSales(Sales & s, const double ar[], int n);

	//收集四季度的销售情况,存储他们进s的sales成员,然后计算,并存储到平均、最大、最小值
	// 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 structures s
	void showSales(const Sales & s);
}

//1.cpp
#include<iostream>
#include"1.h"

int main()
{
	using namespace std;
	using namespace SALES;
	Sales name[2];
	double x[3] = { 1.1,5.5,3.3 };
	setSales(name[0], x, 3);
	setSales(name[1]);
	
	for (int i = 0;i < 2;i++)
		showSales(name[i]);

	system("pause");
	return 0;
}

//2.cpp
#include<iostream>
#include"1.h"

namespace SALES	//把函数定义添加到名称空间之中
{
	void setSales(Sales & s, const double ar[], int n)
	{
		int i;
		for (i = 0;i < n && i < 4;i++)	//赋值
			s.sales[i] = ar[i];

		for (int j = i;j < 4;j++)	//将未赋值的设置为0
			s.sales[j] = 0;

		double total = 0;
		for (int j = 0;j < i;j++)	//total为所有有效值的总和
			total += s.sales[j];
		s.average = total / i;	//设置平均值

		s.max = s.min = s.sales[0];	//最大最小值初始化为第一个值

		for (int j = 0;j < i;j++)	//设置最大最小值
		{
			if (s.sales[j] > s.max)s.max = s.sales[j];
			if (s.sales[j] < s.min)s.min = s.sales[j];
		}

	}

	void setSales(Sales & s)
	{
		for (int i = 0;i < 4;i++)
		{
			s.sales[i] = rand() % 20 + 1;
		}
		int i = 4;
		double total = 0;
		for (int j = 0;j < i;j++)	//total为所有有效值的总和
			total += s.sales[j];
		s.average = total / i;	//设置平均值

		s.max = s.min = s.sales[0];	//最大最小值初始化为第一个值

		for (int j = 0;j < i;j++)	//设置最大最小值
		{
			if (s.sales[j] > s.max)s.max = s.sales[j];
			if (s.sales[j] < s.min)s.min = s.sales[j];
		}
	}

	void showSales(const Sales & s)
	{
		using namespace std;
		cout << "输出:" << endl;
		for (int i = 0;i < 4 && s.sales[i] != 0;i++)
			cout << "s.sales[" << i << "] = " << s.sales[i] << endl;
		cout << "average = " << s.average << endl;
		cout << "max = " << s.max << endl;
		cout << "min = " << s.min << endl;

	}
}


相关文章:

  • 在windows下安装pandas
  • Debugview使用方法
  • 生成不重复的随机数(待完善)
  • 登录流程-1
  • C#通过指针读取文件
  • C# 基础数据与Byte
  • mysql 分页sql
  • SQL Server 2008数据库邮件配置及应用
  • vim 强大复制链接
  • nginx+fpm报499错误的解决方案
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • Linux curl命令
  • 程序员眼中的英文单词是这样的
  • VMware Horizon view打印机出现多个副本
  • 十年工作总结(一)
  • ES6指北【2】—— 箭头函数
  • [case10]使用RSQL实现端到端的动态查询
  • 【译】理解JavaScript:new 关键字
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • Angular数据绑定机制
  • CentOS 7 修改主机名
  • Create React App 使用
  • Date型的使用
  • Java多态
  • jquery ajax学习笔记
  • Js基础知识(一) - 变量
  • js算法-归并排序(merge_sort)
  • leetcode-27. Remove Element
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • Shadow DOM 内部构造及如何构建独立组件
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • Vue2.0 实现互斥
  • 工作手记之html2canvas使用概述
  • 将回调地狱按在地上摩擦的Promise
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 使用docker-compose进行多节点部署
  • 由插件封装引出的一丢丢思考
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • 通过调用文摘列表API获取文摘
  • ​2021半年盘点,不想你错过的重磅新书
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #Spring-boot高级
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (06)金属布线——为半导体注入生命的连接
  • (10)ATF MMU转换表
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (java)关于Thread的挂起和恢复
  • (附源码)ssm码农论坛 毕业设计 231126
  • (附源码)计算机毕业设计大学生兼职系统
  • (原)本想说脏话,奈何已放下
  • (转)Oracle存储过程编写经验和优化措施
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • ***测试-HTTP方法