OneCompiler

cheak () c#

1797

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

class Program
{
static void Main(string[] args)
{
string expression = "(a + b) * (c - d)";
bool result = IsParenthesesBalanced(expression);
Console.WriteLine($"Корректность расстановки скобок: {result}");
}

static bool IsParenthesesBalanced(string expression)
{
    Stack<char> stack = new Stack<char>();

    foreach (char c in expression)
    {
        if (c == '(')
        {
            stack.Push(c);
        }
        else if (c == ')')
        {
            if (stack.Count == 0 || stack.Pop() != '(')
            {
                return false;
            }
        }
    }

    return stack.Count == 0;
}

}