hi i want to convert the prefix form to postfix,prefix and infix form but when i input +5*62 it retuns only 2 what is wrong with my code can anybody help to solve this

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Operator 1
#define notOperator 0
#define empty -1
struct node{
char item;
struct node* leftchild;
struct node* rightchild;
};
void formatting(void);
void getinput(void);
int chkElement(char);
void opFunc(char);

void varfunc(char);
void push(struct node*);
struct node* pop();
void dispTree(void);
void infix(struct node*);
void prefix(struct node*);
void postfix(struct node*);

char equation[50];

int len,i;

struct node* stack[25];
int stackPtr= -1;

struct node* root;
int main()
{

    int count;
  int decission;
   do{


    formatting();
    getinput();
    printf("\nyou have entered");
    puts(equation);


    for(count=0;equation[count]!='\0';count++)
    {
        switch(chkElement(equation[count]))
        {
            case Operator:

            opFunc(equation[count]);

            break;
            case notOperator:
            varfunc(equation[count]);
            break;

            default:
            printf("\nSorry unrecognized entry");

        }
    }
    dispTree();

    printf("\n\npress 0 to exit \t 1 to continue");
    scanf("%d",&decission);
    if(decission==0)
    printf("\n\n\n\n\n\t\t...........thank you visit again!!!............");
    printf("\n\n\n\n\n\n");

    }while(decission);
    getch();
}

void dispTree(void)
{
    char choice;
    printf("\nSelect the output form:[i]nfix,p[r]efix,p[o]stfix");
    choice=getche();
    printf("\n");
    switch(choice)
    {
        case 'i':
        printf("\nInorder representation of output is:");
        infix(stack[stackPtr]);
        //char exp[]="(8+8)/2+5";

        break;
        case 'r':
        printf("\nPreorder representation of output is:");
prefix(stack[stackPtr]);
break;
        case 'o':
        printf("\nPostorder representation of output is:");
        postfix(stack[stackPtr]);
        break;
        default:
        printf("\nyou have pressed the button other than given choices");

    }
}
void infix(struct node* root)
{
    if(root->leftchild!=NULL)
    infix(root->leftchild);
    printf("%c",root->item);
     if(root->rightchild!=NULL)
     infix(root->rightchild);

}
void prefix(struct node*root)
{
    printf("%c",root->item);
    if(root->leftchild!=NULL)
    prefix(root->leftchild);
    if(root->rightchild!=NULL)
    prefix(root->rightchild);
}
void postfix(struct node* root)
{
    if(root->leftchild!=NULL)
   postfix(root->leftchild);
    if(root->rightchild!=NULL)
    postfix(root->rightchild);
      printf("%c",root->item);
}
void opFunc(char optr)
{
    root=(struct node*)malloc(sizeof(struct node));
root->item=optr;


root->leftchild!=NULL;
root->rightchild!=NULL;
push(root);
}

struct node* pop(void)
{

    return(stack[stackPtr--]);
}
void varfunc(char var)
{
    root=(struct node*)malloc(sizeof(struct node));
    root->item=var;

    root->rightchild=NULL;
    root->leftchild=NULL;

push(root);
}
void push(struct node*root)
{

    stack[++stackPtr]=root;

}
int chkElement(char element)
{
    switch(element)
    {
        case '+':
        case '-':
        case '*':
        case '/':
        case '%':
        case '^':
        return(Operator);
        default:
        return(notOperator);

    }
}
void getinput(void)
{
    printf("\nenter equation in the form of prefix");
    gets(equation);

}
void formatting(void)
{
    int i;
    printf("\n");
    for(i=0;i<=79;i++)
    printf("*");
    printf("\n......program title\t\t#Binary tree");
    printf("\n......Created by\t\t#Ashish karna");
    printf("\n......Description\t\tCreation of Expression Tree");
    for(i=0;i<=79;i++)
    printf("*");
}
Member Avatar for I_m_rude

nice attemp! try to visualize it more clearly. if u can write that much, then trying a little bit more, u will get it. try!

i kept the testing condition under for loop till it gets + sign and i moved the stackpointer for each loop bt it didn't work .

Member Avatar for I_m_rude

try to put testing statements in , after and before the loops. this will be more better way to check your loop and other changes in the loop. try this and if you fell the problem, then post it here.

Your tree generation algorithm isn't really doing anything because the nodes aren't linked together. Compare and contrast:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
    char *token;
    struct node *left;
    struct node *right;
};

struct node *make_node(const char *token, struct node *left, struct node *right)
{
    struct node *ret = malloc(sizeof *ret);

    if (ret) {
        ret->token = strdup(token); /* Using a non-standard function for brevity */
        ret->left = left;
        ret->right = right;
    }

    return ret;
}

struct node *generate_expression_tree(const char *tokens[], size_t n)
{
    struct node *stack[1024];
    size_t top = 0;
    size_t i;

    for (i = n - 1; i < (size_t)-1; --i) {
        if (isalnum((unsigned char)tokens[i][0]))
            stack[top++] = make_node(tokens[i], NULL, NULL);
        else {
            /* Assuming all binary operators for brevity */
            struct node *lhs = stack[--top];
            struct node *rhs = stack[--top];

            stack[top++] = make_node(tokens[i], lhs, rhs);
        }
    }

    return stack[--top];
}

void display_prefix(struct node *root)
{
    if (!root)
        return;

    printf("%s ", root->token);
    display_prefix(root->left);
    display_prefix(root->right);
}

void display_postfix(struct node *root)
{
    if (!root)
        return;

    display_postfix(root->left);
    display_postfix(root->right);
    printf("%s ", root->token);
}

void display_infix(struct node *root)
{
    if (!root)
        return;

    display_infix(root->left);
    printf("%s ", root->token);
    display_infix(root->right);
}

int main(void)
{
    const char *tokens[] = {"+", "*", "A", "B", "/", "C", "D"};
    struct node *root = generate_expression_tree(tokens, sizeof tokens / sizeof *tokens);

    display_prefix(root); puts("");
    display_postfix(root); puts("");
    display_infix(root); puts("");

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.