I'm stuck at comparing the operators, it seems I can only compare the current operator to the topmost of the stack, how do I keep on comparing it til the stack is null or the precedence don't hold true anymore?...i've commented the part where I think my program fails...any help?
#include<iostream>
#include<string>
using namespace std;
void push(char);
char pop(char);
int prec(char);
struct node{
string mdas;
node *nxt;
};
node *start=NULL;
string postf;
int main(){
string inf;
const char *p;
const char *q;
cout<<"Enter string: ";
cin>>inf;
p = inf.c_str();
while(*p!='\0'){
if(*p!='+' && *p!='*' && *p!='/' && *p!='-'){
postf.push_back(*p);
}
else{
if(start==NULL)
push(*p);
else{
q = start->mdas.c_str();
/*if(prec(*q)>prec(*p)){
postf.push_back(pop(*q));
}
else
push(*p);*/
cout<<prec(*q)<<" "<<prec(*p)<<endl;
}
}
p++;
}
cout<<postf<<endl;
while(start!=NULL){
cout<<start->mdas<<" ";
start = start->nxt;
}
cout<<endl;
return 0;
}
void push(char p){
node *temp;
temp = new node;
temp->mdas.push_back(p);
if(start==NULL){
start = temp;
start->nxt = NULL;
}
else{
temp->nxt = start;
start = temp;
}
}
int prec(char a){
switch(a){
case '+':
return 1;
break;
case '-':
return 1;
break;
case '/':
return 2;
break;
case '*':
return 2;
break;
}
}
char pop(char b){
node *del;
del = start;
start = start->nxt;
delete del;
return b;
}