-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelement.h
More file actions
87 lines (69 loc) · 2.38 KB
/
element.h
File metadata and controls
87 lines (69 loc) · 2.38 KB
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
#ifndef ELEMENT_H_INCLUDED
#define ELEMENT_H_INCLUDED
#include <iostream>
#include <memory>
#include <string>
#include <map>
//! käytetään std::map kirjastoa nimellä Valuation
using Valuation = std::map<char,int>;
//!\brief Element luokka
//! abstrakti yläluokka
class Element{
public:
virtual ~Element()=default;
//!\brief funktio tekee pointterin kyseisen Elementin alaluokan olioon
//!\return pointteri Element olioon
virtual Element* clone() const = 0;
//!\brief muuttaa olion arvon string muotoon
//!\return olion arvo stringinä
virtual std::string toString() const = 0;
//!\brief antaa kyseisen olion numerisen arvon
//!\param std::map jonka avulla päätellään numerinen arvo jos Element olio on kirjain
//!\return olion arvo kokonaislukuna (tai kyseistä kirjainta vastaava kokonaisluku)
virtual int evaluate(const Valuation& val) const = 0;
};
template <typename T>
class TElement : public Element{
private:
T val;
public:
TElement(): val(0){};
TElement(T t): val(t){};
virtual ~TElement() = default;
T getVal() const{return val;}
void setVal(T t){val = t;}
Element* clone() const{return new TElement<T>(val);}
std::string toString() const;
int evaluate(const Valuation& v) const;
bool operator==(const TElement<T>& i) const{
if(val == i.val) return true;
return false;
}
TElement<T>& operator+=(const TElement<T>& i);
TElement<T>& operator-=(const TElement<T>& i);
TElement<T>& operator*=(const TElement<T>& i);
};
using IntElement = TElement<int>;
using VariableElement = TElement<char>;
//!\brief Yhteiset operaatiot
//!\brief overload operator <<
//!\param joku tulostusstreami
//!\param vakio Elementin oliosta
//!\return ostream olio tulostusstreamiin
std::ostream& operator<<(std::ostream& out, const Element& i);
//!\brief overload operator +
//!\param vakio IntElementin oliosta
//!\param vakio toisesti IntElementin oliosta
//!\return väliaikainen olio
IntElement operator+(const IntElement& a, const IntElement& i);
//!\brief overload operator -
//!\param vakio IntElementin oliosta
//!\param vakio toisesti IntElementin oliosta
//!\return väliaikainen olio
IntElement operator-(const IntElement& a, const IntElement& i);
//!\brief overload operator *
//!\param vakio IntElementin oliosta
//!\param vakio toisesti IntElementin oliosta
//!\return väliaikainen olio
IntElement operator*(const IntElement& a, const IntElement& i);
#endif // ELEMENT_H_INCLUDED