Advertisement
Guest User

pqowieurytlaksjdhfgmznxbcv

a guest
Jan 18th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. enum token{
  5. plus, minus, mult, divi, squrt, expon, ln, empty, error, number
  6. };
  7.  
  8. struct stackComponent{
  9. bool flag;
  10. double number;
  11. token operation;
  12. };
  13. stackComponent emptycomponent = {true, 0, number};
  14. stackComponent errorcomponent = {true, 0, error};
  15. bool compare(stackComponent a, stackComponent b){
  16. if(a.flag == b.flag && a.number == b.number && a.operation == b.operation) return true;
  17. else return false;
  18. }
  19.  
  20. class stack{
  21. stackComponent stck[100];
  22. int tracker;
  23. public:
  24. stack();
  25. stackComponent pop();
  26. void push(stackComponent object);
  27. };
  28. stack::stack(){
  29. for(int i = 0; i < 100; i++) stck[i] = emptycomponent;
  30. tracker = 0;
  31. }
  32. stackComponent stack::pop(){
  33. if(!tracker) return errorcomponent;
  34. else{
  35. tracker--;
  36. stackComponent value = stck[tracker];
  37. stck[tracker] = emptycomponent;
  38. return value;
  39. }
  40. }
  41. void stack::push(stackComponent object){
  42. stck[tracker] = object;
  43. tracker++;
  44. }
  45.  
  46. class calculator{
  47. stack equation;
  48. stack solution;
  49. void add();
  50. void subtract();
  51. void multiply();
  52. void divide();
  53. void squareroot();
  54. void exponent();
  55. void logarithm();
  56. public:
  57. double calculate();
  58. void loadeq();
  59. };
  60. void calculator::add(){
  61. stackComponent opA = solution.pop();
  62. stackComponent opB = solution.pop();
  63. if(!compare(opA, emptycomponent) || !compare(opA, errorcomponent)) return;
  64. if(!compare(opB, emptycomponent) || !compare(opB, errorcomponent)) return;
  65. solution.push({false, opA.number + opB.number, number});
  66. return;
  67. }
  68.  
  69. int main(){
  70. stack s;
  71. s.push({false, 0, empty});
  72. s.push({false, 1, empty});
  73. s.push({false, 2, empty});
  74. s.push({false, 3, empty});
  75. printf("%f\n", s.pop().number);
  76. printf("%f\n", s.pop().number);
  77. printf("%f\n", s.pop().number);
  78. printf("%f\n", s.pop().number);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement