《C++ primer plus中文编程练习答案第12章.docx》由会员分享,可在线阅读,更多相关《C++ primer plus中文编程练习答案第12章.docx(14页珍藏版)》请在三一办公上搜索。
1、C+ primer plus中文编程练习答案第12章1、 /cow.h #ifndef COW_H_ #define COW_H_ #include #include #include using namespace std; class Cow private: char name20; char *hobby; double weight; public: Cow; Cow(const char *nm, const char *ho, double wt); Cow(const Cow &c); Cow; Cow &operator=(const Cow &c); voidShowCow
2、const; ; #endif /cow.cpp #includecow.h Cow:Cow name0 = 0; hobby = new char1; hobby0 = 0; weight = 0; Cow:Cow(const char *nm, const char *ho, double wt) strcpy_s(name, 20, nm); hobby = new charstrlen(ho)+1; strcpy_s(hobby, strlen(ho) + 1, ho); weight = wt; Cow:Cow(const Cow &c) strcpy_s(name, 20, c.n
3、ame); hobby = new charstrlen(c.hobby) + 1; strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby); weight = c.weight; Cow:Cow deletehobby; Cow &Cow:operator=(const Cow &c) if (this = &c) return *this; deletehobby; hobby = new charstrlen(c.hobby) + 1; strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby); strcpy_s(na
4、me, 20, c.name); weight = c.weight; return *this; void Cow:ShowCowconst cout Cow name: name endl; cout Cow hobby: hobby endl; cout Cow weight: weight endl; /main.cpp #includecow.h int main Cow co1; Cow co2(cow1, sport, 123); Cow co3(co2); co1 = co2; co1.ShowCow; co2.ShowCow; co3.ShowCow; system(paus
5、e); return 0; 2、 /String.h #ifndef STRING_H_ #define STRING_H_ #include #include #include #include using namespace std; class String public: String(const char *s); String; String(const String &); String; int lengthconst return len; String &operator=(const String &st); String &operator=(const char *)
6、; char&operator(inti); const char &operator(inti)const; voidstringlow; voidstringup; int has(const char ch); String operator+(const char *s); friend String operator+(const char *s, const String &st); friendbool operator(const String &st1, const String &st2); friendbool operator=(const String &st1, c
7、onst String &st2); friend String operator+(const String &st1, const String &st2); friendostream&operator(istream&is, String &st); staticintHowMany; private: char *str; intlen; staticintnum_strings; staticconstint CINLIM = 80; ; #endif /String.cpp #include String.h int String:num_strings = 0; int Str
8、ing:HowMany returnnum_strings; String:String(const char *s) len = strlen(s); str = new charlen + 1; strcpy_s(str, len + 1, s); num_strings+; String:String len = 4; str = new char1; str0 = 0; num_strings+; String:String(const String &st) num_strings+; len = st.len; str = new charlen + 1; strcpy_s(str
9、, len + 1, st.str); String:String -num_strings; deletestr; String &String:operator=(const String &st) if (this = &st) return *this; deletestr; len = st.len; str = new charlen + 1; strcpy_s(str, len + 1, st.str); return *this; String &String:operator=(const char *s) deletestr; len = strlen(s); str =
10、new charlen + 1; strcpy_s(str, len + 1, s); return *this; char&String:operator(inti) returnstri; const char &String:operator(inti)const returnstri; void String:stringlow for (inti = 0; ilen; i+) if (isupper(stri) stri = tolower(stri); void String:stringup for (inti = 0; ilen; i+) if (islower(stri) s
11、tri = toupper(stri); int String:has(const char ch) int counts = 0; for (inti = 0; ilen; i+) if (stri = ch) counts+; return counts; bool operator(const String &st1, const String &st2) return (strcmp(st1.str, st2.str) (const String &st1, const String &st2) return st2 st1; bool operator=(const String &
12、st1, const String &st2) return (strcmp(st1.str, st2.str) = 0); String String:operator+(const char *s) int lens = strlen(s) + len; char *ps = new charlens + 1; strcpy_s(ps, lens + 1, str); strcat_s(ps, lens + 1, s); return String(ps); String operator+(const char *s, const String &st) int lens = strle
13、n(s) + st.len; char *ps = new charlens + 1; strcpy_s(ps, lens + 1, s); strcat_s(ps, lens + 1, st.str); return String(ps); String operator+(const String &st1,const String &st2) int lens = st1.len + st2.len; char *ps = new charlens + 1; strcpy_s(ps, lens + 1, st1.str); strcat_s(ps, lens + 1, st2.str);
14、 return String(ps); ostream&operator(ostream&os, const String &st) os(istream&is, String &st) char tempString:CINLIM; is.get(temp, String:CINLIM); if (is) st = temp; while (is &is.get != n) continue; return is; /main.cpp #includeString.h int main String s1( and I am a C+ student.); String s2 = Pleas
15、e enter your name: ; String s3; cout s3; s2 = My name is + s3; cout s2 .n; s2 = s2 + s1; s2.stringup; cout The stringn s2 ncontains s2.has(A) A characters in it.n; s1 = red; String rgb3 = String(s1), String(green), String(blue) ; coutans) ans.stringlow; for (inti = 0; i 3; i+) if (ans = rgbi) cout T
16、hats right!n; success = true; break; if (success) break; else cout Try again!n; cout Byen; system(pause); return 0; 3、 /stock.h #ifndef STOCK10_H_ #define STOCK10_H_ #include #include #include using namespace std; class Stock private: char *company; long shares; doubleshare_val; doubletotal_val; voi
17、dset_tot total_val = shares*share_val; public: Stock; Stock(const char *co, long n = 0, double pr = 0.0); Stock; void buy(long num, double price); void sell(long num, double price); void update(double price); friendostream&operator(ostream&os, const Stock &s); const Stock &topval(const Stock &s)cons
18、t; ; #endif /stock.cpp #include stock.h Stock:Stock cout Default constructor calledn; company = new char1; company0 = 0; shares = 0; share_val = 0.0; total_val = 0.0; Stock:Stock(const char *co, long n, double pr) cout Constructor using co calledn; company = new charstrlen(co) + 1; strcpy_s(company,
19、 strlen(co) + 1, co); if (n 0) cout Number of shares cant be negative; company shares set to 0.n; shares = 0; else shares = n; share_val = pr; set_tot; Stock:Stock cout Bye, company !n; deletecompany; void Stock:buy(long num, double price) if (num 0) cout Number of shares purchase cant be negative.
20、Transaction is aborted.n; else shares += num; share_val = price; set_tot; void Stock:sell(long num, double price) if (num 0) cout Number of shares sole cant be negative. shares) cout You cant sell more than you have! Transaction is aborted.n; else shares -= num; share_val = price; set_tot; void Stoc
21、k:update(double price) share_val = price; set_tot; ostream&operator(ostream&os, const Stock &s) ios_base:fmtflagsorig = os.setf(ios_base:fixed, ios_base:floatfield); streamsizeprec = os.precision(3); os Company: pany Shares: s.shares n Share Price: $ s.share_val; os.precision(2); os Total Worth: $ s
22、.total_valtotal_val) return s; else return *this; /main.cpp #include stock.h constint STKS = 4; int main Stock stocksSTKS = Stock(NanoSmart, 12, 20.0), Stock(Boffo Objects, 200, 2.0), Stock(Monolithic Obelisks, 130, 3.25), Stock(Fleep Enterprises, 60, 6.5) ; cout Stock holdings:n; intst; for (st = 0
23、; st STKS; st+) cout stocksst; const Stock *top = &stocks0; for (st = 1; sttopval(stocksst); cout nMost valuable holding: ; cout *top; cin.get; return 0; 4、 /stack.h #ifndef STACK_H_ #define STACK_H_ #include #include #include using namespace std; typedef unsigned long Item; class Stack private: enu
24、m MAX = 10 ; Item *pitems; int top; int size; public: Stack(int n = MAX); Stack(const Stack &st); Stack; boolisemptyconst; boolisfullconst; bool push(const Item &item); bool pop(Item &item); Stack &operator=(const Stack &st); friendostream&operator(ostream&os, const Stack &st); ; #endif /stack.cpp #
25、include stack.h Stack:Stack(int n) pitems = new Itemn; pitems0 = 0; top = 0; size = n; Stack:Stack(const Stack &st) inti = 0; size = st.size; pitems = new Itemsize; for (i = 0; ist.top; i+) pitemsi = st.pitemsi; top = st.top; Stack:Stack deletepitems; bool Stack:isemptyconst return top = 0; bool Sta
26、ck:isfullconst return top = MAX; bool Stack:push(const Item &item) if (top 0) item = pitems-top; return true; else return false; Stack &Stack:operator=(const Stack &st) inti = 0; if (this = &st) return *this; deletepitems; size = st.size; pitems = new Itemsize; for (i = 0; ist.top; i+) pitemsi = st.
27、pitemsi; top = st.top; return *this; ostream&operator(ostream&os, const Stack &st) for (inti = 0; ist.top; i+) osst.pitemsi ; osendl; returnos; /main.cpp #include stack.h int main Stack st; charch; unsigned long po; cout Please enter A to add a purchase order,n ch&toupper(ch) != Q) while (cin.get !=
28、 n) continue; if (!isalpha(ch) cout a; continue; switch (ch) case A: case a: coutpo; if (st.isfull) cout stack already fulln; else st.push(po); break; case P: case p: if (st.isempty) cout stack already emptyn; else st.pop(po); cout PO # po poppedn; break; cout Please enter A to add a purchase order,
29、n P to process a PO, or Q to quit: ; Stack st1(st); Stack st2; st2 = st; coutst st1 st2; cout Byen; cin.get; cin.get; return 0; 5、参照程序清单12.10进行模拟即可 6、 /queue.h #ifndef QUEUE_H_ #define QUEUE_H_ #include #include #include using namespace std; class Customer private: long arrive; intprocesstime; publi
30、c: Customer arrive = processtime = 0; void set(long when); long whenconst return arrive; intptimeconst return processtime; ; typedef Customer Item; class Queue private: struct Node Item item; struct Node *next; ; enum Q_SIZE = 10 ; Node *front; Node *rear; int items; constintqsize; Queue(const Queue
31、 &q) :qsize(0) Queue &operator=(const Queue &q) return *this; public: Queue(intqs = Q_SIZE); Queue; boolisemptyconst; boolisfullconst; intqueuecountconst; boolenqueue(const Item &item); booldequeue(Item &item); ; #endif /queue.cpp #include queue.h Queue:Queue(intqs) :qsize(qs) front = rear = NULL; i
32、tems = 0; Queue:Queue Node *temp; while (front != NULL) temp = front; front = front-next; delete temp; bool Queue:isemptyconst return items = 0; bool Queue:isfullconst return items = qsize; int Queue:queuecountconst return items; bool Queue:enqueue(const Item &item) if (isfull) return false; Node *a
33、dd = new Node; add-item = item; add-next = NULL; items+; if (front = NULL) front = add; else rear-next = add; rear = add; return true; bool Queue:dequeue(Item &item) if (front = NULL) return false; item = front-item; items-; Node *temp = front; front = front-next; delete temp; if (items = 0) rear =
34、NULL; return true; void Customer:set(long when) processtime = rand % 3 + 1; arrive = when; /bank.cpp #include queue.h constint MIN_PER_HR = 60; boolnewcustomer(double x); int main srand(time(0); cout Case Study: Bank of Heather Automatic Tellern; coutqs; Queue line1(qs); Queue line2(qs); cout hours;
35、 longcyclelimit = MIN_PER_HR*hours; coutperhour; doublemin_per_cust; min_per_cust = MIN_PER_HR / perhour; Item temp; longturnaways = 0; long customers = 0; long served = 0; longsum_line = 0; int wait_time1 = 0; int wait_time2 = 0; longline_wait = 0; for (int cycle = 0; cycle cyclelimit; cycle+) if (
36、newcustomer(min_per_cust) if (line1.isfull&line2.isfull) turnaways+; else customers+; temp.set(cycle); if (line1.queuecount = line2.queuecount) line1.enqueue(temp); else line2.enqueue(temp); if (wait_time1 0) wait_time1-; sum_line += line1.queuecount; if (wait_time2 0) wait_time2-; sum_line += line2
37、.queuecount; if (customers 0) cout customers accepted: customers endl; cout customers served: served endl; cout turnaways: turnawaysendl; cout average queue size: ; cout.precision(2); cout.setf(ios_base:fixed, ios_base:floatfield); cout (double)sum_line / cyclelimitendl; cout average wait time: (double)line_wait / served minutesn; else cout No customers!n; cout Done!n; system(pause); return 0; boolnewcustomer(double x) return (rand*x / RAND_MAX 1);