nadia_dr

Untitled

Jan 7th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class Program
  5. {
  6.     // Write a program to check if in a given expression the brackets are put correctly.
  7.     // Example of correct expression: ((a+b)/5-d).
  8.     // Example of incorrect expression: )(a+b)).
  9.  
  10.     static void Main()
  11.     {
  12.         string expression1 = "((a+b)/5-d)";
  13.         CheckingBreckets(expression1);
  14.         string expression2 = ")(a+b))";
  15.         CheckingBreckets(expression2);
  16.     }
  17.     public static void CheckingBreckets(string expression)
  18.     {
  19.         StringBuilder sb = new StringBuilder();
  20.  
  21.         for (int i = 0; i < expression.Length; i++)
  22.             {
  23.             if(expression[i]=='('||expression[i]==')')
  24.                 sb.Append(expression [i]);
  25.             }
  26.  
  27.        bool isSymetric = true;
  28.        int length = sb.Length;
  29.  
  30.        if (length >= 2)
  31.        {
  32.            for (int i = 0; i < length / 2; i++)
  33.            {
  34.                if (sb[i] == '(' && sb[length - i - 1] == ')')
  35.                {
  36.                    isSymetric = true;
  37.                    continue;
  38.                }
  39.                else
  40.                {
  41.                    isSymetric = false;
  42.                    break;
  43.                }
  44.            }
  45.            Console.WriteLine(isSymetric);
  46.        }
  47.        else
  48.        {
  49.            isSymetric = false;
  50.            Console.WriteLine(isSymetric);
  51.        }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment