Go Back   CodeCall Programming Forum > Software Development > C and C++
Register Blogs Search Today's Posts Mark Forums Read

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-08-2009, 06:03 AM
MrCode's Avatar
Newbie
 
Join Date: Oct 2009
Posts: 2
MrCode is an unknown quantity at this point
tree for infix expression

Hi,

Suggest me on how to avoid the problem.
i want my program to work for any expression

but here program is not working for second input.

just let me know the procedure how does the compiler is able to parse the complex expressions.





Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "BST.h"

#define EMPTY 0

void push_exp(int item);
int pop_exp();
int is_stk_exp_empty();
void push_tree();
BST_t *pop_tree();
int is_stk_tree_empty();

BST_t * infixtree(char* infix);

struct stack_exp
{
        int data[MAX];
        int top;
};

struct stack_exp ex;

struct stack_tree
{
        BST_t *nodes[100];
	 int top;
};

struct stack_tree tree;

int emptystacks()
{
        ex.top =-1;
        tree.top =-1;
}

void push_exp(int item)
{
                ++ex.top;
                ex.data[ex.top]=item;
}

int pop_exp()
{
        int ret =0;
        if(ex.top != -1)
        {
                ret= ex.data[ex.top];
                --ex.top;
        }
        return ret;
}

int is_stk_exp_empty()
{
	if(ex.top == -1)

                return 1;
        else
                return 0;
}

int isoperator(char e)
{
        if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%'|| e == '^')

                return 1;
        else
                return 0;
}

BST_t *create_node(int n1)
{
        BST_t *newnode;
        newnode = (BST_t *)malloc(sizeof(newnode));

        newnode->data=n1;
        newnode->left=NULL;
        newnode->right=NULL;

        return newnode;
}

int isoper(char ch)
{
	 char op;
        switch(ch)
        {
        case '+':
        case '-':
        case '/':
        case '*':
        case '^':
                op =1;
                break;
        default:
                op=0;
                break;
        }
return op;
}


BST_t * infixtree(char* infix)
{
        char *i,*p;
        char n1;
        BST_t *temp,*r,*l,*root,*t;
        i = &infix[0];

        while(*i)
        {

                
                while(isblank(*i) && *i != '\0')
		 {
                        i++;
                }
			
                if( isdigit(*i) || isalpha(*i))
                {
                        while( isdigit(*i) || isalpha(*i))
                        {
                                push_exp(*i);
                                i++;
                        }
                }
                if(isoper(*i))
                {
                        push_exp(*i);
                }
                if( *i == '(' )
                {
                        push_exp(*i);
                }

                if( *i == ')')
                {
                while( (n1 = pop_exp()) != '(')
                {

                  if(isoperator(n1))
                   {
                     temp = create_node(n1);
			   t = pop_tree();
                     if(t)
                       temp->right = t;
                   }
                  else
                  {
                    r=create_node(n1);
                    push_tree(r);
                  }

                }
                l=pop_tree();
                if(l){
                        temp->left=l;
                        root=temp;
                        push_tree(root);
                }
}
i++;
}
return root;
}

void push_tree(BST_t *t)
{
        tree.top++;
        tree.nodes[tree.top]=(BST_t *)malloc(sizeof(BST_t));
        tree.nodes[tree.top]=t;
}
            

BST_t *pop_tree()
{
        if(tree.top > -1 )
        	return tree.nodes[tree.top--];
        else
       	 return NULL;
}

int is_stk_tree_empty()
{
        if(tree.top == -1)
                return 1;
        else
                return 0;
}

void inorder(BST_t * root)
{
        if(root != NULL )
        {
            inorder(root->left);
            printf("%c", root->data);
	    inorder(root->right);
        }
}


int main()
{
      char in1[70],in2[70],post[50],ch;
      BST_t * root1, *root2;root1=root2=NULL;
      int i=0;
      printf(" Exp: \"( a + ( ( ( ( b * c ) ^ d ) / ( e + f ) ) * g) )\"\n");
      strcpy(in1," ( a + ( ( ( ( b * c ) ^ d ) / ( e + f ) ) * g))"); '
      emptystacks();
      root1 = infixtree(in1);
      inorder(root1);
      printf("\n");
      printf("Exp : \"a + b * c ^ d / ( e + f ) * g\" \n");
      strcpy(in2,"a + b * c ^ d / ( e + f ) * g"); 
      emptystacks();
      root2 = infixtree(in2);
      inorder(root2);
return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-08-2009, 12:46 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: tree for infix expression

Moved to correct forum.

Without having BST.h, it's a little hard to see what all might be happening. What input/output are you getting?
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-14-2009, 05:44 AM
MrCode's Avatar
Newbie
 
Join Date: Oct 2009
Posts: 2
MrCode is an unknown quantity at this point
Re: tree for infix expression

i am actually using some other macors and all here please consider which are required for the above program only.


Code:
#ifndef __BSTREE__
#define __BSTREE__
        typedef struct Bin_Srch_Tree BST_t;
        struct Bin_Srch_Tree
        {
                BST_t *left;
                int data;
                BST_t *right;
        };

extern  BST_t *nodes[20];

#define Node_Inserted_Successfully  1

#define Node_Creation_Failed     2

#define Duplication_Not_Allowed  3
#define MAX 20

BST_t *create_node(int );
int insert(BST_t **, int );

void inorder(BST_t *);
void preorder(BST_t *);
void postorder(BST_t *);

extern int top, b;
int empty();
void push(BST_t **, BST_t *);
BST_t*  pop(BST_t **);
#endif
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-14-2009, 11:32 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: tree for infix expression

My guess is there may be an error with push or pop.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary Tree with linked lists help BlaineSch Java Help 5 04-28-2009 06:22 PM
AVL Tree balance problem reignreborn1335 C and C++ 1 04-24-2009 07:13 AM
Binary tree with user input data bbto C and C++ 5 03-14-2009 08:47 AM
B+ Tree C Implementation makkx9 C and C++ 1 05-17-2008 06:12 PM
need help with simple C++ TicTacToe game with AI flupke1 C and C++ 11 08-14-2007 11:27 AM


All times are GMT -5. The time now is 06:55 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0