728x90
1๋ฒ
// randwalk.cpp -- Vector ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ค
// vect.cpp ํ์ผ๊ณผ ํจ๊ป ์ปดํ์ผํ๋ค
#include <iostream>
#include<fstream>
#include <cstdlib> // rand(), srand()์ ์ํ
#include <ctime> // time()์ ์ํ
#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 write_randwalk;
write_randwalk.open("randwalk.txt");
if (!write_randwalk.is_open())
exit(EXIT_FAILURE);
cout << "๋ชฉํ ๊ฑฐ๋ฆฌ๋ฅผ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด q): ";
while (cin >> target)
{
cout << "๋ณดํญ์ ์
๋ ฅํ์ญ์์ค: ";
if (!(cin >> dstep))
break;
write_randwalk << "๋ชฉํ ๊ฑฐ๋ฆฌ: " << target << ", ๋ณดํญ: " << dstep<<endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
write_randwalk << steps << ": (x,y) = " << result << endl;
steps++;
}
write_randwalk << steps << " ๊ฑธ์์ ๊ฑธ์ ํ ์ ๊ณ ๋์ ํ์ฌ ์์น:\n";
write_randwalk << result << endl;
result.polar_mode();
write_randwalk << " ๋๋\n" << result << endl;
write_randwalk << "๊ฑธ์๋น ๊ธฐ๋ฅ์์ ๋ฒ์ด๋ ํ๊ท ๊ฑฐ๋ฆฌ = "
<< result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "๋ชฉํ ๊ฑฐ๋ฆฌ๋ฅผ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด q): ";
}
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
cin.clear();
while (cin.get() != '\n')
continue;
write_randwalk.close();
return 0;
}
2๋ฒ
// vect.cpp -- Vector ํด๋์ค๋ฅผ ์ํ ๋ฉ์๋
#include <cmath>
#include "vect.h" // <iostream>์ ํฌํจํ๋ค
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
// 1 ๋ผ๋์ ๋ด์ ๊ฐ๋๋ฅผ ๊ณ์ฐํ์ฌ๋ผ(1 ๋ผ๋์์ ๋ํ degree ๊ฐ์ ๊ณ์ฐ)
const double Rad_to_deg = 45.0 / atan(1.0);
// ๋ฐ๋์ ์ฝ 57.2957795130823 ๊ฐ์ด ๋์ด์ผ ํจ
// private ๋ฉ์๋๋ค
// x๊ฐ๊ณผ y๊ฐ์ผ๋ก ํฌ๊ธฐ๋ฅผ ๊ณ์ฐํ๋ค
double Vector::magval() const // ํฌ๊ธฐ๋ฅผ ๋ณด๊ณ ํ๋ค
{
return sqrt(x * x + y * y);
}
double Vector::angval() const // ๊ฐ๋๋ฅผ ๋ณด๊ณ ํ๋ค
{
if (x == 0.0 && y == 0.0)
return 0;
else
return atan2(y, x);
}
// ๊ทน ์ขํ๋ฅผ ์ฌ์ฉํ์ฌ x๋ฅผ ์ค์ ํ๋ค
void Vector::set_x(double mag,double ang)
{
x = mag * cos(ang);
}
// ๊ทน ์ขํ๋ฅผ ์ฌ์ฉํ์ฌ y๋ฅผ ์ค์ ํ๋ค
void Vector::set_y(double mag,double ang)
{
y = mag * sin(ang);
}
// public ๋ฉ์๋๋ค
Vector::Vector() // ๋ํดํธ ์์ฑ์
{
x = y = 0.0;
mode = RECT;
}
// form์ด r์ด๋ฉด ์ง๊ฐ ์ขํ๋ก๋ถํฐ ๋ฒกํฐ๋ฅผ ๊ตฌ์ฑํ๋ค
// form์ด p์ด๋ฉด ๊ทน ์ขํ๋ก๋ถํฐ ๋ฒกํฐ๋ฅผ ๊ตฌ์ฑํ๋ค
Vector::Vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
set_x(n1, n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
else
{
cout << "Vector()์ ์ ๋ฌ๋ ์ 3์ ๋งค๊ฐ๋ณ์๊ฐ ์๋ชป๋์๋ค.\n";
cout << "๊ทธ๋์ ๋ฒกํฐ๋ฅผ 0์ผ๋ก ์ค์ ํ์๋ค.\n";
x = y = 0.0;
mode = RECT;
}
}
// form์ด RECT์ด๋ฉด ์ง๊ฐ ์ขํ๋ก๋ถํฐ ๋ฒกํฐ๋ฅผ ๊ตฌ์ฑํ๋ค(๋ํดํธ)
// form์ด POL์ด๋ฉด ๊ทน ์ขํ๋ก๋ถํฐ ๋ฒกํฐ๋ฅผ ๊ตฌ์ฑํ๋ค
void Vector::reset(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
set_x(n1, n2 / Rad_to_deg);
set_y(n1, n2 / Rad_to_deg);
}
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 Vector::operator+(const Vector& b) const
{
return Vector(x + b.x, y + b.y);
}
// Vector ๊ฐ์ฒด a์์ Vector ๊ฐ์ฒด b๋ฅผ ๋บ๋ค
Vector Vector::operator-(const Vector& b) const
{
return Vector(x - b.x, y - b.y);
}
// Vector ๊ฐ์ฒด์ ๋ถํธ๋ฅผ ๋ฐ๊พผ๋ค
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
// Vector ๊ฐ์ฒด์ n์ ๊ณฑํ๋ค
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
// ํ๋ ๋ ๋ฉ์๋๋ค
// n์ Vector ๊ฐ์ฒด a๋ฅผ ๊ณฑํ๋ค
Vector operator*(double n, const Vector& a)
{
return a * n;
}
// mode๊ฐ RECT์ด๋ฉด ์ง๊ฐ ์ขํ๋ฅผ ์ถ๋ ฅํ๋ค
// mode๊ฐ POL์ด๋ฉด ๊ทน ์ขํ๋ฅผ ์ถ๋ ฅํ๋ค
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;
}
} // namespace VECTOR์ ๋
// vect.h -- ๋ชจ๋ ์ํ์ <<๋ฅผ ์ฌ์ฉํ๋, Vector ํด๋์ค
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
// ์ง์ฌ๊ฐํ์ ์ํด์๋ RECT๋ฅผ, Polar modes๋ฅผ ์ํด์๋ POL์ ์ฌ์ฉํ๋ค
private:
double x; // ์ํ ์ฑ๋ถ
double y; // ์์ง ์ฑ๋ถ
Mode mode; // POL์ ์ํด์ RECT๋ฅผ (RECT ๋๋ POL)
// ๊ฐ๋ค์ ์ค์ ํ๋ private ๋ฉ์๋๋ค
void set_x(double,double);
void set_y(double,double);
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; } // x๊ฐ์ ๋ณด๊ณ ํ๋ค
double yval() const { return y; } // y๊ฐ์ ๋ณด๊ณ ํ๋ค
double magval() const; // ํฌ๊ธฐ๋ฅผ ๋ณด๊ณ ํ๋ค
double angval() const; // ๊ฐ๋๋ฅผ ๋ณด๊ณ ํ๋ค
void polar_mode(); // ๋ชจ๋๋ฅผ 'p'๋ก ์ค์ ํ๋ค
void rect_mode(); // ๋ชจ๋๋ฅผ 'r'๋ก ์ค์ ํ๋ค
// ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
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);
};
} // namespace VECTOR์ ๋
#endif
3๋ฒ
// randwalk.cpp -- Vector ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ค
// vect.cpp ํ์ผ๊ณผ ํจ๊ป ์ปดํ์ผํ๋ค
#include <iostream>
#include <cstdlib> // rand(), srand()์ ์ํ
#include <ctime> // time()์ ์ํ
#include<new>
#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 n, max, min;
double average;
double target;
double dstep;
cout << "๋ชฉํ ๊ฑฐ๋ฆฌ๋ฅผ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด q): ";
while (cin >> target)
{
cout << "๋ณดํญ์ ์
๋ ฅํ์ญ์์ค: ";
if (!(cin >> dstep))
break;
cout << "์๋ํ ํ์๋ฅผ ์
๋ ฅํ์ญ์์ค: ";
cin >> n;
unsigned long *steps = new unsigned long[n];
for (int i = 0; i < n; i++)
{
steps[i] = 0;
}
for (int i = 0; i < n; i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps[i]++;
}
result.reset(0.0, 0.0);
}
average = max = min = steps[0];
for (int i = 1; i < n; i++)
{
if (max < steps[i])
max = steps[i];
if (min > steps[i])
min = steps[i];
average += steps[i];
}
cout << "์ต๊ณ ๊ฑธ์ ์ : " << max << endl
<< "์ต์ ๊ฑธ์ ์ : " << min << endl
<< "ํ๊ท ๊ฑธ์ ์ : " << average / n << endl;
delete[] steps;
cout << "๋ชฉํ ๊ฑฐ๋ฆฌ๋ฅผ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด q): ";
}
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
4๋ฒ
// mytime3.h -- ํ๋ ๋๋ฅผ ์ฌ์ฉํ๋ Time ํด๋์ค
#ifndef MYTIME3_H_
#define MYTIME3_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& t,const Time& i);
friend Time operator-(const Time& t,const Time& i);
friend Time operator*(const Time& t, double mult) { return mult * t; }
friend Time operator*(double mult,const Time & t);
friend std::ostream& operator<<(std::ostream& os, const Time& t);
};
#endif
// mytime3.cpp -- Time ํด๋์ค์ ๋ฉ์๋ ๊ตฌํ
#include "mytime3.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& t,const Time& i)
{
Time sum;
sum.minutes = i.minutes + t.minutes;
sum.hours = i.hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time operator-(const Time& t,const Time& i)
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = i.minutes + 60 * i.hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time operator*(double mult,const Time &t)
{
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;
}
5๋ฒ
// stonewt.h -- Stonewt ํด๋์ค ์ ์
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
public:
enum MODE { STONE, POUNDS, PDS_LEFT };
private:
enum { Lbs_per_stn = 14 }; // ์คํค๋น ํ์ด๋ ์
int stone; // ์ ์๋ถ(์คํค)
double pds_left; // ์์๋ถ(ํ์ด๋)
double pounds; // ํ์ด๋๋ก ๋ํ๋ธ ์ ์ฒด ๋ฌด๊ฒ
MODE mode;
public:
void set_mode(MODE form);
Stonewt(double lbs,MODE form = STONE);
Stonewt(int stn, double lbs,MODE form = STONE); // stone, lbs๋ฅผ ์ํ ์์ฑ์
Stonewt(); // ๋ํดํธ ์์ฑ์
~Stonewt();
Stonewt operator+(const Stonewt& s) const;
Stonewt operator-(const Stonewt& s) const;
Stonewt operator*(const Stonewt& s) const;
friend std::ostream& operator<<(std::ostream& os, const Stonewt& s);
};
#endif
// stonewt.cpp -- Stonewt ํด๋์ค์ ๋ฉ์๋
#include <iostream>
using std::cout;
#include "stonewt.h"
// doubleํ ๊ฐ์ผ๋ก Stonewt ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
Stonewt::Stonewt(double lbs,MODE form)
{
stone = int(lbs) / Lbs_per_stn; // ์ ์ ๋๋์
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
mode = form;
}
// intํ ๊ฐ๊ณผ doubleํ ๊ฐ์ผ๋ก Stonewt ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
Stonewt::Stonewt(int stn, double lbs,MODE form)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
mode = form;
}
Stonewt::Stonewt() // ๋ํดํธ ์์ฑ์, ๋ฌด๊ฒ = 0
{
stone = pounds = pds_left = 0;
mode = STONE;
}
Stonewt::~Stonewt() // ํ๊ดด์
{
}
void Stonewt::set_mode(MODE form)
{
mode = form;
}
Stonewt Stonewt::operator+(const Stonewt& s) const
{
return Stonewt(pounds + s.pounds,mode);
}
Stonewt Stonewt::operator-(const Stonewt& s) const
{
return Stonewt(pounds - s.pounds,mode);
}
Stonewt Stonewt::operator*(const Stonewt& s) const
{
return Stonewt(pounds * s.pounds,mode);
}
std::ostream& operator<<(std::ostream& os, const Stonewt& s)
{
if (s.mode == Stonewt::STONE)
os << s.stone << "์คํค, " << s.pds_left << "ํ์ด๋";
else if (s.mode == Stonewt::POUNDS)
os << (int)s.pounds << "ํ์ด๋";
else
os << s.pounds << "ํ์ด๋";
return os;
}
// stone.cpp -- ์ฌ์ฉ์ ์ ์ ๋ฐ์ดํฐํ ๋ณํ
// stonewt.cpp์ ํจ๊ป ์ปดํ์ผํ๋ค
#include <iostream>
using std::cout;
using std::endl;
#include "stonewt.h"
int main()
{
Stonewt s1(325.4,Stonewt::PDS_LEFT);
Stonewt s2(24, 8, Stonewt::STONE);
Stonewt s3(223.56, Stonewt::POUNDS);
cout << s1 << "; " << s2 << "; " << s3 << endl;
s1.set_mode(Stonewt::STONE);
s2.set_mode(Stonewt::STONE);
s3.set_mode(Stonewt::STONE);
cout << s1 << "; " << s2 << "; " << s3 << endl;
Stonewt result;
result = s1 + s2;
cout << result << endl;
cout << s1 - s2 << endl;
cout << s1 * s3 << endl;
return 0;
}
6๋ฒ
// stonewt.h -- Stonewt ํด๋์ค ์ ์
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum { Lbs_per_stn = 14 }; // ์คํค๋น ํ์ด๋ ์
int stone; // ์ ์๋ถ(์คํค)
double pds_left; // ์์๋ถ(ํ์ด๋)
double pounds; // ํ์ด๋๋ก ๋ํ๋ธ ์ ์ฒด ๋ฌด๊ฒ
public:
Stonewt(double lbs); // doubleํ ํ์ด๋๋ฅผ ์ํ ์์ฑ์
Stonewt(int stn, double lbs); // stone, lbs๋ฅผ ์ํ ์์ฑ์
Stonewt(); // ๋ํดํธ ์์ฑ์
~Stonewt();
void show_lbs() const; // ํ์ด๋ ํ์์ผ๋ก ์ถ๋ ฅ
void show_stn() const; // ์คํค๊ณผ ํ์ด๋ ์กฐํฉ ํ์์ผ๋ก ์ถ๋ ฅ
bool operator<(const Stonewt& st) const;
bool operator<=(const Stonewt& st) const;
bool operator==(const Stonewt& st) const;
bool operator>(const Stonewt& st) const;
bool operator>=(const Stonewt& st) const;
bool operator!=(const Stonewt& st) const;
};
#endif
// stonewt.cpp -- Stonewt ํด๋์ค์ ๋ฉ์๋
#include <iostream>
using std::cout;
#include "stonewt.h"
// doubleํ ๊ฐ์ผ๋ก Stonewt ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn; // ์ ์ ๋๋์
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
// intํ ๊ฐ๊ณผ doubleํ ๊ฐ์ผ๋ก Stonewt ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}
Stonewt::Stonewt() // ๋ํดํธ ์์ฑ์, ๋ฌด๊ฒ = 0
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt() // ํ๊ดด์
{
}
// ๋ฌด๊ฒ๋ฅผ ์คํค๊ณผ ํ์ด๋์ ์กฐํฉ ํ์์ผ๋ก ์ถ๋ ฅํ๋ค
void Stonewt::show_stn() const
{
cout << stone << "์คํค, " << pds_left << "ํ์ด๋\n";
}
// ๋ฌด๊ฒ๋ฅผ ํ์ด๋ ํ์์ผ๋ก ์ถ๋ ฅํ๋ค
void Stonewt::show_lbs() const
{
cout << pounds << "ํ์ด๋\n";
}
bool Stonewt::operator<(const Stonewt& st) const
{
return (pounds < st.pounds);
}
bool Stonewt::operator<=(const Stonewt& st) const
{
return (pounds <= st.pounds);
}
bool Stonewt::operator==(const Stonewt& st) const
{
return (pounds == st.pounds);
}
bool Stonewt::operator>(const Stonewt& st) const
{
return (pounds > st.pounds);
}
bool Stonewt::operator>=(const Stonewt& st) const
{
return (pounds >= st.pounds);
}
bool Stonewt::operator!=(const Stonewt& st) const
{
return (pounds != st.pounds);
}
// stone.cpp -- ์ฌ์ฉ์ ์ ์ ๋ฐ์ดํฐํ ๋ณํ
// stonewt.cpp์ ํจ๊ป ์ปดํ์ผํ๋ค
#include <iostream>
using std::cout;
using std::endl;
#include "stonewt.h"
int main()
{
Stonewt ar[6] = { 30,24.6,34.2 };
double var;
for (int i = 3; i < 6; i++)
{
std::cin >> var;
ar[i] = var;
}
Stonewt max, min, compare(11,0.0);
max = min = ar[0];
int count = 0;
if (ar[0] >= compare)
count++;
for (int i = 1; i < 6; i++)
{
if (max < ar[i])
max = ar[i];
if (min > ar[i])
min = ar[i];
if (ar[i] >= compare)
count++;
}
cout << "๊ฐ์ฅ ํฐ ์์ : ";
max.show_lbs();
cout << "๊ฐ์ฅ ์์ ์์ : ";
min.show_lbs();
cout << "11์คํค๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ์์์ ๊ฐ์ : " << count << "\n";
return 0;
}
7๋ฒ
//complex0.h
#ifndef COMPLEX0_H_
#define COMPLEX0_H_
class complex {
private:
double real_val;
double imagin_val;
public:
complex(double rval, double ival);
complex();
complex operator+(complex& t) const;
complex operator-(complex& t) const;
complex operator*(complex& t) const;
complex operator~();
friend complex operator*(double m,const complex& t);
friend std::ostream& operator<<(std::ostream& os, const complex& t);
friend std::istream& operator>>(std::istream& os, complex& t);
};
#endif
//complex0.cpp
#include<iostream>
#include"complex0.h"
complex::complex(double rval, double ival)
{
real_val = rval;
imagin_val = ival;
}
complex::complex()
{
real_val = imagin_val = 0.0;
}
complex complex::operator+(complex& t) const
{
return complex(real_val + t.real_val, imagin_val + t.imagin_val);
}
complex complex::operator-(complex& t) const
{
return complex(real_val - t.real_val, imagin_val - t.imagin_val);
}
complex complex::operator*(complex& t) const
{
return complex(real_val * t.real_val - imagin_val * t.imagin_val, real_val * t.imagin_val + t.real_val * imagin_val);
}
complex complex::operator~()
{
return complex(real_val, -imagin_val);
}
complex operator*(double m, const complex& t)
{
return complex(m * t.real_val, m * t.imagin_val);
}
std::ostream& operator<<(std::ostream& os, const complex& t)
{
os << "(" << t.real_val << "," << t.imagin_val << "i)";
return os;
}
std::istream& operator>>(std::istream& os, complex& t)
{
std::cout << "์ค์๋ถ: ";
os >> t.real_val;
std::cout << "ํ์๋ถ: ";
os >> t.imagin_val;
return os;
}
//usecmplx.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 << endl;
cout << "๊ณต์ก๋ณต์์ = " << ~c << endl;
cout << "a = " << a << endl;
cout << "a + c = " << a + c << endl;
cout << "a - c = " << a - c << endl;
cout << "a * c = " << a * c << endl;
cout << "2 * c = " << 2 * c << endl;
cout << "ํ๋์ ๋ณต์์๋ฅผ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด q)\n";
}
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
return 0;
}
728x90