I really need some help to finish my assignment which is due 2005/05/23
I have the other header and c files but i need to do some more work ........if u can help me i would be very greatful plzzzzzzzzz
this is the code that follows for exptree.c

#include "exptree.h"


void make_exptree( treenodeptr *root, char *postfix ){
    char **arr_exp;
    int  argn, i;
    expstack mystack;
    expop    myop;
    treenode *newnode;
    char space = ' ';

    mystack = make_stack(100);

    arr_exp = exptok(postfix, &argn);

    for(i=0; i<argn; i++){
       newnode = (treenodeptr) malloc( sizeof(treenode) );
       newnode->left = NULL;
       newnode->right = NULL;

       switch( arr_exp[i][0] ){
       case '/': case '*': case '+': case '-':
           newnode->operator = arr_exp[i][0];
           pop(mystack, &myop);
           newnode->right = (treenodeptr) myop.op;
           pop(mystack, &myop);
           newnode->left = (treenodeptr) myop.op;
           myop.op = (void *)newnode;
           push(mystack, myop);
           break;

       case '0':  case '1':  case '2':  case '3': case '4':
       case '5':  case '6':  case '7':  case '8': case '9':
           sscanf(arr_exp[i],"%d", &(newnode->operand));
           myop.op = (void *)newnode;
           push(mystack, myop);
           break;

       default:
           break;
       }     
    }

    pop(mystack, &myop);
    *root = (treenodeptr) myop.op;
    destroy_stack(mystack);
    destroy_exp(arr_exp, argn);
}

/*-----------------------------------------------------------*/

void destroy_exptree( treenodeptr * root){
    if (*root == NULL)
        return;

    destroy_exptree( &((*root)->left) );
    destroy_exptree( &((*root)->right) );
    free( *root );
    *root = NULL;
}

/*-----------------------------------------------------------*/

int eval_exptree( treenodeptr exp_root){
    this is the prat that i am having problem
  --> /* implement me */ 

}

note:
make_exptree() creates an expression tree from a postfix expression, consisting of nodes defined in exptree.h. To See this file for details on the implementation of each treenode structure plz ask

Thank you ......and looking forward for your help. :sad:

Edited 3 Years Ago by Dani: Formatting fixed

Hi jean and welcome to Daniweb.

I've removed the email contact from your post, as it is forum policy to encourage all discussion of a topic to occur in the thread, rather than via email.

I've also moved this topic to the appropriate forum section for you. Good luck with your assignment :)

Think recursively. Operands return the value, binary operators perform themselves on the left and right value:

if (is_operand())
  return value;

T a = recurse(left);
T b = recurse(right);

switch (operator()) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}

It's also easy to turn the four function calculator into something more powerful. You can easily modify the algorithm to handle unary operators and add error checking to verify that the syntax of the expression is correct.

This article has been dead for over six months. Start a new discussion instead.