Advertisement
vivex42

Reverse Polish Notation Calculator

Nov 21st, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. //Reverse Polish Notation calculator
  2. //By Potential Difference, 2010
  3.  
  4. string CALCULATIONS="+-^*/";
  5. integer ADD=0;
  6. integer SUB=1;
  7. integer EXP=2;
  8. integer MLT=3;
  9. integer DIV=4;
  10. string NULL_STRING = "";
  11. integer listen_handle;
  12. list message_list;
  13. integer message_length;
  14. integer DEBUG = TRUE;
  15.  
  16. debug(string message)
  17. {
  18. if (DEBUG == TRUE)
  19. llWhisper(0, message);
  20. }
  21.  
  22. default
  23. {
  24.  
  25. state_entry()
  26. {
  27.  
  28. listen_handle = llListen(0, NULL_STRING, llGetOwner(), "/calc");
  29. }
  30.  
  31. listen(integer channel, string name, key id, string message)
  32. {
  33. llListenRemove(listen_handle);
  34. state setup;
  35.  
  36. }
  37.  
  38. }
  39.  
  40. state setup
  41. {
  42.  
  43. state_entry()
  44. {
  45. llWhisper(0, "Accepting next message as input...");
  46. listen_handle = llListen(0, NULL_STRING, llGetOwner(), NULL_STRING);
  47.  
  48. }
  49.  
  50. listen(integer channel, string name, key id, string _message)
  51. {
  52. message_length = llStringLength(_message);
  53. message_list = llParseString2List(_message,[" "],[]);
  54. llListenRemove(listen_handle);
  55. state calculate;
  56. }//end listen
  57.  
  58.  
  59.  
  60. }//end state
  61. // while
  62. state calculate
  63. {
  64. state_entry()
  65. {
  66. integer lLength = llGetListLength(message_list);
  67. integer count = 0;
  68. list stack;
  69. string temp;
  70. string e1; string e2;
  71. while (count < lLength)
  72. {
  73. temp = llList2String(message_list,count);
  74. if (llSubStringIndex(CALCULATIONS,temp) == -1)
  75. {
  76. stack = temp + stack;
  77. }
  78. else
  79. {
  80. e1 = llList2String(stack, 0);
  81. stack = llDeleteSubList(stack,0,0);
  82. e2 = llList2String(stack, 0);
  83. stack = llDeleteSubList(stack,0,0);
  84.  
  85. if (llSubStringIndex(CALCULATIONS, temp) == MLT)
  86. {stack = ((float)e2 * (float)e1) + stack;
  87. debug((string)e2 + "*" + (string)e1);
  88. }
  89. else
  90. if (llSubStringIndex(CALCULATIONS, temp) == ADD)
  91. {stack = ((float)e2 + (float)e1) + stack;
  92. debug((string)e2 + "+" + (string)e1);
  93. }
  94. else
  95. if(llSubStringIndex(CALCULATIONS, temp) == SUB)
  96. {stack = ((float)e2 - (float)e1) + stack;
  97. debug((string)e2 + "-" + (string)e1);
  98. }
  99. else
  100. if(llSubStringIndex(CALCULATIONS, temp) == DIV)
  101. {stack = ((float)e2 / (float)e1) + stack;
  102. debug((string)e2 + "/" + (string)e1);
  103. }
  104. else
  105. if(llSubStringIndex(CALCULATIONS, temp) == EXP)
  106. {stack = llPow((float)e2,(float)e1) + stack;
  107. debug((string)e2 + "^" + (string)e1);
  108. }
  109.  
  110. }
  111. ++count;
  112. }
  113.  
  114. if (llGetListLength(stack) > 1) {llWhisper(0, "Error: too many/invalid arguments");state default;}
  115. else
  116. {llSay(0, llList2String(stack,0));state default;}
  117. }
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement