fork download
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. void evalPf(char*);
  5. double cal(double, char, double);
  6. using namespace std;
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. char input[50];
  11. cin.unsetf(ios::skipws);
  12. cin>>input;
  13. evalPf(input);
  14. system("PAUSE");
  15. return EXIT_SUCCESS;
  16. }
  17. void evalPf(char* postfix) {
  18. double stack[80];
  19. char temp[2];
  20. char token;
  21. int top = 0, i = 0;
  22. temp[1] = '\0';
  23. while(1) {
  24. token = postfix[i];
  25. switch(token) {
  26. case '\0':
  27. printf("ans = %f\n", stack[top]);
  28. return;
  29. case '+': case '-': case '*': case '/':
  30. stack[top-1] =
  31. cal(stack[top-1], token, stack[top]);
  32. top--;
  33. break;
  34. default:
  35. if(top < sizeof(stack) / sizeof(float)) {
  36. temp[0] = postfix[i];
  37. top++;
  38. stack[top] = atof(temp);
  39. }
  40. break;
  41. }
  42. i++;
  43. }
  44. }
  45. double cal(double p1, char op, double p2) {
  46. switch(op) {
  47. case '+':
  48. return p1 + p2;
  49. case '-':
  50. return p1 - p2;
  51. case '*':
  52. return p1 * p2;
  53. case '/':
  54. return p1 / p2;
  55. }
  56. }
Success #stdin #stdout 0.02s 5312KB
stdin
Standard input is empty
stdout
ans = 0.000000