Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- int main() {
- stack <char> st1, st2;
- int i, num1 = 0, num2 = 0, num3 = 0, num4 = 0;
- bool write1 = 0, write2 = 0, write3 = 0, write4 = 0;
- char prev = '0', curr, el, beg = '\0', end = '\0';
- i = 0;
- // for (int c = getchar(); c!= EOF; c = getchar())
- while ((curr = getchar()) != EOF) {
- if (curr == '\n') st1.push('n');
- if (curr == '{') st1.push('{');
- if (curr == '}') st1.push('}');
- if (curr == '\'') st1.push('\'');
- if ((curr == '/') && (prev == '/')) st1.push('/');
- if ((curr == '*') && (prev == '(')) st1.push('(');
- if ((curr == ')') && (prev == '*')) st1.push(')');
- prev = curr;
- }
- while (!st1.empty()) {
- st2.push(st1.top());
- //cout << st2.top();
- st1.pop();
- }
- while (!st2.empty()) {
- if (write1 || write2 || write3 || write4) {
- st1.push(st2.top());
- end = st2.top();
- }
- //Комментарии 1 типа
- if (st2.empty() && write1) {
- write1 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st2.push(st1.top());
- st1.pop();
- }
- continue;
- }
- if ((st2.top() == '(') && !write1 && !write2 && !write3 && !write4) {
- write1 = 1;
- beg = '(';
- st2.pop();
- continue;
- }
- if ((beg == '(') && (end == ')') && write1) {
- write1 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st1.pop();
- }
- num1++;
- continue;
- }
- //Комментарии 2 типа
- if (st2.empty() && write2) {
- write2 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st2.push(st1.top());
- st1.pop();
- }
- continue;
- }
- if ((st2.top() == '{') && !write1 && !write2 && !write3 && !write4) {
- write2 = 1;
- beg = '{';
- st2.pop();
- continue;
- }
- if ((beg == '{') && (end == '}') && write2) {
- write2 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st1.pop();
- }
- num2++;
- continue;
- }
- //Комментарии 3 типа
- if (st2.empty() && write3) {
- write3 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st2.push(st1.top());
- st1.pop();
- }
- continue;
- }
- if ((st2.top() == '/') && !write1 && !write2 && !write3 && !write4) {
- write3 = 1;
- beg = '/';
- st2.pop();
- continue;
- }
- if ((beg == '/') && (end == 'n') && write3) {
- write3 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st1.pop();
- }
- num3++;
- continue;
- }
- //Комментарии 4 типа
- if ((st2.empty() && write4) || ((st2.top() == 'n') && write4)) {
- write4 = 0;
- beg = '\0';
- while (!st1.empty()) {
- st2.push(st1.top());
- st1.pop();
- }
- continue;
- }
- if ((st2.top() == '\'') && !write1 && !write2 && !write3 && !write4) {
- write4 = 1;
- beg = '\'';
- st2.pop();
- continue;
- }
- if ((beg == '\'') && (end == '\'') && write4) {
- write4 = 0;
- beg = '\0';
- end = '\0';
- while (!st1.empty()) {
- st1.pop();
- }
- num4++;
- st2.pop();
- continue;
- }
- if (!st2.empty()) st2.pop();
- }
- cout << num1 << ' ' << num2 << ' ' << num3 << ' ' << num4;
- }
Advertisement
Add Comment
Please, Sign In to add comment