본문 바로가기

studio/programmazione

[C/C++] OPP의 개요 - Project

#include<iostream>

using namespace std;



class Cal{

public:

int ad, su, mu, di;

public:

void init();

double add(double, double);

double sub(double, double);

double mul(double, double);

double div(double, double);

void showCount();

};



void Cal::init()

{

 ad=0;

 su=0;

 mu=0;

 di=0;

}



int main()

{

char op;

double a, b;

Cal c;

c.init();

while(1)

{

printf("Select operator\n[ +  -  *  /  Q ]\n==> ");

cin>>op;

switch(op){

case '+':

cout<<"Input two number : ";

cin>>a>>b;

cout<<a<<"+"<<b<<"="<<c.add(a, b)<<endl;

break;

case '-':

cout<<"Input two number : ";

cin>>a>>b;

cout<<a<<"-"<<b<<"="<<c.sub(a, b)<<endl;

break;

case '*':

cout<<"Input two number : ";

cin>>a>>b;

cout<<a<<"*"<<b<<"="<<c.mul(a, b)<<endl;

break;

case '/':

cout<<"Input two number : ";

cin>>a>>b;

cout<<a<<"/"<<b<<"="<<c.div(a, b)<<endl;

break;

case 'Q':

c.showCount();

exit(1);

break;

default:

cout<<"Please select correct operator!"<<endl;

}

}

return 0;

}




double Cal::add(double a, double b)

{

ad++;

return a+b;

}


double Cal::sub(double a, double b)

{

su++;

return a-b;

}



double Cal::mul(double a, double b)

{

mu++;

return a*b;

}



double Cal::div(double a, double b)

{

di++;

return a/b;

}




void Cal::showCount()

{

cout<<"+ : "<<ad<<endl;

cout<<"- : "<<su<<endl;

cout<<"* : "<<mu<<endl;

cout<<"/ : "<<di<<endl;

}





____________________________________________________________________________________________



멀고도 험난한 C++의 길...