(一一四)第九章编程练习
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;
}
}