944,142 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4212
  • C RSS
May 20th, 2005
0

having problems to complete implement a simple calculator program in c

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jean is offline Offline
1 posts
since May 2005
May 20th, 2005
0

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

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
Team Colleague
Reputation Points: 229
Solved Threads: 149
Grandad
Catweazle is offline Offline
3,826 posts
since Mar 2004
May 20th, 2005
0

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

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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C help calculate grand total
Next Thread in C Forum Timeline: WM_SIZING Probelm in my application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC