-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.cpp
199 lines (179 loc) · 6.04 KB
/
Function.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Function.hpp"
#include "Polynomial.hpp"
const char* Function::WrongNumberOfVariablesException::what() const throw()
{
return "too much";
}
Function::BadFunctionDefinition::BadFunctionDefinition(string definition, int error) : _definition(definition), _error(error)
{
}
const char* Function::BadFunctionDefinition::what() const throw()
{
if (this->_error == 0)
return (Computer::init().getSyntaxErrorFlag()
+ "Too many opening brackets in function definition\n"
+ highlightError(_definition, '(')).c_str();
if (this->_error == 1)
return (Computer::init().getSyntaxErrorFlag()
+ "Function definition does not end in ')'\n"
+ highlightError(_definition, 0)).c_str();
if (this->_error == 2)
return (Computer::init().getSyntaxErrorFlag()
+ "Function name should only contain letters\n"
+ highlightError(_definition, 0, 1)).c_str();
if (this->_error == 3)
return (Computer::init().getSyntaxErrorFlag()
+ "Function has no arguments\n"
+ _definition).c_str();
if (this->_error == 4)
return (Computer::init().getSyntaxErrorFlag()
+ "Function argument missing\n"
+ highlightError(_definition, 0, 2)).c_str();
if (this->_error == 5)
return (Computer::init().getSyntaxErrorFlag()
+ "Argument name should only contain letters\n"
+ highlightError(_definition, 0, 1)).c_str();
if (this->_error == 6)
return (Computer::init().getSyntaxErrorFlag()
+ "Left or right part of Opening bracket missing\n"
+ highlightError(_definition, '(')).c_str();
if (this->_error == 7)
return (Computer::init().getSyntaxErrorFlag()
+ " Duplicate arguments in function definition.").c_str();
return "Something went wrong";
}
Function::Function()
{
Symbol::_type = 5;
}
Function::Function(string &expression, string &variable)
{
Symbol::_type = 5;
this->_expression = Expression(expression);
this->_variables = splitString(variable, ',');
}
Function::Function(Function const &src)
{
Symbol::_type = 5;
*this = src;
}
Function::Function(Expression const &expr)
{
Symbol::_type = 5;
this->_expression = expr;
vector<Symbol*> sym = this->_expression.getSymbols();
for (int idx = 0; idx < (int)sym.size(); idx++)
if (sym[idx]->getType() == -1)
this->_usedVariables.insert(toLower(sym[idx]->getName()));
}
bool Function::hasVariable(string variable) const
{
return this->_usedVariables.count(toLower(variable));
}
Function::Function(string definition)
{
Symbol::_type = 5;
vector<string> blocks = splitString(removeWhiteSpace(definition), '(');
if (blocks.size() > 2)
throw BadFunctionDefinition(definition, 0);
if (blocks.size() < 2 || blocks[0].length() == 0 || blocks[1].length() == 0)
throw BadFunctionDefinition(definition, 6);
if (blocks[1].back() != ')')
throw BadFunctionDefinition(definition, 1);
blocks[1] = blocks[1].substr(0,blocks[1].length()-1);
for (int idx = 0; idx < (int)blocks[0].length(); idx++)
if (!(blocks[0][idx] >='a'&& blocks[0][idx] <= 'z') &&
!(blocks[0][idx] >='A'&& blocks[0][idx] <= 'Z'))
throw BadFunctionDefinition(blocks[0], 2);
vector<string> arguments = splitString(blocks[1],',');
if (arguments.size() == 0)
throw BadFunctionDefinition(definition, 3);
for (int idx = 0; idx < (int)arguments.size(); idx++)
{
if (arguments[idx].length() == 0)
throw BadFunctionDefinition(blocks[1], 4);
for (int idx1 = 0; idx1 < (int)arguments[idx].length(); idx1++)
if (!(arguments[idx][idx1] >= 'a' && arguments[idx][idx1] <= 'z') &&
!(arguments[idx][idx1] >= 'A' && arguments[idx][idx1] <= 'Z'))
throw BadFunctionDefinition(arguments[idx], 5);
}
this->_name = blocks[0];
this->_declaration = definition;
this->_variables = arguments;
for (string argument : arguments)
this->_usedVariables.insert(toLower(argument));
if (this->_variables.size() != this->_usedVariables.size())
throw BadFunctionDefinition(definition, 7);
}
void Function::setExpression(Expression expr)
{
this->_expression = expr;
}
string Function::getFName() const
{
return this->_name;
}
string Function::getDeclaration() const
{
return this->_declaration;
}
Expression Function::getExpression() const
{
return this->_expression;
}
Function::~Function()
{
}
Function &Function::operator=(Function const &rhs)
{
Symbol::_type = 5;
this->_expression = rhs._expression;
this->_variables = rhs._variables;
this->_declaration = rhs._declaration;
return *this;
}
Symbol *Function::operator()(vector<Symbol *> const &vars)
{
if (vars.size() != this->_variables.size())
throw WrongNumberOfVariablesException();
map<string, Symbol *> input;
for (int idx = 0; idx < (int)vars.size(); idx++)
{
input[toLower(this->_variables[idx])] = vars[idx];
}
vector<Symbol *> afterReplace;
vector<Symbol *> sym = this->_expression.getSymbols();
for (int idx = 0; idx < (int)sym.size(); idx++)
{
if (sym[idx]->getType() == POLYNOMIAL)
{
Polynomial* p = dynamic_cast<Polynomial*>(sym[idx]);
if (p->_form.size() != 1)
throw;
string name;
for (auto t : p->_form)
{
if (t.first.size() != 1)
throw;
name = t.first[0].first;
}
afterReplace.push_back(input[toLower(name)]);
}
else
afterReplace.push_back(sym[idx]);
}
Expression known = Expression(afterReplace);
return known.evaluate();
}
int Function::getVariablesCount() const
{
return this->_variables.size();
}
string Function::getName() const
{
return this->_name;
}
vector<string> Function::getVariables() const
{
return this->_variables;
}