Advertisement
Kwwiker

main.cpp

Apr 14th, 2021
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. #include "Stack.h"
  5. #include "StackOnList.h"
  6. using namespace std;
  7.  
  8. int preCalc(string line) {
  9.     Stack stack(line.length()); // StackOnList stack; при использовании стека на однонаправленном списке
  10.     for (int i = line.length() - 1; i >= 0; i--) {
  11.         if (line[i] >= '0' and line[i] <= '9') {
  12.             stack.push(line[i] - '0');
  13.         }
  14.         else {
  15.             int x, y;
  16.             switch (line[i])
  17.             {
  18.             case '+':
  19.                 x = stack.peek();
  20.                 stack.pop();
  21.                 y = stack.peek();
  22.                 stack.pop();
  23.                 stack.push(x + y);
  24.                 break;
  25.             case '-':
  26.                 x = stack.peek();
  27.                 stack.pop();
  28.                 y = stack.peek();
  29.                 stack.pop();
  30.                 stack.push(x - y);
  31.                 break;
  32.             case '*':
  33.                 x = stack.peek();
  34.                 stack.pop();
  35.                 y = stack.peek();
  36.                 stack.pop();
  37.                 stack.push(x * y);
  38.                 break;
  39.             case '/':
  40.                 x = stack.peek();
  41.                 stack.pop();
  42.                 y = stack.peek();
  43.                 stack.pop();
  44.                 stack.push(x / y);
  45.                 break;
  46.             default:
  47.                 break;
  48.             }
  49.         }
  50.     }
  51.     return stack.peek();
  52. }
  53.  
  54. int postCalc(string line) {
  55.     Stack stack(line.length()); // StackOnList stack; при использовании стека на однонаправленном списке
  56.     for (int i = 0; i < line.length(); i++) {
  57.         if (line[i] >= '0' and line[i] <= '9') {
  58.             stack.push(line[i] - '0');
  59.         }
  60.         else {
  61.             int x, y;
  62.             switch (line[i])
  63.             {
  64.             case '+':
  65.                 x = stack.peek();
  66.                 stack.pop();
  67.                 y = stack.peek();
  68.                 stack.pop();
  69.                 stack.push(x + y);
  70.                 break;
  71.             case '-':
  72.                 x = stack.peek();
  73.                 stack.pop();
  74.                 y = stack.peek();
  75.                 stack.pop();
  76.                 stack.push(y - x);
  77.                 break;
  78.             case '*':
  79.                 x = stack.peek();
  80.                 stack.pop();
  81.                 y = stack.peek();
  82.                 stack.pop();
  83.                 stack.push(x * y);
  84.                 break;
  85.             case '/':
  86.                 x = stack.peek();
  87.                 stack.pop();
  88.                 y = stack.peek();
  89.                 stack.pop();
  90.                 stack.push(y / x);
  91.                 break;
  92.             default:
  93.                 break;
  94.             }
  95.         }
  96.     }
  97.     return stack.peek();
  98. }
  99.  
  100. void sum() {
  101.     char symbol;
  102.     StackOnList first, second, result;
  103.     int pr = 0;
  104.     cin >> symbol;
  105.     while (symbol != 'q') {
  106.         first.push(symbol - '0');
  107.         cin >> symbol;
  108.     }
  109.     cin >> symbol;
  110.     while (symbol != 'q') {
  111.         second.push(symbol - '0');
  112.         cin >> symbol;
  113.     }
  114.     while (!first.isEmpty() or !second.isEmpty()) {
  115.         int temp = (!first.isEmpty() ? first.peek() : 0) + (!second.isEmpty() ? second.peek() : 0)  + pr;
  116.         if (!first.isEmpty()) first.pop();
  117.         if (!second.isEmpty()) second.pop();
  118.         if (temp >= 10) {
  119.             result.push(temp - 10);
  120.             pr = 1;
  121.         }
  122.         else {
  123.             result.push(temp);
  124.             pr = 0;
  125.         }
  126.     }
  127.     if (pr == 1) {
  128.         result.push(1);
  129.     }
  130.     while (!result.isEmpty()) {
  131.         cout << result.peek();
  132.         result.pop();
  133.     }
  134. }
  135.  
  136. int main()
  137. {
  138.     SetConsoleCP(1251);
  139.     SetConsoleOutputCP(1251);
  140.     sum();
  141.     /*string line;
  142.     cout << "Введите выражение в префиксной форме:" << endl;
  143.     cin >> line;
  144.     cout << "Результат: " << preCalc(line) << endl;
  145.     cout << "Введите выражение в постфиксной форме:" << endl;
  146.     cin >> line;
  147.     cout << "Результат: " << postCalc(line) << endl;*/
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement