#1
// 1.cpp
#include <iostream>
#include "1.h"
int main()
{
golf gf;
// init golf
setgolf(gf, "Kan SCP", 80);
// show golf
showgolf(gf);
// input golf
setgolf(gf);
// show golf
showgolf(gf);
// reset hc
handicap(gf, 12);
showgolf(gf);
return 0;
}
void setgolf(golf & g, const char * name, int hc) {
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf & g) {
using std::cout;
using std::cin;
cout << "Enter your fullname: ";
cin.getline(g.fullname, Len);
cout << "Enter your handicap: ";
(cin >> g.handicap).get();
if (!strlen(g.fullname))
return false;
return true;
}
// reset handicap
void handicap(golf & g, int hc) {
g.handicap = hc;
}
// show golf struct data
void showgolf(const golf & g) {
using std::cout;
using std::endl;
// output your fullname and handicap
cout << "You fullname: " << g.fullname << endl;
cout << "You handicap: " << g.handicap << endl;
}
#2
// 2.cpp
#include <iostream>
#include <string>
using namespace std;
void strcount(const string & str);
int main()
{
string str;
//char next;
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";
return 0;
}
void strcount(const string & str) {
static int total = 0;
int count = 0;
cout << "\"" << str << "\" contains ";
count = str.size();
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
#3
// 3.cpp
#include <iostream>
#include <new>
using namespace std;
struct chaff {
char dross[20];
int slag;
};
const int LEN = 512;
const int N = 2;
int main()
{
char buffer[LEN];
chaff * jok1 = new chaff[N];
chaff * jok2 = new (buffer) chaff[N];
for (int i = 0; i < N; i++) {
strcpy(jok1[i].dross, "JOK1");
strcpy(jok2[i].dross, "JOK2");
jok1[i].slag = jok2[i].slag = 1000 + i;
}
for (int i = 0; i < N; i++) {
cout << "#Index " << i << endl;
cout << jok1[i].dross << " at " << &jok1[i].dross << endl;
cout << jok1[i].slag << " at " << &jok1[i].slag << endl;
cout << jok2[i].dross << " at " << &jok2[i].dross << endl;
cout << jok2[i].slag << " at " << &jok2[i].slag << endl;
}
delete[] jok1;
return 0;
}
#4
sales.h
#ifndef _SALES_H
#define _SALES_H
// 定義名稱空間 SALES
namespace SALES {
const int QUARTERS = 4;
struct Sales {
double sales[QUARTERS];
double average;
double min;
double max;
};
void setSales(Sales & s, const double ar[], int n);
void setSales(Sales & s);
void showSales(const Sales & s);
}
#endif
sales.cpp
// sales.cpp
#include <iostream>
#include "sales.h"
using namespace SALES;
int main()
{
Sales saleroom;
double ar[QUARTERS] = { 3.2, 1.2, 4.8, 7.2 };
// 非互交式設置
setSales(saleroom, ar, QUARTERS);
showSales(saleroom);
// 互交式設置
setSales(saleroom);
showSales(saleroom);
return 0;
}
saless.cpp
// saless.cpp
#include <iostream>
#include "sales.h"
// SALES 版本
namespace SALES {
using std::cout;
using std::cin;
using std::endl;
void setSales(Sales & s, const double ar[], int n) {
double total = 0;
for (int i = 0; i < n; i++) {
s.sales[i] = ar[i];
total += s.sales[i];
}
s.average = total / QUARTERS;
s.max = s.min = ar[0];
for (int i = 1; i < n; i++) {
if (ar[i] > s.max)
s.max = ar[i];
if (ar[i] < s.min)
s.min = ar[i];
}
}
void setSales(Sales & s) {
double total = 0;
cout << "Incoming sales.\n";
for (int i = 0; i < QUARTERS; i++) {
cout << "#" << i + 1 << ": ";
while (!(cin >> s.sales[i])) {
cin.clear();
while (cin.get() != '\n')
continue;
}
total += s.sales[i];
}
s.average = total / QUARTERS;
s.max = s.min = s.sales[0];
for (int i = 1; i < QUARTERS; i++) {
if (s.sales[i] > s.max)
s.max = s.sales[i];
if (s.sales[i] < s.min)
s.min = s.sales[i];
}
}
void showSales(const Sales & s) {
cout << "Sales.\n";
for (int i = 0; i < QUARTERS; i++)
cout << "#" << i + 1 << " " << s.sales[i] << endl;
cout << "average = " << s.average << endl;
cout << "max = " << s.max << ", "
<< "min = " << s.min << endl;
}
}