Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <stack>
- #include <string>
- using namespace std;
- int main() {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- stack<char> s{};
- string input{};
- cout << "Введіть послідовність дужок: ";
- getline(cin, input);
- for (int i = 0; i < input.size(); i++) {
- char part = input[i];
- if (part == '(' || part == '[' || part == '{') {
- s.push(part);
- }
- else if (part == ')' || part == ']' || part == '}') {
- if (s.empty()) {
- cout << "Неправильна послідовність дужок" << endl;
- return 0;
- }
- char top = s.top();
- s.pop();
- if ((top == '(' && part != ')') ||
- (top == '[' && part != ']') ||
- (top == '{' && part != '}')) {
- cout << "Неправильна послідовність дужок" << endl;
- return 0;
- }
- }
- else {
- cout << "У введеному рядку присутні сторонні символи" << endl;
- return 0;
- }
- }
- if (s.empty()) {
- cout << "Правильна послідовність дужок" << endl;
- }
- else {
- cout << "Неправильна послідовність дужок" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement