Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. // unordered_map::find
  2. #include <iostream>
  3. #include <string>
  4. #include <unordered_map>
  5. #include <cmath>
  6. #include <stack>
  7. #include <map>
  8. #include<stdarg.h>
  9.  
  10. std::string delimiter = " ";
  11. std::stack<double> evaluator;
  12.  
  13. void parse(std::string token, double value) {
  14.     if (token == "x") {
  15.         evaluator.push(value);
  16.         return;
  17.     }
  18.     if (token == "*") {
  19.         double a = evaluator.top();
  20.         evaluator.pop();
  21.         double b = evaluator.top();
  22.         evaluator.pop();
  23.         evaluator.push(a * b);
  24.     }
  25.     else if (token == "/") {
  26.         double a = evaluator.top();
  27.         evaluator.pop();
  28.         double b = evaluator.top();
  29.         evaluator.pop();
  30.         evaluator.push(b / a);
  31.     } else if (token == "+") {
  32.         double a = evaluator.top();
  33.         evaluator.pop();
  34.         double b = evaluator.top();
  35.         evaluator.pop();
  36.         evaluator.push(a + b);
  37.     }
  38.     else if (token == "-") {
  39.         double a = evaluator.top();
  40.         evaluator.pop();
  41.         double b = evaluator.top();
  42.         evaluator.pop();
  43.         evaluator.push(b - a);
  44.     }
  45.     else if (token == "cos") {
  46.         double a = evaluator.top();
  47.         evaluator.pop();
  48.         evaluator.push(cos(a));
  49.     }
  50.     else if (token == "sin") {
  51.         double a = evaluator.top();
  52.         evaluator.pop();
  53.         evaluator.push(sin(a));
  54.     }
  55.     else evaluator.push(std::stod(token));
  56. }
  57.  
  58. double split(std::string s, double value) {
  59.     size_t pos = 0;
  60.     std::string token;
  61.     while ((pos = s.find(delimiter)) != std::string::npos) {
  62.         token = s.substr(0, pos);
  63.         parse(token, value);
  64.         s.erase(0, pos + delimiter.length());
  65.     }
  66.     return evaluator.top();
  67.  
  68. }
  69.  
  70. int main() {
  71.  
  72.     std::string s;
  73.     std::getline(std::cin, s);
  74.     s += delimiter;
  75.  
  76.     std::string d;
  77.     std::getline(std::cin, d);
  78.     double xvalue = std::stod(d);
  79.  
  80.     std::cout << split(s, xvalue);
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement