반응형

7.10> 스택 클래스 Stack을 만들고 푸시(push)용으로 <<연산자를, (pop)을 위해 >> 연산자를, 비어 있는 스택인지를 알기 위해 ! 연산자를 작성하라. 다음 코드를 main()으로 작성하라.

<코드>

#include <iostream>

 

using namespace std;

class Stack{

private:

        int stk[5]; //크기 5짜리 스택

        int index;

public:

        Stack(int index=-1); //디폴트 매개변수 인덱스 = -1(empty)

        Stack& operator<<(int op); //푸시

        friend bool operator!(Stack op);

        void operator>>(int &op); //

};

Stack::Stack(int index){this->index=index;} //index 초기화

Stack& Stack::operator<<(int op){ //푸시 구현

        index++; //인덱스 증가

        stk[index]=op; //오퍼랜드를 해당 인덱스에 넣어줌

        return *this; //현재 객체의 참조 반환

}

bool operator!(Stack op){ //비어있는지 확인

        if(op.index==-1)

               return true; //비었으면 true 리턴

        else

               return false;

}

void Stack::operator>>(int &op){

        index--; //인덱스 감소

        op= stk[index+1]; //제일 위의 원소 pop

}

int main(void){

        Stack stack;

        stack<<3<<5<<10; //3,5,10 순서의 스택

        while(true){

               if(!stack) break;

               int x;

               stack>>x; //제일 위의 원소부터 나옴

               cout<<x<<" ";

        }

        cout << endl;

        return 0;

}

 

<결과창>

               

                    


반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

MyStack Class  (0) 2017.12.31
MyQueue Class  (0) 2017.12.31
Statistics class  (0) 2017.12.25
Circle class 오퍼레이터 오버로딩  (0) 2017.12.25
Circle class  (0) 2017.12.25
반응형

7.9> 통계를 내는 Statistics 클래스를 만들려고 한다. 데이터는 Statistics 클래스 내부에 int 배열을 동적으로 할당받아 유지한다. 다음과 같은 연산이 잘 이루어지도록 Statistics 클래스와 !, >>, <<, ~ 연산자 함수를 작성하라.

<코드>

#include <iostream>

 

using namespace std;

 

class Statistics{

private:

        int size; //동적 배열의 사이즈

        int index; //마지막 원소의 인덱스

        int *p; //동적 배열을 가리킬 포인터

public:

        Statistics(int size=5,int index=-1); //기본 사이즈 5, 인덱스 -1(nothing)

        friend bool operator!(Statistics op);

        void operator>>(int &op);

        Statistics& operator<<(int op);

        void operator~();

       

};

Statistics::Statistics(int size,int index){

        this->size=size;

        this->index=index;

        p=new int[size]; //size 크기만큼의 int 동적 배열 할당

}

 

bool operator!(Statistics op){

        if(op.index==-1) //index -1이면 true 반환

               return true;

        else

               return false;

}

void Statistics::operator>>(int &op){ //평균구하는 함수

        int sum=0;

        for(int i=0;i<index+1;i++){sum+=p[i];} //실제 원소 개수는 index+1 (0부터시작하니까)

        op= sum/(index+1); //op 평균값 넣어줌(참조라서 원본 바뀜)

}

Statistics& Statistics::operator<<(int op){

        index++; //인덱스 증가

        if(size<=index){ //인덱스를 하나키웠는데 size 크기 같으면 배열 크기보다 원소가 많은 경우

               int *tmp=new int[size+5]; //하나씩 키우면 자주 없앴다 만들었다 해야하니 5개씩 늘린다.

               for(int i=0;i<size;i++){ //(size+5 하면 p크기가 size라서 오류날 있음)

                       tmp[i]=p[i]; //p 원소를 전부 tmp 복사

               }

               delete []p; //p 가리키는 동적배열 힙에 반환

               p=tmp; //tmp 가리키는 배열을 p 가리키게 (어차피 tmp 사라지므로 깊은 복사 필요 없음)

               size+=5; //size 5 추가

        }

        p[index]=op; //index 오퍼랜드 대입

        return *this; //현재 객체의 참조 리턴(이름)

}

void Statistics::operator~(){ //전부 출력하는 함수

        for(int i=0;i<index+1;i++){ //원소 개수 = index+1

               cout<<p[i]<<" ";

        }

        cout<<endl;

}

 

int main(void){

        Statistics stat;

        if(!stat) cout<<"현재 통계 데이타가 없습니다."<<endl;

        int x[5];

        cout <<"5 개의 정수를 입력하라>>";

        for(int i=0;i<5;i++) cin >> x[i]; //x[i] 정수 입력

        for(int i=0;i<5;i++) stat << x[i]; //x[i] 값을 통계 객체에 삽입한다.

        stat << 100 << 200; //100, 200 통계 객체에 삽입한다.

        ~stat; //통계 데이타를 모두 출력한다.

 

        int avg;

        stat>>avg; //통계 객체로부터 평균을 받는다.

        cout<<"avg="<<avg<<endl; //평균을 출력한다.

        return 0;

}

 

<결과창>

               

                    


반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

MyQueue Class  (0) 2017.12.31
Stack class  (0) 2017.12.25
Circle class 오퍼레이터 오버로딩  (0) 2017.12.25
Circle class  (0) 2017.12.25
Matrix class 활용, 연산자 오버로딩  (0) 2017.12.25
반응형

7.8> 문제 7번의 Circle 객체에 대해 다음 연산이 가능하도록 연산자를 구현하라.

<코드>

#include <iostream>

 

using namespace std;

 

class Circle{

private:

        int radius;

public:

        Circle(int radius=0){this->radius=radius;}

        void show(){cout<<"radius = "<<radius<<" "<<endl;}

        friend Circle operator++(Circle &c);

        friend Circle operator++(Circle &c,int a);

        friend Circle operator+(int a,Circle c);

};

 

Circle operator++(Circle &c){ //전위 증가

        c.radius++;

        return c;

 

}

Circle operator++(Circle &c,int a){ //후위 증가

        Circle tmp=c;

        c.radius++;

        return tmp;

}

 

Circle operator+(int a,Circle c){ // 정수+객체 오퍼레이터

        Circle tmp;

        tmp.radius=a+c.radius;

        return tmp;

}

 

int main(void){

        Circle a(5), b(4);

        b=1+a;

        a.show();

        b.show();

        return 0;

}

 

<결과창>



반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

Stack class  (0) 2017.12.25
Statistics class  (0) 2017.12.25
Circle class  (0) 2017.12.25
Matrix class 활용, 연산자 오버로딩  (0) 2017.12.25
Matrix class  (0) 2017.12.25
반응형

7.7> 원을 추상화한 Circle 클래스는 간단히 아래와 같다. 다음 연산이 가능하도록 연산자를 프렌드 함수로 작성하라.

<코드>

#include <iostream>

 

using namespace std;

 

class Circle{

private:

        int radius;

public:

        Circle(int radius=0){this->radius=radius;}

        void show(){cout<<"radius = "<<radius<<" "<<endl;}

        friend Circle operator++(Circle &c);

        friend Circle operator++(Circle &c,int a);

 

};

 

Circle operator++(Circle &c){ //전위 증가

        c.radius++;

        return c;

 

}

Circle operator++(Circle &c,int a){ //후위 증가

        Circle tmp=c;

        c.radius++;

        return tmp;

}

 

int main(void){

        Circle a(5), b(4);

        ++a; //증가 리턴

        b=a++; //대입 증가

        a.show();

        b.show();

        return 0;

}

 

<결과창>



반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

Statistics class  (0) 2017.12.25
Circle class 오퍼레이터 오버로딩  (0) 2017.12.25
Matrix class 활용, 연산자 오버로딩  (0) 2017.12.25
Matrix class  (0) 2017.12.25
Trace 클래스  (0) 2017.12.25
반응형

7.6> 2차원 행렬을 추상화한 Matrix 클래스를 활용하는 다음 코드가 있다.

(1) <<. >> 연산자 함수를 Matrix의 멤버 함수로 구현하라.

<코드1>

#include <iostream>

 

using namespace std;

 

class Matrix{

private:

        int mat[4]; //인트형 배열 크기는 4

public:

        Matrix(int x0=0,int x1=0,int x2=0,int x3=0); // 디폴트 매개변수는 0

        void show();

        void operator>>(int *x);

        void operator<<(int *y);

};

Matrix::Matrix(int x0,int x1, int x2, int x3){

        mat[0]=x0;     mat[1]=x1; //멤버 인트 배열을 매개변수로 차례로 초기화

        mat[2]=x2;     mat[3]=x3;

}

void Matrix::show(){ //출력함수

        cout<<"Matrix = {";

        for(int i=0;i<4;i++) {cout<<" "<<mat[i];}

        cout<<" }"<<endl;

}

 

void Matrix::operator>>(int *x){ //x 가리키는 배열에 객체 배열 원소 차례로 대입

        for(int i=0;i<4;i++){

               x[i]=(this->mat)[i];

        }

}

void Matrix::operator<<(int *y){ //객체 배열에 y 가리키는 배열 원소 차례로 대입

        for(int i=0;i<4;i++){

               (this->mat)[i]=y[i];

        }

}

int main(void){

 

        Matrix a(4,3,2,1), b;

        int x[4], y[4]={1,2,3,4}; //2차원 행렬의 4 원소

        a>>x; //a 원소를 배열 x 복사, x[] {4,3,2,1}

        b<<y; // 배열 y 원소 값을 b 원소에 설정

        for(int i=0;i<4;i++) cout<<x[i]<<" "; //x[]출력

        cout<<endl;

        b.show();

        return 0;

}

(2) <<. >> 연산자 함수를 Matrix의 프렌드 함수로 구현하라.

<코드2>

#include <iostream>

 

using namespace std;

 

class Matrix{

private:

        int mat[4]; //인트형 배열 크기는 4

public:

        Matrix(int x0=0,int x1=0,int x2=0,int x3=0); // 디폴트 매개변수는 0

        void show();

        friend void operator>>(Matrix op1,int *op2);

        friend void operator<<(Matrix &op1,int *op2);

};

Matrix::Matrix(int x0,int x1, int x2, int x3){

        mat[0]=x0;     mat[1]=x1; //멤버 인트 배열을 매개변수로 차례로 초기화

        mat[2]=x2;     mat[3]=x3;

}

void Matrix::show(){ //출력함수

        cout<<"Matrix = {";

        for(int i=0;i<4;i++) {cout<<" "<<mat[i];}

        cout<<" }"<<endl;

}

void operator>>(Matrix op1,int *op2){ //객체의 값을 배열에 대입

        for(int i=0;i<4;i++){

               op2[i]=(op1.mat)[i];

        }

}

void operator<<(Matrix &op1,int *op2){ //배열의 값을 객체에 대입

        for(int i=0;i<4;i++){

               (op1.mat)[i]=op2[i];

        }

}

 

 

int main(void){

 

        Matrix a(4,3,2,1), b;

        int x[4], y[4]={1,2,3,4}; //2차원 행렬의 4 원소

        a>>x; //a 원소를 배열 x 복사, x[] {4,3,2,1}

        b<<y; // 배열 y 원소 값을 b 원소에 설정

        for(int i=0;i<4;i++) cout<<x[i]<<" "; //x[]출력

        cout<<endl;

        b.show();

        return 0;

}

 

<결과창>

   

                                

 

반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

Circle class 오퍼레이터 오버로딩  (0) 2017.12.25
Circle class  (0) 2017.12.25
Matrix class  (0) 2017.12.25
Trace 클래스  (0) 2017.12.25
Random 클래스  (0) 2017.12.25
반응형

7.5> 2차원 행렬을 추상화한 Matrix 클래스를 작성하고, show() 멤버 함수와 다음 연산이 가능하도록 연산자를 모두 구현하라.

(1) 연산자 함수를 Matrix의 멤버 함수로 구현하라.

<코드1>

#include <iostream>

 

using namespace std;

 

class Matrix{

private:

        int mat[4]; //인트형 어레이 mat 크기 4

public:

        Matrix(int x0=0,int x1=0,int x2=0, int x3=0);// 디폴트 매개변수는 0

        void show();

        Matrix operator+(Matrix op);

        Matrix operator+=(Matrix op);

        bool operator==(Matrix op);

};

Matrix::Matrix(int x0,int x1,int x2, int x3){ //생성자, 매개변수를 원소에 대입

        mat[0]=x0;     mat[1]=x1;

        mat[2]=x2;     mat[3]=x3;

}

void Matrix::show(){ //출력함수

        cout<<"Matrix = {";

        for(int i=0;i<4;i++) {cout<<" "<<mat[i];}

        cout<<" }"<<endl;

}

 

Matrix Matrix::operator+(Matrix op){ //+연산자 오버로딩, 각원소를 전부 더해서 객체 리턴

        Matrix tmp;

        for(int i=0;i<4;i++){

               (tmp.mat)[i]=(this->mat)[i]+(op.mat)[i];

        }

        return tmp;

}

 

Matrix Matrix::operator+=(Matrix op){ //+=연산자 오버로딩 객체자신과 op 원소를 각각더해서

        for(int i=0;i<4;i++){                 //자기 자신에 대입하고 객체자신 리턴

               (this->mat)[i]=(this->mat)[i]+(op.mat)[i];

        }

        return *this;

}

 

bool Matrix::operator==(Matrix op){ //==오퍼레이터 오버로딩

        for(int i=0;i<4;i++){

               if((this->mat)[i]!=(op.mat)[i]) return false; //같은 인덱스 원소 비교해서 다르면 바로 false 리턴

        }

        return true; //다맞으면 true 리턴

}

int main(void){

        Matrix a(1,2,3,4), b(2,3,4,5), c;

        c=a+b; //a+b값을 c 대입

        a+=b; //a+b값을 a 대입

        a.show(); b.show(); c.show();

        if(a==c) //ac 같으면..

               cout<<"a and c are the same" <<endl;

 

        return 0;

}

 

(2) 연산자 함수를 Matrix의 프렌드 함수로 구현하라.

<코드2>

#include <iostream>

 

using namespace std;

class Matrix{

private:

        int mat[4]; //인트형 어레이 mat 크기 4

public:

        Matrix(int x0=0,int x1=0,int x2=0, int x3=0);// 디폴트 매개변수는 0

        void show();

        friend Matrix operator+(Matrix op1,Matrix op2);

        friend Matrix operator+=(Matrix &op1,Matrix op2);

        friend bool operator==(Matrix op1,Matrix op2);

};

Matrix::Matrix(int x0,int x1,int x2, int x3){ //생성자, 매개변수를 원소에 대입

        mat[0]=x0;     mat[1]=x1;

        mat[2]=x2;     mat[3]=x3;

}

void Matrix::show(){ //출력함수

        cout<<"Matrix = {";

        for(int i=0;i<4;i++) {cout<<" "<<mat[i];}

        cout<<" }"<<endl;

}

Matrix operator+(Matrix op1,Matrix op2){  // 원소를 더해 나온 행렬을 리턴

        Matrix tmp;

        for(int i=0;i<4;i++){

               (tmp.mat)[i]=(op1.mat)[i]+(op2.mat)[i];

        }

        return tmp;

}

Matrix operator+=(Matrix &op1,Matrix op2){ //레퍼런스op1 쓰는 이유는 op1 바뀌어야해서

        for(int i=0;i<4;i++){                

               (op1.mat)[i]=(op1.mat)[i]+(op2.mat)[i]; //배열의 같은 인덱스 값을 더해서 op1 인덱스원소에 대입

        }

        return op1;

}

bool operator==(Matrix op1,Matrix op2){

               for(int i=0;i<4;i++){

               if((op1.mat)[i]!=(op2.mat)[i]) return false; //같은 인덱스 원소 비교해서 다르면 바로 false 리턴

        }

        return true; //다맞으면 true 리턴

}

 

 

int main(void){

        Matrix a(1,2,3,4), b(2,3,4,5), c;

        c=a+b; //a+b값을 c 대입

        a+=b; //a+b값을 a 대입

        a.show(); b.show(); c.show();

        if(a==c) //ac 같으면..

               cout<<"a and c are the same" <<endl;

 

        return 0;

}

 

<결과창>

       

                            

 

반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

Circle class  (0) 2017.12.25
Matrix class 활용, 연산자 오버로딩  (0) 2017.12.25
Trace 클래스  (0) 2017.12.25
Random 클래스  (0) 2017.12.25
배열 빼기  (0) 2017.12.25
반응형
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
#include <iostream>
using namespace std;
 
class Power{
    int kick;
    int punch;
public:
    Power(int kick=0int punch=0){
        this->kick=kick; this->punch=punch;
    }
    void show();
    friend Power operator+(int op1, Power op2); //정수+객체를 위하여..
    Power operator+(int op2); //객체+정수를 위하여..
};
void Power::show(){
    cout<<"kick="<<kick<<","<<"punch="<<punch<<endl;
}
Power operator+(int op1,Power op2){
    Power tmp;
    tmp.kick=op1+op2.kick;
    tmp.punch=op1+op2.punch;
    return tmp;
}
Power Power::operator+(int op2){
    Power tmp;
    tmp.kick=this->kick + op2;
    tmp.punch=this->punch +op2;
    return tmp;
}
 
int main(void){
    Power a(3,5), b,c;
    a.show();
    b.show();
    c.show();
    b=2+a;
    c=a+3;
    a.show();
    b.show();
    c.show();
}
cs




연산자 오버로딩을 클래스의 멤버 함수로 정의하는 방법과 외부 함수로 정의한 후 프렌드 선언을 해주는 방법이 있는데

operand(피연산자) 순서의 차이를 보인다. 만약 둘 중 한가지 방법만 쓴다면

프로그램 작성시 operand의 순서를 조심해야겠다.

반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

생성자 중복 디폴트 매개 변수  (0) 2017.12.25
virtual 함수  (0) 2017.11.23
모스 부호  (0) 2017.11.09
Book 클래스  (1) 2017.11.09
Accumulator 클래스  (0) 2017.11.09
반응형

다수의 클래스를 선언하고 활용하는 간단한 문제이다. 더하기(+), 빼기(-), 곱하기(*), 나누기(/)를 수행하는 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