Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

postfix evaluator in c help


  • Please log in to reply

#1
sportskd2

sportskd2

    Member

  • Member
  • PipPip
  • 36 posts
here is my code and it will run but it wont display the result

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);
}
}
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP