0% found this document useful (0 votes)
23 views

The Algorithm For Is Given: Evaluating A Postfix Expression CC

The document describes a C program to evaluate postfix expressions using a stack. The program takes a postfix string as input, uses a stack to push operands and pop operands and operators. It evaluates each expression using functions like push(), pop() and evaluate() which takes two operands and an operator as arguments, performs the operation and returns the result. Finally it prints the result of evaluating the full postfix expression.

Uploaded by

Md Saquib
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

The Algorithm For Is Given: Evaluating A Postfix Expression CC

The document describes a C program to evaluate postfix expressions using a stack. The program takes a postfix string as input, uses a stack to push operands and pop operands and operators. It evaluates each expression using functions like push(), pop() and evaluate() which takes two operands and an operator as arguments, performs the operation and returns the result. Finally it prints the result of evaluating the full postfix expression.

Uploaded by

Md Saquib
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ednesday, 15 June 2011

C program for Evaluating a Postfix Expression


The Algorithm for Evaluating a Postfix Expression is given here Program:
#include<stdio.h> #include<ctype.h> #include<stdlib.h> #define SIZE 40 int stack[SIZE]; int top=-1; void push(int n) { if(top==SIZE-1) { printf("Stack is full\n"); return; } else { top=top+1; stack[top]=n; printf("Pushed element is %d\n",n); } } int pop() { int n; if(top==-1) { printf("Stack is empty\n"); return; } else { n=stack[top]; top=top-1; printf("The poped element is %d\n",n); return(n); } } int evaluate(int op1, int op2,char ch) { printf("op1=%d op2=%d ch=%c\n",op1,op2,ch); int n; if (op1<op2) { n=op1;

op1=op2; op2=n; } if(ch=='+') n=op1+op2; else if(ch=='-') n=op1-op2; else if(ch=='*') n=op1*op2; else if(ch=='/') n=op1/op2; else if(ch=='%') n=op1%op2; else { printf("The operator is not identified\n"); exit(0); } printf("n=%d\n",n); return(n); } int main() { char str[50],ch,ch1; int i=0,n,op1,op2; printf("Enter the Postfix string\n"); scanf("%s",str); ch=str[i]; while(ch!='\0') { printf("The char is=%c\n",ch); //if(ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5')// if(isdigit(ch)) { n=ch-'0'; push(n); } else { op1=pop(); op2=pop(); n=evaluate(op1,op2,ch); push(n); } ch=str[++i]; } printf("The value of the arithmetic expression is=%d\n",pop()); return; }

You might also like: y Airline Reservation System in C y C Programs Based on Queue

C Programs Based on Stack


LinkWithin

Links to this post Posted by Sunitha at 15:38 0 comments Email ThisBlogThis!Share to TwitterShare to Facebook Labels: Algorithm Implementation, C Simple Projects, Data Structure Programs Reactions:

You might also like