第十五章 编程练习
来源:互联网 发布:zard变声器数据 编辑:程序博客网 时间:2024/05/02 04:25
编程练习1
//tv.h#ifndef TV_H_#define TV_H_#include <iostream>class Tv{public: friend class Remote; enum {Off, On}; enum {MinVal, MaxVal = 20}; enum {Antenna, Cable}; enum {TV, DVD}; Tv(int s = Off, int mc = 125) : state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {} void onoff() { state = (state == On)? Off : On;} bool ison() const {return state == On;} bool volup(); bool voldown(); void chanup(); void chandown(); void set_mode() {mode = (mode == Antenna)?Cable : Antenna;} void set_input() {input = (input == TV)?DVD : TV;} void settings() const; void ChangeReMode(Remote & r);private: int state; int volume; int maxchannel; int channel; int mode; int input;};class Remote{private: int mode; int ReMode;public: friend class Tv; enum {Normal, Interactive}; Remote(int m = Tv::TV, int n = Normal):mode(m), ReMode(n) {} bool volup(Tv & t) {return t.volup();} bool voldown(Tv & t) {return t.voldown();} void onoff(Tv & t) {t.onoff();} void chanup(Tv & t) {t.chanup();} void chandown(Tv & t) {t.chandown();} void set_chan(Tv & t, int c) {t.channel = c;} void set_mode(Tv & t) {t.set_mode();} void set_input(Tv & t) {t.set_input();} void ShowReMode() const { std::cout << "Remote now is at " << (ReMode == Normal? "Normal" : "Interactive") << std::endl;}};#endif
//tv.cpp#include "tv.h"#include <iostream>bool Tv::volup(){ if (volume < MaxVal) { volume++; return true; } else return false;}bool Tv::voldown(){ if (volume > MinVal) { volume--; return true; } else return false;}void Tv::chanup(){ if (channel < maxchannel) channel++; else channel = 1;}void Tv::chandown(){ if (channel > 1) channel--; else channel = maxchannel;}void Tv::settings() const{ using std::cout; using std::endl; cout << "Tv is " << (state == Off? "Off" : "On") << endl; if (state == On) { cout << "Volume setting = " << volume << endl; cout << "Channel setting = " << channel << endl; cout << "Mode = " << (mode == Antenna? "antenna" : "cable") << endl; cout << "Input = " << (input == TV? "TV" : "DVD") << endl; }}void Tv::ChangeReMode(Remote & r){ if (state == On) r.ReMode ^=1;}
//use_tv.cpp#include <iostream>#include "tv.h"int main(){ using std::cout; Tv s42; cout << "Initial settings for 42\" Tv:\n"; s42.settings(); s42.onoff(); s42.chanup(); cout << "\nAdjusted settings for 42\" TV:\n"; s42.chanup(); cout << "\nAdjusted settings for 42\" TV:\n"; s42.settings(); Remote grey; grey.set_chan(s42, 10); grey.volup(s42); grey.volup(s42); cout << "\n42\" settings after using remote:\n"; s42.settings(); grey.ShowReMode(); s42.ChangeReMode(grey); grey.ShowReMode(); system("pause"); return 0;}
编程练习2
//exc_mean.h#ifndef EXC_MEAN_H_#define EXC_MEAN_H_#include <iostream>#include <stdexcept>#include <string>class bad_hmean : public std::logic_error{public: bad_hmean(const std::string &s = "hmean(), invalid argument") : logic_error(s) {}};class bad_gmean : public std::logic_error{public: bad_gmean(const std::string &s = "gmean(), auguments shouble be >= 0") : logic_error(s) {}};#endif
//use exc_mean.cpp#include <iostream>#include <cmath>#include "exc_mean.h"double hmean(double a, double b);double gmean(double a, double b);int main(){ using std::cout; using std::cin; using std::endl; double x, y, z; cout << "Enter two numbers: "; while (cin >> x >> y) { try { z = hmean(x, y); cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << endl; cout << "Enter next set of numbers <q to quite>: "; } catch (bad_hmean & bg) { cout << bg.what() << endl;; cout << "Try again.\n"; continue; } catch(bad_gmean & hg) { cout << hg.what() << endl;; cout << "Sorry, you don't get to play any more.\n"; break; } } cout << "Bye!\n"; system("pause"); return 0;}double hmean(double a, double b){ if (a == -b) throw bad_hmean(); return 2 * a *b /(a+b);}double gmean(double a, double b){ if (a<0 || b<0) throw bad_gmean(); return std::sqrt(a * b);}
编程练习3
//exc_mean.h#ifndef EXC_MEAN_H_#define EXC_MEAN_H_#include <iostream>#include <stdexcept>#include <string>class bad_hmean : public std::logic_error{private: double v1, v2;public: bad_hmean(double x = 0, double y = 0) : std::logic_error("hmean") { v1 = x; v2 = y; } virtual void what() { std::cout << "hmean(), invalid argument. a = " << v1 << ", b = " << v2 << std::endl; }};class bad_gmean : public std::logic_error{private: double v1, v2;public: bad_gmean(double x = 0, double y = 0) : logic_error("gmean") { v1 = x; v2 = y; } virtual void what() { std::cout << "gmean(), auguments shouble be >= 0. a = " << v1 << ", b = " << v2 << std::endl; }};#endif
//use exc_mean.cpp#include <iostream>#include <cmath>#include "exc_mean.h"double hmean(double a, double b);double gmean(double a, double b);int main(){ using std::cout; using std::cin; using std::endl; double x, y, z; cout << "Enter two numbers: "; while (cin >> x >> y) { try { z = hmean(x, y); cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl; cout << "Geometric mean of " << x << " and " << y << " is " << gmean(x, y) << endl; cout << "Enter next set of numbers <q to quite>: "; } catch (bad_hmean & bg) { bg.what(); cout << "Try again.\n"; continue; } catch(bad_gmean & hg) { hg.what(); cout << "Sorry, you don't get to play any more.\n"; break; } } cout << "Bye!\n"; system("pause"); return 0;}double hmean(double a, double b){ if (a == -b) throw bad_hmean(a, b); return 2 * a *b /(a+b);}double gmean(double a, double b){ if (a<0 || b<0) throw bad_gmean(a, b); return std::sqrt(a * b);}
编程练习4
主函数修改即可
#include <iostream>#include "sales.h"int main(){ using std::cout; using std::cin; using std::endl; using std::logic_error; double vals1[12] = { 1220, 1100, 1122, 2212, 1232, 2334, 2884, 2393, 3302, 2922, 3002, 3544}; double vals2[12] = { 12, 11, 22, 21, 32, 34, 28, 29, 33, 29, 32, 35}; Sales sales1(2011, vals1, 12); LabeledSales sales2("Blogstar", 2012, vals2, 12); cout << "First try block: \n"; try { int i; cout << "Year = " << sales1.Year() << endl; for (i = 0; i<12; ++i) { cout << sales1[i] << ' '; if (i%6 ==5) cout << endl; } cout << "Year = " << sales2.Year() << endl; cout << "Label = " << sales2.Label() << endl; for (i = 0; i<=12; ++i) { cout << sales2[i] << ' '; if (i%6 ==5) cout << endl; } cout << "End of try block 1.\n"; } catch(logic_error & le) { cout << le.what(); if(LabeledSales::nbad_index *ni = dynamic_cast<LabeledSales::nbad_index *>(&le)) { cout << "Company: " << ni->label_val() << endl; cout << "bad index: " << ni->bi_val() << endl; } else if(Sales::bad_index *bi = dynamic_cast<Sales::bad_index *>(&le)) cout << "bad index: " << bi->bi_val() << endl; } cout << "\nNext try block:\n"; try { sales2[2] = 37.5; sales1[20] = 23345; cout << "End of try block 2.\n"; } catch(logic_error & le) { cout << le.what(); if(LabeledSales::nbad_index *ni = dynamic_cast<LabeledSales::nbad_index *>(&le)) { cout << "Company: " << ni->label_val() << endl; cout << "bad index: " << ni->bi_val() << endl; } else if(Sales::bad_index *bi = dynamic_cast<Sales::bad_index *>(&le)) cout << "bad index: " << bi->bi_val() << endl; } cout << "done\n"; system("pause"); return 0;}
0 0
- 第十五章 编程练习
- 第十五章编程练习(1)
- 编程练习(第十五周)
- C primer plus(第五版)编程练习第十五章
- C++ Primer Plus第六版 第十五章 编程练习答案
- simply scheme 第十五章 练习
- 第十五章 15.3节练习
- 第十五章 15.4节练习
- 第十五章 15.5节练习
- 第十五章上机练习2
- 第十五章上机练习3
- 第十五章上机练习4
- 第十五章上机练习1
- 第十五章上机练习4
- 第十五章 15.2.1节练习
- 第十五章 15.2.2节练习
- 第十五章 15.2.3节练习
- 第十五章 15.9.2节练习
- socket函数send和recv函数
- HMTL5基本解释
- 表单提交return方法与ajax冲突解决办法
- chaincode:simpledemo
- 第一个Java程序
- 第十五章 编程练习
- [Leetcode] 18. 4Sum 解题报告
- JSF: parse JSON and add new Item
- 网上一篇非常好的讲socket的send和recv的
- JS基本解释
- Shell循环遍历每个元素都是变量的数组
- [Leetcode] 19. Remove Nth Node From End of List 解题报告
- How to get simple dispatch from this.props using connect w/ Redux?
- [Leetcode] 20. Valid Parentheses 解题报告