0
//SOME BODY PLEASE WRITE THIS IN C++ LANGUAGE//
//THANK YOU//
# include<stdio.h>
# include<conio.h>
/*program to evaluate the given postfix expression*/
typedef struct {
    int a[100];
    int top;
} STACK;
void push(STACK *s,int x)
{
    if(s->top==99)
        printf("STACK OVERFLOW\n");
    else
        s->a[++s->top]=x;
}

int pop(STACK *s)
{
    int x;
    if(s->top<0)
        printf("STACK UNDERFLOW\n");
    else {
        x=s->a[s->top--];
        return x;
    }
}
int operation(int p1,int p2,char op)
{
    switch(op) {
    case '+':
        return p1+p2;
    case '*':
        return p1*p2;
    case '-':
        return p1-p2;
    case '/':
        return p1/p2;
    }
}
int evaluate(char pos[])
{
    STACK s1;
    int p1,p2,result,i;
    s1.top=-1;
    for(i=0; pos[i]!='\0'; i++)
        if(isdigit(pos[i]))
            push(&s1,pos[i]-'0');/*use to find the integer value of it*/
        else {
            p2=pop(&s1);
            p1=pop(&s1);
            result=operation(p1,p2,pos[i]);
            push(&s1,result);
        }/*end of for loop*/
    return pop(&s1);
}
void main()
{
    char postfix[100];
    clrscr();
    printf("Please Enter the VALID POSTFIX string\n\n Operands are SINGLE DIGIT\n\n");
    gets(postfix);
    printf("The Result is==>%d",evaluate(postfix));
    getch();
}/*end of main*/

Edited by Narue: Added code tags and formatting

2
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by ayeshashahid
0

What part of the program are you having problems with? I ask because a C program is mostly a C++ program.

0

What part of the program are you having problems with? I ask because a C program is mostly a C++ program.

i cannot understand this one:-
printf("The Result is==>%d",evaluate(postfix));


and not sure if the following remains the same

typedef struct {
int a[100];
int top;
} STACK;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.