Repository navigation
Expand file tree
/
Copy pathevaluations.cpp
More file actions
128 lines (112 loc) · 4.24 KB
/
Copy pathevaluations.cpp
File metadata and controls
128 lines (112 loc) · 4.24 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <map>
#include <vector>
#include "evaluations.hpp"
const std::string operations = "+-*x/()";
/*
* Creating an anonimun namespace for private functions of this file
*/
namespace {
int simpleCalculator(int a, int b, char operation){
switch (operation){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '|': return a | b;
case '&': return a & b;
case '^': return a ^ b;
default: return 0;
}
}
//transfor infix expresions to postfix notation for easyer evaluation using Shunting yard algorithm
std::string infix_postfix(std::string infix){
std::string postfix;
std::string operationStack = "";
std::map<char, int> preference = {
{' ', 0}, {'(', 0},
{'+', 2}, {'|', 2}, {'-', 2},
{'*', 3}, {'x', 3}, {'&', 3}, {'^', 3}, {'/', 3},
{'!', 4},
};
for (char character : infix){
if (operations.find(character) == std::string::npos){
postfix += character;
}
else{
if (!operationStack.empty()){
if (character == ')'){
postfix += operationStack.substr(0, operationStack.find('('));
operationStack = operationStack.substr(operationStack.find('(')+1);
continue;
}else if (character == '('){
operationStack = character + operationStack;
continue;
}else if (preference[character] <= preference[operationStack[0]]){
while (!operationStack.empty() && preference[character] < preference[operationStack[0]]){
postfix += operationStack[0];
operationStack = operationStack.substr(1);
}
}
}
operationStack = character + operationStack;
}
}
postfix += operationStack;
return evaluations::replace(postfix, ' ');
}
}
namespace evaluations{
std::string normalise(std::string function, char operation){
std::string ascii_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string normaliced_function = "";
bool is_variable = false;
bool close_blacket = false;
for (char letter : function){
if (ascii_letters.find(letter) != std::string::npos && !is_variable){
is_variable = true;
}else if (ascii_letters.find(letter) != std::string::npos && is_variable){
normaliced_function += operation;
}else if ((letter == '!' || letter == '(') && is_variable){
normaliced_function += operation;
is_variable = false;
}else{
is_variable = false;
}
if (letter == ')' && !close_blacket){
close_blacket = true;
}else if (close_blacket && (is_variable || letter == '(' || letter == '!')){
normaliced_function += operation;
close_blacket = false;
}else{
close_blacket = false;
}
normaliced_function += letter;
}
return normaliced_function;
}
std::string replace(std::string texto, char remplazar, char remplazo){
for (char& letra : texto){
if (letra == remplazar){
letra = remplazo;
}
}
return texto;
}
int evaluate(std::string function){
std::string postfixFunction = infix_postfix(function);
std::vector<char> stack;
for (char simbol : postfixFunction){
if (operations.find(simbol) == std::string::npos){
stack.push_back(simbol);
}else{
int a = stack.back() - '0';
stack.pop_back();
int b = stack.back() - '0';
stack.pop_back();
stack.push_back(simpleCalculator(b, a, simbol) + '0');
}
}
return stack[0] - '0';
}
}