forked from gustavogutierrezutp/is304
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cpp
More file actions
147 lines (128 loc) · 3.56 KB
/
eval.cpp
File metadata and controls
147 lines (128 loc) · 3.56 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cctype>
#include <iostream>
#include <stack>
#include <stdexcept>
#include <string>
using namespace std;
// Function to check if a character is an operator
bool isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; }
// Function to get precedence of operators
int precedence(char op) {
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}
// Function to perform operation
int applyOp(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw runtime_error("Division by zero");
return a / b;
default:
throw runtime_error("Invalid operator");
}
}
// Function to evaluate expression using stack
int evaluateExpression(const string &expression) {
stack<int> values; // Stack for numbers
stack<char> ops; // Stack for operators
for (int i = 0; i < expression.length(); i++) {
char c = expression[i];
// Skip whitespace
if (isspace(c))
continue;
// If current char is a digit, parse the full number
if (isdigit(c)) {
int num = 0;
while (i < expression.length() && isdigit(expression[i])) {
num = num * 10 + (expression[i] - '0');
i++;
}
values.push(num);
cout << "Pushed number: " << num << endl;
i--; // Adjust for loop increment
}
// If current char is an opening parenthesis
else if (c == '(') {
ops.push(c);
cout << "Pushed '(': " << c << endl;
}
// If current char is a closing parenthesis
else if (c == ')') {
while (!ops.empty() && ops.top() != '(') {
int val2 = values.top();
values.pop();
int val1 = values.top();
values.pop();
char op = ops.top();
ops.pop();
int result = applyOp(val1, val2, op);
values.push(result);
cout << "pushed result: " << result << endl;
}
if (!ops.empty())
ops.pop(); // Pop '('
else
throw runtime_error("Mismatched parentheses");
}
// If current char is an operator
else if (isOperator(c)) {
//cout << "Current operator: " << c << endl;
while (!ops.empty() && precedence(ops.top()) >= precedence(c)) {
cout << "inside the while" << endl;
int val2 = values.top();
values.pop();
int val1 = values.top();
values.pop();
char op = ops.top();
ops.pop();
values.push(applyOp(val1, val2, op));
}
ops.push(c);
cout << "Pushed operator: " << c << endl;
}
else {
throw runtime_error("Invalid character in expression");
}
}
// Process remaining operators
while (!ops.empty()) {
if (ops.top() == '(' || ops.top() == ')')
throw runtime_error("Mismatched parentheses");
int val2 = values.top();
values.pop();
int val1 = values.top();
values.pop();
char op = ops.top();
ops.pop();
int result = applyOp(val1, val2, op);
values.push(result);
cout << "Computing " << val1 << " " << op << " " << val2 << " = " << result << endl;
cout << "pushed result: " << result << endl;
}
if (values.size() != 1)
throw runtime_error("Invalid expression");
return values.top();
}
// Main function
int main() {
string expr;
cout << "Enter an arithmetic expression (e.g., 3+5*(2-8)): ";
getline(cin, expr);
try {
int result = evaluateExpression(expr);
cout << "Result: " << result << endl;
} catch (const exception &e) {
cerr << "Error: " << e.what() << endl;
}
return 0;
}