《C++ Primer Plus》(第6版)第9章编程练习
- 《C++ Primer Plus》(第6版)第9章编程练习
- 1. 打印字符串
- 2. 修改程序清单9.9
- 3. chaff
- 4. sales
《C++ Primer Plus》(第6版)第9章编程练习
1. 打印字符串
下面是一个头文件:
//golf.h
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
void setgolf(golf &g, const char *name, int hc);
int setgolf(golf &g);
void handicap(golf &g, int hc);
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.cpp:
#include "golf.h"
#include <iostream>
#include <cstring>
using namespace std;
void setgolf(golf &g, const char *name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf &g)
{
cout << "Please enter the full name of player: ";
cin.getline(g.fullname, Len);
if (strcmp(g.fullname, "") == 0)
return 0;
cout << "Please enter the handicap of player: ";
cin >> g.handicap;
return 1;
}
void handicap(golf &g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf &g)
{
cout << "Information:" << endl;
cout << "Name: " << g.fullname << endl;
cout << "handicap: " << g.handicap << endl;
}
main.cpp:
#include "golf.h"
#include <iostream>
using namespace std;
int main()
{
golf ann;
setgolf(ann, "Ann Birdfree", 24);
showgolf(ann);
golf andy;
setgolf(andy);
showgolf(andy);
handicap(andy, 30);
showgolf(andy);
system("pause");
return 0;
}
运行结果:
2. 修改程序清单9.9
修改程序清单9.9:用string对象代替字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串“”进行比较,以判断是否为空行。
代码:
#include <iostream>
#include <cstring>
using namespace std;
// const int ArSize = 10;
void strcount(const string str);
int main()
{
string input;
char next;
cout << "Enter a line:\n";
getline(cin, input);
while (cin)
{
strcount(input);
cout << "Enter next line (empty line to quit):\n";
getline(cin, input);
if (input == "")
break;
}
cout << "Bye!\n";
system("pause");
return 0;
}
void strcount(const string str)
{
static int total = 0;
cout << "\"" << str << "\" contains ";
total += str.size();
cout << str.size() << " characters\n";
cout << total << " characters total\n";
}
运行结果:
3. chaff
代码:
#include <iostream>
#include <cstring>
#include <new>
using namespace std;
const int N = 2;
const int BUF = 512;
char buffer[BUF];
struct chaff
{
/* data */
char dross[20];
int slag;
};
int main()
{
chaff *pc1, *pc2;
pc1 = new chaff[N]; // use heap
pc2 = new (buffer) chaff[N]; // use buffer array
char d[20];
// test *pc1
for (int i = 0; i < N; i++)
{
cout << "Enter dross of #" << i + 1 << " chaff: ";
cin.getline(d, 20);
strcpy_s(pc1[i].dross, d);
cout << "Enter slag of #" << i + 1 << " chaff: ";
cin >> pc1[i].slag;
cin.get();
}
for (int i = 0; i < N; i++)
{
cout << "chaff #" << i + 1 << ":\n";
cout << "dross: " << pc1[i].dross << endl;
cout << "slag: " << pc1[i].slag << endl;
}
// test *pc2
for (int i = 0; i < N; i++)
{
cout << "Enter dross of #" << i + 1 << " chaff: ";
cin.getline(d, 20);
strcpy_s(pc2[i].dross, d);
cout << "Enter slag of #" << i + 1 << " chaff: ";
cin >> pc2[i].slag;
cin.get();
}
for (int i = 0; i < N; i++)
{
cout << "chaff #" << i + 1 << ":\n";
cout << "dross: " << pc2[i].dross << endl;
cout << "slag: " << pc2[i].slag << endl;
}
system("pause");
return 0;
}
运行结果:
4. sales
请基于下面这个名称空间编写一个由3个文件组成的程序:
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales& s, const double ar[], int n);
void setSales(Sales& s);
void showSales(const Sales& s);
}
第一个文件是一个头文件,其中包含名称空间;第二个文件是一个源代码文件,它对这个名称空间进行扩展,以提供这三个函数的定义;第三个文件声明两个Sales对象,并使用setSales()的交互式版本为一个结构提供值,然后使用setSales()的非交互式版本为另一个结构提供值。另外它还使用showSales()来显示这两个结构的内容。
代码:
sales.cpp:
#include "sales.h"
#include <iostream>
using namespace std;
namespace SALES
{
void setSales(Sales &s, const double ar[], int n) // 非交互式版本
{
if (n < 4)
{
for (int i = 0; i < n; i++)
s.sales[i] = ar[i];
for (int j = n; j < 4; j++)
s.sales[j] = 0;
}
else
{
for (int i = 0; i < 4; i++)
s.sales[i] = ar[i];
}
s.average = (s.sales[0] + s.sales[1] + s.sales[2] + s.sales[3]) / 4;
double max = 0.0;
double min = 1000000;
for (int i = 0; i < 4; i++)
{
if (s.sales[i] > max)
max = s.sales[i];
if (s.sales[i] < min)
min = s.sales[i];
}
s.max = max;
s.min = min;
}
void setSales(Sales &s) // 交互式版本
{
cout << "Enter sales:\n";
for (int i = 0; i < 4; i++)
{
cout << "The #" << i + 1 << " quarter is: ";
cin >> s.sales[i];
}
s.average = (s.sales[0] + s.sales[1] + s.sales[2] + s.sales[3]) / 4;
double max = 0.0;
double min = 1000000;
for (int i = 0; i < 4; i++)
{
if (s.sales[i] > max)
max = s.sales[i];
if (s.sales[i] < min)
min = s.sales[i];
}
s.max = max;
s.min = min;
}
void showSales(const Sales &s)
{
cout << "The sales of 4 quarters are $" << s.sales[0] << ", $" << s.sales[1] << ", $" << s.sales[2] << ", $" << s.sales[3] << endl;
cout << "Average:" << s.average << endl;
cout << "Max:" << s.max << endl;
cout << "Min:" << s.min << endl;
}
}
main.cpp:
#include "sales.h"
#include <iostream>
using namespace SALES;
using namespace std;
int main()
{
Sales s1, s2;
cout << "#1:" << endl;
setSales(s1);
showSales(s1);
cout << "--------------------------------------------------" << endl;
cout << "#2:" << endl;
double ar[3] = {10, 20, 30};
setSales(s2, ar, 3);
showSales(s2);
system("pause");
return 0;
}
运行结果: