Repository navigation
Expand file tree
/
Copy pathin2post.cpp
More file actions
155 lines (137 loc) · 4.35 KB
/
Copy pathin2post.cpp
File metadata and controls
155 lines (137 loc) · 4.35 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
148
149
150
151
152
153
154
155
// Assignment 3 COP4530
// Ryan Winter
// 10/26/17 (Fall 2017)
#include "stack.h"
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
using namespace cop4530;
int checkPrecedence(string str);
bool alphaTest(string str);
void infix2postfix(vector<string> &infix, vector<string> &outputVector);
void convertTo(vector<string> &outputVector);
void print(vector<string> &outputVector);
int main(){
vector<string> inputVector;
vector<string> outputVector;
string userInput;
do{
cout << "Enter infix expression (\"exit\" to quit): ";
getline(cin, userInput); // get user input
outputVector.clear(); // need to clear out old data or it will tag along
inputVector.clear(); // need to clear out old data or it will tag along
stringstream sstream(userInput); // create stringstream of the userInput
while (sstream >> userInput) // use stringstream to tokenize userInput
inputVector.push_back(userInput);
cout << "Postfix expression: ";
infix2postfix(inputVector, outputVector);
cout << "Postfix evaluation: ";
convertTo(outputVector);
//cout << "Enter infix expression (\"exit\" to quit): ";
}while(userInput != "exit");
return 0;
}
int checkPrecedence(string str){ // test for each operator and assign it a number (using pemdas)
if(str == "("|| str == ")")
return (4);
else if(str == "^")
return (3);
else if(str == "*" || str == "/")
return (2);
else if(str == "+" || str == "-")
return (1);
else
return 0;
}
void print(vector<string> &outputVector){ // print output expression
for(int i = 0; i < int(outputVector.size()); i++)
cout << outputVector[i] << " ";
}
bool alphaTest(string str){
for(int i = 0; i < int(str.size()); i++)
if(isalnum(str[i]) == false) // check if alphanumeric
return false;
return true;
}
void infix2postfix(vector<string> &infix, vector<string> &outputVector){ // sourced from rescitation slides
Stack<string> s;
for(int i = 0; i < int(infix.size()); ++i){ // needs to be ++i
if(checkPrecedence(infix[i]) != 0){
if(((s.empty() == false) && (s.top() != "(")) && (checkPrecedence(s.top()) >= checkPrecedence(infix[i]))){
while (((s.empty() == false) && (s.top() != "(")) && (checkPrecedence(s.top()) >= checkPrecedence(infix[i]))){
outputVector.push_back(s.top());
cout << s.top() << " ";
s.pop();
}
}
s.push(infix[i]);
if(infix[i] == ")"){
s.pop(); // pop the ) right away
while(s.top() != "("){
cout << s.top() << " ";
outputVector.push_back(s.top());
s.pop();
}
s.pop(); // must be a ( so pop it
}
}
else if(alphaTest(infix[i]) == true){
outputVector.push_back(infix[i]);
cout << infix[i] << " ";
}
}
while(s.empty() == false){
cout << s.top() << " ";
outputVector.push_back(s.top());
s.pop();
}
cout << endl;
}
void convertTo(vector<string> &outputVector){
Stack<string> stack;
bool isNum = false;
for(int i = 0; i < int(outputVector.size()); i++){
if(alphaTest(outputVector[i]) == true){
for(int j = 0; j < int(outputVector[j].size()); j++){
if((isdigit(outputVector[i][j]) == true) || (outputVector[i][j] == '.')) // account for decimal number for use of floats/doubles
isNum = true;
else
break;
}
}
}
if(isNum == true){
for(int i = 0; i < int(outputVector.size()); i++){
if(alphaTest(outputVector[i]) == true) // if it is valid alphanumeric then push to stack
stack.push(outputVector[i]);
if(checkPrecedence(outputVector[i]) != 0){ // if it is a *, /, +, -, ^, (, or )
float tempFloat;
float first = stof(stack.top()); // convert string to float
stack.pop(); // pop item and convert next (and pop it too)
float second = stof(stack.top());
stack.pop();
if(outputVector[i][0] == '*')
tempFloat = second * first;
else if(outputVector[i][0] == '/')
tempFloat = second / first;
else if(outputVector[i][0] == '+')
tempFloat = second + first;
else if(outputVector[i][0] == '-')
tempFloat = second - first;
//else if(outputVector[i][0] == '^')
//tempFloat = second^first;
stack.push(to_string(tempFloat)); // convert to string then push string to stack
}
}
print(outputVector);
cout << "= " << stack.top(); // print out answer
cout << endl; // formatting
}
else{ // if not a num
print(outputVector);
cout << "= "; // prints equal sign
print(outputVector);
cout << endl; // formatting
}
}