AP8_(2)_Calculator.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 | class Add { int a; int b; public: void setValue(int x, int y); int calculate(); }; class Sub { int a; int b; public: void setValue(int x, int y); int calculate(); }; class Mul { int a; int b; public: void setValue(int x, int y); int calculate(); }; class Div { int a; int b; public: void setValue(int x, int y); int calculate(); }; | cs |
AP8_(2)_Calculator.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 | #include "AP8_(2)_Calculator.h" void Add::setValue(int x, int y) { a = x; b = y; } void Sub::setValue(int x, int y) { a = x; b = y; } void Mul::setValue(int x, int y) { a = x; b = y; } void Div::setValue(int x, int y) { a = x; b = y; } int Add::calculate() { return a + b; } int Sub::calculate() { return a - b; } int Mul::calculate() { return a * b; } int Div::calculate() { return a / b; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; #include "AP8_(2)_Calculator.h" int main() { int x, y; char op; while (1) { cout << "두 정수와 연산자를 입력하세요>>"; cin >> x >> y >> op; switch (op) { case '+':Add a; a.setValue(x, y); cout << a.calculate() << endl; break; case '-':Sub s; s.setValue(x, y); cout << s.calculate() << endl; break; case '*':Mul m; m.setValue(x, y); cout << m.calculate() << endl; break; case '/':Div d; d.setValue(x, y); cout << d.calculate() << endl; break; default:cout << "잘못된 연산자 입니다" << endl; break; } } } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 3장 10번 (0) | 2018.04.28 |
|---|---|
| 명품 C++ Programming 실습문제 3장 9번 (0) | 2018.04.28 |
| 명품 C++ Programming 실습문제 3장 8-(1)번 (0) | 2018.04.28 |
| 명품 C++ Programming 실습문제 3장 7번 (0) | 2018.04.28 |
| 명품 C++ Programming 실습문제 3장 6번 (0) | 2018.04.28 |