Prefix to Postfix conversion with parentheses

1.5k views Asked by At

I am really stuck in make conversion of special prefix to postfix operation on our task, let me describe the task similarly :

We have such operation to use as operations in our prefixes, here is the method that i am checking them :

bool isOperator(string c)
{
    if (c == "log" || c == "exp" || c == "sum" || c == "div" || c == "abs" || c == "sqrt" || c == "sub" || c == "product" || c == "max" || c== "min" || c == "mod" )     // you may add operator here
        return true;
    return false;
}

Anyway example prefix instructions can have parentheses to make operation precedence, this is what I am stuck at. I know, I need to implement such a recursion, but i can't find a way.

div ( sqrt 5 ) 3

Output should be

5 sqrt 3 div

Another example :

div ( sum ( exp 2 3 ) ( sqrt 5 ) ) 3

Output

2 3 exp 5 sqrt sum 3 div

Every operation, parentheses or number should have space between elements in assumed condition.

My stack implementation

Stack.h

#include<iostream>
using namespace std;

struct node {
    string op ;
    node *next;
};

struct Stack {
    node * head;
    void create();
    void close();
    void push (node *);
    node* pop();
    node* top();
    bool isEmpty();

};

Stack.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "stack.h"
#include <iostream>
#include <stdlib.h>

void Stack::create() {
    head = NULL;
}

void Stack::close() {
    node *p;
    while (head) {
        p = head;
        head = head->next;
        //delete [] p->data;
        delete p;
    }
}

void Stack::push(node *newdata) {
    node *newnode = new node;
    newnode = newdata;
    newnode->op = newdata->op;
    newnode->next = head;
    head = newnode;
}

node *Stack::pop() {
    if (isEmpty())
        return NULL;
    node *topnode = head;
    head = head->next;
    //delete topnode;
    return topnode;
}

node *Stack::top() {
    if (isEmpty())
        return NULL;
    node *topnode = head;
    //delete topnode;
    return topnode;
}
bool Stack::isEmpty() {
    return (head == NULL);
}

as @PaulMcKenzie mentioned, i've tried an implementation below, sub_array string array contains the word list without spaces.

bool isLeftParanthesis(string c)
{
    if (c == "(" )   // you may add operator here
        return true;
    return false;
}
bool isRightParanthesis(string c)
{
    if (c == ")")    // you may add operator here
        return true;
    return false;
}
int main()
{
string prefix;
getline(cin, prefix);
istringstream iss(prefix);
istringstream iss2(prefix);
int count1 = 0, count2 = 0;
string postfix = "";
Stack *st = new Stack;
string t1, t2;

string sub;
string *sub_array;
while (iss >> sub) {
    count1++;
}
sub_array = new string[count1];
while (iss2 >> sub) {
    sub_array[count2] = sub;
    count2++;
}
int l = count1;

int right_p_count = 0;
for (int i = 0; i < count1; i++)
{
    if (isRightParanthesis(sub_array[i]))
    {
        right_p_count++;
    }
}
string *postfixes = new string[right_p_count];
int index_right_p = 0;
for (int i = 0; i < count1; i++) {
    while (!isRightParanthesis(sub_array[i]))
    {
        node *n = new node;
        n->op = sub_array[i];
        st->push(n);
        i++;
        if (i == count1)
        {
            break;
        }
    }
    if( i != count1){
    if (isRightParanthesis(sub_array[i])) {
        postfix = "";
        while (!isLeftParanthesis(st->top()->op))
        {
            string t = st->pop();
            if (!isOperator(t) && !isLeftParanthesis(t) && !isRightParanthesis(t)) {
                postfix = t + " " + postfix;
            }
            else if (isOperator(t)) {
                postfix = postfix + " " + t;
            }
        }
        st->pop();
        postfixes[index_right_p] = postfix;
        index_right_p++;
    }
    }
    postfix = "";
    while ( !st->isEmpty() && index_right_p == right_p_count && i == count1)
    {
        string t = st->pop();
        if (!isOperator(t) && !isLeftParanthesis(t) && !isRightParanthesis(t)) {
            postfix = t+" "+postfix;
        }
        else if (isOperator(t)) {
            postfix = postfix+""+t;
        }
        else {
            break;
        }
    }
}

string result = "";
for (int i = 0; i < right_p_count; i++)
{
    result = result + "" + postfixes[i];
}
result = result + " " + postfix;
cout << result << endl;
}

Variable postfix refers to output postfix, but my output is not wrong for some of the operations such as :

div ( sqrt 5 ) 3

When i saw a parantheses i am checking if it is left or right, using right one for trigger.

abs ( product -2 -4 -8 )

Expected output is :

-2 -4 -8 product abs

UPDATE : I solved stack problem myself, but found out that, algorithm calculates some expressions wrongly...

Example expression :

3 2 3 exp sum

Expected output :

sum 3 ( exp 2 3 )

My output :

2 3 exp 3 sum

My algorithm which is using right parentheses as triggers calculates it wrongly and I don't know how to implement this control into that, are there any suggestions ?

0

There are 0 answers