Advertisement
Nikidrr

Лаба 12

May 27th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <map>
  5.  
  6. template<class T>
  7. struct Node {
  8.     T value;
  9.     Node *next;
  10.  
  11.     Node(T value, Node *next = nullptr)
  12.             : value(value), next(next) {}
  13. };
  14.  
  15. template<class T>
  16. struct Stack {
  17.     Node<T> *top;
  18.  
  19.     Stack() : top(nullptr) {}
  20.  
  21.     void push(T value) {
  22.         this->top = new Node<T>(value, this->top);
  23.     }
  24.  
  25.     T *pop() {
  26.         T *value = this->peek();
  27.         if (this->top != nullptr)
  28.             this->top = this->top->next;
  29.         return value;
  30.     }
  31.  
  32.     T *peek() {
  33.         return this->top == nullptr
  34.                ? nullptr
  35.                : &this->top->value;
  36.     }
  37.  
  38.     size_t size() {
  39.         size_t size = 0;
  40.         for (Node<T> *current = this->top; current != nullptr; size++, current = current->next);
  41.         return size;
  42.     }
  43.  
  44.     bool empty() {
  45.         return this->top == nullptr;
  46.     }
  47.  
  48.     std::string toString() {
  49.         std::ostringstream sout;
  50.         sout << '[';
  51.         Node<T> *current = this->top;
  52.         for (; current != nullptr && current->next != nullptr; current = current->next)
  53.             sout << current->value << ", ";
  54.         if (current != nullptr)
  55.             sout << current->value << ']';
  56.         return sout.str();
  57.     }
  58. };
  59.  
  60. using namespace std;
  61.  
  62. int main() {
  63.     Stack<string> operations;
  64.     map<string, int> precedence {
  65.         pair("(", 1),
  66.         pair("+", 2), pair("-", 2),
  67.         pair("*", 3), pair("/", 3)
  68.     };
  69.  
  70.     string expression;
  71.  
  72.     cout << "Enter an expression: " << endl;
  73.  
  74.     getline(cin, expression);
  75.  
  76.     istringstream input(expression);
  77.     ostringstream output;
  78.  
  79.     while (!input.eof()) {
  80.         string token;
  81.         input >> token;
  82.  
  83.         if (token == "+" || token == "-" || token == "*" || token == "/") {
  84.             for (string *op = operations.peek(); op != nullptr; operations.pop(), op = operations.peek()) {
  85.                 if (precedence[*op] < precedence[token]) {
  86.                     break;
  87.                 }
  88.                 output << *op << ' ';
  89.             }
  90.             operations.push(token);
  91.         } else if (token == "(") {
  92.             operations.push(token);
  93.         } else if (token == ")") {
  94.             for (string *op = operations.pop(); op != nullptr && *op != "("; op = operations.pop())
  95.                 output << *op << ' ';
  96.         } else {
  97.             output << token << ' ';
  98.         }
  99.     }
  100.  
  101.     for (string *op = operations.pop(); op != nullptr; op = operations.pop()) {
  102.         output << *op << ' ';
  103.     }
  104.  
  105.     cout << output.str() << endl;
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement