C++ primer plus中文编程练习答案第10章.docx
C+ primer plus中文编程练习答案第10章第10章 1、 /Customs.h #include <stdio.h> #include <tchar.h> #include <string> #include <iostream> using namespace std; class Customs private: string name; stringaccnum; double balance; public: Customs(const string &client, const string &num, double bal = 0.0); Customs; void showconst; bool deposit(double cash); bool withdraw(double cash); ; /Customs.cpp #include "Customs.h" Customs:Customs(const string &client,const string &num, double bal) accnum = num; name = client; balance = bal; Customs:Customs bool Customs:deposit(double cash) if (cash <= 0) cout<< "Deposit must greater than zeron" return false; else cout<< "Custom deposits $" << cash << " dolars.n" balance += cash; return true; bool Customs:withdraw(double cash) if (cash <= 0 | (balance - cash) < 0) cout<< "Withdraw money error, must less than balance and greater than zeron" return false; else cout<< "Custom withdraws $" << cash << " dolarsn" balance -= cash; return true; void Customs:showconst cout<< "Account custom's name is " << name <<endl; cout<< "Account number is " <<accnum<<endl; cout<< "Custom's balance is " << balance <<endl; /main.cpp #include "Customs.h" int main double input, output; charch; Customs custom=Customs("Jacky","Jc",3000.32); custom.show; cout<< "Please enter A to deposit balance ,n" << "P to withdraw balance, or Q to quit.: " while (cin>>ch&&toupper(ch)!='Q') while (cin.get != 'n') continue; if (!isalpha(ch) cout<< 'a' continue; switch (ch) case'A': case'a': cout<< "Enter a account number to deposit: " cin>> input; if (!custom.deposit(input) cout<< "Add errorn" else cout<< "Add successn" break; case'P': case'p': cout<< "Enter a account number to withdraw: " cin>> output; if (!custom.withdraw(output) cout<< "Withdraw errorn" else cout<< "Withdraw successn" break; custom.show; cout<< "Please enter A to deposit balance ,n" << "P to withdraw balance, or Q to quit: " cout<< "Byen" cin.get; cin.get; return 0; 2、 /person.h #ifndef PERSON_H_ #define PERSON_H_ #include <iostream> #include <stdio.h> #include <tchar.h> #include <string> using namespace std; class Person private: staticconstint Person:LIMIT = 25; stringlname; charfnameLIMIT; public: Person lname = "" fname0 = '0' Person(const string &ln, const char *fn = "Heyyou"); void Showconst; voidFormalShowconst; ; #endif /person.cpp #include "person.h" Person:Person(const string &ln, const char *fn) lname = ln; strncpy_s(fname, fn, LIMIT); fnameLIMIT = '0' void Person:Showconst cout<<fname<< " " <<lname; void Person:FormalShowconst cout<<lname<< ", " <<fname; /usePerson.cpp #include "person.h" int main Person one; Person two("Smythecraft"); Person three("Dimwiddy", "Sam"); one.Show; cout<<endl; one.FormalShow; cout<<endl; two.Show; cout<<endl; two.FormalShow; cout<<endl; three.Show; cout<<endl; three.FormalShow; cout<<endl; cin.get; return 0; 3、 /golf.h #ifndef GOLF_H_ #define GOLF_H_ #include <iostream> #include <string> using namespace std; class golf private: staticconstint Len = 40; charfullnameLen; int handicap; public: golf; golf(const char *name, inthc = 0); golf(golf &g); golf; voidhandicapf(inthc); void showconst; ; #endif /golf.cpp #include "golf.h" golf:golf golf:golf(const char *name, inthc) strncpy_s(fullname, name, Len); fullnameLen = '0' handicap = hc; golf:golf(golf &g) strncpy_s(this->fullname, g.fullname, Len); this->handicap = g.handicap; golf:golf void golf:handicapf( inthc) handicap = hc; void golf:showconst cout<<fullname<< ", " << handicap <<endl; /main.cpp #include "golf.h" int main char name40 = "0" int no; cout<< "Enter a name: " cin.getline(name, 40); cout<< "Enter a level: " cin>> no; golfann(name, no); ann.show; golfandy = golf(ann); andy.show; cin.get; cin.get; return 0; 4、 /Sales.h #ifndef SALE_H_ #define SALE_H_ #include <iostream> #include <string> using namespace std; namespace SALES class Sales private: staticconstint QUARTERS = 4; double salesQUARTERS; double average; double max; double min; public: Sales; Sales(const double *ar); Sales(Sales &s); Sales; voidshowSalesconst; ; #endif /Sales.cpp #include "Sales.h" using namespace SALES; Sales:Sales salesQUARTERS = '0' average = 0.0; max = 0.0; min = 0.0; Sales:Sales(const double *ar) double sum=0.0; for (inti = 0; i< QUARTERS; i+) salesi = ari; sum += salesi; average = sum / QUARTERS; max = sales0; for (inti = 0; i< QUARTERS-1; i+) if (salesi < salesi + 1) max = salesi + 1; min = sales0; for (inti = 0; i< QUARTERS-1; i+) if (salesi > salesi + 1) min = salesi + 1; Sales:Sales(Sales &s) for (inti = 0; i< QUARTERS; i+) this->salesi = s.salesi; this->average = s.average; this->max = s.max; this->min = s.min; Sales:Sales void Sales:showSalesconst cout<< "The sales number is n" for (inti = 0; i< QUARTERS; i+) cout<< salesi << " " cout<<endl; cout<< "The sales average is " << average <<endl; cout<< "The sales max is " << max <<endl; cout<< "The sales min is " << min <<endl; /main.cpp #include "Sales.h" using namespace SALES; int main doublenums4; cout<< "Please enter four numbers: n" for (inti = 0; i< 4; i+) cin>>numsi; Sales sn(nums); sn.showSales; Sales sn1(sn); sn1.showSales; cin.get; cin.get; return 0; 5、 /stack.h #ifndef STACK_H_ #define STACK_H_ #include <iostream> #include <string> #include <stdio.h> using namespace std; struct customer charfullname35; double payment; ; typedefstruct customer Item; class Stack private: enum MAX = 10 ; Item itemsMAX; double sum; int top; public: Stack; Stack; boolisemptyconst; boolisfullconst; bool push(const Item &item); bool pop(Item &item); ; #endif /stack.cpp #include "stack.h" Stack:Stack top = 0; sum = 0.0; Stack:Stack bool Stack:isemptyconst return top = 0; bool Stack:isfullconst return top = MAX; bool Stack:push(const Item &item) if (top < MAX) itemstop+ = item; return true; else return false; bool Stack:pop(Item &item) if (top > 0) item = items-top; sum += item.payment; cout<< sum <<endl; return true; else return false; /main.cpp #include "stack.h" int main Stack st; charch; Item cs; cout<< "Please enter A to add a purchase order,n" << "P to process a PO, or Q to quit.n" while (cin>>ch&&toupper(ch) != 'Q') while (cin.get != 'n') continue; if (!isalpha(ch) cout<< 'a' continue; switch (ch) case 'A': case 'a': cout<< "Enter a PO number to add: " cin>>cs.fullname; cin>>cs.payment; if (st.isfull) cout<< "stack already fulln" else st.push(cs); break; case 'P': case 'p': if (st.isempty) cout<< "stack already emptyn" else st.pop(cs); cout<< "PO #" <<cs.fullname<< "poppedn" break; cout<< "Please enter A to add a purchase order,n" << "P to process a PO, or Q to quit.n" cout<< "Byen" cin.get; cin.get; return 0; 6、 /Move.h #ifndef MOVE_H_ #define MOVE_H_ #include <stdio.h> #include <iostream> #include <string> using namespace std; class Move private: double x; double y; public: Move(double a = 0, double b = 0); voidshowmoveconst; Move add(const Move &m)const; void reset(double a = 0, double b = 0); ; #endif /Move.cpp #include "Move.h" Move:Move(double a, double b) x = a; y = b; void Move:showmoveconst cout<< "x = " << x <<endl; cout<< "y = " << y <<endl; Move Move:add(const Move &m)const Move xy_add; xy_add.x = m.x + this->x; xy_add.y = m.y + this->y; returnxy_add; void Move:reset(double a, double b) x = a; y = b; /main.cpp #include "Move.h" int main Move xy0(1, 1); xy0.showmove; Move xy1; xy1 = Move(2, 2); xy1.showmove; xy1 = xy1.add(xy0); xy1.showmove; xy1.reset(2.5, 2.5); xy1.showmove; cin.get; return 0; 7、 /plorg.h #ifndef PLORG_H_ #define PLORG_H_ #include <iostream> #include <string> #include <stdio.h> using namespace std; classplorg private: staticconstintlen = 19; charfullnamelen; int CI; public: plorg(const char *name = "Plorga", int ci = 50); plorg; voidp_fix(int ci); void showconst; ; #endif /plorg.cpp #include "plorg.h" plorg:plorg(const char *name, int ci) strncpy_s(fullname, name, len); CI = ci; plorg:plorg voidplorg:p_fix(int ci) CI = ci; voidplorg:showconst cout<<fullname<< ", " << CI <<endl; /main.cpp #include "plorg.h" int main plorgpl; pl.show; pl.p_fix(32); pl.show; plorgpll("Plorgb", 27); pll.show; pll.p_fix(32); pll.show; cin.get; return 0; 8、 /list.h #ifndef LIST_H_ #define LIST_H_ #include <iostream> #include <string> #include <stdio.h> using namespace std; typedef unsigned long Item; class List private: enum MAX = 10 ; Item itemsMAX; int head; int rear; public: List; boolisemptyconst; boolisfullconst; bool add(const Item &item); bool cut(Item &item); void showconst; void visit(void(*pf)(Item &); ; #endif /list.cpp #include "list.h" List:List head = 0; rear = 0; bool List:isemptyconst return rear = head; bool List:isfullconst return rear - head = MAX; bool List:add(const Item &item) if (rear < MAX) itemsrear+ = item; return true; else return false; bool List:cut(Item &item) if (rear < MAX) item = items-rear; return true; else return false; void List:showconst for (inti = head; i< rear; i+) cout<< itemsi << ", " cout<<endl; void List:visit(void(*pf)(Item &) for (inti = 0; i< rear; i+) (*pf)(itemsi); /main.cpp #include "list.h" voidshow_s(Item & item); int main List st; charch; unsigned long po; unsigned long *p = &po; cout<< "Please enter A to add a purchase order,n" << "P to process a PO, or Q to quit.n" while (cin>>ch&&toupper(ch) != 'Q') while (cin.get != 'n') continue; if (!isalpha(ch) cout<< 'a' continue; switch (ch) case 'A': case 'a': cout<< "Enter a PO number to add: " cin>>po; if (st.isfull) cout<< "stack already fulln" else st.add(po); break; case 'P': case 'p': if (st.isempty) cout<< "stack already emptyn" else st.cut(po); cout<< "PO #" <<po<< "poppedn" break; case 'V': case 'v': st.visit(show_s); cout<< "Please enter A to add a purchase order,n" << "P to process a PO, or Q to quit.n" st.show; cout<< "Byen" cin.get; cin.get; return 0; voidshow_s(Item &item) item += 100; cout<< item <<endl;