I need help please.
Sep 22, 2012 at 8:33am UTC
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
#include<iostream>
#include<conio>
#include<cstring>
struct node {
int data;
node* next;
};
class stack
{
private :
node* top;
public :
stack() : top(NULL) {}
bool isEmpty()
{
if (top == NULL)
return true ;
}
void push(int );
int pop();
int evaluate(string);
void display();
};
void stack :: push(int i)
{
node* temp;
temp = new node;
temp->data = i;
temp->next = top;
top = temp;
}
int stack :: pop()
{
if (top!=NULL){
int i=0;
node* temp = top;
top = top->next;
i = temp->data;
delete temp;
return i;
}
}
int stack :: evaluate(string exp)
{
char ch;
int result = 0, i=0, temp1, temp2;
while (exp[i] != '\0' )
{
switch (exp[i])
{
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
case '0' :
temp1 = exp[i] - '0' ;
push(temp1);
display(); //for debugging purpose
break ;
case '/' :
temp1 = pop();
temp2 = pop();
push( temp2 / temp1 );
break ;
case '*' :
push(pop()*pop());
break ;
case '+' :
push(pop()+pop());
break ;
case '-' :
temp1 = pop();
temp2 = pop();
push(temp2 - temp1);
break ;
default :
break ;
}
i++;
}
}
void stack :: display()
{
node* temp = top;
for (; temp!=NULL; temp=temp->next)
cout<<temp->data<<endl;
getch();
}
void main()
{
string exp = "52+3-" ;
stack s;
s.evaluate(exp);
int i = s.pop();
cout<<"I :- " <<i<<endl;
s.display();
getch();
}
I dont know whats the problem in this code. I have been looking at it for the last 1 hour. It gives "Abnormal Program Termination" error. I took the output at several points. Its okay until 5 and 2 are added. But terminates before subtracting 7 and 3.
Please help.
Sep 22, 2012 at 8:43am UTC
Possible solution: change void main()
to int main( int argc, char * argv[] )
and add return 0;
after getch();
Topic archived. No new replies allowed.