第一篇:C++Primer Plus 中文版第六版课后题第九章内存模型与名称空间
1. golf.h const int Len = 40;struct golf {
};void setgolf(golf & g, const char * nane, int hc);int setgolf(golf & g);void handicap(golf & g, int hc);void showgolf(const golf & g);golf.cpp char fullname[Len];int handicap;#include
using namespace std;
void setgolf(golf & g, const char * name, int hc){
}
int setgolf(golf & g){ cout << “Enter Golfers' fullname ”;cin.get(g.fullname, Len);strncpy_s(g.fullname, name, Len);g.handicap = hc;
} if(g.fullname[0] == ' ')return 0;cout << “Enter Golfers' handicap ”;while(!(cin >> g.handicap)){
} cin.get();return 1;cin.clear();while(cin.get()!= 'n')continue;cout << “Please enter an integer: ”;void handicap(golf & g, int hc){ }
void showgolf(const golf & g){ } main.cpp #include
using namespace std;cout << “Fullname : ” << g.fullname << “, Handicap : ” << g.handicap << endl;g.handicap = hc;const int Men = 5;int main(){
} 2.#include
} for(int j = 0;j < i;j++)// int i 的Ì?作Á¡Â用®? showgolf(Golfer[j]);if(setgolf(Golfer[i])== 0)break;golf ann;setgolf(ann, “Ann Birdfree”, 24);showgolf(ann);handicap(ann, 22);showgolf(ann);return 0;
} getline(cin, input);while(input!= “"){
} cout << ”Bye“ << endl;return 0;strcount(input);cout << ”Enter next line: “ << endl;getline(cin, input);void strcount(const string str){
} 3.static int total = 0;int count;cout << ”“" << str << ”“ contains ”;count = str.size();total += count;cout << count << “ characters.” << endl;cout << total << “ total characters.” << endl;#include
} 4.sales.h chaff * p = new chaff[2];strcpy_s(p[0].dross, “Good”);p[0].slag = 23;strcpy_s(p[1].dross, “Great”);p[1].slag = 233;for(int i = 0;i < 2;i++)cout << “#” << i+1 <<“ dross: ” << p[i].dross << “, slag: ” << p[i].slag << endl;return 0;namespace SALES {
const int QUARTERS = 4;struct Sales {
};void setSales(Sales & s, const double ar[], int n);void setSales(Sales & s);void showSales(const Sales & s);double sales[QUARTERS];double average;double max;double min;} sales.cpp #include
{
} static double salesmax(const double arr[], unsigned arrsize){
} static double salesmin(const double arr[], unsigned arrsize)double max = arr[0];for(int i =1;i < arrsize;i++){
} return max;if(max < arr[i])else max;max = arr[i];double sum = 0;for(int i = 0;i < arrsize;i++)sum += arr[i];return(sum/arrsize);using std ::cout;using std ::cin;using std ::endl;static double salesaverage(const double arr[], int arrsize){
} void setSales(Sales & s, const double ar[], int n){
} void setSales(Sales & s){
cout << “Enter 4 sales: ” << endl;for(int i = 0;i < QUARTERS;i++){ cout << “# ” << i+1 << “ : ”;cin >> s.sales[i];unsigned times = n < QUARTERS ?(unsigned)n:QUARTERS;for(int i = 0;i < times;i++)s.sales[i] = ar[i];s.sales[i] = 0;for(int i = times;i < QUARTERS;i++)s.average = salesaverage(s.sales, times);s.max = salesmax(s.sales, times);s.min = salesmin(s.sales, times);double min = arr[0];for(int i =1;i < arrsize;i++){
} return min;if(min > arr[i])else min;min = arr[i];
} } s.average = salesaverage(s.sales, QUARTERS);s.max = salesmax(s.sales, QUARTERS);s.min = salesmin(s.sales, QUARTERS);void showSales(const Sales & s){
} } main.cpp #include
using namespace SALES;Sales A, B;double hc[4] = {1.2, 2.3, 3.4, 4.5};cout <<“Show A” << endl;setSales(A, hc, 5);cout << “Sales: ” << endl;for(int i = 0;i < QUARTERS;i++){ } cout << “average: ” << s.average << endl;cout << “max: ” << s.max << endl;cout << “min: ” << s.min << endl;cout << “# ” << i+1 << “ : ” << s.sales[i] < } showSales(A);cout <<“Show B” << endl;setSales(B);showSales(B);return 0;