Question

Write pseudocode for the conversion of infix to prefix notation algorithm Pseudocode may use the following...

Write pseudocode for the conversion of infix to prefix notation algorithm

Pseudocode may use the following

Input : A * B + C / D

Output : + * A B/ C D

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

PSEUDOCODE:-

arr = infix expression

Set loop_counter to length of the array

do

      char = arr[loop_counter]

       if char is operator

                   push char to prefix stack

       if char is ‘)’

                   push char to stack

       if char is ‘(’

                  do

                        pop char and push it to prefix_stack

                  until char equals ‘(‘

       if char is operator

                 if char precedence is greater or is equal to stack[top] precedence

                              push to stack

                else

                          do

                                pop from stack and push to prefix stack

                          until condition satisfies

          loop_counter--

while loop_counter is not equal to 0

pop out prefix stack and reverse and display each element

CODE IN C:-

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define max 100
char s[max];
int top=-1;
void push(char);
char pop();
int precede(char);
main(){
   char infix[100],postfix[100],ch,a[100];
   int i=0,j=0,k,l;
   printf("Enter infix expression:");
   scanf("%s",a);
   l=strlen(a);
   infix[l]='\0';
   l--;
   while(a[i]!='\0'){
       if(a[i]=='('){
           infix[l]=')';
       }else if(a[i]==')'){
           infix[l]='(';
       }else{
           infix[l]=a[i];
       }
       l--;
       i++;
   }
   i=0;
   while((ch=infix[i++])!='\0'){
       if(isalnum(ch)){
           postfix[j++]=ch;
       }else if(ch=='('){
           push(ch);
       }else if(ch==')'){
           while(s[top]!='('){
               postfix[j++]=pop();
           }
           top--;
       }else{
           if(top==-1||precede(ch)>=precede(s[top])){
               push(ch);
           }else{
               while(precede(ch)<=precede(s[top])&&top!=-1){
                   postfix[j++]=pop();
               }
               push(ch);
           }
       }
   }
   while(top!=-1){
       postfix[j++]=pop();
   }
   postfix[j]='\0';
   l=strlen(postfix);
   l--;
   i=0;
   while(l>=0){
       a[i]=postfix[l];
       i++;
       l--;
   }
   a[i]='\0';
   printf("Infix expression = %s\nPrefix expression = %s",infix,a);
}
void push(char a)
{
   s[++top]=a;
}
char pop(){
   return s[top--];
}
int precede(char a){
   if(a=='('||a==')'){
       return 1;
   }else if(a=='-'||a=='+'){
       return 2;
   }else if(a=='%'||a=='/'||a=='*'){
       return 3;
   }
}

OUTPUT:-

NOTE:- If you have any doubts , please comment below.PLease give positive rating,THUMBS UP, THANK YOU!!!

Add a comment
Know the answer?
Add Answer to:
Write pseudocode for the conversion of infix to prefix notation algorithm Pseudocode may use the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT