练习代码为本人所写,欢迎大家学习交流
若有疑问请留言,我将尽快回复!!!!
题9.1:
#golf.h-------------------------------------------------------------------
#ifndef COORDIN_H_
#define COORDIN_H_
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);
#endif
#golf.cpp-------------------------------------------------------------------
#include <iostream>
#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 << "Enter the full name: ";
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 << "Full name: " << g.fullname << endl;
cout << "Handicap: " << g.handicap << endl;
}
#file.cpp-------------------------------------------------------------------
#include <iostream>
#include <ctime>
#include "golf.h";
using namespace std;
int main()
{
golf g[10];
int n = 0;
cout << "Enter the information of golf player: " << endl;
while ((n < 10) && (setgolf(g[n])))
{
n++;
cout << "Next golf player: " << endl;
}
cout << "Show all golf player information: " << endl;
for (int i = 0; i < n; i++)
{
showgolf(g[i]);
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
题9.2:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
const int ArSize = 10;
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";
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void strcount(const string str)
{
static int total = 0;
int count = 0;
cout << "\"" << str.c_str() << "\" contains ";
while (str[count])
{
count++;
}
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
题9.3:
#include <iostream>
#include <ctime>
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
char buffer[1024];
int main()
{
chaff *pcha = new (buffer) chaff[2];
char *pc = new char[1024];
chaff *pcha2 = 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(pcha[i].dross, dross);
strcpy(pcha2[i].dross, dross);
pcha[i].slag = pcha2[i].slag = slag;
}
for (int i = 0; i < 2; i++)
{
cout << "staff #" << (i + 1) << ":" << endl;
cout << "pcha.dross: " << pcha[i].dross << ". pcha.slag: " << pcha[i].slag << endl;
cout << "pcha2.dross: " << pcha2[i].dross << ". pcha2.slag: " << pcha2[i].slag << endl;
}
cout << "address of buffer: " << (void *)buffer << endl;
cout << "address of pcha: " << pcha << ". address of pcha[0]: " << &pcha[0] << ". address of pcha[1]: " << &pcha[1] << endl;
cout << "address of pc: " << (void *)pc << endl;
cout << "address of pcha2:" << pcha2 << ". address of pcha2[0]: " << &pcha2[0] << ". address of pcha2[1]: " << &pcha2[1] << endl;;
delete[] pc;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
题9.4:
#sales.h-------------------------------------------------------------------
#include <iostream>
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 "sales.h"
using namespace std;
namespace SALES
{
void setSales(Sales & s, const double ar[], int n)
{
double total = 0.0;
double max = ar[0];
double min = ar[0];
for (int i = 0; i < n; i++)
{
total += ar[i];
s.sales[i] = ar[i];
if (max < ar[i])
{
max = ar[i];
}
if (min > ar[i])
{
min = ar[i];
}
}
for (int i = n; i < QUARTERS; i++)
{
s.sales[i] = 0.0;
}
s.average = total / n;
s.max = max;
s.min = min;
}
void setSales(Sales & s)
{
double total = 0.0;
double max = 0.0;
double min = 0.0;
double input = 0.0;
cout << "Enter 4 double number; " << endl;
for (size_t i = 0; i < QUARTERS; i++)
{
cin >> input;
if (i == 0)
{
max = input;
min = input;
}
if (min > input)
{
min = input;
}
if (max < input)
{
max = input;
}
total += input;
s.sales[i] = input;
}
s.average = total / QUARTERS;
s.max = max;
s.min = min;
}
void showSales(const Sales & s)
{
cout << "sales: ";
for (size_t i = 0; i < QUARTERS; i++)
{
cout << s.sales[i] << " ";
}
cout << endl;
cout << "average: " << s.average << endl;
cout << "max: " << s.max << endl;
cout << "min: " << s.min << endl;
}
}
#file.cpp-------------------------------------------------------------------
#include <iostream>
#include <ctime>
#include "sales.h"
using namespace std;
using namespace SALES;
int main()
{
Sales sales1;
Sales sales2;
double ar[3] = { 32.1, 23.2, 65.3 };
setSales(sales1, ar, 3);
setSales(sales2);
showSales(sales1);
showSales(sales2);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}