Advertisement
Guest User

Untitled

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