Reverse Polish Notation

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

Join Date: Feb 2009
Posts: 57
Reputation: Ineedhelpplz has a little shameless behaviour in the past 
Solved Threads: 1
Ineedhelpplz Ineedhelpplz is offline Offline
Junior Poster in Training

Reverse Polish Notation

 
0
  #1
Oct 27th, 2009
So I have written a reverse polish notation calculator:
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUMBER '0'
  6. #define MAXOP 100
  7.  
  8. int main ()
  9. {
  10. int type;
  11. int op1, op2;
  12. char s[MAXOP];
  13. while ((type = getop(s)) != EOF)
  14. switch (type)
  15. {
  16. case NUMBER:
  17. push(atoi(s));
  18. break;
  19. case '^':
  20. push ((unsigned int) pop() ^ (unsigned int) pop());
  21. break;
  22. case '~':
  23. push(~(unsigned int) pop());
  24. break;
  25. case '+':
  26. case '*':
  27. if (type == '+')
  28. push(pop() + pop());
  29. else
  30. push(pop() * pop());
  31. break;
  32. case '-':
  33. op2 = pop();
  34. push(pop() - op2);
  35. break;
  36. case '/':
  37. case '%':
  38. if ((op2 = pop()) != 0.0) {
  39. if (type == '/')
  40. push(pop() / op2);
  41. else {
  42. op1 = pop();
  43. push(op1 - op2 * ((int) (op1/op2)));
  44. }
  45. } else
  46. printf("Error: Zero divisor!\n");
  47. break;
  48. case '\n':
  49. printf("The answer is %d\n", pop());
  50. break;
  51. default:
  52. printf("Error: Unknown command %s!\n", s);
  53. break;
  54. }
  55. return 0;
  56. }
I now need to run the infix string " ~(((202%16) + (292/16)*16) ^292 " through my calculator...however the string is in infix, RPN takes postfix. I have been having trouble converting this expression to postfix and need help. The program itself is done I just want to test that input and don't quite understand how to make it work in postfix.
Last edited by Ineedhelpplz; Oct 27th, 2009 at 10:33 am. Reason: fixed code
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
1
  #2
Oct 27th, 2009
Just explore this site a bit.
Infix to postfix conversion has been discussed earlier in many threads.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 57
Reputation: Ineedhelpplz has a little shameless behaviour in the past 
Solved Threads: 1
Ineedhelpplz Ineedhelpplz is offline Offline
Junior Poster in Training
 
0
  #3
Oct 27th, 2009
Thanks! Got my answer:
292 16 % 292 16 / + 16 * 292 ^ ~
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 312 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC