Repository navigation
Expand file tree
/
Copy pathutility.h
More file actions
118 lines (96 loc) · 3.26 KB
/
Copy pathutility.h
File metadata and controls
118 lines (96 loc) · 3.26 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
//
// Created by User on 09.06.2022.
//
#ifndef MAIN_CPP_UTILITY_H
#define MAIN_CPP_UTILITY_H
#include <sstream>
#include "shunting_yard.h"
float roundValue(float number) { // round the number
return round(number * 10) / 10;
}
string numberToString(float number) {
stringstream ss; // create a stringstream
string str; // create string
ss << number; // add number to the stream
ss >> str; // push the contents onto a string
return str; // return the string
}
double stack_calc(queue<Token>& rpn_tokens, double x) { // here the values are taken out one by one,
double result; // and the solution stays on the stack at the top
stack <double> stack_;
while (!rpn_tokens.empty()) {
Token token = rpn_tokens.front();
if (token.type == Token_type::NUMBER) {
double number = stof(token.value);
stack_.push(number);
}
else if (token.type == Token_type::CONSTANT) {
double constant;
if (token.value == "e") {
constant = M_E;
stack_.push(constant);
}
if (token.value == "x" || token.value == "X") { // additional ability to using uppercase X
constant = x; // This can also be done for all constants and
stack_.push(constant); // functions, but I don't see the point.
}
if (token.value == "pi") {
constant = M_PI;
stack_.push(constant);
}
}
else if (token.type == Token_type::OPERATOR) {
double a = stack_.top();
stack_.pop();
double b = stack_.top();
stack_.pop();
if (token.value == "+") {
stack_.push(a + b);
}
if (token.value == "-") {
stack_.push(b - a);
}
if (token.value == "*") {
stack_.push(a * b);
}
if (token.value == "/") {
stack_.push(b / a);
}
if (token.value == "^") {
stack_.push(pow(b, a));
}
}
else if (token.type == Token_type::FUNCTION) {
double c = stack_.top();
stack_.pop();
if (token.value == "sqrt") {
stack_.push(sqrt(c));
}
if (token.value == "sin") {
stack_.push(sin(c));
}
if (token.value == "cos") {
stack_.push(cos(c));
}
if (token.value == "tan") {
stack_.push((sin(c) / cos(c)));
}
if (token.value == "cot") {
stack_.push((cos(c) / sin(c)));
}
if (token.value == "ln") {
stack_.push((log(c)));
}
}
rpn_tokens.pop();
}
result = stack_.top();
return result;
}
double evaluate(string& expression,double x) {
vector <Token> tokens = parse(expression);
vector <Token> tokens_ = parseNegativeNumbers(tokens);
queue <Token> rpn_tokens = RPN(tokens_);
return stack_calc(rpn_tokens,x);
}
#endif //MAIN_CPP_UTILITY_H