Advertisement
myname0

практика_стек_6

Jun 29th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <stack> //1 вариант
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     ifstream in("input.txt");
  10.     ofstream out("output.txt");
  11.  
  12.     stack <char> b;
  13.     string brackets = "()[]{}";
  14.     string tmp;
  15.     getline(in,tmp);
  16.     in.close();
  17.     char a;  
  18.     int k = 0;          
  19.     for(int i = 0; i < (int)tmp.length(); i++)
  20.     {
  21.         a = tmp[i];
  22.         if (brackets.find(a) % 2 == 0)
  23.             b.push(a);
  24.         else if ( b.empty())
  25.         {
  26.             out << "Not" << endl;
  27.             k++;  
  28.             break;
  29.         }
  30.         else if ( b.top() != brackets [brackets.find(a) - 1])
  31.         {
  32.             out << "Not" << endl;
  33.             k++;
  34.             break;
  35.         }
  36.         else b.pop();
  37.     }
  38.    if(k == 0)
  39.     if(b.empty())
  40.         out << "Yes" << endl;
  41.     out.close();
  42.     return 0;
  43. }
  44.  
  45. // 2 вариант
  46. #include <stack>
  47. #include <string>
  48. #include <fstream>
  49.  
  50. using namespace std;
  51.  
  52. int main()
  53. {
  54.     ifstream in("input.txt");
  55.     ofstream out("output.txt");
  56.  
  57.     stack <char> b;
  58.     string brackets = "()[]{}";
  59.     char a;
  60.     int k =0;
  61.     while(in.peek() != EOF)
  62.     {
  63.         in >> a;
  64.         if (brackets.find(a) % 2 == 0)
  65.             b.push(a);
  66.         else if ( b.empty())
  67.     {
  68.             out << "Not" << endl;
  69.         k++;   
  70.         break;
  71.     }
  72.         else if ( b.top() != brackets [brackets.find(a) - 1])
  73.     {
  74.                 out << "Not" << endl;
  75.         k++;
  76.         break;
  77.     }
  78.         else b.pop();  
  79.     }
  80.     if(k == 0)
  81.     if(b.empty())
  82.         out << "Yes" << endl;
  83.  
  84.    
  85.  
  86.     in.close();
  87.     out.close();
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement