PhotoShaman

Laba14

Mar 1st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <clocale>
  4. #include <windows.h>
  5. #include <fstream>
  6. #include <stack>
  7.  
  8. using namespace std;
  9.  
  10. void main()
  11. {
  12.     SetConsoleCP(1251);
  13.     SetConsoleOutputCP(1251);
  14.  
  15.     ifstream Fin;
  16.     Fin.open("C:\\Users\\User\\Desktop\\mathematics.txt");
  17.  
  18.     const string L = "([{",
  19.         R = ")]}";
  20.  
  21.     string str;
  22.     stack <char> S;
  23.  
  24.     ofstream Fout;
  25.     Fout.open("C:\\Users\\User\\Desktop\\control.txt");
  26.     while (Fin >> str)
  27.     {
  28.         bool error = false;
  29.         int errorPosition, p;
  30.  
  31.         for (int i = 0; i < (int)str.size(); i++)
  32.         {
  33.             p = L.find(str[i]);
  34.             if (p >= 0)
  35.                 S.push(str[i]);
  36.             p = R.find(str[i]);
  37.             if (p >= 0)
  38.             {
  39.                 if (S.empty())
  40.                     error = true;
  41.                 else
  42.                 {
  43.                     char c = S.top();
  44.                     S.pop();
  45.                     if (p != L.find(c))
  46.                         error = true;
  47.                 }
  48.                 if (error)
  49.                 {
  50.                     errorPosition = i;
  51.                     break;
  52.                 }
  53.             }
  54.         }
  55.         if (!error)
  56.         {
  57.             cout << str << " - Скобки расставлены верно." << endl;
  58.             Fout << str << " - верно." << endl;
  59.         }
  60.         else
  61.         {
  62.             cout << str << " - Скобки расставлены неверно. Ошибка в позиции: " << errorPosition + 1 << endl;
  63.             Fout << str << " - неверно. Ошибка в позиции: " << errorPosition + 1 << endl;
  64.         }
  65.     }
  66.     Fin.close();
  67.     Fout.close();
  68.  
  69.     while (!GetAsyncKeyState(VK_ESCAPE));
  70. }
Advertisement
Add Comment
Please, Sign In to add comment