Advertisement
Guest User

RPN.cpp

a guest
Nov 19th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include "ReversePolandNotation.h"
  2.  
  3.  
  4.  
  5. ReversePolandNotation::ReversePolandNotation()
  6. {
  7. }
  8.  
  9. int ReversePolandNotation::getPriority(char character)
  10. {
  11.     return
  12.         character == '+' || character == '-' ? 2 :
  13.         character == '*' || character == '/' ? 3 :
  14.         character == '(' ? 1 :
  15.         character == ')' ? 0 :
  16.         -1;
  17. }
  18.  
  19. bool ReversePolandNotation::isAlphaNum(string  str)
  20. {
  21.     for (auto element : str)
  22.     {
  23.         if (!isalnum(element))
  24.             return false;
  25.     }
  26.     return true;
  27. }
  28.  
  29. bool ReversePolandNotation::isDigit(string  str)
  30. {
  31.     for (auto element : str)
  32.     {
  33.         if (!isdigit(element))
  34.             return false;
  35.     }
  36.     return true;
  37. }
  38.  
  39. vector<string> * ReversePolandNotation::parseFormula(string * input)
  40. {
  41.     vector<string> * output = new vector<string>;
  42.     shared_ptr <stack <char>> signs = make_shared<stack <char>>();
  43.     char previousChar = '_';
  44.     //
  45.     while (input->size() > 0)
  46.     {
  47.         if (isalnum(input->at(0)))
  48.         {
  49.             //check if current char is a part of a number
  50.             if (!isalnum(previousChar))
  51.             {
  52.                 output->push_back(string(1, input->at(0)));
  53.             }
  54.             else {
  55.                 output->back().append(1, input->at(0));
  56.             }
  57.         }
  58.         else
  59.         {//todo: move all "else" to a distinct method
  60.             if (getPriority(input->at(0)) >= 2)
  61.             {
  62.                 //in case priority of signs in stack are equal or greater than current sign
  63.                 while ((signs->size() != 0) && (getPriority(signs->top()) >= getPriority(input->at(0))))//signs may be empty!!!
  64.                 {
  65.                     output->push_back(string(1, signs->top()));//inline function?
  66.                     signs->pop();
  67.                 }
  68.                 //add current sign to the stack
  69.                 signs->push(input->at(0));
  70.             }
  71.             else if (getPriority(input->at(0)) == 1)
  72.             {
  73.                 //add '(' to the stack
  74.                 signs->push(input->at(0));
  75.             }
  76.             else if (getPriority(input->at(0)) == 0)
  77.             {
  78.                 //find '('
  79.                 while (getPriority(signs->top()) != 1)
  80.                 {
  81.                     output->push_back(string(1, signs->top()));
  82.                     signs->pop();
  83.                 }
  84.                 signs->pop();
  85.             }
  86.  
  87.         }
  88.         //remove current char from input string after cycle's done
  89.         previousChar = (*input)[0];
  90.         input->erase(0, 1);
  91.     }
  92.     //distinct method
  93.     while (signs->size() != 0)
  94.     {
  95.         output->push_back(string(1, signs->top()));
  96.         signs->pop();
  97.     }
  98.     return output;
  99. }
  100.  
  101. string ReversePolandNotation::evaluateFormula(vector<string> * formula)
  102. {
  103.     int val1, val2;
  104.     string result;
  105.     unique_ptr <stack <int>> numbers = make_unique<stack <int>>();
  106.     for (auto element : *formula)
  107.     {
  108.         if (isAlphaNum(element))
  109.         {
  110.             numbers->push(atoi(element.c_str()));
  111.         }
  112.         else {
  113.             val2 = numbers->top();
  114.             numbers->pop();
  115.             val1 = numbers->top();
  116.             numbers->pop();
  117.             switch (element.at(0))
  118.             {
  119.             case '*':
  120.                 numbers->push(val1 * val2);
  121.                 break;
  122.             case '-':
  123.                 numbers->push(val1 - val2);
  124.                 break;
  125.             case '/':
  126.                 numbers->push(val1 / val2);
  127.                 break;
  128.             case '+':
  129.                 numbers->push(val1 + val2);
  130.                 break;
  131.             }
  132.         }
  133.     }
  134.     result = to_string(numbers->top());
  135.     numbers->pop();
  136.     return result;
  137. }
  138.  
  139. string ReversePolandNotation::performCalculation(string formula)
  140. {
  141.     return evaluateFormula(parseFormula(&formula));
  142. }
  143.  
  144.  
  145. ReversePolandNotation::~ReversePolandNotation()
  146. {
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement