-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.cc
160 lines (154 loc) · 4.36 KB
/
expression.cc
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
/*
* expression.cc - Implementation of a class representing simple arithmetic
* expressions, as declared in expression.h. Part of CPS222
* Project 2.
*
* Skeleton copyright (c) 2001, 2013 - Russell C. Bjork
*
*/
#include "expression.h"
#include <stack>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::stack;
using std::string;
using std::cerr;
/* The following methods are to be written by students, and constitute
* CPS222 Project 2.
*/
string Expression::convertToPostfix(string infix) {
std::stack<char> theStack;
string postfix = "";
bool expectOperand = true;
int infixLength = infix.length();
int tail = infixLength - 1;
for (int i = 0; i < infixLength; i++) {
if (isdigit(infix[i])) {
if (!expectOperand) {
throw SyntaxError(i, "Operator expected");
} else {
postfix += infix[i];
expectOperand = false;
}
} else if (infix[i] == '+' || infix[i] == '-') {
if (expectOperand && infix[i] == '-') {
infix[i] = '#';
theStack.push(infix[i]);
} else if (expectOperand) {
throw SyntaxError(i, "Operand expected");
} else {
while (!theStack.empty() && theStack.top() != '(') {
postfix += theStack.top();
theStack.pop();
}
theStack.push(infix[i]);
expectOperand = true;
}
} else if (infix[i] == ')') {
if (expectOperand) {
throw SyntaxError(i, "Operand expected");
} else {
while (!theStack.empty() && theStack.top() != '(') {
postfix += theStack.top();
theStack.pop();
}
if (theStack.empty()) {
throw SyntaxError(i, "Unbalanced parentheses");
} else {
theStack.pop();
}
}
} else if (infix[i] == '*' || infix[i] == '/') {
if (expectOperand) {
throw SyntaxError(i, "Operand expected");
} else {
while (!theStack.empty() && (theStack.top() == '*' ||
theStack.top() == '/' || theStack.top() == '#')) {
postfix += theStack.top();
theStack.pop();
}
theStack.push(infix[i]);
expectOperand = true;
}
} else if (infix[i] == '(') {
if (!expectOperand) {
throw SyntaxError(i, "Operator expected");
} else {
theStack.push(infix[i]);
}
} else {
throw SyntaxError(i, "Invalid character");
}
}
if (infix[tail] == '+' || infix[tail] == '-' || infix[tail] == '*' ||
infix[tail] == '/' || infix[tail] == '(') {
throw SyntaxError(tail, "Operand expected");
} else if (infixLength == 0) {
throw SyntaxError(0, "Operand expected");
}
while (!theStack.empty()) {
if (theStack.top() == '(') {
throw SyntaxError(tail, "Unbalanced parentheses");
}
postfix += theStack.top();
theStack.pop();
}
return string(postfix);
}
int Expression::evaluate(string postfix) {
std::stack<int> theStack;
int postfixLength = postfix.length();
for (int i = 0; i < postfixLength; i++) {
if (isdigit(postfix[i])) {
theStack.push(postfix[i] - 48);
} else if (postfix[i] == '#') {
int x = theStack.top();
theStack.pop();
theStack.push(-x);
} else {
int x = theStack.top();
theStack.pop();
int y = theStack.top();
theStack.pop();
if (postfix[i] == '+') {
theStack.push(y + x);
} else if (postfix[i] == '-') {
theStack.push(y - x);
} else if (postfix[i] == '*') {
theStack.push(y * x);
} else if (postfix[i] == '/') {
if (x == 0) {
throw DivideByZeroError(i);
} else {
theStack.push(y / x);
}
}
}
}
return theStack.top();
}
string Expression::convertToPrefix(string postfix) {
stack<string> theStack;
int postfixLength = postfix.length();
for (int i = 0; i < postfixLength; i++) {
if (isdigit(postfix[i])) {
theStack.push(postfix.substr(i, 1));
} else if (postfix[i] == '#') {
string op1 = theStack.top();
theStack.pop();
string negation = postfix[i] + op1;
theStack.push(negation);
} else {
string op1 = theStack.top();
theStack.pop();
string op2 = theStack.top();
theStack.pop();
string convert = postfix[i] + op2 + op1;
theStack.push(convert);
}
}
return theStack.top();
}