一* 函数重载
//golf.h -- for pe9-1.cpp -- 头文件
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);
//peg9-1.cpp -- 源代码文件1
#include <iostream>
#include "golf.h"
int main() {
using std::cout;
using std::cin;
golf ann;
setgolf(ann, "Ann Birdfree", 24);
cout << "****** Example ******\n";
showgolf(ann);
golf andy;
label:cout << "\n**** Test****\n";
if (setgolf(andy)) {
cout << "\n****** PASS ******\n"
"How many people: ";
unsigned short n;
cin >> n;
if (n > 0) {
golf* arr = new golf[n];
for (int i = 0; i < n; ++i)
setgolf(arr[i]);
cout << "\n****** Information ******\n";
for (int i = 0; i < n; ++i)
showgolf(arr[i]);
delete[] arr;
cout << "\n****** Bye! ******\n";
}
}
else
goto label;
return 0;
}
//golf.cpp -- 源代码文件2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "golf.h"
非交互式版本,设置结构并提供成员信息,按引用传递
void setgolf(golf& g, const char* name, int hc) {
strcpy(g.fullname, name);
g.handicap = hc;
}
//交互式版本,向用户请求结构成员信息并设置,0名字为空,1名字输入
int setgolf(golf& g) {
using namespace std;
cout << "Name: ";
cin.ignore();
; cin.getline(g.fullname, Len);
if (strlen(g.fullname) != 0){
cout << "Score: ";
cin >> g.handicap;
cin.ignore();
return 1;
}
else
return 0;
}
//设置handicap的新值
void handicap(golf& g, int hc) {
g.handicap = hc;
}
//显示结构内容
void showgolf(const golf& g) {
std::cout << g.fullname<<"\t"<<g.handicap<<std::endl;
}
二* 静态存储
//static.cpp -- using a static local variable
#include <iostream>
#include <string>
void strcount(const std::string str);
int main() {
using namespace std;
string input;
cout << "Enter a line:\n";
getline(cin,input);
while (input != " ") {//是否空行
strcount(input);
cout << "Enter next line (empty line to quit):\n";
cin.ignore();
getline(cin, input);
}
cout << "Bye\n";
return 0;
}
void strcount(const std::string str) {
using namespace std;
static int total = 0;
int count = 0;
cout << "\"" << str << "\"contains ";
for(int i=0;str[i];++i)//是否为'\n'
count++;
total += count;
cout << count << " characters\n"
<< total << " characters total\n";
}
三* 定位new ?
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <new>
struct chaff {
char dross[20];
int slag;
};
int buffer[22];
int main() {
chaff* strstorge = new(buffer)chaff[2];
std::cout << "****** Dross | Slaf ******\n";
for (int i = 0; i < 2; ++i) {
std::cout << "Dross Name #"<<i+1<<": ";
char dro[20];
std::cin.getline(dro, 20);
strcpy(strstorge->dross, dro);
std::cout << "Dross buckets #" << i + 1 << ": ";
std::cin >> strstorge->slag;
std::cin.get();
}
std::cout << "****** Collection of Dross ******\n";
for (int i = 0; i < 2; ++i)
std::cout << strstorge->dross << "\t" << strstorge->slag << "\n";
delete[] strstorge;
return 0;
}
运行完出现如下断点:
四 待修改
//thing.h -- namsspace,prototype
namespace SALES {
const int QUARTERS = 4;
struct Sales {
double dales[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);
}
//thing.cpp -- definition
#include <iostream>
#include "thing.h"
namespace SALES {
//非交互式:存储4个季度的销售情况、平均值,最大,最小;没有设置为0
void setSales(Sales& s, const double ar[], int n) {
s.dales = ar;
double sum=0;
for (int i = 0; i < QUARTERS - 1; ++i) {
if (s.dales[i] > s.dales[i + 1]) {
s.max = s.dales[i];
s.min = s.dales[i + 1];
}
else {
s.max = s.dales[i + 1];
s.min = s.dales[i];
}
sum += s.dales[i];
}
sum += s.dales[QUARTERS-1];
s.average = sum / QUARTERS;
}
//交互式:收集并存储4个季度的销售情况
void setSales(Sales& s) {
using std::cout;
using std::cin;
cout << "Season #1: ";
cin >> s.dales[0];
for (int i = 1;i < QUARTERS;) {
if (s.dales[i] >= 0) {
cout << "Season #" << i << ": ";
cin >> s.dales[i];
++i;
}
else
continue;
}
}
//显示结构s的全部信息
void showSales(const Sales& s) {
for (int i = 0; i < QUARTERS; ++i)
std::cout << s.dales[i];
std::cout << s.average<< s.max << s.min;
}
}
//thing1.cpp -- function calls
#include <iostream>
#include "thing.h"
int main() {
using namespace SALES;
Sales ann, andy;
double arr[4] = {1500,2310,1200,6000};
setSales(ann, arr, 4);
showSales(ann);
setSales(andy);
showSales(andy);
return 0;
}
报错: