I am writing a program that will convert an expression from infix notation to postfix notation for a class project. My program compiles fine but when it tries the conversion, it sometimes stops working and sometimes it just continues to run without doing anything. Here is my entire code:
#include <iostream>
#include <string>
usingnamespace std;
struct node{
char data;
node* next;
};
struct head{
int count;
node* top;
};
class stack{
private:
head h;
public:
stack(); //default constructor
~stack(); //destructor
void push(char); //adds a character to the top of the stack
char pop(); //removes a character from the top of the stack, and returns the character value or NULL if stack is empty
char stackTop(); //returns the character at the top of the stack without removing it
bool isEmpty(); //returns true if stack is empty
};
stack::stack(){
h.count = 0;
h.top = NULL;
}
stack::~stack(){
node *current = h.top, *previous;
while(current->next != NULL){
previous = current;
current = current->next;
delete previous;
}
delete current;
}
void stack::push(char c){
node* newnode = new node;
if(h.top == NULL){
newnode->data = c;
newnode->next = NULL;
h.top = newnode;
h.count = 1;
}
else{
newnode->data = c;
newnode->next = h.top;
h.top = newnode;
h.count++;
}
}
char stack::pop(){
if(isEmpty())
return NULL;
char c;
c = h.top->data;
if(h.top->next == NULL)
h.top = NULL;
else
h.top = h.top->next;
h.count--;
return c;
}
char stack::stackTop(){
if(h.top == NULL)
return NULL;
return h.top->data;
}
bool stack::isEmpty(){
if(h.top == NULL)
returntrue;
returnfalse;
}
class expression{
private:
string infix;
string postfix;
public:
expression(); //default constructor
void setInfix(); //changes the infix expression
string getInfix(); //returns infix expression
void updatePostfix(); //updates postfix expression based on infix expression
string getPostfix(); //returns postrix expression
};
expression::expression(){
infix = "";
postfix = "";
}
void expression::setInfix(){
cout << "Type in your infix expression:" << endl;
cin >> infix;
}
string expression::getInfix(){
return infix;
}
void expression::updatePostfix(){
stack s;
string str = "";
for(int i = 0; i < infix.length(); i++){ //go through each character
char c = infix.at(i);
if(c == '(') //add ( to the stack
s.push(c);
elseif(c == ')'){
while(s.stackTop() != '('); //extract characters from the stack until a ( is met
str += s.pop();
s.pop(); //get rid of the (
}
elseif(c == '*' || c == '/') //has the highest priority, so push it to the stack
s.push(c);
elseif(c == '+' || c == '-'){ //has a lower priority
if(s.stackTop() == '*' || s.stackTop() == '/'){//check if the top of the stack has a higher priority
while(s.stackTop() != '(' && !s.isEmpty())//if so, extract characters until the stack is empty or a ( is met
str += s.pop();
s.pop(); //will do nothing if stack is empty, will get rid of the ( if not
}
else //top of stack is not a higher priority, so push the + or - to the stack
s.push(c);
}
else //add the operand to the final string
str += c;
}
while(!s.isEmpty()) //finish extracting operators
str += s.pop();
postfix = str;
}
string expression::getPostfix(){
return postfix;
}
int main(){
int select;
expression e;
do{
system("CLS");
cout << "Please select one of the following:" << endl;
cout << "1. Change infix expression" << endl;
cout << "2. Show infix expression" << endl;
cout << "3. Update postfix expression" << endl;
cout << "4. Show postfix expression" << endl;
cout << "5. Quit" << endl;
cin >> select;
system("CLS");
switch(select){
case 1:
e.setInfix();
break;
case 2:
cout << e.getInfix() << endl;
system("PAUSE");
break;
case 3:
e.updatePostfix();
break;
case 4:
cout << e.getPostfix() << endl;
system("PAUSE");
}
}while(select < 5);
return 0;
}
Please help, I can't seem to find the problem and im using microsoft visual c++ express edition, and there is no debugger.
Well the debugger won't work on mine, it always gives a pop up saying there is no debug information or that it is not valid, and then it will not show the autos, etc.
I'm pretty sure I can return NULL as a character value. I thought that was the problem at first and I changed it to '0' to see if that fixed it, but it didn't so I changed it back.
I thought that was what I was doing... I go up to the debug menu and click "Start Debugging" and it says project out of date blah blah would you like to build so I say yes. Then when it's finished compiling, it gives me that debug error popup thing.
That's something else. In Visual Studio they're called Configurations, you get two by default, Debug and Release.
The Debug Configuration is set up to generate the information the debugger needs. The Release Configuration is set up to create a production binary.
You can run the debugger with any executable, but if it doesn't have the expected kinds of symbols, you won't see the details of your code.
You're expected to develop your code using the Debug Configuration, you're not expected to release code built this way. When you're happy with your code, you're expected to switch to the Release Configuration and work with that.
I suspect you're building in Release. You should check that you're compiling using the Debug Config.
EDIT:
It didn't work, it's in debug mode and here's the error it gives me:
"Debugging information for 'Postfix.exe' cannot be found or does not match. Binary was not built with debug information. Do you want to continue debugging?"