1. 编写三个文件
注意:记得运用C风格字符串函数:strcpy()、strcmp(),以及如何比较字符串和空字符串。
// golf.h -- for pe9-1.cpp
const int Len = 40;
struct golf {
char fullname[Len];
int handicap;
};
// 重载形式1:输入并设置结构golf中的参数,无返回值,非交互形式
void setgolf(golf& g, const char* name, int hc);
// 重载形式2:输入并设置结构golf中的参数,一个返回值:int,交互形式
// 若输入名字,返回1;否则返回0
int setgolf(golf& g);
// 重新设置handicap值
void handicap(golf& g, int hc);
// 展示golf结构中的参数
void showgolf(const golf& g);
#include <iostream>
#include <cstring>
#include "golf.h"
// 重载形式1:输入并设置结构golf中的参数,无返回值,非交互形式
void setgolf(golf& g, const char* name, int hc) {
strcpy(g.fullname, name); // 复制字符串函数
g.handicap = hc;
}
// 重载形式2:输入并设置结构golf中的参数,一个返回值:int,交互形式
// 若输入名字,返回1;否则返回0
int setgolf(golf& g) {
using std::cin;
using std::cout;
using std::endl;
cout << "Enter the name: ";
cin.getline(g.fullname, Len);
if (strcmp(g.fullname, "") == 0)
return 0;
cout << "Enter the handicap: ";
cin >> g.handicap;
cin.get();
return 1;
}
// 重新设置handicap值
void handicap(golf& g, int hc) {
g.handicap = hc;
}
// 展示golf结构中的参数
void showgolf(const golf& g) {
using std::cout;
using std::endl;
cout << "The name is " << g.fullname;
cout << " and the candicap is " << g.handicap << endl;
}
// 9-1
#include <iostream>
#include "golf.h"
const int Num = 5;
int main() {
golf my_golf[Num];
int if_over = 0;
int i;
for (i = 0; i < Num; i++) {
if_over = setgolf(my_golf[i]);
if (! if_over) break;
std::cout << "Enter next golf player: \n";
}
std::cout << "Show all of golf player's information: \n";
for (int j = 0; j < i; j++)
showgolf(my_golf[j]);
return 0;
}
2. 修改程序清单9.9:用string对象代替字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串“”进行比较,以判断是否为空行。
程序清单9.9:
// 程序清单9.9
#include <iostream>
const int ArSize = 10;
void strcount(const char* str);
int main() {
using std::cin;
using std::cout;
using std::endl;
char input[ArSize];
char next;
cout << "Enter a line: \n";
cin.get(input, ArSize);
while (cin) {
cin.get(next);
while (next != '\n')
cin.get(next);
strcount(input);
cout << "Enter next line (empty line to quit): \n";
cin.get(input, ArSize); //当输入空行时,get(char*, int)会置错误位,while(cin)即while(false)
}
cout << "Bye.\n";
return 0;
}
void strcount(const char* str) {
using std::cin;
using std::cout;
using std::endl;
static int total = 0; // static静态变量,可以用于记录函数被定义了几次
int count = 0;
cout << "\"" << str << "\" contains ";
while (*str++)
count++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
9-2解:
// 9-2
#include <iostream>
#include <string>
using std::string;
void strcount(const string str);
int main() {
using std::cin;
using std::cout;
using std::endl;
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); //当输入空行时,get(char*, int)会置错误位,while(cin)即while(false)
}
cout << "Bye.\n";
return 0;
}
void strcount(const string str) {
using std::cin;
using std::cout;
using std::endl;
static int total = 0; // static静态变量,可以用于记录函数被定义了几次
int count = str.length();
cout << "\"" << str << "\" contains ";
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
3. 编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构的成员赋值(对于char数组,使用strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓冲区;另一种方法是使用常规new运算符来分配缓冲区。
//9-3
#include <iostream>
struct chaff {
char dross[20];
int slag;
};
char buffer[1024]; //法一:使用静态数组作为缓冲区
int main() {
using std::cin;
using std::cout;
using std::endl;
chaff* c_ptr = new (buffer) chaff[2];
char* pc = new char[1024]; //法二:new运算符分配缓冲区
chaff* c_ptr2 = new (pc) chaff[2];
char dross[20] = { 0 };
int slag = 0;
for (int i = 0; i < 2; i++) {
cout << "Enter dross of #" << i << " chaff: " << endl;
cin.getline(dross, 20);
cout << "Enter slag of #" << i << " chaff: " << endl;
cin >> slag;
cin.get();
strcpy(c_ptr[i].dross, dross);
strcpy(c_ptr2[i].dross, dross);
c_ptr[i].slag = c_ptr2[i].slag = slag;
}
cout << "Memory address: \n" << "static: " << (void*)buffer << endl;
for (int i = 0; i < 2; i++) {
cout << c_ptr[i].dross << " at " << &c_ptr[i].dross << ";";
cout << c_ptr[i].slag << " at " << &c_ptr[i].slag << endl;
}
cout << "Memory address: \n" << "heap: " << c_ptr2 << endl;
for (int i = 0; i < 2; i++) {
cout << c_ptr2[i].dross << " at " << &c_ptr2[i].dross << ";";
cout << c_ptr2[i].slag << " at " << &c_ptr2[i].slag << endl;
}
delete[] pc;
return 0;
}
4. 三个文件
// sales.h
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.cpp
#include <iostream>
#include "sales.h"
namespace SALES {
void setSales(Sales& s, const double ar[], int n) {
if (n <= 0) return;
s.sales[0] = ar[0];
double min = ar[0];
double max = ar[0];
double sum = ar[0];
int i;
for (i = 1; i < n && i < QUARTERS; i++) {
s.sales[i] = ar[i];
min = min > ar[i] ? ar[i] : min;
max = max < ar[i] ? ar[i] : max;
sum += ar[i];
}
while (i < QUARTERS) {
s.sales[i] = 0;
i++;
}
s.max = max;
s.min = min;
s.average = sum / (n * 1.0);
}
void setSales(Sales& s) {
using std::cout;
using std::cin;
using std::endl;
double temp;
cout << "Enter the #1 sales: ";
cin >> temp;
s.sales[0] = temp;
double min = temp;
double max = temp;
double sum = temp;
for (int i = 1; i < QUARTERS; i++) {
cout << "Enter the #" << i + 1 << ": ";
cin >> temp;
s.sales[i] = temp;
min = min > temp ? temp : min;
max = max > temp ? max : temp;
sum += temp;
}
s.max = max;
s.min = min;
s.average = sum / (QUARTERS * 1.0);
}
void showSales(const Sales& s) {
using std::cout;
using std::endl;
cout << "The 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 is " << s.max << endl;
cout << "The min is " << s.min << endl;
}
}
//9-4
#include <iostream>
#include "sales.h"
int main() {
using namespace SALES;
Sales sales1;
Sales sales2;
double ar[2] = { 1.4, 5.6 };
setSales(sales1, ar, 2);
setSales(sales2);
showSales(sales1);
showSales(sales2);
return 0;
}
|