Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<iomanip>
  5. #include<stdio.h>
  6. #include<cmath>
  7. using namespace std;
  8. #include "list.cpp"
  9. #include "stack.h"
  10.  
  11. double sum(double rhs, double lhs);
  12. double subtract(double rhs, double lhs);
  13. double mult(double rhs, double lhs);
  14. double quotient(double rhs, double lhs);
  15. double power(double rhs, double lhs);
  16.  
  17. int main(){
  18. Stack<double> astack;
  19. double d, rhs, lhs, res;
  20. char ch;
  21.  
  22. while(cin.peek() != EOF) {
  23. cin >> ch;
  24. if(isspace(ch))
  25. cin.ignore();
  26.  
  27. if(isdigit(ch) || ch == '.') {
  28. d = ch;
  29. astack.push(d);
  30. }
  31.  
  32. switch(ch) {
  33. case '+':
  34. rhs = astack.pop();
  35. lhs = astack.pop();
  36. astack.push(sum(rhs, lhs));
  37. break;
  38. case '-':
  39. rhs = astack.pop();
  40. lhs = astack.pop();
  41. astack.push(subtract(rhs, lhs));
  42. break;
  43. case '*':
  44. rhs = astack.pop();
  45. lhs = astack.pop();
  46. astack.push(mult(rhs, lhs));
  47. break;
  48. case '/':
  49. rhs = astack.pop();
  50. lhs = astack.pop();
  51. astack.push(quotient(rhs, lhs));
  52. break;
  53. case '^':
  54. rhs = astack.pop();
  55. lhs = astack.pop();
  56. astack.push(power(rhs, lhs));
  57. break;
  58. default:
  59. cerr << "Error. Invalid expression" << endl;
  60. }
  61. }
  62.  
  63. res = astack.pop();
  64. cout << fixed << showpoint << setprecision(6) << res << endl;
  65.  
  66. return 0;
  67. }//main
  68.  
  69. double sum(double rhs, double lhs) {
  70. return lhs + rhs;
  71. }
  72. double subtract(double rhs, double lhs) {
  73. return lhs - rhs;
  74. }
  75. double mult(double rhs, double lhs) {
  76. return lhs * rhs;
  77. }
  78. double quotient(double rhs, double lhs) {
  79. return lhs / rhs;
  80. }
  81. double power(double rhs, double lhs) {
  82. return pow(lhs, rhs);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement