1.下面是一个头文件:
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
//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);
//function solicits name and handicap from user
//and sets the member of g to the values entered
//returns 1 if name is enteren, 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.h
#pragma once
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
//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);
//function solicits name and handicap from user
//and sets the member of g to the values entered
//returns 1 if name is enteren, 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 "golf.h"
#include <cstring>
#include <iostream>
using namespace std;
void setgolf(golf& g, const char* name, int hc)
{
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf& g)
{
int temp = 1;
cout << "The fullname is:";
cin.getline(g.fullname, Len);
if (strcmp(g.fullname, "") == 0)
{
temp = 0;
return temp;
}
else
{
cout << "The handicap is:";
cin >> g.handicap;
cin.get();//消耗回车
return temp;
}
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
cout << "The name of golf is:" << g.fullname << endl;
cout << "The handicap of golf is:" << g.handicap << endl;
}
//main.cpp
const int SIZE = 2;
int main(void)
{
golf g[SIZE];
int count = 0;
cout << "Enter the information of golf players:" << endl;
while ( (count < SIZE) && (setgolf(g[count])) )
{
cout << "Enter the next information of golf player:" << endl;
count++;
}
cout << "\nShow all the golf players informations:" << endl;
for (int i = 0; i < count; i++)
{
showgolf(g[i]);
}
cout << "\nReset all the golf players informations:" << endl;
for (int i = 0; i < count; i++)
{
handicap(g[i], 90);
showgolf(g[i]);
}
return 0;
}
2.修改程序清单9.9:用string对象代替字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串“”进行比较,以判断是否为空行。
void strcount(const string str);
int main(void)
{
string input;
string next;
cout << "Enter a line:" << endl;
getline(cin,input);
while (input != "")
{
strcount(input);
cout << "Enter next line(empty line to quit):" << endl;
getline(cin, input);
}
cout << "Bye!" << endl;
return 0;
}
void strcount(const string str)
{
static int total = 0;
int count = 0;
count = str.size();
total += count;
cout << count << " characters" << endl;
cout << total << " characters total" << endl;
}
3.下面是一个结构声明:
struct chaff
{
char dross[20];
int slag;
};
编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构体的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓冲区;另一种方法是使用常规new运算符来分配缓冲区。
struct chaff
{
char dross[20];
int slag;
};
const int BUF = 512;
char buffer[BUF];
void Show(const chaff&p);
int main(void)
{
chaff *point_ch1 = new chaff[2];
chaff *point_ch2 = new (buffer) chaff[2];
char dross[20];
int slag;
for (int i = 0; i < 2; i++)
{
cout << "#" << i + 1 << ": " << endl;
cout << "Enter the dross: ";
cin.getline(dross, 20);
cout << "Enter the slag: ";
cin >> slag;
cin.get();//消耗回车
strcpy(point_ch1[i].dross, dross);
strcpy(point_ch2[i].dross, dross);
point_ch1[i].slag = point_ch2[i].slag = slag;
}
cout << "\nShow all:" << endl;
for (int i = 0; i < 2; i++)
{
cout << "point_ch1[" << i + 1 << "]:" << endl;
Show(point_ch1[i]);
cout << "point_ch2[" << i + 1 << "]:" << endl;
Show(point_ch2[i]);
}
return 0;
}
void Show(const chaff& p)
{
cout << "The dross is:" << p.dross;
cout << "\t The slag is:" << p.slag;
cout << endl;
}
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 structure s
void ShowSales(const Sales& s);
}
第一个文件是一个头文件,其中包含名称空间;第二个文件是一个源代码文件,它对这个名称空间进行扩展,以提供这三个函数的定义;第三个文件声明两个Sales对象,并使用SetSales()的交互式版本为一个结构体赋值,然后使用SetSales()的非交互式版本为另一个结构体赋值。另外它还使用ShowSales()来显示这两个结构的内容。
实现:
Sales.h
#pragma once
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);
}
Sales.cpp
#include <iostream>
#include "sales.h"
using std::cin;
using std::cout;
using std::endl;
namespace SALES
{
void SetSales(Sales& s, const double ar[], int n)
{
double total = 0.0;
int i = 0;
for (; i < n && i < 4; ++i)
{
s.sales[i] = ar[i];
total += s.sales[i];
}
s.average = total / i;
s.max = s.sales[0];
s.min = s.sales[0];
for (int j = 1; j < i; ++j)
{
s.max = (s.max > s.sales[j]) ? s.max : s.sales[j];
s.min = (s.min < s.sales[j]) ? s.min : s.sales[j];
}
if (n < 4)
{
for (int k = n; k < 4; k++)
{
s.sales[k] = 0;
}
}
}
void SetSales(Sales& s)
{
double total = 0.0;
cout << "Enter 4 sales quarters:" << endl;
for (int i = 0; i < QUARTERS; i++)
{
cout << "#" << i + 1 << ":";
cin >> s.sales[i];
total += s.sales[i];
if (i == 0)
{
s.max = s.sales[i];
s.min = s.sales[i];
}
else
{
s.max = (s.max > s.sales[i]) ? s.max : s.sales[i];
s.min = (s.min < s.sales[i]) ? s.min : s.sales[i];
}
}
s.average = total / QUARTERS;
}
void ShowSales(const Sales& s)
{
cout << "The 4 sales are:";
for (int i = 0; i < QUARTERS; i++)
{
cout << s.sales[i] << " ";
}
cout << endl;
cout << "The average is:" << s.average << endl;
cout << "The max value is:" << s.max << endl;
cout << "The min value is:" << s.min << endl;
}
}
main.cpp
using namespace SALES;
int main(void)
{
double arr[4] = { 11.1, 22.2, 33.3, 44.4 };
Sales sales;
SetSales(sales, arr, 3);
ShowSales(sales);
SetSales(sales);
ShowSales(sales);
return 0;
}