/****************************************************** Name: Filename: Create.cpp ******************************************************/ #include "Tree.h" // The following function will read a double from a string char* GetDouble(char *expression, double *result) { char *c_expr=expression; double c_val=0; // Compute integer part while ((*c_expr>='0') && (*c_expr<='9')) { c_val= c_val*10 + (*c_expr - '0'); c_expr++; } if (*c_expr=='.') // we have a decimal part { // Compute decimal part double c_mult=0.1; c_expr++; while ((*c_expr>='0') && (*c_expr<='9')) { c_val+= c_mult * (*c_expr - '0'); c_expr++; c_mult*=0.1; } } // Set result *result=c_val; // Check for errors if(c_expr==expression) { return NULL; } // All ok! return c_expr; } // Forward declaration of function HandleSum is needed because it is used from HandleProduct char *HandleSum(char *expression, CTree **tree); // Will handle an expression substring considering it a product // Return value: the new position in the evaluated expression // Will set the computed expression tree in variable "tree" char *HandleProduct(char *expression, CTree **tree) { char *c_expr=expression; CTree *head=new CTree; if(head==NULL) { return NULL; } // Creating subtree for the first part of the product in head->left if(*c_expr=='(') // Open parenthesis { c_expr++; c_expr=HandleSum(c_expr,&head->left); // compute the tree for the sum in the paranthesis if(c_expr==NULL) { return NULL; } if(*c_expr!=')') // Parenthesis needs to be closed { return NULL; } c_expr++; } else { c_expr=GetDouble(c_expr,&head->value); // No paranthesis, we have just a value if(c_expr==NULL) { return NULL; } } // Checking if we have a second part if(*c_expr!='*') { // We don't have a second part if(head->left!=NULL) // If we have already computed a subtree return this { *tree=head->left; delete(head); } else // Otherwise return a leaf { head->type=TYPE_LEAF; *tree=head; } return c_expr; } // Creating subtree for the first part of the product in head->right c_expr++; head->type=TYPE_MULTIPLY_NODE; if(head->left==NULL) // We have a second part of the product so put first(left) part in a subtree if not already so { head->left=new CTree; head->left->type=TYPE_LEAF; head->left->value=head->value; } c_expr=HandleProduct(c_expr,&head->right); // Computing second part subtree if(c_expr==NULL) { return NULL; } // Returning results *tree=head; return c_expr; } // Will handle an expression substring considering it a sum // Return value: the new position in the evaluated expression // Will set the computed expression tree in variable "tree" char *HandleSum(char *expression, CTree **tree) { char *c_expr=expression; CTree *head=new CTree; if(head==NULL) { return NULL; } // Creating subtree for the first part of the sum in head->left c_expr=HandleProduct(c_expr,&head->left); if(c_expr==NULL) { return NULL; } // Checking if we have a second part if(*c_expr!='+') { // We don't have a second part *tree=head->left; delete(head); return c_expr; } // Creating subtree for the second part of the sum in head->right head->type=TYPE_ADD_NODE; c_expr++; c_expr=HandleSum(c_expr,&head->right); if(c_expr==NULL) { return NULL; } // Returning results *tree=head; return c_expr; }