-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarequation.cpp
75 lines (67 loc) · 1.73 KB
/
varequation.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
// Class implentation for VariableEquation
// William Immendorf - 2016
#include <locale>
#include "varequation.hpp"
namespace EquParser
{
VariableEquation::VariableEquation()
{
varx = 0.0;
}
VariableEquation::VariableEquation(const char * equation, const double x) : Equation(), varx(x)
{
infix(equation);
}
VariableEquation::VariableEquation(const std::string & equation, const double x) : Equation(), varx(x)
{
infix(equation);
}
VariableEquation::VariableEquation(const Equation & equation, const double x) : Equation(equation), varx(x)
{
// Left blank
}
// Handle X variable in infix
bool VariableEquation::handle_input(const char c, std::deque<std::string> & output_queue, std::stack<char> & operator_stack, LastAdded last_added) const
{
std::locale loc;
std::string last_term;
bool empty = true;
if (c == 'X' || c == 'x')
{
if (!output_queue.empty())
{
last_term = output_queue.back();
empty = false;
}
output_queue.push_back("X");
// If last term is a digit, mutiply it by X
if (!empty && last_added == Queue && isdigit(last_term[0], loc))
{
operator_stack.push('*');
}
return true;
}
else if (c == '(')
{
// Special case for X variable, add an additional multiply operator if it preceeds the open parenthesis (like a number)
std::string last_term = output_queue.back();
if (last_term[0] == 'X' || isdigit(last_term[0], loc))
{
operator_stack.push('*');
}
operator_stack.push(c);
return true;
}
return false;
}
// Handle X when evaluating equation
bool VariableEquation::handle_term(const std::string term, std::stack<double> & result_stack) const
{
if (term == "X")
{
result_stack.push(varx);
return true;
}
return false;
}
}