Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write a program to check if in a given expression the brackets are put correctly.
- Example of correct expression: ((a+b)/5-d).
- Example of incorrect expression: )(a+b)).*/
- namespace EX03.BracketsCheck
- {
- using System;
- using System.Text;
- class BracketsCheck
- {
- static void Main(string[] args)
- {
- string textBarckets = Console.ReadLine();//"((a+b)/5-d)";
- int indexBackward = 0;
- int indexForward = 0;
- indexForward = textBarckets.IndexOf('(');
- indexBackward = textBarckets.IndexOf(')');
- BarcketsCheck(indexBackward, indexForward, textBarckets);
- }
- private static void BarcketsCheck(int indexBackword, int indexForward, string textBarckets)
- {
- if (indexForward >= 0 && indexBackword > 0 && indexForward < indexBackword)
- {
- indexForward = textBarckets.IndexOf('(', indexForward + 1);
- indexBackword = textBarckets.IndexOf(')', indexBackword + 1);
- BarcketsCheck(indexBackword, indexForward, textBarckets);
- return;
- }
- else if (indexForward == -1 && indexBackword == -1)
- {
- Console.WriteLine("Correct expression");
- return;
- }
- else
- {
- Console.WriteLine("Incorrect expression");
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement