Prefix using 'C'

Prefix documentation, otherwise called Clean documentation, is an approach to addressing numerical articulations where the administrator is put before its operands. In prefix documentation, enclosures are not expected to demonstrate the request for tasks.


Here is a guide to show prefix documentation:


Infix documentation: (2 + 3) * 4 Prefix documentation: * + 2 3 4


To assess an articulation in prefix documentation, you can utilize a stack-based calculation. This is the carefully guarded secret:


1. Begin with a vacant stack.


2. Peruse the articulation from right to left.


3. For every token (operand or administrator) in the articulation:


·        In the event that the token is an operand, push it onto the stack.


·        Assuming the token is an administrator, pop the main two operands from the stack.


·        The primary popped operand turns into the principal operand in the activity.


·        The second popped operand turns into the second operand in the activity.


·        Play out the activity utilizing the administrator and the operands.


·        Push the result back onto the stack.


4. In the wake of examining all tokens, the eventual outcome will be the main worth left on the stack.


Involving the past prefix articulation for instance:


Prefix documentation: * + 2 3 4 Assessment:


Push 4 onto the stack.

Push 3 onto the stack.

Pop 3 and 4 from the stack. Play out the expansion (3 + 4 = 7). Push 7 onto the stack.

Push 2 onto the stack.

Pop 2 and 7 from the stack. Play out the augmentation (2 * 7 = 14). Push 14 onto the stack.

The eventual outcome is 14.

Thus, the articulation "(2 + 3) * 4" in infix documentation assesses to 14 in prefix documentation.


Prefix documentation is ordinarily utilized in software engineering and programming dialects, particularly in stack-based calculations and articulation assessment calculations. It kills the requirement for enclosures and gives a steady and unambiguous portrayal of numerical articulations.

Program:-

#include<stdio.h>

#include<conio.h>

#include<string.h>

 

 

int pri(char ch)

{

    if(ch=='+'||ch=='-')

        return 1;

    if(ch=='*'||ch=='/')

        return 2;

    if(ch=='^')

        return 3;

 

    return 0;

 

}

void main()

{

    char  p[100],in[100],ch,c,s[100],t[100],pr[100];

    int i,k=0,top=-1;

    printf("\n enter infix expreesion:");

    scanf("%s",t);

    rev(t,in);

    s[++top]='(';

    strcat(in,")");

    for(i=0;in[i]!='\0';i++)

    {

        ch=in[i];

        if(isalnum(ch))

            p[k++]=ch;

        else if(ch=='(')

                    s[++top]=ch;

        else if(ch==')')

        {

            c=s[top--];

            while(c!='(')

 

            {

                p[k++]=c;

                c=s[top--];

            }

        }

        else{

 

            while(pri(ch)<=pri(s[top])&&top!=-1){

 

 

                p[k++]=s[top--];

            }

            s[++top]=ch;

 

 

        }

    }

   

    p[k]='\0';

    rev(p,pr);

    printf("\n postfix exp:%s",pr);

}

 

 

 

void rev(char  in[100],char t[100])

{

    int len=strlen(in),i=0,j=0;

    j=len-1;

    while(j>=0)

    {

        if(in[j]=='(')

            t[i]=')';

        else if(in[j]==')')

            t[i]='(';

        else

            t[i]=in[j];

        i++;

        j--;

    }

    t[i]='\0';

}

Output:-