I get these errors:
- "Unhandled exception at 0x00ee1576 in Stack.exe: 0xC0000005: Access violation reading location 0x33532ebc."
- >Stack.exe!pop(stack * s) Line 47 + 0xc bytes C++
When debugging this code after getting the postfix to display this code
FYI my professor gave permission to use code we find as long as we give credit, implement a particular piece of code, and write a document explaining how it works according to each line of code. Yes I know weird and that it's probably more work then doing it by myself but she's new at teaching this subject and I'd have to make the document regardless.
//Source Code from user born2c0de of http://www.dreamincode.net
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
#define MAX 50
#define EMPTY -1
//Construct the Stack that is to be manipulated by the code
struct stack
{
int data[MAX];// Allow for "max" chracter input
int top;// To be used when "moving" a part of the stack
};//End of stack contruction
// Determine if a stack is empty when manipulating the stack
void emptystack(struct stack* s)
{
s->top=EMPTY;
}//End of Empty Determin-er
void push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
++s->top;
s->data[s->top]=item;
}
}
int pop(struct stack* s)
{
int ret=EMPTY;
if(s->top == EMPTY)
printf("\nSTACK EMPTY");
else
{
ret= s->data[s->top];
--s->top;
}
return ret;
}
void display(struct stack s)
{
while(s.top != EMPTY)
{
printf("\n%d",s.data[s.top]);
s.top--;
}
}
//Evaluates the stack
int evaluate(char *postfix)
{
char *p;
struct stack X;
int op1,op2,result;
result = pop(&X);
emptystack(&X);
p = &postfix[0];
while(*p != '\0')
{
/* removes tabs and spaces */
while(*p == ' ' || *p == '\t')
{
p++;
}
/* if is digit */
if(isdigit(*p))
{
push(&X,*p - 48);
}
else
{
//it is an operator
op1 = pop(&X);
op2 = pop(&X);
push(&X,result);
}
p++;
}
return result;
}//End of the evaluation code
int isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}
//Determines what operators can be used in accordence to the code
char isoperator(char e)
{
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
return 1;
else
return 0;
}//END of operator determin-er
//Determines the priority of the operators in relation to each other
char priority(char e)
{
int pri = 0;
if(e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
return pri;
}//End of priority Determin-er
//Changes Infix notation into prefix notation
void infix2postfix(char* infix, char * postfix, int insertspace)
{
char *i,*p; //Varibles for the infix and postfix of the stack
struct stack X; // Variable for the precontructed stack
char n1;
emptystack(&X);
i = &infix[0]; //Determines the use of the "i variable"
p = &postfix[0]; //Determines the use of the "i variable"
//While loop for when a person uses a space when entering the stack
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
i++;
}
if( isoperator(*i) )
{
if(isempty(&X))
push(&X,*i);
else
{
n1 = pop(&X);
while(priority(n1) >= priority(*i))
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
*p = '\0';
}
int main()
{
char in[50],post[50];
char exp[MAX];
strcpy(&post[0],"");
printf("Enter Infix Expression: ");
gets(in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is: %s\n",&post[0]);//Prints Infix expression
gets(exp);
infix2postfix(&in[0],&exp[0],1);
printf("The Evaluation of the expression is %d\n",exp,evaluate(&exp[0]));
std::getchar();
return 0;
}
Sorry wrong section, can someone move this?
If not moved in 2 hours I'll post it in the proper section

New Topic/Question
Reply



MultiQuote





|