C++ Primer Plus(第六版)第九章課後程式設計答案
阿新 • • 發佈:2018-12-03
1.
//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);
//golf.cpp 函式定義 #include<iostream> #include"golf.h" #include<cstring> using namespace std; void setgolf(golf &g, const char *name, int hc) { strcpy_s(g.fullname, name); g.handicap = hc; } int setgolf(golf & g) { static int i = 0; //靜態變數 cout << "#" << ++i << ":\n"; cout << "Please enter the name: "; cin.getline(g.fullname, Len); int temp = 1; if (strcmp(g.fullname, " ") == 0) { temp = 0; return temp; } else { cout << "Please enter the grade:"; cin >> g.handicap; cin.get(); return temp; } } void handicap(golf &g, int hc) { g.handicap = hc; } void showgolf(const golf &g) { cout << "The name of golf is: " << g.fullname << endl; cout << "The grade of golf is: " << g.handicap << endl; }
//main.cpp #include<iostream> #include"golf.h" using namespace std; int main() { golf ann; setgolf(ann, "Ann Birdfree", 24); showgolf(ann); golf andy[5]; int count = 0; while ((count < 5) && (setgolf(andy[count]))) count++; for (int i = 0; i < count; i++) showgolf(andy[i]); int change; for (int i = 0; i < count; i++) { cout << "#" << (i + 1) << ":\n"; cout << "Please enter the change:"; cin >> change; handicap(andy[i], change); showgolf(andy[i]); } return 0; }
2.
#include<iostream> #include<string> using namespace std; void strcount(const string & str); int main() { string input; cout << "Enter a line:\n"; getline(cin, input); while(input!="") //測試時,使用enter來鍵入空行 { strcount(input); cout << "Enter next line (empty line to quit):\n"; getline(cin, input); } cout << "Bye\n"; return 0; } void strcount(const string & str) { using namespace std; static int total = 0; //靜態區域性變數 int count = 0; cout << "\"" << str << "\" contains "; while (str[count]) count++; //或使用str.size()成員函式來計算string類的長度 total += count; cout << count << " characters\n"; cout << total << " characters total\n"; }
3.
#include<iostream>
#include<cstring>
#include<new>
const int BUF = 512;
char buffer[BUF];
using namespace std;
struct chaff
{
char dross[20];
int slag;
};
void get(chaff &str,char * ch);
void show(const chaff &str);
int main()
{
chaff * p1 = new (buffer) chaff[2]; //定位new運算子,buffer為靜態記憶體,不需要delete釋放記憶體
//delete只能用於這樣的指標:指向常規new運算子分配的堆記憶體
for (int i = 0; i < 2; i++)
{
static int count = 0;
cout << "#" << ++count << ":\n";
cout << "Please enter the dross:";
char ch[20];
cin.getline(ch, 20);
get(p1[i],ch);
}
for (int i = 0; i < 2; i++)
show(p1[i]);
return 0;
}
void get(chaff &str,char * ch)
{
strcpy_s(str.dross, ch);
cout << "Please enter the slag:";
cin >> str.slag;
cin.get();
}
void show(const chaff &str)
{
static int count = 0;
cout << "#" << ++count << ":\n";
cout << "dross of the chaff is: " << str.dross << endl;
cout << "slag of the chaff is: " << str.slag << endl;
}
4.
//namesp.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);
}
//namesp.cpp函式定義
#include<iostream>
#include"namesp.h"
namespace SALES
{
void setSales(Sales & s, const double ar[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
sum += s.sales[i];
}
s.average =sum/n;
s.max = s.min = s.sales[0];
for (int i = 0; i < n; i++)
{
if (s.max < s.sales[i])
s.max = s.sales[i];
if (s.min > s.sales[i])
s.min = s.sales[i];
}
}
void setSales(Sales &s)
{
std::cout << "Please enter sales:";
double sum = 0.0;
for (int i = 0; i < QUARTERS; i++)
{
std::cin >> s.sales[i];
sum += s.sales[i];
}
std::cin.get();
s.average = sum/QUARTERS;
s.max = s.min = s.sales[0];
for (int i = 0; i < QUARTERS; i++)
{
if (s.max < s.sales[i])
s.max = s.sales[i];
if (s.min > s.sales[i])
s.min = s.sales[i];
}
}
void showSales(const Sales & s)
{
for (int i = 0; i < QUARTERS; i++)
std::cout << s.sales[i] << " "<<std::endl;
std::cout<<"The average is:"<<s.average << std::endl;
std::cout <<"The max is:"<< s.max << std::endl;
std::cout <<"The min is:"<< s.min << std::endl;
}
}
//main.cpp
#include<iostream>
#include"namesp.h"
const int ArSize=4;
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
using SALES::Sales;
using SALES::setSales;
using SALES::showSales;
Sales s1,s2; //宣告兩個Sales物件
double a[ArSize]; //互動式
cout << "Please enter numbers:";
for (int i = 0; i < ArSize; i++)
cin >> a[i];
setSales(s1, a, ArSize);
showSales(s1);
setSales(s2); //非互動式
showSales(s2);
return 0;
}