SHARE
    TWEET
    Lavig

    Другий семестр. Лабораторна робота No14-15 (Завдання 3)

    Apr 20th, 2025
    461
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 1.43 KB | None | 0 0
    1. #include <iostream>
    2. #include <windows.h>
    3. #include <stack>
    4. #include <string>
    5. using namespace std;
    6. int main() {
    7. SetConsoleOutputCP(1251);
    8. SetConsoleCP(1251);
    9. stack<char> s{};
    10. string input{};
    11. cout << "Введіть послідовність дужок: ";
    12. getline(cin, input);
    13. for (int i = 0; i < input.size(); i++) {
    14. char part = input[i];
    15. if (part == '(' || part == '[' || part == '{') {
    16. s.push(part);
    17. }
    18. else if (part == ')' || part == ']' || part == '}') {
    19. if (s.empty()) {
    20. cout << "Неправильна послідовність дужок" << endl;
    21. return 0;
    22. }
    23. char top = s.top();
    24. s.pop();
    25. if ((top == '(' && part != ')') ||
    26. (top == '[' && part != ']') ||
    27. (top == '{' && part != '}')) {
    28. cout << "Неправильна послідовність дужок" << endl;
    29. return 0;
    30. }
    31. }
    32. else {
    33. cout << "У введеному рядку присутні сторонні символи" << endl;
    34. return 0;
    35. }
    36. }
    37. if (s.empty()) {
    38. cout << "Правильна послідовність дужок" << endl;
    39. }
    40. else {
    41. cout << "Неправильна послідовність дужок" << endl;
    42. }
    43. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /