Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //infix -> prefix
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- char stack[50],infix[50],prefix[50];
- int top=-1;
- char pop()
- {
- char ch;
- ch=stack[top];
- top--;
- return ch;
- }
- void push(char ch)
- {
- top++;
- stack[top]=ch;
- }
- int main()
- {
- int i,j=0;
- clrscr();
- printf("\nEnter the Infix expression: ");
- gets(infix);
- strcat(infix,")"); //insert ENd ")"
- strrev(infix); //revers string
- strcat(infix,"("); //insert starting"("
- puts(strrev(infix));
- strrev(infix);
- for(i=0;i<=strlen(infix);i++)
- {
- switch(infix[i])
- {
- case ')':
- push(')');
- break;
- case '(':
- while(stack[top]!=')')
- {
- prefix[j]=pop();
- j++;
- }
- top--;
- break;
- case '^':
- push('^');
- break;
- case '*':
- case '/':
- while(stack[top]=='^')
- {
- prefix[j]=pop();
- j++;
- }
- push(infix[i]);
- break;
- case '+':
- case '-':
- while(stack[top]=='^'||stack[top]=='*' ||stack[top]=='/')
- {
- prefix[j]=pop();
- j++;
- }
- push(infix[i]);
- break;
- default:
- prefix[j]=infix[i];
- j++;
- }
- printf("\n[%d]-> %c || %s || %s \n",i+1,infix[i],stack,prefix);
- }
- while(top!=-1)
- {
- prefix[j]=pop();
- j++;
- }
- stack[0]=NULL;
- printf("\n[%d]-> %c || %s || %s \n",i+1,infix[i],stack,strrev(prefix));
- prefix[j]=NULL;
- printf("\n %s <=== INFIX TO PREFIX ===> %s",strrev(infix),prefix);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement