Facebook
From Queen Wolf, 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 273
  1. #include<iostream>
  2. #include<stack>
  3. #include<string>
  4. using namespace std;
  5. bool ppkt_b(string);
  6. int main()
  7. {
  8.         string napis;
  9.         getline(cin, napis);
  10.         cout<<ppkt_b(napis)<<endl;
  11.     return 0;
  12. }
  13. bool ppkt_b(string nap)
  14. {
  15.         stack <char> stos;
  16.         char tabo[] = { '(','[','{','<' };
  17.         char tabz[] = { ')',']','}','>' };
  18.         //////////////////Wrzucenie na stos otwierajacych
  19.         for (int i = 0;i < nap.length();i++)
  20.         {              
  21.                 for (int j = 0;j < 4;j++)
  22.                 {
  23.                         if(nap[i]==tabo[j]) stos.push(nap[i]);
  24.                         if (nap[i] == tabz[j])
  25.                         {
  26.                                 for (int k = 0;k < 4;k++)
  27.                                 {
  28.                                         if (stos.top() == tabo[k])
  29.                                         {
  30.                                                 if (j == k)
  31.                                                 {
  32.                                                         stos.pop();
  33.                                                         break;
  34.                                                 }
  35.  
  36.                                         }
  37.                                 }
  38.                         }
  39.                 }      
  40.         if ((i < nap.length() - 1) && (stos.empty() == true)) return false;
  41.         }
  42.         if (stos.empty()) return true;
  43.         else return false;
  44. }