Advertisement
Mikhail-Podbolotov

Untitled

Apr 17th, 2025
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. //chatgpt
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. template <typename T>
  7. class Stack {
  8. private:
  9.     struct Node {
  10.         T data;
  11.         Node* next;
  12.         Node(const T& value, Node* nextNode = nullptr)
  13.             : data(value), next(nextNode) {}
  14.     };
  15.  
  16.     Node* topNode;
  17.  
  18. public:
  19.     Stack() : topNode(nullptr) {}
  20.  
  21.     ~Stack() {
  22.         while (!isEmpty()) {
  23.             pop();
  24.         }
  25.     }
  26.  
  27.     void push(const T& value) {
  28.         topNode = new Node(value, topNode);
  29.     }
  30.  
  31.     void pop() {
  32.         if (isEmpty()) {
  33.             std::cerr << "Error: Stack is empty, cannot pop.\n";
  34.             return;
  35.         }
  36.         Node* temp = topNode;
  37.         topNode = topNode->next;
  38.         delete temp;
  39.     }
  40.  
  41.     T& top() {
  42.         if (isEmpty()) {
  43.             std::cerr << "Error: Stack is empty.\n";
  44.             std::exit(1);
  45.         }
  46.         return topNode->data;
  47.     }
  48.  
  49.     bool isEmpty() const {
  50.         return topNode == nullptr;
  51.     }
  52. };
  53.  
  54. // Простейшая проверка: является ли строка числом (включая отрицательные)
  55. bool isNumber(const std::string& s) {
  56.     if (s.empty()) return false;
  57.     size_t start = 0;
  58.     if (s[0] == '-') {
  59.         if (s.size() == 1) return false;
  60.         start = 1;
  61.     }
  62.     for (size_t i = start; i < s.size(); ++i) {
  63.         if (s[i] < '0' || s[i] > '9') return false;
  64.     }
  65.     return true;
  66. }
  67.  
  68. // Простейший stoi, без std::stoi
  69. int toInt(const std::string& s) {
  70.     int result = 0;
  71.     bool negative = false;
  72.     size_t i = 0;
  73.  
  74.     if (s[0] == '-') {
  75.         negative = true;
  76.         i = 1;
  77.     }
  78.  
  79.     for (; i < s.size(); ++i) {
  80.         result = result * 10 + (s[i] - '0');
  81.     }
  82.  
  83.     return negative ? -result : result;
  84. }
  85.  
  86. void evaluateRPN(const std::string& input) {
  87.     Stack<int> stack;
  88.     std::string token = "";
  89.  
  90.     for (size_t i = 0; i <= input.length(); ++i) {
  91.         char ch = (i < input.length()) ? input[i] : ' ';
  92.  
  93.         if (ch != ' ') {
  94.             token += ch;
  95.         } else if (!token.empty()) {
  96.             if (isNumber(token)) {
  97.                 stack.push(toInt(token));
  98.             } else if (token == "+" || token == "-" || token == "*" || token == "/") {
  99.                 if (stack.isEmpty()) { std::cerr << "Not enough operands\n"; return; }
  100.                 int b = stack.top(); stack.pop();
  101.  
  102.                 if (stack.isEmpty()) { std::cerr << "Not enough operands\n"; return; }
  103.                 int a = stack.top(); stack.pop();
  104.  
  105.                 if (token == "+") stack.push(a + b);
  106.                 else if (token == "-") stack.push(a - b);
  107.                 else if (token == "*") stack.push(a * b);
  108.                 else if (token == "/") stack.push(a / b); // Без проверки деления на 0 для простоты
  109.             } else {
  110.                 std::cerr << "Unknown token: " << token << "\n";
  111.                 return;
  112.             }
  113.             token = "";
  114.         }
  115.     }
  116.  
  117.     if (!stack.isEmpty()) {
  118.         std::cout << "Result: " << stack.top() << "\n";
  119.     } else {
  120.         std::cerr << "No result on stack\n";
  121.     }
  122. }
  123.  
  124. int main() {
  125.     std::string input;
  126.     std::cout << "Enter RPN expression (e.g. '3 4 + 2 *'): ";
  127.     std::getline(std::cin, input);
  128.  
  129.     evaluateRPN(input);
  130.  
  131.     return 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement