Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <clocale>
- #include <windows.h>
- #include <fstream>
- #include <stack>
- using namespace std;
- void main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- ifstream Fin;
- Fin.open("C:\\Users\\User\\Desktop\\mathematics.txt");
- const string L = "([{",
- R = ")]}";
- string str;
- stack <char> S;
- ofstream Fout;
- Fout.open("C:\\Users\\User\\Desktop\\control.txt");
- while (Fin >> str)
- {
- bool error = false;
- int errorPosition, p;
- for (int i = 0; i < (int)str.size(); i++)
- {
- p = L.find(str[i]);
- if (p >= 0)
- S.push(str[i]);
- p = R.find(str[i]);
- if (p >= 0)
- {
- if (S.empty())
- error = true;
- else
- {
- char c = S.top();
- S.pop();
- if (p != L.find(c))
- error = true;
- }
- if (error)
- {
- errorPosition = i;
- break;
- }
- }
- }
- if (!error)
- {
- cout << str << " - Скобки расставлены верно." << endl;
- Fout << str << " - верно." << endl;
- }
- else
- {
- cout << str << " - Скобки расставлены неверно. Ошибка в позиции: " << errorPosition + 1 << endl;
- Fout << str << " - неверно. Ошибка в позиции: " << errorPosition + 1 << endl;
- }
- }
- Fin.close();
- Fout.close();
- while (!GetAsyncKeyState(VK_ESCAPE));
- }
Advertisement
Add Comment
Please, Sign In to add comment