Problem

I have the following problem on my C++ Program and I am not sure why it is not working. I am creating a infix to postfix program through an inherited class which I am not sure it is working. I have been working on this for days and any help would help. Thanks

#include <iostream>
#include <stack>
using namespace std;

int in_stack_Priority(char a){
if(a == '*' || a == '/')
return 2;
if(a== '+' || a == '-')
return 1;
if(a == '(')
return 0;
}

int in_coming_Priority(char a){
if(a == '*' || a == '/')
return 2;
if(a== '+' || a == '-')
return 1;
if(a == '(')
return 4;
}

template <class datatype>
class LLNode{
public:
datatype datavalue;
LLNode *Next;
};

template <class datatype>
class clinkedlist{
private:
LLNode<datatype> *head,
*current;
public:
clinkedlist();
bool listempty(void);
void insertbegin(datatype data);
datatype returnbegin(void);
void removebegin(void);
void moveCurrentbegin(void);
void moveCurrentnext(void);
datatype returnCurrent(void);
bool currentnull(void);
void insertcurrent(datatype datavalue);
void removecurrent(void);
void insertend(datatype data);
datatype returnend(void);
void removeend(void);
};


template<class datatype>
class cstack :public clinkedlist<datatype>{
private:
//Linked List class inherited to make a stack class
public:
cstack();
void push(datatype data);
datatype pop(void);
datatype top(void);
bool stackempty(void);
};

template<class datatype>
cstack<datatype>::cstack(){
clinkedlist<datatype>;
};
template<class datatype>
void cstack<datatype>::push(datatype data){ //push data all defined down here
insertbegin(data);
};

template<class datatype>
datatype cstack<datatype>::pop(void){
datatype tempvalue;
tempvalue = returnbegin();
removebegin();
return tempvalue;
};

template<class datatype>
datatype cstack<datatype>::top(void){
return returnbegin();
};

template<class datatype>
bool cstack<datatype>::stackempty(void){
return listempty();
};

template <class datatype> //Main Class
bool clinkedlist<datatype>::currentnull(void){
bool temp = true;
if (current != NULL){
temp = false;
}
return temp;
}

template <class datatype>
datatype clinkedlist<datatype>::returnCurrent(void){
datatype tempdata;
if (current != NULL){
tempdata = current->datavalue;
}
return tempdata;
}

template <class datatype>
void clinkedlist<datatype>::moveCurrentnext(void){
if(!listempty() && current != NULL){
current = current->Next;
}
};

template <class datatype>
void clinkedlist<datatype>::moveCurrentbegin(void){
current = head;
};

template <class datatype>
clinkedlist<datatype>::clinkedlist(){
head = NULL;
current = NULL;
};

template <class datatype>
bool clinkedlist<datatype>::listempty(void){
bool tempvalue = true;
if (head == NULL){
tempvalue = true;
}else{
tempvalue = false;
}

return tempvalue;

};

template <class datatype>
void clinkedlist<datatype>::insertbegin(datatype data){
LLNode<datatype> *temp;
temp = new LLNode<datatype>;
temp ->datavalue = data;
temp ->Next = head;
head = temp;

};

template <class datatype>
datatype clinkedlist<datatype>::returnbegin(void){
return head->datavalue;
};

template <class datatype>
void clinkedlist<datatype>::removebegin(void){
LLNode<datatype> *temp;
temp = head;
head = head->Next;
delete temp;
};

template<class datatype>
void clinkedlist<datatype>::insertcurrent(datatype datavalue){
if(current != NULL){
if(listempty()){
insertbegin(datavalue);
}else{
LLNode<datatype> *temp;
temp = new LLNode<datatype>;
temp ->datavalue = datavalue;
temp ->Next = current ->Next;
current = temp;
}
}
};


template<class datatype>
void clinkedlist<datatype>::removecurrent(void){
if(current != NULL){
if(current == head){
removebegin();
current = head;
}else{
LLNode<datatype> *temp;
temp = head;
while(temp->Next != current){
temp = temp->Next;
}
temp->Next = current->Next;
delete current;
current = temp;
}
}
};

template<class datatype>
void clinkedlist<datatype>::insertend(datatype data){
if(listempty()){
insertbegin(data);
}else{
LLNode<datatype> *temp;
temp = new LLNode<datatype>;
temp ->datavalue = data;
temp->Next = NULL;
moveCurrentbegin();
while(current->Next != NULL){
moveCurrentnext();
}
current->Next = temp;
}

};


template<class datatype>
datatype clinkedlist<datatype>::returnend(void){
datatype tempdata;
if(!listempty()){
moveCurrentbegin();
while(current->Next != NULL){
moveCurrentnext();
}

tempdata = returnCurrent();
}

return tempdata;
};

template<class datatype>
void clinkedlist<datatype>::removeend(void){
if(!listempty()){
moveCurrentbegin();
while(current->Next->Next != NULL){
moveCurrentnext();
}

delete current->Next;
current->Next = NULL;
}
};


int main()
{
cstack<char> x;
clinkedlist<char> infix, postfix; <inherited class to create program, using Linked List to create the program>
char c;
bool endflag = true;

c = cin.get(); //getting user input
while(c != ';')
{
infix.insertend(c);
}
if(infix.listempty())
{
endflag=false;
}
infix.insertend(';');

infix.moveCurrentbegin();
while(endflag==true){
infix.moveCurrentnext();
if(c == '(' || c== ')'|| c == '+'||c=='-'||c=='*'||c=='/'){

if(c == '('){ //using stack to convert
x.push(c);
}
else{
if(c == ')'){

while(x.top() != '(')
{
c = x.pop();
postfix.insertend(c);
}
}
}

}else{
if(x.stackempty())
x.push(c);
else{
bool flag_x = true; //checking the operator priority. Is it of higher priority or not
while(flag_x== true && !x.stackempty()){
if (in_stack_Priority(x.top())>= in_coming_Priority(c)){
c = x.pop();
postfix.insertend(c);
}else{
flag_x= false;

}
x.push(c);
}
}
}
}
infix.moveCurrentnext();
if(c == ';')If it is ; then the program is set to false, the flag is set to false again
{
endflag = false;
while(!x.stackempty()){ <clears everything in the stack and inserts it
postfix.insertend(c);
}
}
cout<<"The postfix is " << &postfix<<endl;

//outputs postfix.
return 0;
}




Last edited on
That's too much code to wade through without code tags, proper indentation or comments. Please edit your post so that it is readable.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
I have edited the code your help would help
I still don't see code tags or any kind of indentation.
Topic archived. No new replies allowed.