欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOCX文档下载  

    C++ primer plus中文编程练习答案第12章.docx

    • 资源ID:3153805       资源大小:40.68KB        全文页数:14页
    • 资源格式: DOCX        下载积分:6.99金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要6.99金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    C++ primer plus中文编程练习答案第12章.docx

    C+ primer plus中文编程练习答案第12章1、 /cow.h #ifndef COW_H_ #define COW_H_ #include<iostream> #include<string> #include<stdio.h> 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); voidShowCowconst; ; #endif /cow.cpp #include"cow.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.name); 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(name, 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 #include"cow.h" int main Cow co1; Cow co2("cow1", "sport", 123); Cow co3(co2); co1 = co2; co1.ShowCow; co2.ShowCow; co3.ShowCow; system("pause"); return 0; 2、 /String.h #ifndef STRING_H_ #define STRING_H_ #include <iostream> #include <stdio.h> #include <cstring> #include <string.h> 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 *); 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, const String &st2); friendbool operator=(const String &st1, const String &st2); friend String operator+(const String &st1, const String &st2); friendostream&operator<<(ostream&os, const String &st); friendistream&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 String: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, 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 = 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; i<len; i+) if (isupper(stri) stri = tolower(stri); void String:stringup for (inti = 0; i<len; i+) if (islower(stri) stri = toupper(stri); int String:has(const char ch) int counts = 0; for (inti = 0; i<len; i+) if (stri = ch) counts+; return counts; bool operator<(const String &st1, const String &st2) return (strcmp(st1.str, st2.str) < 0); bool operator>(const String &st1, const String &st2) return st2 < st1; bool operator=(const String &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 = strlen(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); return String(ps); ostream&operator<<(ostream&os, const String &st) os<<st.str; returnos; istream&operator>>(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 #include"String.h" int main String s1(" and I am a C+ student."); String s2 = "Please enter your name: " String s3; cout<< s2; cin>> 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") ; cout<< "Enter the name of a primary color for mixing light: " String ans; bool success = false; while (cin>>ans) ans.stringlow; for (inti = 0; i< 3; i+) if (ans = rgbi) cout<< "That's 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 <string> #include <iostream> #include <stdio.h> using namespace std; class Stock private: char *company; long shares; doubleshare_val; doubletotal_val; voidset_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)const; ; #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, strlen(co) + 1, co); if (n < 0) cout<< "Number of shares can't 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 can't be negative." << "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 can't be negative." << "Transaction is aborted.n" else if (num> shares) cout<< "You can't sell more than you have!" << "Transaction is aborted.n" else shares -= num; share_val = price; set_tot; void Stock: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.total_val<< 'n' os.setf(orig, ios_base:floatfield); os.precision(prec); returnos; const Stock &Stock:topval(const Stock &s) const if (s.total_val>total_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; st< STKS; st+) cout<< stocksst; const Stock *top = &stocks0; for (st = 1; st< STKS; st+) top = &top->topval(stocksst); cout<< "nMost valuable holding: " cout<< *top; cin.get; return 0; 4、 /stack.h #ifndef STACK_H_ #define STACK_H_ #include <iostream> #include <string> #include <stdio.h> using namespace std; typedef unsigned long Item; class Stack private: enum 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 #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; i<st.top; i+) pitemsi = st.pitemsi; top = st.top; Stack:Stack deletepitems; bool Stack:isemptyconst return top = 0; bool Stack:isfullconst return top = MAX; bool Stack:push(const Item &item) if (top < MAX) pitemstop+ = item; return true; else return false; bool Stack:pop(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; i<st.top; i+) pitemsi = st.pitemsi; top = st.top; return *this; ostream&operator<<(ostream&os, const Stack &st) for (inti = 0; i<st.top; i+) os<<st.pitemsi << " " os<<endl; returnos; /main.cpp #include "stack.h" int main Stack st; charch; unsigned long po; cout<< "Please enter A to add a purchase order,n" << "P to process a PO, 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 PO number to add: " cin>>po; 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,n" << "P to process a PO, or Q to quit: " Stack st1(st); Stack st2; st2 = st; cout<<st<< st1 << st2; cout<< "Byen" cin.get; cin.get; return 0; 5、参照程序清单12.10进行模拟即可 6、 /queue.h #ifndef QUEUE_H_ #define QUEUE_H_ #include <cstdlib> #include <iostream> #include <ctime> using namespace std; class Customer private: long arrive; intprocesstime; public: 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 &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; items = 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 *add = 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 = 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" cout<< "Enter maximum size of queue: " intqs; cin>>qs; Queue line1(qs); Queue line2(qs); cout<< "Enter the number of simulation hours: " int hours; cin>> hours; longcyclelimit = MIN_PER_HR*hours; cout<< "Enter the average number of customers per hour: " doubleperhour; cin>>perhour; 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 (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 && !line1.isempty) line1.dequeue(temp); wait_time1 = temp.ptime; line_wait += cycle - temp.when; served+; if (wait_time1 > 0) wait_time1-; sum_line += line1.queuecount; if (wait_time2 <= 0 && !line2.isempty) line2.dequeue(temp); wait_time2 = temp.ptime; line_wait += cycle - temp.when; served+; if (wait_time2 > 0) wait_time2-; sum_line += line2.queuecount; if (customers > 0) cout<< "customers accepted: " << customers <<endl; cout<< " customers served: " << served <<endl; cout<< " turnaways: " <<turnaways<<endl; cout<< "average queue size: " cout.precision(2); cout.setf(ios_base:fixed, ios_base:floatfield); cout<< (double)sum_line / cyclelimit<<endl; 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);

    注意事项

    本文(C++ primer plus中文编程练习答案第12章.docx)为本站会员(牧羊曲112)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开