C++ Primer 11 프로그래밍 연습
01
// randwalk1.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vect.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;
fout.open("thewalk.txt");
cout << "목표 거리를 입력하십시오(끝내려면 q) : ";
while (cin >> target)
{
cout << "보폭을 입력하십시오 : ";
if (!(cin >> dstep))
break;
fout << "목표 거리 : " << target << ", 보폭 : " << dstep << endl;
fout << steps << " : " << result << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
fout << steps << " : " << result << endl;
}
fout << steps << " 걸음을 걸은 후 술고래의 현재 위치 : \n";
fout << result << endl;
result.polar_mode();
fout << " 또는\n" << result << endl;
fout << "걸음당 기둥에서 벗어난 평균 거리 = "
<< result.magval()/steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "목표 거리를 입력하십시오(끝내려면 q) : ";
}
cout << "프로그램을 종료합니다.\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
02
// vect1.h
#ifndef VECT1_H_
#define VECT1_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
private:
double x;
double y;
Mode mode;
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~Vector();
double xval() const {return x;};
double yval() const {return y;};
double magval() const;
double angval() const;
void polar_mode();
void rect_mode();
Vector operator+(const Vector & b) const;
Vector operator-(const Vector & b) const;
Vector operator-() const;
Vector operator*(double n) const;
friend Vector operator*(double n, const Vector & a);
friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};
}
#endif
// vect1.cpp
#include <cmath>
#include "vect1.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);
double Vector::magval() const
{
return sqrt(x * x + y * y);
}
double Vector::angval() const
{
if (x == 0.0 && y == 0.0)
return 0.0;
else
return atan2(y, x);
}
Vector::Vector()
{
x = y = 0.0;
mode = RECT;
}
Vector::Vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
double mag = n1;
double ang = n2 / Rad_to_deg;
x = mag * cos(ang);
y = mag * sin(ang);
}
else
{
cout << "Vector()에 전달된 제3의 매개변수가 잘못되었다.\n";
cout << "그래서 벡터를 0으로 설정하였다.\n";
x = y = 0.0;
mode = RECT;
}
}
void Vector::reset(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
double mag = n1;
double ang = n2 / Rad_to_deg;
x = mag * cos(ang);
y = mag * sin(ang);
}
else
{
cout << "Vector()에 전달된 제3의 매개변수가 잘못되었다.\n";
cout << "그래서 벡터를 0으로 설정하였다.\n";
x = y = 0.0;
mode = RECT;
}
}
Vector::~Vector()
{
}
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode = RECT;
}
Vector Vector::operator+(const Vector & b) const
{
return Vector(x + b.x, y + b.y);
}
Vector Vector::operator-(const Vector & b) const
{
return Vector(x - b.x, y - b.y);
}
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
Vector operator*(double n, const Vector & a)
{
return a * n;
}
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == Vector::POL)
{
os << "(m,a) = (" << v.magval() << ", "
<< v.angval() * Rad_to_deg << ")";
}
else
os << "Vector 객체의 모든 지정이 틀렸습니다.\n";
return os;
}
}
03
// usevector.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
int num;
double max_step = 0;
double min_step = INT_MAX;
double total_step = 0;
cout << "목표 거리를 입력하십시오 : ";
cin >> target;
cout << "보폭을 입력하십시오 : ";
cin >> dstep;
cout << "총 몇 번 시도하시겠습니까? : ";
cin >> num;
for(int i = 0; i < num; i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
}
if (max_step < steps)
max_step = steps;
if (min_step > steps)
min_step = steps;
total_step += steps;
steps = 0;
result.reset(0.0, 0.0);
}
cout << "최고 걸음 수 : " << max_step << endl;
cout << "최저 걸음 수 : " << min_step << endl;
cout << "평균 걸음 수 : " << total_step / num << endl;
cout << "프로그램을 종료합니다.\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
04
// mytime4.h
#ifndef MYTIME4_H_
#define MYTIME4_H_
#include <iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
friend Time operator+(const Time & a, const Time & b);
friend Time operator-(const Time &a, const Time & b);
friend Time operator*(const Time & t, double n);
friend Time operator*(double m, const Time & t){ return t * m; };
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif
// mytime4.cpp
#include "mytime4.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time operator+(const Time & a, const Time & b)
{
Time sum;
sum.minutes = a.minutes + b.minutes;
sum.hours = a.hours + b.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time operator-(const Time & a, const Time & b)
{
Time diff;
int tot1, tot2;
tot1 = b.minutes + 60 * b.hours;
tot2 = a.minutes + 60 * a.hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time operator*(const Time & t, double mult)
{
Time result;
long totalminutes = t.hours * mult * 60 + t.minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
os << t.hours << "시간, " << t.minutes << "분";
return os;
}
// usetime4.cpp
#include "mytime4.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida와 Tosca:\n";
cout << aida << "; " << tosca << endl;
temp = aida + tosca;
cout << "Aida + Tosca : " << temp << endl;
temp = aida * 1.17;
cout << "Aida * 1.17 : " << temp << endl;
cout << "10 * Tosca : " << 10 * tosca << endl;
return 0;
}
05
// stonewt2.h
#ifndef STONEWT2_H_
#define STONEWT2_H_
class Stonewt
{
public:
enum Mode {STONE, INT_POUND, DOUBLE_POUND};
private:
enum {Lbs_per_stn = 14};
int stone;
double pds_left;
double pounds;
Mode mode;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs, Mode form = STONE);
Stonewt();
~Stonewt();
void setStnMode(){ mode = STONE; }
void setIntMode(){ mode = INT_POUND; }
void setFloatMode(){ mode = DOUBLE_POUND; }
Stonewt operator+(const Stonewt & s) const;
Stonewt operator-(const Stonewt & s) const;
Stonewt operator*(double n) const;
friend Stonewt operator*(double n, Stonewt & s);
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif
// stonewt2.cpp
#include <iostream>
using std::cout;
#include "stonewt2.h"
Stonewt::Stonewt(double lbs)
{
mode = STONE;
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs, Mode form)
{
mode = form;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
mode = STONE;
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
Stonewt Stonewt::operator+(const Stonewt & s) const
{
return Stonewt(pounds + s.pounds);
}
Stonewt Stonewt::operator-(const Stonewt & s) const
{
return Stonewt(pounds - s.pounds);
}
Stonewt Stonewt::operator*(double n) const
{
return Stonewt(pounds * n);
}
Stonewt operator*(double n, Stonewt & s)
{
return Stonewt(s.pounds * n);
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
if (s.mode == Stonewt::STONE)
os << s.stone << "스톤, " << s.pds_left << "파운드\n";
else if (s.mode == Stonewt::INT_POUND)
os << (int) s.pounds << "파운드\n";
else
os << s.pounds << "파운드\n";
return os;
}
// stone2.cpp
#include <iostream>
using std::cout;
#include "stonewt2.h"
void display(const Stonewt & st, int n);
int main()
{
Stonewt pavarotti = 275;
Stonewt wolfe(285.7);
Stonewt taft(21, 8);
cout << "테너 가수의 몸무게 : ";
cout << pavarotti;
cout << "탐정의 몸무게 : ";
wolfe.setIntMode();
cout << wolfe;
cout << "대통령의 몸무게 : ";
taft.setDoubleMode();
cout << taft;
pavarotti = 276.8;
taft = 325;
cout << "저녁 식사를 마친 후 테너 가수의 몸무게 : ";
cout << pavarotti;
cout << "저녁 식사를 마친 후 대통령의 몸무게 : ";
cout << taft;
display(taft, 2);
cout << "레슬링 선수는 그보다 더 무겁다.\n";
display(422, 2);
cout << "비만은 건강의 최대의 적이다.\n";
return 0;
}
void display(const Stonewt & st, int n)
{
for (int i = 0; i < n; i++)
{
cout << "와! ";
cout << st;
}
}
06
// stonewt3.h
#ifndef STONEWT3_H_
#define STONEWT3_H_
class Stonewt
{
private:
enum {Lbs_per_stn = 14};
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt();
friend bool operator<(const Stonewt & a, const Stonewt & b);
friend bool operator>(const Stonewt & a, const Stonewt & b);
friend bool operator==(const Stonewt & a, const Stonewt & b);
friend bool operator!=(const Stonewt & a, const Stonewt & b);
friend bool operator<=(const Stonewt & a, const Stonewt & b);
friend bool operator>=(const Stonewt & a, const Stonewt & b);
void show_stn() const;
void show_lbs() const;
};
#endif
// stonewt3.cpp
#include <iostream>
using std::cout;
#include "stonewt3.h"
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
bool operator<(const Stonewt & a, const Stonewt & b)
{
if (a.pounds < b.pounds)
return true;
else
return false;
}
bool operator>(const Stonewt & a, const Stonewt & b)
{
if (a.pounds > b.pounds)
return true;
else
return false;
}
bool operator==(const Stonewt & a, const Stonewt & b)
{
if (a.pounds == b.pounds)
return true;
else
return false;
}
bool operator!=(const Stonewt & a, const Stonewt & b)
{
if (a.pounds != b.pounds)
return true;
else
return false;
}
bool operator<=(const Stonewt & a, const Stonewt & b)
{
if (a.pounds <= b.pounds)
return true;
else
return false;
}
bool operator>=(const Stonewt & a, const Stonewt & b)
{
if (a.pounds >= b.pounds)
return true;
else
return false;
}
void Stonewt::show_stn() const
{
cout << stone << "스톤, " << pds_left << "파운드\n";
}
void Stonewt::show_lbs() const
{
cout << pounds << "파운드\n";
}
// stone3.cpp
#include <iostream>
using std::cout;
using std::cin;
#include "stonewt3.h"
int main()
{
Stonewt arr[6] = {100, 10, 200};
Stonewt cmp = 11;
int min = 0, max = 0, count = 0;
double pds;
for (int i = 3; i < 6; i++)
{
cout << i + 1 << "번째는 몇 파운드입니까? : ";
cin >> pds;
arr[i] = pds;
}
for (int i = 0; i < 6; i++)
{
if (arr[min] > arr[i])
min = i;
if (arr[max] < arr[i])
max = i;
if (arr[i] >= cmp)
count++;
}
cout << "최소 원소 : ";
arr[min].show_lbs();
cout << "최대 원소 : ";
arr[max].show_lbs();
cout << "11스톤보다 큰 원소의 개수 : " << count;
return 0;
}
07
// complex0.h
#ifndef XXX_H_
#define XXX_H_
class Complex
{
private:
double real;
double imagine;
public:
Complex(double r, double i);
Complex();
~Complex();
friend Complex operator+(const Complex & a, const Complex & b);
friend Complex operator-(const Complex & a, const Complex & b);
friend Complex operator*(const Complex & a, const Complex & b);
friend Complex operator*(double mul, const Complex & a);
Complex operator~();
friend std::ostream & operator<<(std::ostream & os, const Complex & c);
friend std::istream & operator>>(std::istream & is, Complex & c);
};
#endif
// complex0.cpp
#include <iostream>
#include "complex0.h"
Complex::Complex(double r, double i)
{
real = r;
imagine = i;
}
Complex::Complex()
{
real = 0.0;
imagine = 0.0;
}
Complex::~Complex()
{
}
Complex operator+(const Complex & a, const Complex & b)
{
return Complex(a.real + b.real, a.imagine + b.imagine);
}
Complex operator-(const Complex & a, const Complex & b)
{
return Complex(a.real - b.real, a.imagine - b.imagine);
}
Complex operator*(const Complex & a, const Complex & b)
{
return Complex(a.real * b.real - a.imagine * b.imagine,
a.real * b.imagine + a.imagine * b.real);
}
Complex operator*(double mul, const Complex & a)
{
return Complex(mul * a.real, mul * a.imagine);
}
Complex Complex::operator~()
{
return Complex(real, -imagine);
}
std::ostream & operator<<(std::ostream & os, const Complex & c)
{
os << "(" << c.real << ", " << c.imagine << "i)";
return os;
}
std::istream & operator>>(std::istream & is, Complex & c)
{
std::cout << "실수부를 입력해주세요 : ";
if (!(is >> c.real))
return is;
std::cout << "허수부를 입력해주세요 : ";
is >> c.imagine;
return is;
}
// usecomplex.cpp
#include <iostream>
using namespace std;
#include "complex0.h"
int main()
{
Complex a(3.0, 4.0);
Complex c;
cout << "하나의 복소수를 입력하십시오 (끝내려면 q) : \n";
while (cin >> c)
{
cout << "c = " << c << '\n';
cout << "공액복소수 = " << ~c << '\n';
cout << "a = " << a << '\n';
cout << "a + c = " << a + c << '\n';
cout << "a - c = " << a - c << '\n';
cout << "a * c = " << a * c << '\n';
cout << "2 * c = " << 2 * c << '\n';
cout << "하나의 복소수를 입력하십시오 (끝내려면 q) : \n";
}
cout << "프로그램을 종료합니다.\n";
return 0;
}