Beispiel #1
0
 /// <summary>
 /// this airthmetic expressetion using balance this instance
 /// </summary>
 public void Airthexp()
 {
     try
     {
         int count = 0;
         //// creating one utility object
         Utility u = new Utility();
         //// creating one stack objects
         //// Stack<char> sl = new Stack<char>();
         LinkedListClass list = new LinkedListClass();
         Console.WriteLine("enter expression ");
         string s1 = Console.ReadLine();
         //// string is converting into the charater array
         char[] ch = s1.ToCharArray();
         for (int i = 0; i < ch.Length; i++)
         {
             if (ch[i] == '(')
             {
                 list.Push(ch[i]);
             }
             else if (ch[i] == ')')
             {
                 list.Pop();
             }
         }
         if (list.Equals(s1))
         {
             Console.WriteLine("Balanced expression");
         }
         else
         {
             Console.WriteLine("Expression is not Balance Expressin");
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Beispiel #2
0
        public void BalancedParenthesisDemo()
        {
            try
            {
                //// string to store the expression
                string expression;
                Console.WriteLine("Enter the expression");
                expression = Console.ReadLine();
                //// self implemented Linked list
                LinkedListClass brackets = new LinkedListClass();
                foreach (char c in expression)
                {
                    //// If open found its pushed
                    switch (c)
                    {
                    case '(':
                        brackets.Push(c);
                        break;

                    //// if closed parenthesis encountered it pops the open parenthesis
                    case ')':
                        switch (brackets.Pop())
                        {
                        case false:
                            Console.WriteLine("Already empty equation not balanced");
                            return;
                        }

                        break;

                    case '{':
                        brackets.Push(c);
                        break;

                    case '}':
                        switch (brackets.Pop())
                        {
                        case false:
                            Console.WriteLine("Already empty equation not balanced");
                            return;
                        }

                        break;

                    case '[':
                        brackets.Push(c);
                        break;

                    case ']':
                        switch (brackets.Pop())
                        {
                        case false:
                            Console.WriteLine("Already empty equation not balanced");
                            return;
                        }

                        break;
                    }
                    //// if stack empty then parenthesis are balanced
                }

                if (brackets.IsEmpty())
                {
                    Console.WriteLine("The expression is balanced");
                }
                else
                {
                    Console.WriteLine("The expression is not balanced");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Process could noit be comnpleted as " + e);
            }
        }