954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

having problems to complete implement a simple calculator program in c

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){
    [COLOR=DarkRed]this is the prat that i am having problem  with[/COLOR]
  --> /* 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:

jean
Newbie Poster
1 post since May 2005
Reputation Points: 10
Solved Threads: 0
 

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 :)

Catweazle
Grandad
Team Colleague
4,335 posts since Mar 2004
Reputation Points: 229
Solved Threads: 149
 

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.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You