Advertisement
akatsuki_no_yami

Untitled

Dec 7th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. !past
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <stack>
  6.  
  7. void calc(std::stack <int> &arr_nums,
  8.           std::stack <char> &arr_ops){
  9.     int b = arr_nums.top();    
  10.     arr_nums.pop();
  11.     int a = arr_nums.top();    
  12.     arr_nums.pop();
  13.     char nya = arr_ops.top();
  14.     switch(nya){
  15.         case '+':   arr_nums.push(a+b); break;
  16.         case '-':   arr_nums.push(a-b); break;
  17.         case '*':   arr_nums.push(a*b); break;
  18.         case '/':   arr_nums.push(a/b); break;
  19.     }
  20.     arr_ops.pop();
  21.     std::cout << a << nya << b << '=' << arr_nums.top() << '\n';
  22. }
  23.  
  24. void high(std::stack <int> &arr_nums,
  25.           std::stack <char> &arr_ops,
  26.           bool &counter, char symb){
  27.     counter = false;
  28.     if (arr_ops.empty() == false){
  29.         char nya = arr_ops.top();
  30.         char j = '*';
  31.         char g = '/';
  32.         if(nya == j ||  nya == g){
  33.             calc(arr_nums, arr_ops);
  34.             arr_ops.push(symb);
  35.         }else{
  36.         arr_ops.push(symb);
  37.         }  
  38.     }else{
  39.         arr_ops.push(symb);
  40.     }
  41.     std::cout << arr_ops.top() << '\n';
  42. }
  43.  
  44. void low(std::stack <int> &arr_nums,
  45.           std::stack <char> &arr_ops,
  46.           bool &counter, char symb){
  47.     counter = false;
  48.     if (arr_ops.empty() == false){
  49.         char z = arr_ops.top();
  50.         if (z == '(' || z == ')'){
  51.             arr_ops.push(symb);
  52.         }else{
  53.             calc(arr_nums, arr_ops);
  54.             arr_ops.push(symb);
  55.         }
  56.     }else{
  57.         arr_ops.push(symb);
  58.     }
  59.     std::cout << arr_ops.top() << '\n';
  60. }
  61. void nums(std::stack <int> &arr_nums,
  62.           std::stack <char> &arr_ops,
  63.           bool &counter, char symb){
  64.     int v = (int)symb - 48;
  65.     std::cout<< "int " << v << ' ';
  66.     if (counter == true){
  67.         int x = arr_nums.top();
  68.         arr_nums.pop();
  69.         arr_nums.push(x * 10 + v);
  70.         std::cout << "Изменено " << arr_nums.top() <<'\n';
  71.     }else{
  72.         arr_nums.push(v);
  73.         std::cout << "Добавлено " << arr_nums.top() <<'\n';
  74.     }
  75.     std::cout << arr_nums.top() << '\n';
  76. }
  77. int main(){
  78.     std::cout << "Enter expression: ";
  79.     std::string msg;
  80.     std::cin >> msg;
  81.     int n = msg.length();
  82.     bool counter;
  83.     std::stack<char> arr_ops;
  84.     std::stack<int> arr_nums;
  85.     std::string a = {'+','-','*','/','(', ')'};
  86.  
  87.     for (int i=0; i < n; i++){
  88.         switch(msg.at(i)){
  89.             case '*':   high(arr_nums, arr_ops, counter, msg.at(i)); break;
  90.             case '/':   high(arr_nums, arr_ops, counter, msg.at(i)); break;
  91.             case '+':    low(arr_nums, arr_ops, counter, msg.at(i)); break;
  92.             case '-':    low(arr_nums, arr_ops, counter, msg.at(i)); break;
  93.             case '(':  
  94.                 counter = false;
  95.                 arr_ops.push(msg.at(i));
  96.                 break;
  97.             case ')':
  98.                 counter = false;
  99.                 while(arr_ops.top() != '('){
  100.                     calc(arr_nums, arr_ops);}
  101.                 arr_ops.pop();
  102.                 break;
  103.             default:    
  104.                 nums(arr_nums, arr_ops, counter, msg.at(i));
  105.                 counter = true;
  106.            
  107.         std::cout << i<< "/" << n << " " << msg.at(i) << "\n";
  108.         }
  109.     }
  110.     while (arr_nums.size() > 1){calc(arr_nums, arr_ops);}
  111.     std::cout << "Верхушка стека: "<< arr_nums.top() << "\n";
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement