728x90
1λ²
// tv.h -- Tv ν΄λμ€μ Remote ν΄λμ€
#ifndef TV_H_
#define TV_H_
class Tv
{
public:
friend class Remote; // Remoteλ Tvμ private λΆλΆμ μ κ·Όν μ μλ€
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 set_mode2(Remote& r);
private:
int state; // on λλ off
int volume; // λμ§νΈ λ³Όλ₯¨μ΄λΌκ³ κ°μ νλ€
int maxchannel; // μ΅λ μ±λ μ
int channel; // νμ¬ μ€μ λ μ±λ
int mode; // μ§μν λ°©μ‘ λλ μΌμ΄λΈ λ°©μ‘
int input; // TV μ
λ ₯ λλ DVD μ
λ ₯
};
class Remote
{
friend class Tv;
private:
int mode; // TV μ‘°μ κΈ° λλ DVD μ‘°μ κΈ°
int mode2; //λν λͺ¨λ λλ μΌλ° λͺ¨λ
public:
enum{Normal,Conversation};
Remote(int m = Tv::TV,int m2 = Normal) : mode(m),mode2(m2) {}
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 show_mode2();
};
inline void Tv::set_mode2(Remote& r) { if (state == On) r.mode2 = (r.mode2 == Remote::Normal) ? Remote::Conversation : Remote::Normal; }
#endif
// tv.cpp -- Tv ν΄λμ€λ₯Ό μν λ©μλλ€(Remote λ©μλλ€μ μΈλΌμΈμΌλ‘ μ¬μ©)
#include <iostream>
#include "tv.h"
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 << endl;
cout << "μ±λ = " << channel << endl;
cout << "λͺ¨λ = "
<< (mode == Antenna ? "μ§μν λ°©μ‘" : "μΌμ΄λΈ λ°©μ‘") << endl;
cout << "μ
λ ₯ = " << (input == TV ? "TV" : "DVD") << endl;
}
}
void Remote::show_mode2()
{
std::cout << "λͺ¨λ2 = " << (mode2 == Normal ? "Normal" : "Conversation") << '\n';
}
//use_tv.cpp -- Tvμ Remote ν΄λμ€λ₯Ό μ¬μ©νλ€
#include <iostream>
#include "tv.h"
int main()
{
using std::cout;
Tv s42;
cout << "42\" TVμ μ΄κΈ° μ€μ κ°:\n";
s42.settings();
s42.onoff();
s42.chanup();
cout << "\n42\" TVμ λ³κ²½λ μ€μ κ°:\n";
s42.settings();
Remote grey;
cout << "\ngrey 리λͺ¨μ»¨μ μ΄κΈ° μ€μ κ°:\n";
grey.show_mode2();
grey.set_chan(s42, 10);
grey.volup(s42);
grey.volup(s42);
cout << "\n리λͺ¨μ½ μ¬μ© ν 42\" TVμ μ€μ κ°:\n";
s42.settings();
s42.set_mode2(grey);
grey.show_mode2();
s42.onoff();
cout << "\nTv μ’
λ£ ν Remoteμ λͺ¨λ λ³κ²½\n";
s42.settings();
s42.set_mode2(grey);
grey.show_mode2();
return 0;
}
2λ²
// exc_mean.h -- hmean(), gmean()μ μν μμΈ ν΄λμ€
#include <iostream>
#include<stdexcept>
class bad_hmean : public std::logic_error
{
public:
explicit bad_hmean(const std::string &s= "μλͺ»λ 맀κ°λ³μ: a = -b\n") : logic_error(s) {}
};
class bad_gmean : public std::logic_error
{
public:
explicit bad_gmean(const std::string&s= "gmean() 맀κ°λ³μλ€μ >= 0μ΄μ΄μΌ ν©λλ€.\n")
: logic_error(s) {}
};
//error4.cpp -- μμΈ ν΄λμ€λ€μ μ¬μ©νλ€
#include <iostream>
#include <cmath> // (λλ math.h) unix μ¬μ©μλ -lm νλκ·Έκ° νμνλ€
#include "exc_mean.h"
// ν¨μ μν
double hmean(double a, double b) throw(bad_hmean);
double gmean(double a, double b) throw(bad_gmean);
int main()
{
using std::cout;
using std::cin;
using std::endl;
double x, y, z;
cout << "λ μλ₯Ό μ
λ ₯νμμμ€: ";
while (cin >> x >> y)
{
try { // try λΈλ‘μ μμ
z = hmean(x, y);
cout << x << ", " << y << "μ μ‘°ννκ· μ "
<< z << "μ
λλ€.\n";
cout << x << ", " << y << "μ κΈ°ννκ· μ "
<< gmean(x, y) << "μ
λλ€.\n";
cout << "λ€λ₯Έ λ μλ₯Ό μ
λ ₯νμμμ€(λλ΄λ €λ©΄ q): ";
} // try λΈλ‘μ λ
catch (bad_hmean& bg) // catch λΈλ‘μ μμ
{
bg.what();
cout << "λ€μ νμμμ€.\n";
continue;
}
catch (bad_gmean& hg)
{
cout << hg.what();
cout << "μ£μ‘ν©λλ€. λ μ΄μ μ§νν μ μμ΅λλ€.\n";
break;
} // catch λΈλ‘μ λ
}
cout << "νλ‘κ·Έλ¨μ μ’
λ£ν©λλ€.\n";
return 0;
}
double hmean(double a, double b)
{
if (a == -b)
throw bad_hmean();
return 2.0 * 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 -- hmean(), gmean()μ μν μμΈ ν΄λμ€
#include <iostream>
#include<stdexcept>
class bad_mean : public std::logic_error
{
protected:
double v1;
double v2;
public:
bad_mean(double a = 0, double b = 0,const std::string &s = "none")
:v1(a),v2(b),logic_error(s) {}
virtual ~bad_mean() {}
virtual void mesg() = 0;
};
class bad_hmean : public bad_mean
{
public:
explicit bad_hmean(double a=0,double b=0,const std::string &s= "bad_mean()μμ μλ¬ λ°μ\n")
: bad_mean(a,b,s) {}
void mesg()
{
std::cout <<"a: " << v1 << ", b: "<< v2 << '\n';
std::cout<<"μλͺ»λ 맀κ°λ³μ: a = -b\n";
}
};
class bad_gmean : public bad_mean
{
public:
explicit bad_gmean(double a = 0, double b = 0, const std::string&s= "gmean()μμ μλ¬ λ°μ\n")
: bad_mean(a, b, s) {}
void mesg()
{
std::cout << "a: " << v1 << ", b: " << v2 << '\n';
std::cout << "gmean() 맀κ°λ³μλ€μ >= 0μ΄μ΄μΌ ν©λλ€.\n";
}
};
//error4.cpp -- μμΈ ν΄λμ€λ€μ μ¬μ©νλ€
#include <iostream>
#include <cmath> // (λλ math.h) unix μ¬μ©μλ -lm νλκ·Έκ° νμνλ€
#include "exc_mean.h"
// ν¨μ μν
double hmean(double a, double b) throw(bad_hmean);
double gmean(double a, double b) throw(bad_gmean);
int main()
{
using std::cout;
using std::cin;
using std::endl;
double x, y, z;
cout << "λ μλ₯Ό μ
λ ₯νμμμ€: ";
while (cin >> x >> y)
{
try { // try λΈλ‘μ μμ
z = hmean(x, y);
cout << x << ", " << y << "μ μ‘°ννκ· μ "
<< z << "μ
λλ€.\n";
cout << x << ", " << y << "μ κΈ°ννκ· μ "
<< gmean(x, y) << "μ
λλ€.\n";
cout << "λ€λ₯Έ λ μλ₯Ό μ
λ ₯νμμμ€(λλ΄λ €λ©΄ q): ";
} // try λΈλ‘μ λ
catch (bad_mean& bg) // catch λΈλ‘μ μμ
{
if (typeid(bad_hmean) == typeid(bg))
{
cout<<bg.what();
bg.mesg();
break;
}
else if (typeid(bad_gmean) == typeid(bg))
{
cout<<bg.what();
bg.mesg();
break;
}
}
}
cout << "νλ‘κ·Έλ¨μ μ’
λ£ν©λλ€.\n";
return 0;
}
double hmean(double a, double b)
{
if (a == -b)
throw bad_hmean(a,b);
return 2.0 * 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λ²
// use_sales.cpp -- λ΄ν¬λ μμΈ
#include <iostream>
#include "sales.h"
int main()
{
using std::cout;
using std::cin;
using std::endl;
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);
LabeledSales::nbad_index* p;
cout << "첫 λ²μ§Έ try λΈλ‘:\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 << "try λΈλ‘ 1μ λ.\n";
}
catch (Sales::bad_index& bad)
{
cout << bad.what();
if (p = dynamic_cast<LabeledSales::nbad_index*>(&bad))
cout << "Company: " << p->label_val() << endl;
cout << "μλͺ»λ μΈλ±μ€: " << bad.bi_val() << endl;
}
cout << "\nλ€μ try λΈλ‘:\n";
try
{
sales2[2] = 37.5;
sales1[20] = 23345;
cout << "try λΈλ‘ 2μ λ.\n";
}
catch (Sales::bad_index& bad)
{
cout << bad.what();
if (p = dynamic_cast<LabeledSales::nbad_index*>(&bad))
cout << "Company: " << p->label_val() << endl;
cout << "μλͺ»λ μΈλ±μ€: " << bad.bi_val() << endl;
}
cout << "νλ‘κ·Έλ¨μ μ’
λ£ν©λλ€.\n";
return 0;
}
728x90