반응형

다수의 클래스를 선언하고 활용하는 간단한 문제이다. 더하기(+), 빼기(-), 곱하기(*), 나누기(/)를 수행하는 4개의 클래스 Add, Sub, Mul, Div 를 만들고자 한다. 이들은 모두 공통으로 다음 멤버를 가진다. Int 타입 변수 a,b : 피연산자 void setValue(int x, int y)함수 : 매개 변수 x,y를 멤버 a,b에 복사 int calculate() 함수 : 연산을 실행하고 결과 리턴 main()함수는 Add, Sub, Mul, Div 클래스 타입의 객체 a, s, m, d를 생성하고 ,아래와 같이 키보드로부터 두 개의 정수와 연산자를 입력받고 a, s, m, d 객체 중에서 연산을 처리할 객체의 setValue() 함수를 호출한 후 calculate()를 호출하여 결과를 화면에 출력한다. 프로그램은 무한 루프를 돈다




< cpp 파일에 전부 넣은 것>

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using namespace std;
/*각 클래스의 함수들을 직접 구현하는 구현부 cpp파일*/
class Add{
private:
    int a,b;  //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y); // 인자 대입
    int calculate(); //계산
};
class Sub{
private:
    int a,b; //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y);
    int calculate();
};
class Mul{
private:
    int a,b; //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y);
    int calculate();
};
class Div{
private:
    int a,b; //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y);
    int calculate();
};
void Add::setValue(int x,int y){ //값 지정 x를 a에, y를 b에 대입한다.
    a=x;
    b=y;
}
int Add::calculate(){ //더하기 연산
    return a+b;
}
void Sub::setValue(int x,int y){
    a=x;
    b=y;
}
int Sub::calculate(){ //빼기 연산
    return a-b;
}
void Mul::setValue(int x,int y){
    a=x;
    b=y;
}
int Mul::calculate(){ //곱하기 연산
    return a*b;
}
void Div::setValue(int x,int y){ 
    a=x;
    b=y;
}
int Div::calculate(){//나누기 연산
    return a/b;
}
int main(void){
    while(1){
        int x;
        int y;
        char opr; //operator를 구별하기 위한 대입용 캐릭터형 변수
        Add a; Sub s; Mul m; Div d; //각 클래스 객체 생성
        cout<<"두 정수와 연산자를 입력하세요>>";
        cin>>x>>y>>opr;
        if(opr=='+'){ //opr이 +면
            a.setValue(x,y); //operand 값 set 해주고
            cout<<a.calculate()<<endl//계산값 출력
        }
        else if(opr=='-'){ //opr이 -면
            s.setValue(x,y);
            cout<<s.calculate()<<endl;
        }
        else if(opr=='*'){ //opr이 *면
            m.setValue(x,y);
            cout<<m.calculate()<<endl;
        }
        else if(opr=='/'){ //opr이 /면
            d.setValue(x,y);
            cout<<d.calculate()<<endl;
        }
    }
    return 0;
}
 
cs

<코드-헤더파일 Calcuator.h>

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
32
33
#ifndef CALC_H //헤더 파일 중복 include 방지용
#define CALC_H
 
class Add{
private:
    int a,b;  //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y); // 인자 대입
    int calculate(); //계산
};
class Sub{
private:
    int a,b; //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y);
    int calculate();
};
class Mul{
private:
    int a,b; //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y);
    int calculate();
};
class Div{
private:
    int a,b; //operand로 쓸 int형 변수 a,b
public:
    void setValue(int x,int y);
    int calculate();
};
#endif
 
cs

<코드-클래스cpp파일 Calculatop.cpp>


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
32
33
34
#include "Calculator.h" //선언부 include
#include <iostream>
using namespace std;
/*각 클래스의 함수들을 직접 구현하는 구현부 cpp파일*/
 
void Add::setValue(int x,int y){ //값 지정 x를 a에, y를 b에 대입한다.
    a=x;
    b=y;
}
int Add::calculate(){ //더하기 연산
    return a+b;
}
void Sub::setValue(int x,int y){
    a=x;
    b=y;
}
int Sub::calculate(){ //빼기 연산
    return a-b;
}
void Mul::setValue(int x,int y){
    a=x;
    b=y;
}
int Mul::calculate(){ //곱하기 연산
    return a*b;
}
void Div::setValue(int x,int y){ 
    a=x;
    b=y;
}
int Div::calculate(){//나누기 연산
    return a/b;
}
 
cs



<코드-메인cpp파일 main.cpp>

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
32
#include <iostream>
#include "Calculator.h" //calculator 내에 클래스 사용 위한 include
using namespace std;
 
int main(void){
    while(1){
        int x;
        int y;
        char opr; //operator를 구별하기 위한 대입용 캐릭터형 변수
        Add a; Sub s; Mul m; Div d; //각 클래스 객체 생성
        cout<<"두 정수와 연산자를 입력하세요>>";
        cin>>x>>y>>opr;
        if(opr=='+'){ //opr이 +면
            a.setValue(x,y); //operand 값 set 해주고
            cout<<a.calculate()<<endl//계산값 출력
        }
        else if(opr=='-'){ //opr이 -면
            s.setValue(x,y);
            cout<<s.calculate()<<endl;
        }
        else if(opr=='*'){ //opr이 *면
            m.setValue(x,y);
            cout<<m.calculate()<<endl;
        }
        else if(opr=='/'){ //opr이 /면
            d.setValue(x,y);
            cout<<d.calculate()<<endl;
        }
    }
    return 0;
}
 
cs




반응형

+ Recent posts