full prog about evaluating postfix expression
12:28 PM
//Any quire then let me know// //help nd supported by shankhar koirala//
/*program to evaluate postfix expression
AUTHER: Suman Thapa
DATE:013/07/02
TIME:1:11 AM*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define STACKSIZE 20
struct stack
{
int top;
double items[STACKSIZE];
};
double oper(int symbol, double x,double y);
int isDigit (char c);
double pop (struct stack *);
void push (struct stack *,double data);
double evaluate (char expr[]);
int main (void)
{
char expression[16] =
{'6','2','3','+','-','3','8','2','/','+','*','2','$','3','+'};
printf("\nthe given expression is : %s",expression);
printf("\nthe evaluated value is : %f",evaluate(expression));
getchar ();
return 0;
}
double evaluate (char expr[])
{
char c;
int i;
double operand1, operand2, value;
struct stack s;
s.top = -1;
double num;
for (i=0; (expr[i] )!=!'\0'; i++)
{
c=expr[i];
if (isDigit(c) == 1)
{
num = c - 48;
push(&s,num);
}
else
{
operand2 = pop(&s);
operand1 = pop(&s);
value = oper(c,operand1,operand2);
push(&s,value);
}
}
return (pop(&s));
}
void push (struct stack *s,double data)
{
s->top++;
s->items[s->top] = data;
}
double pop (struct stack *s)
{
return (s->items[(s->top)--]);
}
int isDigit (char c)
{
if (c >= '0' && c<= '9')
return 1;
else
return 0;
}
double oper(int symbol, double x,double y)
{
switch (symbol)
{
case '+' :
return (x+y);
case '-' :
return (x-y);
case '*' :
return (x*y);
case '/' :
return (x/y);
case '$' :
return (pow(x,y));
}
}
/*program to evaluate postfix expression
AUTHER: Suman Thapa
DATE:013/07/02
TIME:1:11 AM*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define STACKSIZE 20
struct stack
{
int top;
double items[STACKSIZE];
};
double oper(int symbol, double x,double y);
int isDigit (char c);
double pop (struct stack *);
void push (struct stack *,double data);
double evaluate (char expr[]);
int main (void)
{
char expression[16] =
{'6','2','3','+','-','3','8','2','/','+','*','2','$','3','+'};
printf("\nthe given expression is : %s",expression);
printf("\nthe evaluated value is : %f",evaluate(expression));
getchar ();
return 0;
}
double evaluate (char expr[])
{
char c;
int i;
double operand1, operand2, value;
struct stack s;
s.top = -1;
double num;
for (i=0; (expr[i] )!=!'\0'; i++)
{
c=expr[i];
if (isDigit(c) == 1)
{
num = c - 48;
push(&s,num);
}
else
{
operand2 = pop(&s);
operand1 = pop(&s);
value = oper(c,operand1,operand2);
push(&s,value);
}
}
return (pop(&s));
}
void push (struct stack *s,double data)
{
s->top++;
s->items[s->top] = data;
}
double pop (struct stack *s)
{
return (s->items[(s->top)--]);
}
int isDigit (char c)
{
if (c >= '0' && c<= '9')
return 1;
else
return 0;
}
double oper(int symbol, double x,double y)
{
switch (symbol)
{
case '+' :
return (x+y);
case '-' :
return (x-y);
case '*' :
return (x*y);
case '/' :
return (x/y);
case '$' :
return (pow(x,y));
}
}
0 comments: