1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help me plz.. evaluating infix.prefix and postfix

Discussion in 'Meet and Greet' started by darkconsydii, May 18, 2012.

  1. darkconsydii

    darkconsydii New Member

    Joined:
    May 18, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i have a code that will convert infix,postfix and prefix... i need also to evaluate the answer it but i dont know how.. ex. in infix form 2+2*3/(5-3) ans:6 ..i need to get the answr help me plzz...

    this is my code:
    Code:
    #include <stdio.h>
    #include <fstream.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    #include<stdlib.h>
    #define MAX 50
    #define Operator 1
    #define notOperator 0
    #define empty -1
    
    
    typedef struct node{
    char info;
    node *left;
    node *right;
    }node;
    node *root;
    
    char output[MAX] ;
    char stack[MAX] ;
    char input[MAX] ;
    char *s, *t ;
    char ch;
    int top;
    int l=0;
    
    
    void SetExpression (char *);
    char PopFromStack ();
    void PushOnStack (char);
    int priority (char);
    int chkElement(char);
    void ConvertToPostfix ();
    void opFunc(char);
    void varFunc(char);
    void push(struct node*);
    struct node *pop();
    void infix( node*);
    void prefix( node*);
    void postfix( node*);
    
    char equation[25];
    struct node* stack1[25];
    int stackPtr = -1;
    
    
    void main( )
    { int count,b;
    clrscr( ) ;
    
    printf("\n\t\t\t=menu=\n\n");
    printf("\t\t\t1.infix \n");
    printf("\t\t\t2.prefix \n");
    printf("\t\t\t3.postfix \n\n");
    printf("\t\t\tenter ur choice: ");
    scanf("%d",&b);
    switch(b) {
    case 1:{
    printf("\t\t\tenter an infix expression: ");
    scanf("\t\t\t%s",&input);
    
    SetExpression (input) ;
    
    
    ConvertToPostfix ( ) ;
    for(count = 0; output[count] != '\0'; count++)
    {
    switch(chkElement(output[count]))
    {
    case 1:
    opFunc(output[count]);
    break;
    case 0:
    varFunc(output[count]);
    break;
    default:
    printf("\n\t\t\tSorry unrecognized entry!!");
    }
    }
    ofstream in,pre,post;
    in.open("infix.txt");
    in<<input;
    in.close();
    pre.open("prefix.txt");
    pre<<root->info;
    pre.close();
    post.open("postfix.txt");
    post<<output;
    post.close();
    
    
    printf("\n\t\t\tthe prefix form:");
    prefix(stack1[stackPtr]);
    printf("\n");
    
    printf("\n\t\t\tthe postfix form:");
    postfix(stack1[stackPtr]);
    
    
    
    
    getch(); break;
    }
    
    case 2:{
    printf("\n\t\t\tenter an prefix expression: ");
    scanf("%s",&input); SetExpression (input) ;
    
    
    ConvertToPostfix ( ) ;
    for(count = 0; output[count] != '\0'; count++)
    {
    switch(chkElement(output[count]))
    {
    case 1:
    opFunc(output[count]);
    break;
    case 0:
    varFunc(output[count]);
    break;
    default:
    printf("\nSorry unrecognized entry");
    }
    }
    ofstream in,pre,post;
    in.open("infix.txt");
    in<<input;
    in.close();
    pre.open("prefix.txt");
    pre<<root->info;
    pre.close();
    post.open("postfix.txt");
    post<<output;
    post.close();
    
    
    printf("\n\t\t\tthe infix form:");
    infix(stack1[stackPtr]);
    printf("\n");
    
    printf("\n\t\t\tthe postfix form:");
    postfix(stack1[stackPtr]);
    
    
    
    
    getch(); break;
    }
    case 3:{
    printf("\t\t\tenter an postfix expression: ");
    scanf("%s",&input);
    
    SetExpression (input) ;
    
    
    ConvertToPostfix ( ) ;
    for(count = 0; output[count] != '\0'; count++)
    {
    switch(chkElement(output[count]))
    {
    case 1:
    opFunc(output[count]);
    break;
    case 0:
    varFunc(output[count]);
    break;
    default:
    printf("\t\t\t\nSorry unrecognized entry");
    }
    }
    ofstream in,pre,post;
    in.open("infix.txt");
    in<<input;
    in.close();
    pre.open("prefix.txt");
    pre<<root->info;
    pre.close();
    post.open("postfix.txt");
    post<<output;
    post.close();
    
    
    printf("\n\t\t\tthe infix form:");
    infix(stack1[stackPtr]);
    printf("\n");
    printf("\t\t\tthe prefix form:");
    prefix(stack1[stackPtr]);
    printf("\n");
    
    
    
    
    getch(); break;
    }
    }}
    int chkElement(char element)
    {
    switch(element)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '%':
    case '^':
    return 1;
    
    default:
    return 0;
    }
    }
    
    void opFunc(char optr)
    {
    root = (node*)malloc(sizeof(node));
    root->info = optr;
    root->right = pop();
    root->left = pop();
    push(root);
    }
    
    struct node* pop()
    {
    return(stack1[stackPtr--]);
    }
    void varFunc(char var)
    {
    root = (node*)malloc(sizeof(node));
    root->info = var;
    root->right = NULL;
    root->left = NULL;
    push(root);
    }
    
    void push(node* root)
    {
    stack1[++stackPtr] = root;
    }
    
    void infix(node* root)
    {
    if(root->left != NULL)
    infix(root->left);
    printf("%c",root->info);
    if(root->right !=NULL)
    infix(root->right);
    }
    
    void prefix(node* root)
    {
    ofstream pre;
    pre.open("prefix.txt");
    pre<<root->info;
    printf("%c",root->info);
    if(root->left != NULL)
    prefix(root->left);
    pre<<root->left->info;
    if(root->right !=NULL)
    prefix(root->right);
    pre<<root->right->info;
    }
    
    void postfix(node* root)
    {
    if(root->left != NULL)
    postfix(root->left);
    if(root->right !=NULL)
    postfix(root->right);
    printf("%c",root->info);
    }
    
    
    
    void SetExpression ( char *str )
    {
    
    s = str ;
    l = strlen ( s ) ;
    *( output + l ) = '\0' ;
    t = output;
    }
    
    void PushOnStack ( char c )
    {
    if ( top == MAX - 1 )
    printf("\n\t\t\tStack is full.\n");
    else
    {
    top++ ;
    stack[top] = c ;
    }
    }
    
    char PopFromStack ()
    {
    if ( top == -1 )
    return -1 ;
    else
    {
    char info = stack[top] ;
    top-- ;
    return info ;
    }
    }
    
    
    int priority ( char c )
    {
    if ( c == '^' ) return 3 ;
    if ( c == '*' || c == '/' || c == '%' ) return 2 ;
    else if ( c == '+' || c == '-' ) return 1 ;
    else return 0 ;
    }
    
    
    void ConvertToPostfix ()
    {
    char opr ;
    
    while ( *( s ) )
    {
    if ( *( s ) == ' ' || *( s ) == '\t' )
    {
    s++ ;
    continue ;
    }
    
    if ( isdigit ( *( s ) ) || isalpha ( *(s ) ) )
    {
    while ( isdigit ( *( s ) ) || isalpha ( * ( s ) ) )
    {
    *( t ) = *( s ) ;
    s++ ;
    t++ ;
    }
    }
    if ( *( s ) == '(' )
    {
    PushOnStack ( *( s ) ) ;
    s++ ;
    }
    if ( *( s ) == '*' || *( s ) == '+' || *( s )== '/' || *( s ) == '%' || *( s ) == '-' || *( s ) == '^' )
    {
    if ( top != -1 )
    {
    opr = PopFromStack ( ) ;
    while ( priority ( opr ) >=
    priority ( *( s ) ) )
    {
    *( t ) = opr ;
    t++ ;
    opr =
    PopFromStack ( ) ;
    }
    PushOnStack ( opr ) ;
    PushOnStack ( *( s ) ) ;
    }
    else PushOnStack ( *( s ) ) ;
    s++ ;
    }
    if ( *( s ) == ')' )
    {
    opr = PopFromStack ( ) ;
    while ( opr != '(' )
    {
    *( t ) = opr ;
    t++ ;
    opr = PopFromStack ( ) ;
    }
    s++ ;
    }
    }
    while ( top != -1 )
    {
    opr = PopFromStack ( ) ;
    *( t ) = opr ;
    t++ ;
    }
    t++ ;
    getch();
    }
     

Share This Page