E D R , A S I H C RSS

Class로 계산기 짜기

~cpp

#include <iostream>
using namespace std;

class Memory
{
private:
	int firstNumber;
	int secondNumber;
	int resultNumber;
	char sign;
public:
	void setFirstNumber(int firstNumber){ this->firstNumber = firstNumber;}
	void setSecondNumber(int secondNumber){ this->secondNumber = secondNumber;}
	void setResultNumber(int resultNumber){ this->resultNumber = resultNumber;}
	void setSign(char sign){ this->sign = sign;}

	int getFirstNumber(){ return firstNumber;}
	int getSecondNumber(){ return secondNumber;}
	int getResultNumber(){ return resultNumber;}
	char getSign(){	return sign;}
};

class NumberInputer
{
public:
	void pushButton(Memory * memory)
	{
		inputFirstNumber(memory);
		inputSecondNumber(memory);
		inputSign(memory);

	}
	void inputFirstNumber(Memory * memory)
	{
		int firstNumber;
		cin >> firstNumber;
		memory->setFirstNumber(firstNumber);
	}
	void inputSecondNumber(Memory * memory)
	{
		int secondNumber;
		cin >> secondNumber;
		memory->setSecondNumber(secondNumber);
	}
	void inputSign(Memory * memory)
	{
		char sign;
		cin >> sign;
		switch(sign)
		{
		case '+':
		case '-':
		case '*':
		case '/':
			memory->setSign(sign);
			break;
		default:
			cout << "입력이 잘못되었습니다." << endl;
			exit(0);
		}
	}
};

class ComputeDevice
{
public:
	void compute(Memory * memory)
	{
		switch(memory->getSign())
		{
		case '+':
			memory->setResultNumber(memory->getFirstNumber() + memory->getSecondNumber());
			break;
		case '-':
			memory->setResultNumber(memory->getFirstNumber() - memory->getSecondNumber());
			break;
		case '*':
			memory->setResultNumber(memory->getFirstNumber() * memory->getSecondNumber());
			break;
		case '/':
			memory->setResultNumber(memory->getFirstNumber() / memory->getSecondNumber());
			break;
		}
	}

};

class Lcd
{
public:
	void output(Memory * memory) { cout << memory->getResultNumber() << endl;}
};

class Calculator
{
private:
	NumberInputer numberInputer;
	ComputeDevice computeDevice;
	Lcd lcd;
	Memory * memory;
public:
	Calculator() { memory = new Memory; }
	void create() {
		numberInputer.pushButton(memory);
		computeDevice.compute(memory);
		lcd.output(memory);
	}
	~Calculator() { delete memory; }
};

int main()
{
	Calculator calculator;
	calculator.create();
	return 0;
}


상욱형이 짜주신 계산기 메인은 짧은데 클래스 길이가;;;
  • 그냥 보여주기 위한 코드에요. 결코 올바른 프로그래밍은 아니라는거 명심하세요^^; - 상욱
MFCStudy2006/Class로 계산기 짜기 <- 이런 이름이 더 좋을듯 싶네요^^ - 아영
----
MFC Study 2006
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0886 sec