《C++ Primer Plus(第六版)》(15)(第九章 内存模型和命名空间 编程题答案)
1.
Test.h
#ifndef _Test_H_ #define _Test_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
Test.cpp
#include "Test.h" #include <iostream> using namespace std; void setgolf(golf& g, const char* name, int hc) { strcpy_s(g.fullname, name); g.handicap = hc; } int setgolf(golf& g) { cout << "Please enter fullname: "; cin.get(); cin.getline(g.fullname, 40); if (strcmp(g.fullname, "") == 0) { return 0; } cout << "Please enter handcap: "; cin >> g.handicap; return 1; } void handicap(golf& g, int hc) { g.handicap = hc; } void showgolf(const golf& g) { cout << g.fullname << ": " << g.handicap << endl; }
main.cpp
#include <iostream> #include "Test.h" using namespace std; int main(int argc, const char * argv[]) { golf g[5]; char str[40] = ""; int hc = 0; cout << "Please enter fullname: "; cin >> str; cout << "Please enter handcap: "; cin >> hc; setgolf(g[0], str, hc); int num = 1; for (int i = 1; i < 5; i ++) { if (setgolf(g[i]) == 0) { break; } num++; } cout << "Show Golf" << endl; for (int i = 0; i < num; i++) { showgolf(g[i]); } return 0; }
2.
修改前:
#include <iostream> #include "Test.h" using namespace std; const int ArSize = 10; void strcount(const char* str); int main(int argc, const char * argv[]) { char input[ArSize]; char next; cout << "Enter a line:\n"; cin.get(input, ArSize); while (cin) { cin.get(next); while (next != '\n') { cin.get(next);//取出多余的字符扔掉 } strcount(input); cout << "Enter next line(empty line to quit):\n"; cin.get(input, ArSize); } cout << "Bye\n"; return 0; } void strcount(const char* str) { static int total = 0; int count = 0; cout << "\"" << str << "\" contains "; while (*str++) { count++; } total += count; cout << count << " character\n"; cout << total << " characters total\n"; }
修改后:
#include <iostream> #include "Test.h" #include <string> using namespace std; void strcount(const string& str); int main(int argc, const char * argv[]) { string input; cout << "Enter a line:\n"; getline(cin, input); while (cin) { 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 = str.length(); cout << "\"" << str << "\" contains "; total += count; cout << count << " character\n"; cout << total << " characters total\n"; }
3.
#include <iostream> #include "Test.h" #include <new> using namespace std; const int BUF = 512; const int N = 2; char buffer[BUF]; struct chaff { char dross[20]; int slag; }; int main(int argc, const char * argv[]) { //使用静态数组作为缓冲区 chaff* cf = new(buffer)chaff[N]; for (int i = 0; i < N; i ++) { cout << "Please enter dross: "; char dross[20]; cin.getline(dross,20); strcpy_s(cf[i].dross, dross); cout << "Please enter slag:"; cin >> cf[i].slag; cin.get(); } for (int i = 0; i < N; i++) { cout << cf[i].dross << " : " << cf[i].slag << endl; } //使用动态数组作为缓冲区 char* buffer2 = new char[BUF]; chaff* cf2 = new(buffer2)chaff[N]; for (int i = 0; i < N; i++) { cout << "Please enter dross: "; char dross[20]; cin.getline(dross, 20); strcpy_s(cf2[i].dross, dross); cout << "Please enter slag:"; cin >> cf2[i].slag; cin.get(); } for (int i = 0; i < N; i++) { cout << cf2[i].dross << " : " << cf2[i].slag << endl; } cf2 = NULL;//把这个置为空指针 delete[] buffer2;//把缓冲区删除了 return 0; }
4.
Test.h
#ifndef _Test_H_ #define _Test_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); } #endifTest.cpp
#include "Test.h" #include <iostream> using namespace std; void SALES::setSales(Sales & s, const double ar[], int n) { double total = 0; for (int i = 0; i < QUARTERS; i++) { if (i >= n) { s.sales[i] = 0; } else { s.sales[i] = ar[i]; } if (i == 0) { s.max = s.sales[i]; s.min = s.sales[i]; } else { if (s.sales[i] > s.max) { s.max = s.sales[i]; } if (s.sales[i] < s.min) { s.min = s.sales[i]; } } total += s.sales[i]; } s.average = total / QUARTERS; } void SALES::setSales(Sales & s) { double d[QUARTERS]; for (int i = 0; i < QUARTERS; i++) { cout << "Enter the sales:"; cin >> d[i]; } setSales(s, d, QUARTERS); } void SALES::showSales(const Sales& s) { cout << "Sales:"; for (int i = 0; i < QUARTERS; i++) { cout << s.sales[i]; cout << ", "; } cout << "\nMin:" << s.min << " Max:" << s.max << " average:" << s.average << endl; }main.cpp
#include <iostream> #include "Test.h" #include <new> using namespace std; int main(int argc, const char * argv[]) { double d[4] = { 123.3, 323, 342.333, 8933 }; SALES::Sales s1, s2; setSales(s1, d, 4); setSales(s2); showSales(s1); showSales(s2); return 0; }
人生如戏,还是戏如人生?微信公众号:传说之路
csdn博客 http://blog.csdn.net/u012175089/article/list/2