I am creating my own stack for my data structures class. For our assignment we are using the assignment to convert a real-time infix equation into a postfix equation.
I thought my program:
took input
determines if it was digit or number(operand)
prints it out
determines if input is operator (+,-,/,*)
adds to stack or prints out, depending on stack precedence
Instead it prints out the operands as expect, but I get this error when I enter an operator
>.../dorun.sh line 33: 4136 Segmentation fault <core dumped> sh "$<SHFILE>"
#include <vector>
using namespace std;
class DishWell{
public:
char ReturnEnd(){
return Well.back();
}
void Push(char x){
Well.push_back(x);
}
void Pop(){
Well.pop_back();
}
bool IsEmpty(){
return Well.empty();
}
private:
vector<char> Well;
};
#include <iostream>
bool Precidence(char Input, char Stack){
int InputPrecidence,StackPrecidence;
switch (Input){
case '*':
InputPrecidence = 4;
break;
case '/':
InputPrecidence = 4;
break;
case '+':
InputPrecidence = 3;
break;
case '-':
InputPrecidence = 3;
break;
case '(':
InputPrecidence = 2;
break;
default:
InputPrecidence = 0;
}
switch (Stack){
case '*':
StackPrecidence = 4;
break;
case '/':
StackPrecidence = 4;
break;
case '+':
StackPrecidence = 3;
break;
case '-':
StackPrecidence = 3;
break;
case '(':
StackPrecidence = 2;
break;
default:
StackPrecidence = 0;
}
if(InputPrecidence>StackPrecidence) return true;
else return false;
}
int main(int argc, char** argv) {
DishWell DishTray;
char Input;
bool InputFlag;
InputFlag = true;
while(InputFlag){
cin>>Input;
if((((Input>='a'&&Input<='z')||(Input>='A'&&Input<='Z'))|| (Input>='0'&&Input<='9')))//If Digit or Number
cout<<Input;
if((Input=='*'||Input=='/'||Input=='+'||Input=='-')){//if operand
if(Precidence(Input,DishTray.ReturnEnd()))
DishTray.Push(Input);
else if(!Precidence(Input,DishTray.ReturnEnd()))
cout<<Input;
}
else if(!((((Input>='a'&&Input<='z')||(Input>='A'&&Input<='Z'))|| (Input>='0'&&Input<='9')))||((Input=='*'||Input=='/'||Input=='+'||Input=='-')))//if not digit/numer or operand
InputFlag = false;
}
while(!DishTray.IsEmpty()){
cout<<DishTray.ReturnEnd();
DishTray.Pop();
}
return 0;
My code is very length, I know, but I appreciate help. Especially any times for efficency or future coding.
Thanks again
P.S. Dr. Zemoudeh, this is your student Macaire
I’ll expand on Rup’s answer to answer the question you didn’t ask, but is more important: How can I find out where my program is crashing?
One way is to put
std::coutorprintfstatements throughout your program. Put a statement at the beginning of every function saying, “function x enter” and at the end saying, “function x exit”. Run your program and when it crashes, you’ll see what function it’s in. At that point, you can add lines to print the contents of each variable to find out what’s going wrong.Another way is to use a debugger, like
gdb.First, compile your program with the
-gswitch to enable debugging information.Next, tell the debugger
gdbto run your program.At the gdb prompt, type
runto start your program. I entered4*(3+2)and the program crashedat prog.cpp:7, which is the linereturn Well.back();.For more complex programs, you’ll often need a list of all the functions that are currently being called. You can get that information with
bt, short for “backtrace”. In the following backtrace, you see that the functionmain(#1) is calling functionDishWell::ReturnEnd(#0). #0 is the current function because functions form a stack, where the current function is the top of the stack (offset 0 is the top).With only these 2 commands (
run,bt), you’ve solved 80% of the problem: finding where your program has crashed. If you stopped reading here, you should be able to solve the problem by adding print statements or asserts to see what the state ofWellis and whyback()is crashing your program. But let’s usegdbsome more…You can type
listto see the source code around that line for more context without leaving the debugger.gdb can print variables and simple expressions. Printing the value of
Wellhere is not too helpful for the novice, because it’s a complex data structure and not a simple variable. But we can tell gdb to call a method on that variable…Ah ha,
Wellis empty, and you’ve calledback()on it. When we look at some good documentation forstd::vector, we see that you invoke undefined behavior, which in this case is a program crash.Now look through your program and try to figure out why
Wellwas empty when your program wasn’t expecting it to be empty. If you likegdb, read some tutorials on it and learn how to do things like set breakpoints, or single step.