Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
92 lines (71 loc) · 1.62 KB
/
Copy pathmain.cpp
File metadata and controls
92 lines (71 loc) · 1.62 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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <cctype>
#include "ArrayStack.h"
using namespace std;
// Token
struct Token {
string value; // number, operator, or parenthesis
};
// Tokenizer
vector<Token> tokenize(const string& line) {
vector<Token> tokens;
// TODO
return tokens;
}
// Helpers
bool isOperator(const string& s) {
return s == "+" || s == "-" || s == "*" || s == "/";
}
int precedence(const string& op) {
// TODO
return 0;
}
// Detection
bool isValidPostfix(const vector<Token>& tokens) {
// TODO
return false;
}
bool isValidInfix(const vector<Token>& tokens) {
// TODO
return false;
}
// Conversion
vector<Token> infixToPostfix(const vector<Token>& tokens) {
vector<Token> output;
// TODO
return output;
}
// Evaluation
double evalPostfix(const vector<Token>& tokens) {
ArrayStack<double> stack;
// TODO
return 0.0;
}
// Main
int main() {
string line;
getline(cin, line);
vector<Token> tokens = tokenize(line);
if (isValidPostfix(tokens)) {
cout << "FORMAT: POSTFIX\n";
cout << "RESULT: " << evalPostfix(tokens) << "\n";
}
else if (isValidInfix(tokens)) {
vector<Token> postfix = infixToPostfix(tokens);
cout << "FORMAT: INFIX\n";
cout << "POSTFIX: ";
for (const auto& t : postfix) {
cout << t.value << " ";
}
cout << "\n";
cout << "RESULT: " << evalPostfix(postfix) << "\n";
}
else {
cout << "FORMAT: NEITHER\n";
cout << "ERROR: invalid expression\n";
}
return 0;
}