stack is not working
Sep 12, 2012 at 9:18pm UTC
Hi so my stack program is supposed to allow a user to enter a postfix expression and provide the result or an underflow, overflow error message. Example the user enters 345*. I am very lost and do not know what to add or change to make this work. Please can someone help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
//header
const int MAX = 10;
typedef int tin_t;
class stack
{
private :
tin_t tin[MAX];
int top;
public :
class Overflow{};
class Underflow{};
stack(); // constructor
~stack(); // destructor
void push(tin_t);
void pop(tin_t&);
void topElem(tin_t&);
bool isEmpty();
bool isFull();
void displayAll();
void clearIt();
};
//stack.cpp file
#include <iostream>
#include "stack.h"
using namespace std;
stack::stack()
{ top = -1; // indicate an empty stack }
stack::~stack()
{ // nothing to do }
bool stack::isEmpty()
{ if (top == -1) return true ; else return false ; )
bool stack::isFull()
{ if (top == MAX-1 ) return true ; else return false ; }
void stack::push(el_t elem)
{ if (isFull()) stackError(“overflow”);
else { top++; tin[top] = elem; }}
void stack::pop(tin_t& elem)
{ if (isEmpty()) stackError(“stack underflow”);
else { elem = tin[top]; top--;}}
void stack::topElem(tin_t& elem)
{ if (isEmpty()) stackError(“stack is empty”);
else { elem = tin[top]; } }
void stack::displayAll()
{ if (isEmpty()) cout << .[ empty ]. << endl;
else for (int i=top; i>=0; i--)
{ cout << tin[i] << endl; }}
void stack::clearIt()
{ tin_t getrid;
while ( !isEmpty())
pop(getrid);
}
//main.cpp
#include <iostream>
#include <string>
#include "stack.h"
using namespace std;
int main()
{
stack postfixstack;
string expression;
cout << "type a postfix expression: " ;
cin >> expression;
int i = 0;
char item;
int box1;
int box2;
while (expression[i] != '\0' )
{
try
{
item = expression[i];
if (isdigit(item)) {int x = int (expression[i]) - 48;; postfixstack.push(item); }
else if ( (item == '+' ) || (item == '-' ) || (item == '*' ))
{
postfixstack.pop(box1);
postfixstack.pop(box2);
// a whole bunch of cases
if (item == '+' ) result = box1+box2;
if (item == '-' ) result = box2-box1;
if (item == '*' ) result = box2*box1;
// push the result
}
// Catch exceptions and report problems and quit the program now.
catch (stack::overflow)
{cout<<"overflow" <<endl; }
catch (stack::underflow)
{cout<<"Empty stack" <<endl; }
catch (string errorcode)
{cout<<"Error" <<endl; }
}
}
Sep 13, 2012 at 1:46am UTC
You should probably start with code that compiles. It's not clear whether you have issues with the syntax or the logic. In any event, you can only work with one at a time.
Topic archived. No new replies allowed.