#include <iostream>
#include <map>
#include <vector>
std::map<std::string, float> prec;
std::vector<std::string> toInfixArray(std::string input){
std::vector<std::string> arr;
std::string str;
for (int i=0;i<input.size();i++){
if (input.at(i) == '+' || input.at(i) == '-' || input.at(i) == '*' || input.at(i) == '/' || input.at(i) == '(' || input.at(i) == ')' || input.at(i) == '^' || input.at(i) == '%' || input.at(i) == '_' || input.at(i) == '{' || input.at(i) == '}' || input.at(i) == ','){
if (str.length() > 0){arr.push_back(str);}
std::string s(1, input.at(i));
arr.push_back(s);
str = "";
}
else if (input.at(i) == '!' || input.at(i) == '=' || input.at(i) == '<' || input.at(i) == '>'){
if (i+1 < input.length() && input.at(i+1) == '='){
if (str.length() > 0){arr.push_back(str);}
std::string s(1, input.at(i));
s += input.at(i+1);
arr.push_back(s);
i++;
}
else {
if (str.length() > 0){arr.push_back(str);}
std::string s(1, input.at(i));
arr.push_back(s);
}
str = "";
}
else if (input.at(i) == ' '){
if (str.length() > 0){arr.push_back(str);}
str = "";
}
else {
str += input.at(i);
}
}
if (str.length() > 0){arr.push_back(str);}
return arr;
}
std::vector<std::string> toPostfixArray(std::vector<std::string> input){
std::vector<std::string> stack;
std::vector<std::string> postfix;
for (int i=0;i<input.size();i++){
if (prec.find(input[i]) != prec.end()){
if (stack.size() == 0){
stack.push_back(input[i]);
}
else if (prec[stack[stack.size()-1]] < prec[input[i]]){
stack.push_back(input[i]);
}
else if (stack[stack.size()-1] == "^" && input[i] == "^"){
stack.push_back(input[i]);
}
else {
while (stack.size() > 0 && prec[stack[stack.size()-1]] >= prec[input[i]]) {
if (stack[stack.size()-1] == "^" && input[i] == "^"){break;}
std::string last = stack[stack.size()-1];
postfix.push_back(last);
stack.pop_back();
}
stack.push_back(input[i]);
}
}
else if (input[i] == "(" || input[i] == "{"){
stack.push_back(input[i]);
}
else if (input[i] == ")"){
while (stack.size() > 0 && stack[stack.size()-1] != "(") {
std::string last = stack[stack.size()-1];
postfix.push_back(last);
stack.pop_back();
}
if (stack.size() > 2 && stack[stack.size()-2] == "_"){
stack.pop_back();
stack.pop_back();
std::string last = stack[stack.size()-1];
postfix.push_back(last);
}
stack.pop_back();
}
else if (input[i] == "}"){
while (stack.size() > 0 && stack[stack.size()-1] != "{") {
std::string last = stack[stack.size()-1];
postfix.push_back(last);
stack.pop_back();
}
stack.pop_back();
}
else {
postfix.push_back(input[i]);
}
}
while (stack.size() > 0) {
std::string last = stack[stack.size()-1];
postfix.push_back(last);
stack.pop_back();
}
return postfix;
}
int main() {
prec[","]=1;
prec["="]=1.5;
prec["<="]=1.5;
prec[">="]=1.5;
prec["=="]=1.5;
prec["!="]=1.5;
prec[">"]=1.5;
prec["<"]=1.5;
prec["+"]=2;
prec["-"]=2;
prec["*"]=3;
prec["/"]=3;
prec["^"]=4;
prec["%"]=10;
prec["max"]=10;
prec["min"]=10;
prec["sum"]=10;
prec["sin"]=10;
prec["cos"]=10;
prec["tan"]=10;
prec["csc"]=10;
prec["sec"]=10;
prec["cot"]=10;
prec["ln"]=10;
prec["log"]=10;
prec["_"]=11;
std::vector<std::string> arr = toInfixArray("3+14*(5+7)-2");
for (int i=0;i<arr.size();i++){
std::cout << arr[i] << ", ";
}
std::cout << "\n";
std::vector<std::string> postfix = toPostfixArray(arr);
for (int i=0;i<postfix.size();i++){
std::cout << postfix[i] << ", ";
}
}
This repl is an implementation of an algorithm to convert an infix expression into a postfix array in C++.
To view this on TripleLog, click here.
There is just the one main.cpp that runs the algorithm. You can edit it to accept user input or whatever. You probably want the output to be nicer or do something.
Compile the file by running g++ -o main main.cpp in the shell.
Then run the file by running ./main in the shell.