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

lab4&5

Uploaded by

sachindravandse8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lab4&5

Uploaded by

sachindravandse8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* Develop a Program in C for converting an Infix Expression to Postfix Expression.

Program
should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and
alphanumeric
operands. */
//
//
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//
//
char stack[20];
int top=-1;
//
//
int precedence(char c )
{
int p;
switch(c){
case '#':p=0;break;
case '(':p=1;break;
case '+':
case '-':p=2;break;
case '*':
case '/':
case '%':p=3;break;
case '^':
case '$':p=4;break;
}/*switch*/
return p;
}/*precedence*/
//
//
void push(char ch)
{
top++;
stack[top]=ch;
}/*push*/
//
//
int pop()
{
return stack[top--];
}/*pop*/
//
//
void InfixToPostfix(char infix[20],char postfix[20])
{
int i=0;
int j=0;
char ch;
char x;
push('#');
while(infix[i]!='\0')
{
ch=infix[i];
switch(ch){
case '(' : push(ch);
break;
case ')' : x=pop();
while(x!='(')
{
postfix[j++]=x;
x=pop();
}/*while inside switch*/
break;
case '+':
case '-':
case '*':
case '%':
case '/':
case '^':
case '$':while(precedence(stack[top])>=precedence(ch))
postfix[j++]=pop();
push(ch);
break;
default:postfix[j++]=ch;
}/*switch*/
i++;
}/*while*/
while(top>0)
postfix[j++]=pop();
postfix[j]='\0';
}/*InfixToPostfix*/
//
//
void main()
{
char infix[25],postfix[25];
printf("\n\nEnter the infix notation : \n\n");
scanf("%s",infix);
InfixToPostfix(infix,postfix);
printf("Postfix notation: %s\n", postfix);
} /*main*/
/*
A+B*C -> ABC*+
(A+B)*C -> AB+*C
A^B+C -> AB^+C
(A+B)%C -> AB+%C
*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 25
int stack[MAX];
int top=-1;
void push(int x)
{ top++;
stack[top]=x;
}/* push*/
int pop()
{
return (stack[top--]);
}/*pop*/
int evaluate(char postfix[])
{
int i=0,op1,op2,value,result;
char c;
while(postfix[i]!='\0')
{
c=postfix[i];
if(isalpha(c)){
printf("Enter the value %c:",c);
scanf("%d",&value);
push(value);
}/*isalpha*/
else if(isdigit(c)){
push(c-'0');
}/*isdigit*/
else{
op1=pop();
op2=pop();
switch(c){
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
case '^':push(pow(op1,op2));break;
case '%':push(op1%op2);break;
}/*switch*/
}/* operator*/
i++;
}/*while*/
result=pop();return result;
}/*evaluate*/
void main()
{
char postfix[25];int result;
printf("enter postfix expression");
scanf("%s",postfix);
result=evaluate(postfix);
printf("Result= %d",result);
}/*main*/

You might also like