-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.h
67 lines (58 loc) · 2.05 KB
/
variables.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
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
#pragma once
#include <iostream>
#include <map>
#include <set>
#include "types.h"
using namespace std;
class VariableContainer {
public:
static void addVar(string variableName, number value) {
storeVariableName(variableName);
_numVariables.insert_or_assign(variableName, value);
}
static void addVar(string variableName, string value) {
storeVariableName(variableName);
_strVariables.insert_or_assign(variableName, value);
}
static number getVarNum(string variableName) {
if (_numVariables.contains(variableName)) {
return _numVariables.at(variableName);
} else if (!_strVariables.contains(variableName)) {
return 0;
} else {
cout << "Zmienna " << variableName << " jest stringiem, a nie liczbą!" << endl;
exit(-1);
}
}
static string getVarStr(string variableName) {
if (_strVariables.contains(variableName)) {
return _strVariables.at(variableName);
} else if (!_numVariables.contains(variableName)) {
return "";
} else {
cout << "Zmienna " << variableName << " jest liczbą, a nie stringiem!" << endl;
exit(-1);
}
}
static void printVar(string variableName) {
if (_strVariables.contains(variableName)) {
cout << getVarStr(variableName) << endl;
} else if (_numVariables.contains(variableName)) {
cout << getVarNum(variableName) << endl;
} else {
cout << "" << endl;
}
}
private:
static void storeVariableName(string variableName) {
if (_variableNames.contains(variableName)) {
_numVariables.erase(variableName);
_strVariables.erase(variableName);
} else {
_variableNames.insert(variableName);
}
}
inline static set<string> _variableNames;
inline static map<string, number> _numVariables;
inline static map<string, string> _strVariables;
};