Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int Priority(char x){
    if (x=='+' || x=='-')
        return 1;
    else if (x=='*'||x=='/')
        return 2;
    else if (x=='^')
        return 3;
    else
        return -1;
}
int InfixToPostfix(char expression[]){
    char *e,x;
    Stack<char> stack(10);
    e=expression;
    while(*e!='\0'){
        if ((*e >= 'a' && *e <= 'z') || (*e >= 'A' && *e <= 'Z')
            || (*e >= '0' && *e <= '9'))
            cout<<*e;
        else if (*e=='(')
            stack.Push(*e);
        else if (*e==')'){
            while ((x=stack.Pop())!='(')
                cout<<"x";
        }
        else{
            while(Priority(stack.Top())>= Priority(*e))
                cout<<stack.Pop();
            stack.Push(*e);
        }
 e++;
    }
   
    while (!stack.IsEmpty()){
        cout<<stack.Pop();
    }
}
int main(){
    InfixToPostfix("(a+(b*(d+2)))");
}


What I have tried:

int Priority(char x){
    if (x=='+' || x=='-')
        return 1;
    else if (x=='*'||x=='/')
        return 2;
    else if (x=='^')
        return 3;
    else
        return -1;
}
int InfixToPostfix(char expression[]){
    char *e,x;
    Stack<char> stack(10);
    e=expression;
    while(*e!='\0'){
        if ((*e >= 'a' && *e <= 'z') || (*e >= 'A' && *e <= 'Z')
            || (*e >= '0' && *e <= '9'))
            cout<<*e;
        else if (*e=='(')
            stack.Push(*e);
        else if (*e==')'){
            while ((x=stack.Pop())!='(')
                cout<<"x";
        }
        else{
            while(Priority(stack.Top())>= Priority(*e))
                cout<<stack.Pop();
            stack.Push(*e);
        }
  e++;
    }
  
    while (!stack.IsEmpty()){
        cout<<stack.Pop();
    }
}
int main(){
    InfixToPostfix("(a+(b*(d+2)))");
}
Posted
Updated 26-Apr-22 9:40am
v2
Comments
Richard MacCutchan 26-Apr-22 15:14pm    
Which line causes the error?
Rishabh Vishwakarma 2022 26-Apr-22 15:21pm    
InfixToPostfix("(a+(b*(d+2)))");
Richard MacCutchan 26-Apr-22 15:27pm    
That does not make sense. Which compiler are you using?
Rishabh Vishwakarma 2022 26-Apr-22 15:38pm    
I am using Clion ide and my output is abd2xxx

1 solution

The problem a string literal value is constant and the function is not set up for that. Here is how you can fix it :
C++
int InfixToPostfix( const char expression[] ){
    const char *e;
    Stack<char> stack(10);
    e=expression;
    while(*e!='\0'){
        if ((*e >= 'a' && *e <= 'z') || (*e >= 'A' && *e <= 'Z')
            || (*e >= '0' && *e <= '9'))
            cout<<*e;
        else if (*e=='(')
            stack.Push(*e);
        else if (*e==')'){
            while ((x=stack.Pop())!='(')
                cout<<"x";
        }
        else{
            while(Priority(stack.Top())>= Priority(*e))
                cout<<stack.Pop();
            stack.Push(*e);
        }
    }
    e++;
    while (!stack.IsEmpty()){
        cout<<stack.Pop();
    }
}
It basically amounts to making the function take a const char pointer.
 
Share this answer
 
Comments
Rishabh Vishwakarma 2022 26-Apr-22 15:52pm    
#include <iostream>
#include <algorithm>
using namespace std;

template <class t="">
class Stack{
private:
int top;
int capacity;
T* stack;
public:
Stack(int capacity);
bool IsEmpty()const;
int Size()const;
void Push(const T& data);
T& Pop();
T& Top()const;
};

template <class t="">
Stack<t>::Stack(int stackCapacity):capacity(stackCapacity) {
if (stackCapacity<1)throw "Stack Capacity must be >0";
stack=new T[capacity];
top=-1;
}

template <class t="">
inline bool Stack<t>::IsEmpty() const { return top==-1;}

template <class t="">
inline int Stack<t>::Size() const { return top+1;}

template <class t="">
inline T& Stack<t>::Top() const {
if (IsEmpty())throw "Stack is Empty";
return stack[top];
}
template<class t="">
void ChangeSize(T*&a,const int oldSize,const int newSize){
if (newSize<0)throw "NewLength must be >=0";
T* temp=new T[newSize];
int number= min(oldSize,newSize);
copy(a,a+number,temp);
delete []a;
a=temp;
}

template <class t="">
void Stack<t>::Push(const T &data) {
if (top==capacity-1){
ChangeSize(stack,capacity,2*capacity);
capacity=2*capacity;
}
stack[++top]=data;
}

template <class t="">
T& Stack<t>::Pop(){
if (IsEmpty())throw "Stack is Empty";
return stack[top--];
}

int Priority(char x){
if (x=='+' || x=='-')
return 1;
else if (x=='*'||x=='/')
return 2;
else if (x=='^')
return 3;
else
return -1;
}
int InfixToPostfix( const char expression[] ){
const char *e;
char x;
Stack<char> stack(10);
e=expression;
while(*e!='\0'){
if ((*e >= 'a' && *e <= 'z') || (*e >= 'A' && *e <= 'Z')
|| (*e >= '0' && *e <= '9'))
cout<<*e;
else if (*e=='(')
stack.Push(*e);
else if (*e==')'){
while ((x=stack.Pop())!='(')
cout<<"x";
}
else{
while(Priority(stack.Top())>= Priority(*e))
cout<
CPallini 27-Apr-22 2:01am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900