Advertisement
Guest User

Untitled

a guest
Apr 5th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. class Enigmanation
  4. {
  5.     private static string[] FindElements(string input)
  6.     {
  7.         //FindElements returns a string array with all elements (a.k.a signs or numbers). if there are any brackets they become 1 element (number)
  8.         List<string> outputList = new List<string>();
  9.         for (int i = 0; i < input.Length; i++)
  10.         {
  11.             // if there is a sign it becomes a new element in the outputList
  12.             if ((char)input[i] == '+' || (char)input[i] == '-' || (char)input[i] == '*' || (char)input[i] == '/' || (char)input[i] == '%')
  13.             {
  14.                 outputList.Add(Convert.ToString(input[i]));
  15.             }
  16.             // if there is a number it becomes a new element in the outputList
  17.             if ((int)input[i] >= 48 && (int)input[i] <= 57)
  18.             {
  19.                 outputList.Add(Convert.ToString(input[i]));
  20.             }
  21.             // if there is an opening bracket it finds its closing bracket and uses the FindElements and DoMath funcion to find the result in the brackets -> then the value becomes a new element in the outputList
  22.             if ((char)input[i] == '(')
  23.             {
  24.                 for (int j = i; j < input.Length; j++)
  25.                 {
  26.                     if ((char)input[j] == ')')
  27.                     {
  28.                         string[] newInput = FindElements(input.Substring(i + 1, j - i + 1));
  29.                         outputList.Add(DoMath(newInput));
  30.                         // the loop should continiue from the closing bracket, so i = j
  31.                         i = j;
  32.                         break;
  33.                     }
  34.                 }
  35.             }
  36.             if ((char)input[i] == '=')
  37.             {
  38.                 outputList.Add(Convert.ToString(input[i]));
  39.                 break;
  40.             }
  41.         }
  42.         // returns the string array with elements
  43.         return outputList.ToArray();
  44.     }
  45.     private static string DoMath(string[] input)
  46.     {
  47.         // DoMath finds the value of the given string array
  48.         List<double> numberElements = new List<double>();
  49.         List<double> bigElements = new List<double>();
  50.         List<char> signs = new List<char>();
  51.         for (int i = 0; i < input.Length; i++)
  52.         {
  53.             if ((int)input[i][0] >= 48 && (int)input[i][0] <= 57)
  54.             {
  55.                 numberElements.Add(Convert.ToDouble(input[i]));
  56.             }
  57.         }
  58.         bool isThereSecondarySign = false;
  59.         double result = numberElements[0];
  60.         int counter = 1;
  61.         for (int i = 0; i < input.Length; i++)
  62.         {
  63.             if ((char)input[i][0] == '*')
  64.             {
  65.                 result = result * numberElements[counter];
  66.                 counter++;
  67.             }
  68.             if ((char)input[i][0] == '/')
  69.             {
  70.                 result = result / numberElements[counter];
  71.                 counter++;
  72.             }
  73.             if ((char)input[i][0] == '%')
  74.             {
  75.                 result = result % numberElements[counter];
  76.                 counter++;
  77.             }
  78.             if ((char)input[i][0] == '-' || (char)input[i][0] == '+')
  79.             {
  80.                 bigElements.Add(result);
  81.                 result = numberElements[counter];
  82.                 counter++;
  83.                 isThereSecondarySign = true;
  84.             }
  85.             if ((char)input[i][0] == '=' || (char)input[i][0] == ')')
  86.             {
  87.                 bigElements.Add(result);
  88.                 result = bigElements[0];
  89.             }
  90.         }
  91.         counter = 1;
  92.         for (int i = 0; i < input.Length; i++)
  93.         {
  94.             if ((char)input[i][0] == '-')
  95.             {
  96.                 result = result - bigElements[counter];
  97.                 counter++;
  98.             }
  99.             if ((char)input[i][0] == '+')
  100.             {
  101.                 result = result + bigElements[counter];
  102.                 counter++;
  103.             }
  104.         }
  105.         return result.ToString();
  106.     }
  107.     static void Main()
  108.     {
  109.         string input = Console.ReadLine();
  110.         Console.WriteLine("{0:F3}", Convert.ToDouble(DoMath(FindElements(input))));
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement