/**C PROGRAM FOR CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION**/

infix to postfix program in c

techsayright


This program converts infix expression to postfix expression. (infix to postfix program in c | DS practical gtu)

* This program assume that there are Five operators: (*, /, +, -,^)
in infix expression and operands can be of single-digit only.(infix to postfix program in c | DS practical gtu)

* In this program we have used many user defined function for became
easily understandable (infix to postfix program in c | DS practical gtu)
continue reads below

* Here is a code for infix to postfix programming which you will get easily copy👇👇

INPUT:


//written by www.techsayright.blogspot.com//

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define size 50

char stack[size];
int top=-1;

void push(char x)
{
 if(top>=size-1)
 {
   printf("\nstack is overflow");
 }
 else
 {
  top++;
  stack[top]=x;
 }
}

char pop()
{
 char x;
 if(top==-1)
 {
  printf("\n stack is underflow");
  exit(1);
 }
 else
 {
  x=stack[top];
  top--;
  return x;
 }
 return 0;
}

int is_op(char s)
{
 if(s=='^' || s=='*' || s=='/' || s=='+' || s=='-')
 {
  return 1;
 }
 else
 {
  return 0;
 }
}

int pr(char s)
{
 if(s=='^')
 {
  return 3;
 }
 else if(s=='*' || s=='/')
 {
  return 2;
 }
 else if(s=='+' || s=='-')
 {
  return 1;
 }
 else
 {
   return 0;
 }
}

void infixTopostfix(char infix_exp[],char postfix_exp[])
{
 int i=0,j=0;
 char item,x;
 push('(');

 strcat(infix_exp,")");
 item=infix_exp[i];
  while(item!='\0')
 {
  if(item=='(')
  {
   push(item);
  }
  else if(isdigit(item) || isalpha(item))
  {
   postfix_exp[j]=item;
   j++;
  }
  else if(is_op(item)==1)
  {
   x=pop();
   while(is_op(x)==1 && pr(x)>=pr(item))
   {
    postfix_exp[j]=x;
    j++;
    x=pop();
   }
   push(x);
   push(item);
  }
  else if(item==')')
  {
   x=pop();
   while(x!='(')
   {
    postfix_exp[j]=x;
    j++;
    x=pop();
   }
  }
  else
  {
   printf("invalid infix expression");
  }
  i++;
  item=infix_exp[i];
 }
 postfix_exp[j]='\0';
}

void main()
{
 char infix[50],postfix[50];
 clrscr();
 printf("\nenter the infix expression:");
 gets(infix);
 infixTopostfix(infix,postfix);
 printf("\npostfix expression:");
 puts(postfix);
 getch();
}


OUTPUT:


*HAPPY CODING☺*

3 تعليقات

Hii

إرسال تعليق

Hii

أحدث أقدم