Advertisement
matikap2

sin

Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <stack>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <iterator>
  8. #include <cstdlib>
  9.  
  10. // Simply determine if character is one of the four standard operators.
  11. bool isOperator(char character) {
  12.     if (character == '+' || character == '-' || character == '*' || character == '/'|| character == '^'|| character == 's'|| character == 'c'|| character == 't') {
  13.         return true;
  14.     }
  15.     return false;
  16. }
  17.  
  18.  
  19. // If the character is not an operator or a parenthesis, then it is assumed to be an operand.
  20. bool isOperand(char character) {
  21.     if (!isOperator(character) && character != '(' && character != ')') {
  22.         return true;
  23.     }
  24.     return false;
  25. }
  26.  
  27. // Compare operator precedence of main operators.
  28. int firstOperator(char x) {
  29.     switch(x) {
  30.         case '+':
  31.         case '-':
  32.             return 1;
  33.             break;
  34.         case '*':
  35.         case '/':
  36.             return 2;
  37.             break;
  38.         case '^':
  39.             return 3;
  40.             break;
  41.         case 's':
  42.         case 'c':
  43.         case 't':
  44.             return 4;
  45.             break;
  46.     }
  47.     return -1;
  48. }
  49.  
  50. std::string InfixToPostfix (std::string infix)
  51. {
  52.     // Empty character stack and blank postfix string.
  53.     std::stack<char> opStack;
  54.     std:: string postFixString = "";
  55.  
  56.  
  57.     // Get a pointer to our character array.
  58.     const char *cPtr = infix.c_str();
  59.  
  60.     // Loop through the array (one character at a time) until we reach the end of the string.
  61.     while (*cPtr != '\0') {
  62.         if (isOperand(*cPtr))
  63.         {
  64.             // If operand, simply add it to our postfix string.
  65.             postFixString += *cPtr;
  66.  
  67.         }
  68.         else if (isOperator(*cPtr)) {
  69.             // If it is an operator, pop operators off our stack until it is empty,
  70.             // an open parenthesis or an operator with less than or equal precedence.
  71.             postFixString += " ";
  72.             while (!opStack.empty() && opStack.top() != '(' &&
  73.                    (firstOperator(*cPtr) <= firstOperator(opStack.top())) )
  74.             {
  75.                 postFixString += opStack.top();
  76.                 postFixString += " ";
  77.                 opStack.pop();
  78.             }
  79.             opStack.push(*cPtr);
  80.         }
  81.         else if (*cPtr == '(')
  82.         {
  83.             // Simply push all open parenthesis onto our stack
  84.             opStack.push(*cPtr);
  85.         }
  86.         else if (*cPtr == ')') {
  87.             // When we reach a closing one, start popping off operators until we run into the opening parenthesis.
  88.             while (!opStack.empty()) {
  89.                 if (opStack.top() == '(') { opStack.pop(); break; }
  90.                 postFixString += " ";
  91.                 postFixString += opStack.top();
  92.                 opStack.pop();
  93.             }
  94.         }
  95.         // Advance our pointer to next character in string.
  96.         cPtr++;
  97.     }
  98.  
  99.     // After the input expression has been ran through, if there is any remaining operators left on the stack
  100.     // pop them off and put them onto the postfix string.
  101.     while (!opStack.empty()) {
  102.         postFixString += " ";
  103.         postFixString += opStack.top();
  104.         opStack.pop();
  105.     }
  106.  
  107.  
  108.     // Show the postfix string at the end.
  109.     return postFixString;
  110. }
  111.  
  112. double rpn(const std::string &expr){
  113.     std::istringstream iss(expr);
  114.     std::vector<double> stack;
  115.     std::string token;
  116.     while (iss >> token) {
  117.         double tokenNum;
  118.         if (std::istringstream(token) >> tokenNum) {
  119.             stack.push_back(tokenNum);
  120.         } else {
  121.             double secondOperand = stack.back();
  122.             stack.pop_back();
  123.             double firstOperand = stack.back();
  124.             stack.pop_back();
  125.             if (token == "*")
  126.                 stack.push_back(firstOperand * secondOperand);
  127.             else if (token == "/")
  128.                 stack.push_back(firstOperand / secondOperand);
  129.             else if (token == "-")
  130.                 stack.push_back(firstOperand - secondOperand);
  131.             else if (token == "+")
  132.                 stack.push_back(firstOperand + secondOperand);
  133.             else if (token == "^")
  134.                 stack.push_back(std::pow(firstOperand, secondOperand));
  135.             else if (token == "s")
  136.                 stack.push_back(std::sin(firstOperand)); //testy
  137.             else if (token == "c")
  138.                 stack.push_back(std::cos(firstOperand)); //testy
  139.             else if (token == "t")
  140.                 stack.push_back(std::tan(firstOperand)); //testy
  141.             else { //just in case
  142.                 std::cerr << "Error" << std::endl;
  143.                 std::exit(1);
  144.             }
  145.         }
  146.     }
  147.     return stack.back();
  148. }
  149.  
  150. int main() {
  151.     std::string test;
  152.     std::cout << "Podaj wyrazenie: " << std::endl;
  153.     std::cin >> test;
  154.     std::cout<<"String: " << test << std::endl;
  155.     std::cout<<"RPN: " << InfixToPostfix(test) << std::endl;
  156.     std::cout<<"Wynik: " <<rpn(InfixToPostfix(test)) << std::endl;
  157.     return 0;
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement