Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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