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("*");
}