fork(2) download
  1. //*****************************************************************************
  2. // Postfix expression evaluation
  3. // 11/13/2012
  4. // by DJH
  5. //
  6. // all applicable copyrights apply
  7. //
  8. // this program evaluates an input postfix string
  9. //
  10. //
  11. // created using Dev-C++ 5.2.0.3
  12. //*****************************************************************************
  13.  
  14. // ----------------------------------libraries---------------------------------
  15.  
  16. #include <iostream> // For cin, cout and endl
  17. #include <string>
  18. #include <cctype>
  19. #include <vector>
  20. #include <sstream>
  21. #include <cstdlib>
  22. #include <cmath>
  23. #include <iomanip>
  24.  
  25. using namespace std;
  26. // ------------------------------ Globals -------------------------------------
  27. string postfix;
  28.  
  29. // --------------------------- stack class ------------------------------------
  30. class Stack{
  31. public:
  32. enum {MaxStack = 50};
  33. void init() {top = -1;}
  34. void push( double p ){
  35. if ( isFull() ) {
  36. cerr << "Full Stack. DON'T PUSH\n";
  37. return;
  38. }
  39. else {
  40. arr[ ++top ] = p;
  41. cout << "Just pushed " << p << endl;
  42. return;}
  43. }
  44. int pop() {
  45. if (isEmpty() ) {
  46. cerr << "\tEmpty Stack. Don't Pop\n\n";
  47. return 1;
  48. }
  49. else
  50. return arr[top--];
  51. }
  52. bool isEmpty() {return top < 0 ? 1 : 0;}
  53. bool isFull() {return top >= MaxStack -1 ? top : 0;}
  54. void dump_stack() {
  55. cout << "The Stack contents, from top to bottom, from a stack dump are: " << endl;
  56. for (int s = top; s >= 0; s--)
  57. cout << "\t\t" << arr[s] << endl;
  58. }
  59. private:
  60. int top;
  61. double arr[MaxStack];
  62. }pStack;
  63. // ------------------------------ end stack class -----------------------------
  64.  
  65. // -----------------------------function prototypes----------------------------
  66. void evalPostfix (string);
  67. double decimalEvaluate (string, int, double);
  68. // ----------------------------------------------------------------------------
  69.  
  70. // -----------------------------------Main()-----------------------------------
  71. int main()
  72. {
  73. cout << "Enter a postfix expression\n\t (without spaces - using '_' for delimiters):\n\t";
  74. cout << "For example: 8_5_3_+_*_2_/_5_+\n" << endl;
  75. getline(cin, postfix);
  76.  
  77. // postfix = "7_2_*_5_+_2_*";
  78. cout << "You entered: " << postfix << endl;
  79.  
  80. int c=0;
  81.  
  82. while ( postfix[c] !='\0' )
  83. {
  84. ++c; // this loop counts the characters in the input string including whitespace
  85. }
  86.  
  87. cout << "\tThe string length is:\t" << c << endl;
  88. evalPostfix (postfix);
  89. int result = pStack.pop();
  90. cout << "The result of the postfix expression is: " << result << endl;
  91.  
  92.  
  93. /*
  94. stack commands:
  95. Stack a_stack; // creates new stack
  96. a_stack.init(); // initializes top element
  97. a_stack.pop(); // pops top of stack
  98. a_stack.push(n); // push element to top of stack
  99. a_stack.dump_stack(); // displays the contents of the stack from top to bottom
  100. */
  101. return 0;
  102. cin.get();
  103. }
  104.  
  105.  
  106.  
  107. // --------------------------------end of Main()-------------------------------
  108.  
  109.  
  110. // ------------------------------functions follow------------------------------
  111. void evalPostfix (string)
  112. {
  113. double ch=0,dc=0,b=0,a=0,d=0;
  114. double tempDC=0;
  115. double tempDCD=0;
  116. char op;
  117. int i=0,j=0,k=0,m=0,n=0,q=0;
  118. while (postfix[i] != '\0')
  119. {
  120.  
  121. if (postfix[i] == '_') {i++;}
  122.  
  123. else if (isdigit (postfix[i]))
  124. {
  125. ch = postfix[i] - '0'; // for numbers only
  126. j=i+1;
  127. while (postfix[j] != '_')
  128. {
  129. if (isdigit (postfix[j]))
  130. {
  131. ch = ch*10 + (postfix[j] - '0');
  132. k=j+1;
  133. if (postfix[k] == '.') // this accounts for decimals by skipping the '.' and conducting operations on trailing numbers
  134. {
  135. dc=0;
  136. decimalEvaluate (postfix, k, dc);
  137. dc = tempDC / tempDCD;
  138. d=ch+dc;
  139. k++;
  140.  
  141. }
  142. j=k-1;
  143. j++;
  144. }
  145. }
  146. cout << "Post decimal function k: " << k << endl;
  147. cout << "Post decimal function dc: " << setprecision (12) << dc << endl;
  148. cout << "Post decimal function d: " << d << endl;
  149. pStack.push(d);
  150. i=j-1;
  151. i++;
  152. }
  153. else if (postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '^')
  154. {
  155. b=pStack.pop();
  156. a=pStack.pop();
  157. op = postfix[i]; // for operators only
  158. switch(op)
  159. {
  160. case '+':pStack.push(a+b);
  161. break;
  162. case '-':pStack.push(a-b);
  163. break;
  164. case '*':pStack.push(a*b);
  165. break;
  166. case '^':pStack.push(pow(a,b));
  167. break;
  168. case '/':if(b==0)
  169. {
  170. cout << "Division by zero not allowed!" << endl;
  171. cin.get();
  172. exit(0);
  173. }
  174. pStack.push(a/b);
  175. default:cout << "Invalid Operation" << endl;
  176. }
  177. i++;
  178. }
  179.  
  180. }
  181. }
  182. // ----------------------------------------------------------------------------
  183.  
  184. double decimalEvaluate (string postfix, int k, double dc)
  185. {
  186. dc=0;
  187. double tempDC=0;
  188. double tempDCD=0;
  189.  
  190. int n=0, m=0,lenDC=0;
  191.  
  192. n=k;
  193.  
  194. while ( postfix[n] != '_' )
  195. {
  196. if ((isdigit (postfix[n]))== false)
  197. {
  198. n++;
  199. }
  200. cout << "This step (1) n: " << n << endl;
  201. if (isdigit (postfix[n]))
  202. {
  203. m=n;
  204. while ( postfix[m] != '_' ) // assumes characters between a '.' and '_' are all digits (may need to check)
  205. {
  206. lenDC++;
  207. m++; // this loop counts the digits in the input trailing a decimal point
  208. }
  209. cout << "This step (2) m: " << m << endl;
  210. cout << "This step (2) lenDC: " << lenDC << endl;
  211. while ((postfix[n]) != '_')
  212. {
  213. tempDC = tempDC*10 + (postfix[n]) - '0';
  214. n++;
  215. }
  216. cout << "This step (3) n: " << n << endl;
  217. cout << "This step (3) tempDC: " << tempDC << endl;
  218. }
  219. k=n;
  220.  
  221. tempDCD = pow(10,lenDC);
  222.  
  223. dc = tempDC / tempDCD;
  224.  
  225. cout << "This step (4) k: " << k << endl;
  226. cout << "This step (4) tempDCD: " << tempDCD << endl;
  227. cout << "This step (4) tempDC: " << tempDC << endl;
  228. cout << "This step (4) dc: " << dc << endl;
  229. }
  230. return dc,k,tempDC,tempDCD;
  231. }
  232.  
  233.  
Success #stdin #stdout 0.02s 2868KB
stdin
17.3_13.7_+
stdout
Enter a postfix expression
	 (without spaces - using '_' for delimiters):
	For example: 8_5_3_+_*_2_/_5_+

You entered: 17.3_13.7_+
	The string length is:	11
This step (1) n: 3
This step (2) m: 4
This step (2) lenDC: 1
This step (3) n: 4
This step (3) tempDC: 3
This step (4) k: 4
This step (4) tempDCD: 10
This step (4) tempDC: 3
This step (4) dc: 0.3
Post decimal function k: 4
Post decimal function dc: nan
Post decimal function d: nan
Just pushed nan
This step (1) n: 8
This step (2) m: 9
This step (2) lenDC: 1
This step (3) n: 9
This step (3) tempDC: 7
This step (4) k: 9
This step (4) tempDCD: 10
This step (4) tempDC: 7
This step (4) dc: 0.7
Post decimal function k: 9
Post decimal function dc: nan
Post decimal function d: nan
Just pushed nan
Just pushed -4294967296
The result of the postfix expression is: -2147483648