having problems to complete implement a simple calculator program in c

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 1
Reputation: jean is an unknown quantity at this point 
Solved Threads: 0
jean jean is offline Offline
Newbie Poster

having problems to complete implement a simple calculator program in c

 
0
  #1
May 20th, 2005
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
  1. #include "exptree.h"
  2.  
  3.  
  4. void make_exptree( treenodeptr *root, char *postfix ){
  5. char **arr_exp;
  6. int argn, i;
  7. expstack mystack;
  8. expop myop;
  9. treenode *newnode;
  10. char space = ' ';
  11.  
  12. mystack = make_stack(100);
  13.  
  14. arr_exp = exptok(postfix, &argn);
  15.  
  16. for(i=0; i<argn; i++){
  17. newnode = (treenodeptr) malloc( sizeof(treenode) );
  18. newnode->left = NULL;
  19. newnode->right = NULL;
  20.  
  21. switch( arr_exp[i][0] ){
  22. case '/': case '*': case '+': case '-':
  23. newnode->operator = arr_exp[i][0];
  24. pop(mystack, &myop);
  25. newnode->right = (treenodeptr) myop.op;
  26. pop(mystack, &myop);
  27. newnode->left = (treenodeptr) myop.op;
  28. myop.op = (void *)newnode;
  29. push(mystack, myop);
  30. break;
  31.  
  32. case '0': case '1': case '2': case '3': case '4':
  33. case '5': case '6': case '7': case '8': case '9':
  34. sscanf(arr_exp[i],"%d", &(newnode->operand));
  35. myop.op = (void *)newnode;
  36. push(mystack, myop);
  37. break;
  38.  
  39. default:
  40. break;
  41. }
  42. }
  43.  
  44. pop(mystack, &myop);
  45. *root = (treenodeptr) myop.op;
  46. destroy_stack(mystack);
  47. destroy_exp(arr_exp, argn);
  48. }
  49.  
  50. /*-----------------------------------------------------------*/
  51.  
  52. void destroy_exptree( treenodeptr * root){
  53. if (*root == NULL)
  54. return;
  55.  
  56. destroy_exptree( &((*root)->left) );
  57. destroy_exptree( &((*root)->right) );
  58. free( *root );
  59. *root = NULL;
  60. }
  61.  
  62. /*-----------------------------------------------------------*/
  63.  
  64. int eval_exptree( treenodeptr exp_root){
  65. [COLOR=DarkRed]this is the prat that i am having problem with[/COLOR]
  66. --> /* implement me */
  67.  
  68. }

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.
Last edited by Catweazle; May 20th, 2005 at 9:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 3,826
Reputation: Catweazle is a jewel in the rough Catweazle is a jewel in the rough Catweazle is a jewel in the rough Catweazle is a jewel in the rough 
Solved Threads: 144
Team Colleague
Catweazle Catweazle is offline Offline
Grandad

Re: having problems to complete implement a simple calculator program in c

 
0
  #2
May 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: having problems to complete implement a simple calculator program in c

 
0
  #3
May 20th, 2005
Think recursively. Operands return the value, binary operators perform themselves on the left and right value:
  1. if (is_operand())
  2. return value;
  3.  
  4. T a = recurse(left);
  5. T b = recurse(right);
  6.  
  7. switch (operator()) {
  8. case '+': return a + b;
  9. case '-': return a - b;
  10. case '*': return a * b;
  11. case '/': return a / b;
  12. }
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC