⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 infix to postfix.cpp

📁 Stack, queue, sorting exmaples in C/C++.
💻 CPP
字号:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>


struct stack {
	 int top;
	 char item[30];
	 };

void push(struct stack *p,char u)
  {
	p->top++;
	p->item[p->top]=u;
      }


void pop(struct stack *p)
   {
	p->top--;
      }


int emp(struct stack *p)   // function for checking the token left in the stack
{
if(p->top==-1)
{ return (1);}

else
{
return (0);}
}


int up(char stp,char oo)     //function declaration for checking the precedence category(pre fix)
{
 if(((stp=='+')||(stp=='-'))&&((oo=='*')||(oo=='/')))
  { return (0);}

 if(stp==')')
{ return (0);}

 if((stp!='(')&&(oo==')'))
{ return (0);}

 if((stp!=')')&&(oo=='('))

{  return (1);}
 }

int isp(char stp,char oo)       //function declaration for checking the precedence category(post fix)
{
 if(((stp=='+')||(stp=='-'))&& ((oo=='*')||(oo=='/')))
  { return (0);}

 if(stp=='(')
{ return (0);}

 if((stp!=')')&&(oo=='('))
{ return (0);}

 if((stp!='(')&&(oo==')'))

{  return (1);}
}


void main()
{
  struct stack q;
  char input[30],output[30],xp[30];
  char u;
  q.top=-1;
  printf("enter the expression:\n");
  scanf("%s",xp);


int i=0,j=0,v=0;
int a;
cout<<"enter 1 for pre and 2 for post";
scanf("%d",&a);                           // selecting the post of prefix

if(a==1)
{
// starting the pre-operation

int w,y,z=0;
w=strlen(xp);                    // checking the length of the string

for(y=w;y>0;y--)                    // reversing the string
{
 input[z]=xp[y-1];                  // storing it in different location
 z++;
 }                    
input[z]='\0';                       // initializing the last element of the reverse to be '\0'

while(input[i]!='\0')                //checking the last element
  {
	if(isalpha(input[i]))           //checking only alphanumeric value
	    {
	     output[j]=input[i];         // storing the alphanumeric value in the array(as stack)
	     j++;
		   }
   else
      {
	 while(!emp(&q)&&(up(q.item[q.top],input[i])))  //checking the precedence category
		  {
		   u=q.item[q.top];
		   pop(&q);                             // removing the token of higher precedence
		   output[j]=u;
		   j++;
		}
     if(emp(&q)||input[i]!='(')
	{
	  push(&q,input[i]);
		  }
		  else
		  {
		   pop(&q);
		   }
	 }
      i++;
     }

while(!emp(&q))                             //checking the token in the stack
{
  u=q.item[q.top];
  pop(&q);
  output[j]=u;
  j++;
  }
printf("\nprefix form:\t");
output[j]='\0';

v=0;
for(i=j;i>0;i--)                     // again reversing the expression
{
 input[v]=output[i-1];
 printf("%c",input[v]);                //displaying the value
 v++;
  }
}
else
// starting the post operation
{
 while(xp[i]!='\0')          //checking the last element of the expression
  {
	if(isalpha(xp[i]))     // checking the alphanumeric value
	    {
	     output[j]=xp[i];
	     j++;
		   }
   else
      {
	 while(!emp(&q)&&(isp(q.item[q.top],xp[i]))) 
		  {
		   u=q.item[q.top];
		   pop(&q);
		   output[j]=u;

		   j++;
		}
      if(xp[i]!=')')
	{
	  push(&q,xp[i]);
		  }
		  else
		  {
		   pop(&q);
		   }
	 }
      i++;
     }

while(!emp(&q))
{
  u=q.item[q.top];
  pop(&q);
  output[j]=u;
  j++;
  }
cout<<"\npostfix:\t";
output[j]='\0';

while(output[v]!='\0')
{
 printf("%c",output[v]);
 v++;
  }

}
getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -