[물리·과학·IT]

C++ 프로그래밍

kipacti 2011. 1. 12. 11:59

C++의 가장 큰 장점은 class라고 하겠다.(내 개인적인 생각)

 

C만 쓰다가, Java 잠깐 대학교에서 배우고, 실험실에서는 Labview도 쓰다가,

C++로 된 프로그램을 고쳐야 될 일이 있어서 C++ 공부 시작.

 

뭐 여러가지 복잡 미묘한 기능들이 많은데,

graphic 쪽을 C++로 다루는건 좀 짜증나니 그냥 Labview쓰는게 속편할거 같고

Labview의 단점을 보안할 수 있는 class 개념 발견 뚜둥~!!!

 

"자세한 설명은 생략"하고 싶지만...

이랬다간 이xx 뭐냐며, 궁금증만 유발시키고 내빼는 거냐며 항의가 들어올지 아닐지...

 

그냥 class란 double, int, short, char 요런거다.

double x, y;

x = 5.1;

y = 3.5;

(x+y-2.5)/283;

등등의 연산이 가능하듯이

 

class를 만들고 +,- 등의 연산이 어떻게 이루어질지 정의해주면,

Vector나 Matrix, Complex number 등을 손쉽게 다루는게 가능해진다.

 

아래는 내가 짜본 class Complex

 

더 이상의 설명은 정말 생략;;

 

#include <iostream>
#include <cmath>

using namespace std;

class Complex{
 public :
  double real;
  double image;
 public :
  // constructor
  Complex(double r=0)    { real=r; image=0; };
  Complex(double r=0, double i=0) { real=r; image=i; };
  ~Complex(){ real=0; image=0; };
  
  Complex& Set(double r=0, double i=0)   { real=r; image=i; return *this; };
  Complex& r_theta_Set(double r=0, double theta=0){ real=r*cos(theta); image=r*sin(theta);  return *this; };
  Complex Coj() const{ Complex res; res.real=this->real; res.image=-this->image; return res; };
  double radius() const{ return sqrt(real*real + image*image); };
  double theta() const {
   double theta;
   theta=acos(real/this->radius()); // (a,b)*(1,0)=|a,b|cos(theta)
   if( image<0 ){ theta=-theta; }
   return theta;
  };
  
  Complex& operator=(const Complex &rH) { real=rH.real; image=rH.image;  return *this; };
  Complex& operator=(const double &rH)  { real=rH;  image=0;   return *this; };
  
  Complex operator-() const{Complex res; res.real=-real; res.image=-image; return res; };
  Complex operator+(const Complex &rH) const;
  Complex operator+(const double &rH) const;
  Complex operator+(const double &lH, const Complex &rH);
  Complex operator-(const Complex &rH) const;
  Complex operator-(const double &rH) const;
  Complex operator-(const double &lH, const Complex &rH) const;
  Complex operator*(const Complex &rH) const;
  Complex operator*(const double &rH) const;
  Complex operator*(const double &lH, const Complex &rH) const;
  Complex operator/(const Complex &rH) const;
  Complex operator/(const double &rH) const;
  Complex operator/(const double &lH, const Complex &rH) const;
  
  Complex& operator+=(const Complex &rH){ real+=rH.real; image+=rH.image; return *this; };
  Complex& operator+=(const double &rH) { real+=rH;       return *this; };
  Complex& operator-=(const Complex &rH){ real-=rH.real; image-=rH.image; return *this; };
  Complex& operator-=(const double &rH) { real-=rH;       return *this; };
  Complex& operator*=(const Complex &rH){ (*this)=(*this)*rH;     return *this; };
  Complex& operator*=(const double &rH) { real*=rH;  image*=rH;   return *this; };
  Complex& operator/=(const Complex &rH){ (*this)=(*this)/rH;     return *this; };
  Complex& operator/=(const double &rH) { real/=rH;  image/=rH;   return *this; };
  
  friend ostream &operator<<(ostream &os, const Complex &comObj);
};

Complex Complex::operator+(const Complex &rH) const{
 Complex res;
 res.real  = real  + rH.real;
 res.image = image + rH.image;
 return res;
}
Complex Complex::operator+(const double &rH) const{
 Complex res;
 res.real  = real + rH;
 res.image = image;
 return res;
}
Complex operator+(const double &lH,const Complex &rH){
 Complex res;
 res.real  = lH + rH.real;
 res.image = rH.image;
 return res;
}
Complex Complex::operator-(const Complex &rH) const{
 Complex res;
 res.real  = real  - rH.real;
 res.image = image - rH.image;
 return res;
}
Complex Complex::operator-(const double &rH) const{
 Complex res;
 res.real  = real  - rH;
 res.image = image;
 return res;
}
Complex Complex::operator-(const double &lH, const Complex &rH) const{
 Complex res;
 res.real  = lH  - rH.real;
 res.image = - rH.image;
 return res;
}
Complex Complex::operator*(const Complex &rH) const{
 Complex res;
 res.real  = real*rH.real  - image*rH.image;
 res.image = real*rH.image + image*rH.real;
 return res;
}
Complex Complex::operator*(const double &rH) const{
 Complex res;
 res.real  = real *rH;
 res.image = image*rH;
 return res;
}
Complex Complex::operator*(const double &lH, const Complex &rH) const{
 Complex res;
 res.real  = lH*rH.real;
 res.image = lH*rH.image;
 return res;
}
Complex Complex::operator/(const Complex &rH) const{
 if( rH.real==0 && rH.image==0 ){
  error(COMPLEX_DIV);
 }
 Complex res;
 res = (*this)*rH.Coj();
 res /= rH.real*rH.real + rH.image*rH.image;
 return res;
}
Complex Complex::operator/(const double &rH) const{
 if( rH==0 ){
  error(COMPLEX_DIV);
 }
 Complex res;
 res.real  = real/rH;
 res.image = image/rH;
 return res;
}
Complex Complex::operator/(const double &lH, const Complex &rH) const{
 Complex res;
 res = lH*rH.Coj();
 res /= rH.real*rH.real + rH.image*rH.image;
 return res;
}

ostream &operator<<(ostream &os, const Complex &p){
 if( p.image==0 ){
  os<<p.real;
 }
 else if( p.real==0 ){
  if( p.image==1 ){
   os<<"i";
  }
  else if( p.image==-1 ){
   os<<"-i";
  }
  else{
   os<<p.image<<"i";
  }
 }
 else if( p.image>0 ){
  if(p.image==1 ){
   os<<p.real<<"+i";
  }
  else{
   os<<p.real<<"+"<<p.image<<"i";
  }
 }
 else{
  if( p.image==-1 ){
   os<<p.real<<"-i";
  }
  else{
   os<<p.real<<p.image<<"i";
  }
 }
 return os;
}

////////////////////////////////////////////////////////////////////////////////test
int main(){
 Complex x(1), y(0,1), z;
 cout<<"x : "<<x<<endl;
 cout<<"x.Coj() : "<<x.Coj()<<endl;
 cout<<"y : "<<y<<endl;
 cout<<"y.Coj() : "<<y.Coj()<<endl;
 cout<<"z : "<<z<<endl;
 cout<<"z.Set(1,-1) : "<<z.Set(1,-1)<<endl;
 cout<<"z.radius() : "<<z.radius<<endl;
 cout<<"z.theta() : "<<z.theta()<<endl;
 cout<<"z.Coj() : "<<z.Coj()<<endl;
 cout<<"x=z : "<<x=z<<endl;
 cout<<"x+y : "<<x+y<<endl;
 cout<<"(x+y).Coj() : "<<(x+y).Coj()<<endl;
 cout<<"x-y : "<<x-y<<endl;
 cout<<"x*y : "<<x*y<<endl;
 cout<<"x/y : "<<x/y<<endl;
 cout<<"-x : "<<-x<<endl;
 cout<<"-x/y : "<<-x/y<<endl;
 cout<<"x/y+x : "<<x/y+x<<endl;
 cout<<"x+x/y : "<<x+x/y<<endl;   //연산자 순서를 따른다? *,/ 먼저. +,-는 나중에..
 cout<<"Complex(3,5)+Complex(10,1)*y : "<<Complex(3,5)+Complex(10,1)*y<<endl; //되나?
 cout<<"z.Set(1,0)+z.Set(0,1) : "<<z.Set(1,0)+z.Set(0,1)<<endl; //되나?
 cout<<"5+x+y : "<<5+x+y<<endl;
 
 return 0;
}
////////////////////////////////////////////////////////////////////////////////test

'[물리·과학·IT]' 카테고리의 다른 글

한글의 우수성  (0) 2011.01.12
랩뷰(Labview) 프로그래밍  (0) 2011.01.12
수면의 과학  (0) 2011.01.10
시선 추적 안경 + Remote Controller(리모컨 or 무선 마우스)  (0) 2010.03.11
우주  (0) 2008.09.10