Beispiel #1
0
        //Testing
        public static void Main()
        {
            OrderOfOperations go = new OrderOfOperations();
            var a = go.brackets("(12)5");

            Console.WriteLine("Result: " + a);
        }
Beispiel #2
0
 public decimal brackets(String s)
 {   //Separate brackets
     check = new OrderOfOperations();
     while (s.Contains(Char.ToString('(')) || s.Contains(Char.ToString('(')))
     {
         for (int o = 0; o < s.Length; o++)
         {
             try
             {   //If there is not sign it is treated as a multiplication
                 if ((s[o] == ')' || Char.IsDigit(s[o])) &&
                     s[o + 1] == '(')
                 {
                     s = s.Substring(0, o + 1) + "*" + (s.Substring(o + 1));
                 }
             }
             catch (Exception ignored) { }
             if (s[o] == ')')
             {   //search for a closing bracket
                 for (int i = o; i >= 0; i--)
                 {
                     if (s[i] == '(')
                     {   //search for a opening bracket
                         String inn = s.Substring(i + 1, o);
                         inn = check.recognize(inn);
                         s   = s.Substring(0, i) + inn + s.Substring(o + 1);
                         i   = o = 0;
                     }
                 }
             }
         }
         if (s.Contains(Char.ToString('(')) || s.Contains(Char.ToString(')')) ||
             s.Contains(Char.ToString('(')) || s.Contains(Char.ToString(')')))
         {
             Console.WriteLine("Error: incorrect brackets placement");
             return(0);
         }
     }
     s = check.recognize(s);
     return(Convert.ToDecimal(s));
 }