/************************************
* Data Structures & Algotihms *
* Project 1 *
* Levent Kaya *
* 1800003183 *
************************************/
#include <iostream>
#include <string>
#include "myStack.h"
#include "stackADT.h"
using namespace std;
class infixToPostfix{
public:
void convertToPostfix();
bool precedence(char opr1, char opr2);
void getInfix(string);
void showInfix();
void showPostfix();
infixToPostfix(string);
string getPfx();
int evaluatePostfix(char* exp);
private:
string infx;
string postfx;
};
bool infixToPostfix::precedence(char opr1, char opr2){
char c1 = opr1, c2 = opr2;
int flag1 = 0, flag2 = 0;
if(c1 == '(' || c1 == ')'){
flag1 = 0;
}
else if(c2 == '(' || c2 == ')'){
flag2 = 0;
}
else if(c1 == '+' || c1 == '-'){
flag1 = 1;
}
else if(c1 == '*' || c1 == '/'){
flag1 = 2;
}
else if(c2 == '+' || c2 == '-'){
flag2 = 1;
}
else if(c2 == '*' || c2 == '/'){
flag2 = 2;
}
if(flag1 >= flag2){
return true;
}
else
return false;
}
string infixToPostfix::getPfx(){
return postfx;
}
void infixToPostfix::showPostfix(){
cout << getPfx() << endl;
}
void infixToPostfix::getInfix(string inputInfix){
infx = inputInfix;
}
void infixToPostfix::showInfix(){
cout << infx << endl;
}
int checkSymOperator(char symbol){
if(symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'){
return 1;
}
else
{
return 0;
}
}
int checkSymOperand(char symbol){
if(isalpha(symbol) || isdigit(symbol)){
return 1;
}
else{
return 0;
}
}
void infixToPostfix::convertToPostfix(){
stackType<char> stack;
postfx = "";
infx.append(")");
stack.push('(');
for (int i = 0; i < infx.length(); i++){
if(checkSymOperator(infx[i])){
if(checkSymOperator(stack.top())){
if(precedence(infx[i], stack.top()) == 0){
postfx = postfx + stack.top();
stack.pop();
}
else if(precedence(infx[i], stack.top()) == 1){
stack.push(infx[i]);
}
else{
postfx = postfx + stack.top();
stack.pop();
}
}
else{
stack.push(infx[i]);
}
}
else{
if(infx[i] = ')'){
while (stack.top() != '('){
postfx = postfx + stack.top();
stack.pop();
}
stack.pop();
}
else{
postfx = postfx + infx[i];
}
}
}
}
infixToPostfix::infixToPostfix(string pref){
pref = "";
}
int main(){
string input;
infixToPostfix inf2post;
cout << "Infix Expression: ";
cin >> input;
inf2post.getInfix(input);
inf2post.convertToPostfix();
cout << "Postfix Expression: ";
inf2post.showPostfix();
cout << endl;
return 0;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}