Website Summary Files Commits Branches @commit @diff @stats
path: root / Polymorphism / MathExpression / Operation.hpp
blob: ade84047cc4d499d6bd53b8724e7bdbae71ac107 (plain) (blame)
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
#pragma once
#include "Expression.hpp"
class Operation : public Expression
{

public:
	Expression* left;
	char op;
	Expression* right;
	Operation(Expression* left, char ch, Expression* right)
	{

		this->left = left;
		op = ch;
		this->right = right;
		type = 2;

	}
	void Print()
	{
		std::cout << '(';
		left->Print();
		std::cout << op;
		right->Print();
		std::cout << ')';
	}
	int Evaluate()
	{
		int result = left->Evaluate();
		int value2 = right->Evaluate();

		if (op == '+')
			result += value2;
		else if (op == '*')
			result *= value2;
		else
			return 0; //Invalid operation
		return result;
	}
	~Operation()
	{

		delete left;
		delete right;
		op = '\0';
	}

};