Skip to content

Commit ea1cb2b

Browse files
committed
alles omgezet naar functies en structs
1 parent b43b50b commit ea1cb2b

File tree

5 files changed

+54
-65
lines changed

5 files changed

+54
-65
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ enable_testing()
66

77
set(SOURCES
88
main.cpp
9-
# calculator.cpp
9+
calculator.cpp
1010
tokengenerator.cpp
1111
rpngenerator.cpp
1212
vartable.cpp)
1313

1414
set(HEADERS
15-
# calculator.h
15+
calculator.h
1616
tokengenerator.h
1717
rpngenerator.h
1818
vartable.h

calculator.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
#include <iostream>
22
#include "calculator.h"
33

4-
Calculator::Calculator() {}
5-
6-
float Calculator::calc(std::vector<RpnGenerator::RPNToken>& tokenlist){
4+
float calc(std::vector<RPNToken>& tokenlist, VarTable * vartabel){
75
if (tokenlist.size() == 0) return 0.f;
86

97
float res = 0;
108
float v1 = 0;
119
float v2 = 0;
1210

13-
// RpnGenerator::RPNToken last = tokenlist.back();
14-
// tokenlist.pop_back();
15-
1611
cursor--;
1712
if (cursor<0) cout << "\n\n !!! ERROR cursor !!!\n";
18-
RpnGenerator::RPNToken last = tokenlist[cursor];
13+
RPNToken last = tokenlist[cursor];
1914

2015
//remember that operands will be eaten in reverse order
2116
switch (last.opcode) {
2217
case OC::NUM: res = last.value; break;
23-
case OC::VAR: res = vartab->getValue((int)last.value); break;
24-
case OC::MUL: res = calc(tokenlist) * calc(tokenlist); break;
25-
case OC::DIV: v1 = calc(tokenlist); res = calc(tokenlist)/v1; break;
26-
case OC::ADD: res = calc(tokenlist) + calc(tokenlist); break;
27-
case OC::SUB: v1 = calc(tokenlist); res = calc(tokenlist) - v1; break;
28-
case OC::PAS: res = calc(tokenlist); break;
29-
case OC::CHS: res = -calc(tokenlist); break;
30-
case OC::LT: res = calc(tokenlist) > calc(tokenlist); break;
31-
case OC::LE: res = calc(tokenlist) >= calc(tokenlist); break;
32-
case OC::GT: res = calc(tokenlist) < calc(tokenlist); break;
33-
case OC::GE: res = calc(tokenlist) <= calc(tokenlist); break;
34-
case OC::EQ: res = calc(tokenlist) == calc(tokenlist); break;
35-
case OC::NE: res = calc(tokenlist) != calc(tokenlist); break;
36-
case OC::ASS: res = calc(tokenlist);
37-
vartab->setVar(tokenlist[cursor-1].value, res); cursor--; break;
38-
case OC::COL: v1 = calc(tokenlist); v2=calc(tokenlist);
39-
res = calc(tokenlist)?v2:v1; break;
18+
case OC::VAR: res = vartabel->getValue((int)last.value); break;
19+
case OC::MUL: res = calc(tokenlist, vartabel) * calc(tokenlist, vartabel); break;
20+
case OC::DIV: v1 = calc(tokenlist, vartabel); res = calc(tokenlist, vartabel)/v1; break;
21+
case OC::ADD: res = calc(tokenlist, vartabel) + calc(tokenlist, vartabel); break;
22+
case OC::SUB: v1 = calc(tokenlist, vartabel); res = calc(tokenlist, vartabel) - v1; break;
23+
case OC::PAS: res = calc(tokenlist, vartabel); break;
24+
case OC::CHS: res = -calc(tokenlist, vartabel); break;
25+
case OC::LT: res = calc(tokenlist, vartabel) > calc(tokenlist, vartabel); break;
26+
case OC::LE: res = calc(tokenlist, vartabel) >= calc(tokenlist, vartabel); break;
27+
case OC::GT: res = calc(tokenlist, vartabel) < calc(tokenlist, vartabel); break;
28+
case OC::GE: res = calc(tokenlist, vartabel) <= calc(tokenlist, vartabel); break;
29+
case OC::EQ: res = calc(tokenlist, vartabel) == calc(tokenlist, vartabel); break;
30+
case OC::NE: res = calc(tokenlist, vartabel) != calc(tokenlist, vartabel); break;
31+
case OC::ASS: res = calc(tokenlist, vartabel);
32+
vartabel->setVar(tokenlist[cursor-1].value, res); cursor--; break;
33+
case OC::COL: v1 = calc(tokenlist, vartabel); v2=calc(tokenlist, vartabel);
34+
res = calc(tokenlist, vartabel)?v2:v1; break;
4035
default: break;
4136
}
4237
return res;
4338
}
4439

45-
void Calculator::calcandprint(std::vector<RpnGenerator::RPNToken> &tokenlist, bool prt) {
40+
void calcandprint(std::vector<RPNToken> &tokenlist, VarTable * vartabel, bool prt) {
4641
cursor = tokenlist.size();
47-
float result = calc(tokenlist);
42+
float result = calc(tokenlist, vartabel);
4843
if (prt){
4944
std::cout << "EVALUATION RESULT ==> " << std::to_string(result) << std::endl;
50-
vartab->printVarTable();
45+
vartabel->printVarTable();
5146
cout << "_______________________________________________________________________________" << std::endl;
5247
}
5348
}

calculator.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,14 @@
44
#include <functional>
55
#include "rpngenerator.h"
66

7-
class Calculator
8-
{
9-
public:;;
7+
void calcandprint(std::vector<RPNToken> &tokenlist, VarTable * vartabel, bool prt);
8+
float calc(std::vector<RPNToken> &tokenlist, VarTable * vartabel);
109

11-
Calculator();
12-
Calculator(VarTable *vt) : vartab(vt){}
13-
void calcandprint(std::vector<RpnGenerator::RPNToken> &tokenlist, bool prt);
14-
float calc(std::vector<RpnGenerator::RPNToken> &tokenlist);
10+
// VarTable * vartab;
1511

16-
private:
17-
VarTable * vartab;
18-
19-
std::vector<RpnGenerator::RPNToken> inputRPNTokenList;
2012
static float fun_elv(float x, float y, float z){
2113
if (x>0.5) return y; else return z;}
22-
int cursor;
23-
24-
};
25-
14+
static int cursor;
2615

2716

2817
#endif // CALCULATOR_H

main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "vartable.h"
88
#include "tokengenerator.h"
99
#include "rpngenerator.h"
10-
// #include "calculator.h"
10+
#include "calculator.h"
1111
#ifdef WIN32
1212
#include <windows.h>
1313
#endif
@@ -127,7 +127,7 @@ int main(int argc, char *argv[])
127127

128128
vector<RPNToken> tokensRPN = makeRPN(tokenList);
129129

130-
// calc.calcandprint(tokensRPN, true);
130+
calcandprint(tokensRPN, &vvv, true);
131131

132132
} else cout << "choice not allowed" << endl;
133133
}else

rpngenerator.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,40 @@ expr14_assgn ::= var op14 expr13
4444
expr ::= expr14 | expr13
4545
*/
4646

47-
//PASS 2 will scan the output of PASS1 and generate a list of tokens
48-
// precedence is according to https://en.cppreference.com/w/cpp/language/operator_precedence#cite_note-2
49-
// or https://en.wikipedia.org/wiki/Order_of_operations
47+
// PASS 2 will scan the output of PASS1 and generate a list of tokens
48+
// precedence is according to https://en.cppreference.com/w/cpp/language/operator_precedence#cite_note-2
49+
// or https://en.wikipedia.org/wiki/Order_of_operations
5050

51-
52-
struct TkList{
51+
// TkList has the list of input tokens built from the input expression as text
52+
struct TkList
53+
{
5354
vector<Token> tokens;
5455
int cursor = 0;
55-
Token pop(){
56-
if (cursor >= tokens.size()) throw invalid_argument("symbols missing!");
56+
Token pop()
57+
{
58+
if (cursor >= tokens.size())
59+
throw invalid_argument("symbols missing!");
5760
return tokens[cursor++];
5861
};
59-
Token get(int shift){
62+
Token get(int shift)
63+
{
6064
int newCursor = cursor + shift;
61-
if ((newCursor < 0) || (newCursor > tokens.size())) throw invalid_argument("out of reach");
65+
if ((newCursor < 0) || (newCursor > tokens.size()))
66+
throw invalid_argument("out of reach");
6267
return tokens[newCursor];
6368
}
64-
bool done(){
69+
bool done()
70+
{
6571
return (cursor > tokens.size());
6672
}
6773
};
6874

69-
// RPNToken is used in the OUTPUT of RPNizer, en input for the Interpreter
70-
struct RPNToken{
71-
OC opcode;
72-
int arity;
73-
float value;
74-
} ;
75-
76-
vector<RPNToken> makeRPN(vector<Token> tkListIn);
77-
75+
// RPNToken is used in the OUTPUT of RPNizer, en input for the Interpreter
76+
struct RPNToken
77+
{
78+
OC opcode;
79+
int arity;
80+
float value;
81+
};
7882

83+
vector<RPNToken> makeRPN(vector<Token> tkListIn);

0 commit comments

Comments
 (0)