第九章 编程练习答案
1.
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);
#include <iostream>
#include <cstring>
#include "golf.h"
void setgolf(golf & g, const char * name, int hc)
{
using namespace std;
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf & g)
{
using namespace std;
int temp = 1;
cout << "The fullname is: ";
cin.getline(g.fullname, Len);
if(strcmp(g.fullname, "") == 0)
{
temp = 0;
return temp;
}
else{
cout << "The handicap is: ";
cin >> g.handicap;
cin.get();
return temp;
}
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
using namespace std;
cout << "The fullname is: " << g.fullname << endl;
cout << "The handicap is: " <<g.handicap<<endl;
}
#include <iostream>
#include "golf.h"
int main()
{
using namespace std;
golf g[10];
int count = 0;
cout << "Enter the information of golf player: " << endl;
while((count < 10) && (setgolf(g[count])))
{
cout << "Next golf player: " << endl;
count++;
}
cout << "\nShow all golf player information: " << endl;
for (int i = 0; i < count; i++)
{
showgolf(g[i]);
}
cout << "\nReset the value of handicap, and "
"Show all golf player information: " << endl;
for (int i = 0; i < count; i++)
{
handicap(g[i], 99);
showgolf(g[i]);
}
return 0;
}
2.
#include <iostream>
#include <cstring>
using namespace std;
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";
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 << " haraccters total\n";
}
3.
#include <iostream>
#include <cstring>
#include <new>
struct chaff
{
char dross[20];
int slag;
} ;
const int BUF = 512;
const int N = 2;
char buffer[BUF];
const char ch[] = "hello";
int main()
{
using namespace std;
chaff *p1, *p2;
int i;
cout << "Calling new and placement new:\n";
p1 = new chaff;
p2 = new (buffer) chaff;
for (i = 0; i < N; i++){
p2[i].slag = p1[i].slag = 1000 + 20 * i;
strcpy(p1[i].dross, ch);
strcpy(p2[i].dross, ch);
}
cout << "Memory addresses:\n" << " heap: " << p1
<< " static: " << (void*) buffer << endl;
cout << "Memory contents:\n";
for (i = 0; i < N; i++)
{
cout << p1[i].dross << " at " << &p1[i].dross << endl;
cout << p1[i].slag << " at " << &p1[i].slag << endl;
cout << " "
<< p2[i].dross << " at " << &p2[i].dross << endl;
cout << " "
<< p2[i].slag << " at " << &p2[i].slag << endl;
}
cout << "\nCalling new and placement new a second time:\n";
chaff *p3, *p4;
p3 = new chaff;
p4 = new (buffer) chaff;
for (i = 0; i < N; i++)
{
p4[i].slag = p3[i].slag = 1000 + 40 * i;
strcpy(p1[i].dross, ch);
strcpy(p2[i].dross, ch);
}
cout << "Memory addresses:\n" << " heap: " << p1
<< " static: " << (void*) buffer << endl;
cout << "Memory contents:\n";
for (i = 0; i < N; i++)
{
cout << p3[i].dross << " at " << &p3[i].dross << endl;
cout << p3[i].slag << " at " << &p3[i].slag << endl;
cout << " "
<< p4[i].dross << " at " << &p4[i].dross << endl;
cout << " "
<< p4[i].slag << " at " << &p4[i].slag << endl;
}
cout << "\nCalling new and placemen new a third time:\n";
delete [] p1;
p1 = new chaff;
p2 = new (buffer + N * sizeof(chaff)) chaff;
for (i = 0; i < N; i++)
{
p2[i].slag = p1[i].slag = 1000 + 60 * i;
strcpy(p1[i].dross, ch);
strcpy(p2[i].dross, ch);
}
cout << "Memory addresses:\n" << " heap: " << p1
<< " static: " << (void*) buffer << endl;
cout << "Memory contents:\n";
for (i = 0; i < N; i++)
{
cout << p1[i].dross << " at " << &p1[i].dross << endl;
cout << p1[i].slag << " at " << &p1[i].slag << endl;
cout << " "
<< p2[i].dross << " at " << &p2[i].dross << endl;
cout << " "
<< p2[i].slag << " at " << &p2[i].slag << endl;
}
delete [] p1;
delete [] p3;
return 0;
}
4.
#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);
}
#include "sales.h"
namespace SALES
{
using std::cout;
using std::cin;
void setSales(Sales &s, const double ar[], int n)
{
double a, b, c;
a = b = ar[0];
c = 0.0;
for(int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
c += ar[i];
if(ar[i] < a)
a = ar[i];
if(ar[i] > b)
b = ar[i];
}
s.average = c / n;
s.max = b;
s.min = a;
}
void setSales(Sales &s)
{
double ar[QUARTERS];
cout << "Enter 4 numbers: " << std::endl;
for(int i = 0; i < QUARTERS; i++)
{
cin >> ar[i];
}
double a, b, c;
a = b = ar[0];
c = 0.0;
for(int i = 0; i < QUARTERS; i++)
{
s.sales[i] = ar[i];
c += ar[i];
if(ar[i] < a)
a = ar[i];
if(ar[i] > b)
b = ar[i];
}
s.average = c / QUARTERS;
s.max = b;
s.min = a;
}
void showSales(const Sales &s)
{
for(int i = 0; i < QUARTERS; i++)
cout << s.sales[i] << " ";
cout << "\nThe average is: "<< s.average << " The max is: "
<< s.max << " The min is: " << s.min << std::endl;
}
}
#include <iostream>
#include "sales.h"
int main()
{
SALES::Sales s1;
SALES::Sales s2;
int n;
std::cout << "Please enter n items:";
std::cin >> n;
double a[n];
std::cout << "Enter numbers: " << std::endl;
for(int i = 0; i < n; i++)
{
std::cin >> a[i];
}
SALES::setSales(s1, a, n);
SALES::showSales(s1);
std::cout << std::endl;
SALES::setSales(s2);
SALES::showSales(s2);
return 0;
}