Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ReversePolandNotation.h"
- ReversePolandNotation::ReversePolandNotation()
- {
- }
- int ReversePolandNotation::getPriority(char character)
- {
- return
- character == '+' || character == '-' ? 2 :
- character == '*' || character == '/' ? 3 :
- character == '(' ? 1 :
- character == ')' ? 0 :
- -1;
- }
- bool ReversePolandNotation::isAlphaNum(string str)
- {
- for (auto element : str)
- {
- if (!isalnum(element))
- return false;
- }
- return true;
- }
- bool ReversePolandNotation::isDigit(string str)
- {
- for (auto element : str)
- {
- if (!isdigit(element))
- return false;
- }
- return true;
- }
- vector<string> * ReversePolandNotation::parseFormula(string * input)
- {
- vector<string> * output = new vector<string>;
- shared_ptr <stack <char>> signs = make_shared<stack <char>>();
- char previousChar = '_';
- //
- while (input->size() > 0)
- {
- if (isalnum(input->at(0)))
- {
- //check if current char is a part of a number
- if (!isalnum(previousChar))
- {
- output->push_back(string(1, input->at(0)));
- }
- else {
- output->back().append(1, input->at(0));
- }
- }
- else
- {//todo: move all "else" to a distinct method
- if (getPriority(input->at(0)) >= 2)
- {
- //in case priority of signs in stack are equal or greater than current sign
- while ((signs->size() != 0) && (getPriority(signs->top()) >= getPriority(input->at(0))))//signs may be empty!!!
- {
- output->push_back(string(1, signs->top()));//inline function?
- signs->pop();
- }
- //add current sign to the stack
- signs->push(input->at(0));
- }
- else if (getPriority(input->at(0)) == 1)
- {
- //add '(' to the stack
- signs->push(input->at(0));
- }
- else if (getPriority(input->at(0)) == 0)
- {
- //find '('
- while (getPriority(signs->top()) != 1)
- {
- output->push_back(string(1, signs->top()));
- signs->pop();
- }
- signs->pop();
- }
- }
- //remove current char from input string after cycle's done
- previousChar = (*input)[0];
- input->erase(0, 1);
- }
- //distinct method
- while (signs->size() != 0)
- {
- output->push_back(string(1, signs->top()));
- signs->pop();
- }
- return output;
- }
- string ReversePolandNotation::evaluateFormula(vector<string> * formula)
- {
- int val1, val2;
- string result;
- unique_ptr <stack <int>> numbers = make_unique<stack <int>>();
- for (auto element : *formula)
- {
- if (isAlphaNum(element))
- {
- numbers->push(atoi(element.c_str()));
- }
- else {
- val2 = numbers->top();
- numbers->pop();
- val1 = numbers->top();
- numbers->pop();
- switch (element.at(0))
- {
- case '*':
- numbers->push(val1 * val2);
- break;
- case '-':
- numbers->push(val1 - val2);
- break;
- case '/':
- numbers->push(val1 / val2);
- break;
- case '+':
- numbers->push(val1 + val2);
- break;
- }
- }
- }
- result = to_string(numbers->top());
- numbers->pop();
- return result;
- }
- string ReversePolandNotation::performCalculation(string formula)
- {
- return evaluateFormula(parseFormula(&formula));
- }
- ReversePolandNotation::~ReversePolandNotation()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement