basically the program takes a string splits it up into numbers and operators and solves the postfix expression but for some reason it will not display the output... any ideas?
ps i know the code is ugly, sorry its my first go around with data structures.
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define maxcols 80
struct stack {
int top;
double items [maxcols];
};
double operation(int symb, double op1, double op2);
int empty (struct stack *ps);
int pop (struct stack *ps);
void push (struct stack *, int x);
double eval(char expr[]);
int findchar (char *str, char let);
void split (char *str, char *op);
int main ()
{
char expr[maxcols];
int position = 0;
printf("Please enter the expression\n") ;
gets(expr);
printf("%s%s", "the orginal postfix expression is", expr);
printf("\n%f", eval(expr));
system("pause");
return 0;
}
/* Push function is used to insert the item into the stack */
void push (struct stack *ps, int x)
/* Parameter x is the data to be inserted and it's type is dependent on
the type of the element in the stack */
{
if (ps->top == maxcols - 1)
printf("\nSTACK OVERFLOW");
else{
ps->items [++ps->top] = x;
}
return;
}
int empty (struct stack *ps)
{
if (ps->top == -1)
return (TRUE);
else{
return (FALSE);
}
}
int pop (struct stack *ps)
{ int y;
if (empty (ps)== TRUE) {
printf("\nSTACK UNDERFLOW");
return -1;
}
else{
y = ps->items[ps->top];
ps->top--;
}
return y;
}
double eval(char expr[])
{
double c;
int position;
double opnd1, opnd2, value;
struct stack stack;
char token[7];
stack.top = -1 ;
while ((strlen(expr)) != 0){
split (expr, token);
if (atof == 0){
opnd2 = pop(&stack);
opnd1 = pop(&stack);
value = operation (token[0], opnd1, opnd2);
}
else{
push (&stack, atof (token));
}
}
return(pop(&stack));
}
double operation(int symb, double pop1, double pop2)
{
switch(symb)
{
case '+': return pop1 + pop2;
case '-': return pop1 - pop2;
case '/': return pop1 / pop2;
case '*': return pop1 * pop2;
case '$': return ((pow(pop1, pop2)));
default : printf("%s", "illegal operation");
exit(1);
}
}
int findchar (char *str, char let)
{
int i=0, found=0;
while (*(str+i) !='\0' && !found)
if (*(str+i) == let)
found = 1;
else
i++;
if (!found)
i = -1;
return i;
}
void split (char *str, char *op)
{
int pos;
int i=0, found=0;
char let;
while (*(str+i) !='\0' && !found)
if (*(str+i) == let)
found = 1;
else
i++;
if (!found)
i = -1;
if (pos >= 0) {
strncpy(op, str, pos);
*(op+pos) = '\0';
strcpy(str, str+pos+1);
}
}