第九章编程练习答案
9.1根据以下头文件内容编写多文件程序,提示用户输入姓名的等级,存在结构中
//9.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:
// 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);
//golf.cpp
#include
#include
#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 << "输入姓名:";
string strTmp;
getline(cin, strTmp);
if ("" == strTmp)
return (0);
strcpy(g.fullname, strTmp.c_str());
cout << "输入等级:";
int n;
cin >> n;
cin.get();
if (!n)
return (0);
g.handicap = n;
return (1);
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
cout << g.fullname << '\t' << g.handicap;
}
//mian.cpp
#include
#include
#include "golf.h"
using namespace std;
int main ()
{
const unsigned k_size = 4;
golf golfers[k_size];
unsigned numGolfers = 0;
while (numGolfers < k_size && setgolf(golfers[numGolfers]))
++numGolfers;
for (unsigned i = 0; i < numGolfers; ++i) {
showgolf(golfers[i]);
cout << endl;
}
}
9.2修改程序清单9.9,使用string类型代替字符数组
//9.2修改程序清单9.9,使用string类型代替字符数组
#include
#include
using namespace std;
int main()
{
string input;
unsigned total = 0;
cout << "Enter a line:\n";
getline(cin, input);
while (cin && "" != input)
{
cout << "\"" << input <<"\" contains ";
cout << input.size() << " characters\n";
total += input.size();
cout << total << " characters total\n";
cout << "Enter next line (empty line to quit):\n";
getline(cin, input);
}
cout << "Bye" << endl;
}
9.3对结构体chaff,在函数中用new为含两个结构体的数组分配空间
//9.3对结构体chaff,在函数中用new为含两个结构体的数组分配空间
#include
#include
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
int main ()
{
chaff* Chaff = new chaff [2];
strcpy(Chaff[0].dross, "foo");
Chaff[0].slag = 2;
strcpy(Chaff[1].dross, "bar");
Chaff[1].slag = 8;
for (unsigned i = 0; i < 2; ++i)
cout << Chaff[i].dross << '\t' << Chaff[i].slag << endl;
delete [] Chaff;
}
9.4根据以下头文件内容,编写完成多文件程序
//9.4根据以下头文件内容,编写完成多文件程序
//sale.h
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 structure s
void showSales(const Sales & s);
}
//sale.cpp
#include
#include "Sales.h"
using namespace std;
namespace SALES
{
static double calcAverage (double arr[], unsigned arrSize)
{
double sum = 0;
for (unsigned i = 0; i < arrSize; ++i)
sum += arr[i];
return (sum / arrSize);
}
static double calcMax (double arr[], unsigned arrSize)
{
unsigned idxMax = 0;
for (unsigned i = 0; i < arrSize; ++i)
if (arr[i] > arr[idxMax])
idxMax = i;
return (arr[idxMax]);
}
static double calcMin (double arr[], unsigned arrSize)
{
unsigned idxMin = 0;
for (unsigned i = 0; i < arrSize; ++i)
if (arr[i] < arr[idxMin])
idxMin = i;
return (arr[idxMin]);
}
void setSales (Sales & s, const double ar[], int n)
{
unsigned times = n < QUARTERS ? (unsigned)n : QUARTERS;
for (unsigned i = 0; i < times; ++i)
s.sales[i] = ar[i];
for (unsigned i = times; i < QUARTERS; ++i)
s.sales[i] = 0;
s.average = calcAverage(s.sales, times);
s.max = calcMax(s.sales, times);
s.min = calcMin(s.sales, times);
}
void setSales(Sales & s)
{
cout << "输入" << QUARTERS << "个销售记录:";
unsigned times = QUARTERS;
double arr[QUARTERS] = {0};
for (unsigned i = 0; i < QUARTERS; ++i) {
cin >> arr[i];
if (!cin) {
times = i;
break;
}
}
setSales(s, arr, QUARTERS);
s.average = calcAverage(s.sales, times);
s.max = calcMax(s.sales, times);
s.min = calcMin(s.sales, times);
}
void showSales (const Sales & s)
{
cout << "sales: ";
for (const auto& e : s.sales)
cout << e << ' ';
cout << endl;
cout << "average: " << s.average << endl;
cout << "max: " << s.max << endl;
cout << "min: " << s.min << endl;
}
}
//main.cpp
#include
#include "sales.h"
using namespace std;
int main ()
{
using namespace SALES;
Sales salesBook;
double salesLst[] = {12.2, 11.16, 10.61, 16.24, 11.53};
setSales(salesBook, salesLst, sizeof(salesLst)/sizeof(salesLst[0]));
showSales(salesBook);
Sales salesPen;
setSales(salesPen);
showSales(salesPen);
}