JuliaMelkozerova

HW1E

Mar 11th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     stack <char> st1, st2;
  8.     int i, num1 = 0, num2 = 0, num3 = 0, num4 = 0;
  9.     bool write1 = 0, write2 = 0, write3 = 0, write4 = 0;
  10.     char prev = '0', curr, el, beg = '\0', end = '\0';
  11.  
  12.     i = 0;
  13.     // for (int c = getchar(); c!= EOF; c = getchar())
  14.     while ((curr = getchar()) != EOF) {
  15.         if (curr == '\n') st1.push('n');
  16.         if (curr == '{') st1.push('{');
  17.         if (curr == '}') st1.push('}');
  18.         if (curr == '\'') st1.push('\'');
  19.         if ((curr == '/') && (prev == '/')) st1.push('/');
  20.         if ((curr == '*') && (prev == '(')) st1.push('(');
  21.         if ((curr == ')') && (prev == '*')) st1.push(')');
  22.  
  23.         prev = curr;
  24.     }
  25.  
  26.     while (!st1.empty()) {
  27.         st2.push(st1.top());
  28.         //cout << st2.top();
  29.         st1.pop();
  30.     }
  31.  
  32.     while (!st2.empty()) {
  33.         if (write1 || write2 || write3 || write4) {
  34.             st1.push(st2.top());
  35.             end = st2.top();
  36.         }
  37.  
  38.         //Комментарии 1 типа
  39.         if (st2.empty() && write1) {
  40.             write1 = 0;
  41.             beg = '\0';
  42.             while (!st1.empty()) {
  43.                 st2.push(st1.top());
  44.                 st1.pop();
  45.             }
  46.             continue;
  47.         }
  48.         if ((st2.top() == '(') && !write1 && !write2 && !write3 && !write4) {
  49.             write1 = 1;
  50.             beg = '(';
  51.             st2.pop();
  52.             continue;
  53.         }
  54.         if ((beg == '(') && (end == ')') && write1) {
  55.             write1 = 0;
  56.             beg = '\0';
  57.             while (!st1.empty()) {
  58.                 st1.pop();
  59.             }
  60.             num1++;
  61.             continue;
  62.         }
  63.  
  64.         //Комментарии 2 типа
  65.         if (st2.empty() && write2) {
  66.             write2 = 0;
  67.             beg = '\0';
  68.             while (!st1.empty()) {
  69.                 st2.push(st1.top());
  70.                 st1.pop();
  71.             }
  72.             continue;
  73.         }
  74.         if ((st2.top() == '{') && !write1 && !write2 && !write3 && !write4) {
  75.             write2 = 1;
  76.             beg = '{';
  77.             st2.pop();
  78.             continue;
  79.         }
  80.         if ((beg == '{') && (end == '}') && write2) {
  81.             write2 = 0;
  82.             beg = '\0';
  83.             while (!st1.empty()) {
  84.                 st1.pop();
  85.             }
  86.             num2++;
  87.             continue;
  88.         }
  89.  
  90.         //Комментарии 3 типа
  91.         if (st2.empty() && write3) {
  92.             write3 = 0;
  93.             beg = '\0';
  94.             while (!st1.empty()) {
  95.                 st2.push(st1.top());
  96.                 st1.pop();
  97.             }
  98.             continue;
  99.         }
  100.         if ((st2.top() == '/') && !write1 && !write2 && !write3 && !write4) {
  101.             write3 = 1;
  102.             beg = '/';
  103.             st2.pop();
  104.             continue;
  105.         }
  106.         if ((beg == '/') && (end == 'n') && write3) {
  107.             write3 = 0;
  108.             beg = '\0';
  109.             while (!st1.empty()) {
  110.                 st1.pop();
  111.             }
  112.             num3++;
  113.             continue;
  114.         }
  115.  
  116.         //Комментарии 4 типа
  117.         if ((st2.empty() && write4) || ((st2.top() == 'n') && write4)) {
  118.             write4 = 0;
  119.             beg = '\0';
  120.             while (!st1.empty()) {
  121.                 st2.push(st1.top());
  122.                 st1.pop();
  123.             }
  124.             continue;
  125.         }
  126.         if ((st2.top() == '\'') && !write1 && !write2 && !write3 && !write4) {
  127.             write4 = 1;
  128.             beg = '\'';
  129.             st2.pop();
  130.             continue;
  131.         }
  132.         if ((beg == '\'') && (end == '\'') && write4) {
  133.             write4 = 0;
  134.             beg = '\0';
  135.             end = '\0';
  136.             while (!st1.empty()) {
  137.                 st1.pop();
  138.             }
  139.             num4++;
  140.             st2.pop();
  141.             continue;
  142.         }
  143.  
  144.         if (!st2.empty()) st2.pop();
  145.  
  146.     }
  147.  
  148.     cout << num1 << ' ' << num2 << ' ' << num3 << ' ' << num4;
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment