1. 下面是一个头文件:
(……省略……)
根据这个头文件,创建一个多文件程序。其中的一个文件名为golf.cpp,它提供了与头文件中的原型匹配的函数定义;另一个文件应包含main(),并演示原型化函数的所有特性。例如,包含一个让用户输入的循环,并使用输入的数据来填充一个由golf结构组成的数组,数组被填满或用户将高尔夫选手的姓名设置为空字符串时,循环将结束。main()函数只使用头文件中原型化的函数来访问golf结构。
golf.cpp
#include<iostream>
#include <string>
#include <cstring>
#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 << "Please enter the full name of golf player: ";
cin.getline(g.fullname, Len);
if (strcmp(g.fullname, "") == 0)
{
return 0;
}
cout << "Please enter the hanicap of golf player: ";
cin >> g.handicap;
cin.get();
return 1;
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
cout << "Here is the golf contents:\n";
cout << "Name: " << g.fullname << endl;
cout << "Handicap: " << g.handicap << endl;
}
2. 修改程序清单9.8:用string对象代替字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串“”进行比较,以判断是否为空行。
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int ArSize = 10;
void strcount(const string & str);
int main() {
string str;
cout << "Enter a line: \n";
getline(cin, str);
while (str != "") {
strcount(str);
cout << "Enter next line(empty line to quit): \n";
getline(cin, str);
}
cout << "Bye\n";
system("pause");
return 0;
}
void strcount(const string & str){
using namespace std;
static int total = 0;
int count = 0;
cout << "\"" << str << "\" contains ";
count = str.length();
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
3. 下面是一个结构声明:
struct chaff
{
char dross[20];
int slag;
};
编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓冲区;另一种方法是使用常规new运算符来分配缓冲区。
#include<iostream>
#include<new>
#include<cstring>
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
void show_struct(chaff & p);
char buffer2[500];
int main()
{
chaff *p = new(buffer2) chaff[2];
strcpy(p[0].dross, "Yushuai Zhang");
strcpy(p[1].dross, "Shandong Province");
p[0].slag = 23;
p[1].slag = 100;
for (int i = 0; i < 2; i++)
{
show_struct(p[i]);
}
system("pause");
return 0;
}
void show_struct(chaff & p)
{
cout << "Dross: " << p.dross << endl;
cout << "SLAG: " << p.slag << endl;
cout << endl;
}
4. 请基于下面这个名称空间编写一个由3个文件组成的程序:
(……省略……)
第一个文件是一个头文件,其中包含名称空间;第二个文件是一个源代码文件,它对这个名称空间进行扩展,以提供这三个函数的定义;第三个文件声明两个Sales对象,并使用setSales()的交互式版本为一个结构提供值,然后使用setSales()的非交互式版本为另一个结构提供值。另外它还使用showSales()来显示这两个结构的内容。
exercise4.h
#pragma once
#ifndef SALES_H_
#define SALES_H_
namespace SALES {
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales &s);
void showSales(const Sales &s);
}
#endif
exercise4_fun.cpp
#include<iostream>
#include "exercise4.h"
namespace SALES
{
void setSales(Sales & s)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter sales:" << endl;
for (int i = 0; i < QUARTERS; ++i)
{
cout << "sales[" << i << "]: ";
cin >> s.sales[i];
}
// get average, max, min
double sum = 0.0;
double max = s.sales[0], min = s.sales[0];
for (int i = 0; i < QUARTERS; ++i)
{
double cur = s.sales[i];
if (cur > max)
max = cur;
if (cur < min)
min = cur;
sum += cur;
}
s.average = sum / (float)QUARTERS;
s.max = max;
s.min = min;
}
void showSales(const Sales & s)
{
using std::cout;
using std::endl;
// show sales
cout << "SALES: ";
for (int i = 0; i < QUARTERS; ++i)
cout << s.sales[i] << " ";
cout << endl;
// show average
cout << "AVERAGE: " << s.average << endl;
// show max and min
cout << "MAX: " << s.max << endl;
cout << "MIN: " << s.min << endl;
}
}
exercise4_main.cpp
#include<iostream>
#include "exercise4.h"
int main(void)
{
using namespace SALES;
Sales s1;
setSales(s1);
showSales(s1);
system("pause");
return 0;
}
备注:
操作系统:Windows 10 x64(1803)
编译器:Microsoft Visual Studio 2015


文章评论