第九章 编程练习
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);
//interative 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);
main()演示原型化函数的所有特性。例如,包含一个让用户输入循环,并使用输入的数据来填充一个由golf结构组成的数组,数组被填满或用户将高尔夫选手的姓名设置为空字符串时,循环将结束。main()函数只使用头文件中原型化的函数来访问golf结构。
头文件golf.h
//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);
//interative 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);
源文件1:
//golf.cpp --匹配头文件中的原型函数
#include<iostream>
#include<cstring>
#include"golf.h"
void setgolf(golf &g,const char * name,int hc)
{
strcpy(g.fullname,name);
g.handicap=hc;
}
int setgolf(golf & g)
{
using namespace std;
int t;
cout<<"请输入姓名:";
cin.get(g.fullname,Len);
if (strlen(g.fullname)==0)
return 0;
else
{
cin.get();
cout<<"请输入等级:";
cin>>g.handicap;
cin.get();
return 1;
}
}
void handicap(golf & g ,int hc)
{
g.handicap=hc;
}
void showgolf(const golf & g)
{
using namespace std;
cout<<"姓名:\t"<<g.fullname<<endl;
cout<<"等级:\t"<<g.handicap<<endl;
}
源文件2:
//main()演示原型化函数的所有特性。例如,包含一个让用户输入循环,并使用输入的数据来填充一个由golf结构组成的数组,
//数组被填满或用户将高尔夫选手的姓名设置为空字符串时,循环将结束。
//main()函数只使用头文件中原型化的函数来访问golf结构。
#include<iostream>
#include"golf.h"
int main()
{
using namespace std;
const int size =5;
golf array[size];
int num =0;
int setgolf_return;
for (int i=0;i<size;i++)
{
setgolf_return=setgolf(array[i]);
if (setgolf_return==0)
break;
else
num +=setgolf_return;
}
for (int i=0;i<num;i++)
{
cout<<"第"<<i+1<<"个"<<endl;
showgolf(array[i]);
}
handicap(array[0],0);
cout<<"修改第1个的handicap为0后:\n";
for (int i=0;i<num;i++)
{
cout<<"第"<<i+1<<"个"<<endl;
showgolf(array[i]);
}
setgolf(array[0],"Ann Birdfree",24);
cout<<"修改第1个的fullname和handicap后:\n";
for (int i=0;i<num;i++)
{
cout<<"第"<<i+1<<"个"<<endl;
showgolf(array[i]);
}
return 0;
}
2.修改程序清单9.9:用string对象代替字符数组。这样该程序将不再需要检查输入的字符串是否过长,同时可以输入字符串同字符串“”进行比较,以判断是否为空行。
//修改程序清单9.9:用string对象代替字符数组。
//这样该程序将不再需要检查输入的字符串是否过长,同时可以输入字符串同字符串“”进行比较,以判断是否为空行。
#include<iostream>
#include<string>
using namespace std;
// function prototype
void strcount(const string str);
int main()
{
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";
// code to keep window open for MSVC++
/*
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/
return 0;
}
void strcount(const string str)
{
static int total = 0; // static local variable
int count = 0; // automatic local variable
cout << "\"" << str <<"\" contains ";
while (str[count]) // go to end of string
count++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
3.编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓存区;另一种方法是使用常规new运算符来分配缓冲区。
//编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。
//然后,给结构的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。
//一种方法是像程序清单9.10那样将一个静态数组用作缓存区;另一种方法是使用常规new运算符来分配缓冲区。
#include<iostream>
#include<new>
#include<string>
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
int main()
{
char buffer[512];
cout << "buffer的地址为:\t" << (void *)buffer << endl;
chaff * p1 = new (buffer) chaff[2];
cout << "p1的地址为:\t" << p1 << endl;
strcpy(p1[0].dross, "abc");
p1[0].slag = 1;
strcpy(p1[1].dross, "def");
p1[1].slag = 2;
for (int i = 0; i < 2; i++)
{
cout << "p1[" << i << "] at " << &p1[i] << endl;
cout << "dross:\t" << p1[i].dross << endl;
cout << "slag:\t" << p1[i].slag << endl;
}
cin.get();
return 0;
}
4.头文件
//包含名称空间
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);
}
源文件1:
//对名称空间进行扩展,以提供这三个函数的定义
#include<iostream>
#include"sales.h"
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])/QUARTERS;
double max=s.sales[0];
for (int i=1;i<QUARTERS;i++)
{
if (max<s.sales[i])
max=s.sales[i];
}
s.max=max;
double min=s.sales[0];
for (int i=1;i<QUARTERS;i++)
{
if (min>s.sales[i])
min=s.sales[i];
}
s.min=min;
}
void setSales(Sales &s)
{
for(int i=0;i<QUARTERS;i++)
{
cout<<"请输入第"<<i+1<<"季度的销售额:";
cin>>s.sales[i];
cin.get();
}
s.average=(s.sales[0]+s.sales[1]+s.sales[2]+s.sales[3])/QUARTERS;
double max=s.sales[0];
for (int i=1;i<QUARTERS;i++)
{
if (max<s.sales[i])
max=s.sales[i];
}
s.max=max;
double min=s.sales[0];
for (int i=1;i<QUARTERS;i++)
{
if (min>s.sales[i])
min=s.sales[i];
}
s.min=min;
}
void showSales(const Sales & s)
{
cout<<"display all information in structure s:\n";
for (int i=0;i<QUARTERS;i++)
cout<<"the "<<i+1<<"quarters is "<<s.sales[i]<<endl;
cout<<endl<<"the average:\t"<<s.average<<endl;
cout<<"the max:\t"<<s.max<<endl;
cout<<"the min:\t"<<s.min<<endl;
}
}
源文件2:
//声明两个Sales对象,并使用setSales()的交互式版本为一个结构提供值,然后使用setSales()的非交互式版本为另一个结构提供值。
//使用showSales()来显示这两个结构的内容。
#include<iostream>
#include"sales.h"
using namespace std;
using namespace SALES;
int main()
{
Sales object1;
Sales object2;
cout<<"The following is the first sales' information:\n";
setSales(object1);
showSales(object1);
cout<<endl<<"The following is the second sales' information:\n";
double ar[3]={12.5,55.2,60.7};
setSales(object2, ar, 3);
showSales(object2);
cout<<endl;
cin.get();
return 0;
}