Advertisement
Josif_tepe

Untitled

Feb 26th, 2025
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int maxn = 1000;
  5.  
  6. struct stek {
  7.     char niza[maxn];
  8.     int idx;
  9.    
  10.     void init() {
  11.         idx = -1;
  12.     }
  13.    
  14.     void push(char x) {
  15.         if(idx + 1 >= maxn) {
  16.             cout << "NEMA DOVOLNO KAPACITET" << endl;
  17.             return;
  18.         }
  19.         idx++;
  20.         niza[idx] = x;
  21.     }
  22.    
  23.     char pop() {
  24.         if(idx == -1) {
  25.             cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
  26.             return '\0';
  27.         }
  28.         char result = niza[idx];
  29.         idx--;
  30.         return result;
  31.     }
  32.    
  33.     char top() {
  34.         if(idx == -1) {
  35.             cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
  36.             return '\0';
  37.         }
  38.         return niza[idx];
  39.     }
  40.    
  41.     int size() {
  42.         return idx + 1;
  43.     }
  44.    
  45.     int isEmpty() {
  46.         if(idx == -1) {
  47.             return 1;
  48.         }
  49.         else {
  50.             return 0;
  51.         }
  52.     }
  53. };
  54. int main() {
  55.     string s;
  56.     cin >> s;
  57.    
  58.     stek st;
  59.     st.init();
  60.    
  61.     for(int i = 0; i  <s.size(); i++) {
  62.         if(s[i] == '(' || s[i] == '{' || s[i] == '[') {
  63.             st.push(s[i]);
  64.         }
  65.         else {
  66.             if(s[i] == ')') {
  67.                 if(st.isEmpty() == 0 && st.top() == '(') {
  68.                     st.pop();
  69.                 }
  70.             }
  71.             else if(s[i] == ']') {
  72.                 if(st.isEmpty() == 0 && st.top() == '[') {
  73.                     st.pop();
  74.                 }
  75.             }
  76.             else if(s[i] == '}') {
  77.                 if(st.isEmpty() == 0 && st.top() == '{') {
  78.                     st.pop();
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     if(st.isEmpty() == 1) {
  84.         cout << "DA" << endl;
  85.     }
  86.     else {
  87.         cout << "NE" << endl;
  88.     }
  89.     return 0;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement