Advertisement
soxa

Expression

Jan 15th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. /*Write a program to check if in a given expression the brackets are put correctly.
  2. Example of correct expression: ((a+b)/5-d).
  3. Example of incorrect expression: )(a+b)).*/
  4.  
  5. namespace EX03.BracketsCheck
  6. {
  7.     using System;
  8.     using System.Text;
  9.  
  10.     class BracketsCheck
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string textBarckets = Console.ReadLine();//"((a+b)/5-d)";
  15.             int indexBackward = 0;
  16.             int indexForward = 0;
  17.  
  18.             indexForward = textBarckets.IndexOf('(');
  19.             indexBackward = textBarckets.IndexOf(')');
  20.  
  21.             BarcketsCheck(indexBackward, indexForward, textBarckets);
  22.         }
  23.  
  24.         private static void BarcketsCheck(int indexBackword, int indexForward, string textBarckets)
  25.         {
  26.             if (indexForward >= 0 && indexBackword > 0 && indexForward < indexBackword)
  27.             {
  28.                 indexForward = textBarckets.IndexOf('(', indexForward + 1);
  29.                 indexBackword = textBarckets.IndexOf(')', indexBackword + 1);
  30.  
  31.                 BarcketsCheck(indexBackword, indexForward, textBarckets);
  32.                 return;
  33.             }
  34.             else if (indexForward == -1 && indexBackword == -1)
  35.             {
  36.                 Console.WriteLine("Correct expression");
  37.                 return;
  38.             }
  39.             else
  40.             {
  41.                 Console.WriteLine("Incorrect expression");
  42.                 return;
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement