Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <string>
- #include <stack>
- #include <sstream>
- #include <iostream>
- #include <cmath>
- #include <iterator>
- #include <cstdlib>
- // Simply determine if character is one of the four standard operators.
- bool isOperator(char character) {
- if (character == '+' || character == '-' || character == '*' || character == '/'|| character == '^'|| character == 's'|| character == 'c'|| character == 't') {
- return true;
- }
- return false;
- }
- // If the character is not an operator or a parenthesis, then it is assumed to be an operand.
- bool isOperand(char character) {
- if (!isOperator(character) && character != '(' && character != ')') {
- return true;
- }
- return false;
- }
- // Compare operator precedence of main operators.
- int firstOperator(char x) {
- switch(x) {
- case '+':
- case '-':
- return 1;
- break;
- case '*':
- case '/':
- return 2;
- break;
- case '^':
- return 3;
- break;
- case 's':
- case 'c':
- case 't':
- return 4;
- break;
- }
- return -1;
- }
- std::string InfixToPostfix (std::string infix)
- {
- // Empty character stack and blank postfix string.
- std::stack<char> opStack;
- std:: string postFixString = "";
- // Get a pointer to our character array.
- const char *cPtr = infix.c_str();
- // Loop through the array (one character at a time) until we reach the end of the string.
- while (*cPtr != '\0') {
- if (isOperand(*cPtr))
- {
- // If operand, simply add it to our postfix string.
- postFixString += *cPtr;
- }
- else if (isOperator(*cPtr)) {
- // If it is an operator, pop operators off our stack until it is empty,
- // an open parenthesis or an operator with less than or equal precedence.
- postFixString += " ";
- while (!opStack.empty() && opStack.top() != '(' &&
- (firstOperator(*cPtr) <= firstOperator(opStack.top())) )
- {
- postFixString += opStack.top();
- postFixString += " ";
- opStack.pop();
- }
- opStack.push(*cPtr);
- }
- else if (*cPtr == '(')
- {
- // Simply push all open parenthesis onto our stack
- opStack.push(*cPtr);
- }
- else if (*cPtr == ')') {
- // When we reach a closing one, start popping off operators until we run into the opening parenthesis.
- while (!opStack.empty()) {
- if (opStack.top() == '(') { opStack.pop(); break; }
- postFixString += " ";
- postFixString += opStack.top();
- opStack.pop();
- }
- }
- // Advance our pointer to next character in string.
- cPtr++;
- }
- // After the input expression has been ran through, if there is any remaining operators left on the stack
- // pop them off and put them onto the postfix string.
- while (!opStack.empty()) {
- postFixString += " ";
- postFixString += opStack.top();
- opStack.pop();
- }
- // Show the postfix string at the end.
- return postFixString;
- }
- double rpn(const std::string &expr){
- std::istringstream iss(expr);
- std::vector<double> stack;
- std::string token;
- while (iss >> token) {
- double tokenNum;
- if (std::istringstream(token) >> tokenNum) {
- stack.push_back(tokenNum);
- } else {
- double secondOperand = stack.back();
- stack.pop_back();
- double firstOperand = stack.back();
- stack.pop_back();
- if (token == "*")
- stack.push_back(firstOperand * secondOperand);
- else if (token == "/")
- stack.push_back(firstOperand / secondOperand);
- else if (token == "-")
- stack.push_back(firstOperand - secondOperand);
- else if (token == "+")
- stack.push_back(firstOperand + secondOperand);
- else if (token == "^")
- stack.push_back(std::pow(firstOperand, secondOperand));
- else if (token == "s")
- stack.push_back(std::sin(firstOperand)); //testy
- else if (token == "c")
- stack.push_back(std::cos(firstOperand)); //testy
- else if (token == "t")
- stack.push_back(std::tan(firstOperand)); //testy
- else { //just in case
- std::cerr << "Error" << std::endl;
- std::exit(1);
- }
- }
- }
- return stack.back();
- }
- int main() {
- std::string test;
- std::cout << "Podaj wyrazenie: " << std::endl;
- std::cin >> test;
- std::cout<<"String: " << test << std::endl;
- std::cout<<"RPN: " << InfixToPostfix(test) << std::endl;
- std::cout<<"Wynik: " <<rpn(InfixToPostfix(test)) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement