티스토리 뷰

IT/C++

[C++] 계산기 예제

DongChul 2014. 10. 8. 14:39


이전 예제에서는 메인함수안에서 모든 함수를 사용하여 만든 기본적인 계산예제 입니다.

이번예제에서는 객체지향적 개념을 추가하여 메인함수와 각각 계산을 담당하는 클래스의 함수와

헤더파일로 구분하여 실행해 보겠습니다.


메인소스 입니다.


#include <cstdlib>

#include <iostream>


#include "Add.h" // 각각 계산을 담당하는 헤더파일을 선언합니다.

#include "Sub.h"

#include "Mul.h"

#include "Div.h"



using namespace std;


char selectMenu();


int main(int argc, char *argv[])

{

    int x,y;

    

    Add a;

    Sub s;

    Mul m;

    Div d;

    

    while(1){

    cout<<"****사칙연산프로그램****"<<endl;

    cout<<"두 정수를 입력하세요 : "<<endl;

    

    cin >>x>>y;

    

    switch(selectMenu()){

                         case '1':{

                                          a.setValue(x,y);

                                          cout<<"결과 : "<<a.calculate()<<endl; 

                                   }

                                   break;

                         case '2':{

                                          s.setValue(x,y);

                                          cout<<"결과 : "<<s.calculate()<<endl;

                                   }

                                   break;          

                         case '3':{

                                          m.setValue(x,y);

                                          cout<<"결과 : "<<m.calculate()<<endl;

                                   }

                                   break;

                         case '4':{

                                          d.setValue(x,y);

                                          cout<<"결과 : "<<d.calculate()<<endl;

                                   }

                                   break;

                         case 'q':{

                                         return 0;                              

                                   }

                                   break;                               

                         }

    cout << endl;  

    system("PAUSE");

    system("cls");

    }

    

    return EXIT_SUCCESS;

    

}


char selectMenu(){

     char menu;

     cout <<"---------"<<endl;

     cout <<"1. 덧  셈"<<endl;

     cout <<"2. 뺄  셈"<<endl;

     cout <<"3. 곱  셈"<<endl;

     cout <<"4. 나눗셈"<<endl;

     cout <<"q. 종  료"<<endl;

     cout <<"---------"<<endl;

     cout <<"메뉴선택:"<<endl;

     cin >> menu;

     return menu;

}


Add 헤더파일과 함수cpp 파일 입니다.


#include <cstdlib>
#include <iostream>
#ifndef ADD_H //헤더파일의 중복을 막기위해 ADD_H 변수를 정의 하여 오류를 방지합니다.
#define ADD_H

class Add{
      int a, b;
      public:
             void setValue(int x, int y);
             int calculate();
};

#endif

함수파일

#include <cstdlib>
#include <iostream>

#include "Add.h" // 헤더파일 선언


void Add:: setValue(int x, int y){
     a = x;
     b = y;
}

int Add:: calculate(){
    return a+b;
}

Sub 헤더파일과 함수cpp 파일입니다. 위와 동일한 방식으로 선언합니다.

#include <cstdlib>
#include <iostream>

#ifndef SUB_H
#define SUB_H

class Sub{
      int a, b;
      public:
             void setValue(int x, int y);
             int calculate();
};

#endif


#include <cstdlib>
#include <iostream>

#include "Sub.h"

void Sub:: setValue(int x, int y){
     a = x;
     b = y;
}

int Sub:: calculate(){
    return a-b;
}


Mul 헤더파일과 함수cpp 파일 입니다.

#include <cstdlib>
#include <iostream>

#ifndef MUL_H
#define MUL_H

class Mul{
      int a, b;
      public:
             void setValue(int x, int y);
             int calculate();
};

#endif


#include <cstdlib>
#include <iostream>

#include "Mul.h"

void Mul:: setValue(int x, int y){
     a = x;
     b = y;
}

int Mul:: calculate(){
    return a*b;
}

마지막으로 Div 헤더파일과 함수cpp 파일 입니다.

#include <cstdlib>

#include <iostream>


#ifndef DIV_H

#define DIV_H


class Div{

      int a, b;

      public:

             void setValue(int x, int y);

             int calculate();

};


#endif



#include <cstdlib>
#include <iostream>

#include "Div.h"

using namespace std;

void Div:: setValue(int x, int y){
     a = x;
     b = y;
}

int Div:: calculate(){
    if(b == 0){
               cout<<"0으로 나눌 수 없다."<<endl;
               return -1;
               } 
    return a/b;
}

실행결과 입니다.




'IT > C++' 카테고리의 다른 글

[C++] String Class  (0) 2014.11.06
[C++] 배열과 객체의 동적 할당  (2) 2014.10.29
[C++] 생성자 소멸자 응용 프로그램  (0) 2014.10.08
통장관리프로그램  (2) 2014.09.26
[C++] BankAccount 예제  (0) 2014.09.25
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함