JuliaMelkozerova

Комментарии PASCAL

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