Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Enigmanation
- {
- private static string[] FindElements(string input)
- {
- //FindElements returns a string array with all elements (a.k.a signs or numbers). if there are any brackets they become 1 element (number)
- List<string> outputList = new List<string>();
- for (int i = 0; i < input.Length; i++)
- {
- // if there is a sign it becomes a new element in the outputList
- if ((char)input[i] == '+' || (char)input[i] == '-' || (char)input[i] == '*' || (char)input[i] == '/' || (char)input[i] == '%')
- {
- outputList.Add(Convert.ToString(input[i]));
- }
- // if there is a number it becomes a new element in the outputList
- if ((int)input[i] >= 48 && (int)input[i] <= 57)
- {
- outputList.Add(Convert.ToString(input[i]));
- }
- // 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
- if ((char)input[i] == '(')
- {
- for (int j = i; j < input.Length; j++)
- {
- if ((char)input[j] == ')')
- {
- string[] newInput = FindElements(input.Substring(i + 1, j - i + 1));
- outputList.Add(DoMath(newInput));
- // the loop should continiue from the closing bracket, so i = j
- i = j;
- break;
- }
- }
- }
- if ((char)input[i] == '=')
- {
- outputList.Add(Convert.ToString(input[i]));
- break;
- }
- }
- // returns the string array with elements
- return outputList.ToArray();
- }
- private static string DoMath(string[] input)
- {
- // DoMath finds the value of the given string array
- List<double> numberElements = new List<double>();
- List<double> bigElements = new List<double>();
- List<char> signs = new List<char>();
- for (int i = 0; i < input.Length; i++)
- {
- if ((int)input[i][0] >= 48 && (int)input[i][0] <= 57)
- {
- numberElements.Add(Convert.ToDouble(input[i]));
- }
- }
- bool isThereSecondarySign = false;
- double result = numberElements[0];
- int counter = 1;
- for (int i = 0; i < input.Length; i++)
- {
- if ((char)input[i][0] == '*')
- {
- result = result * numberElements[counter];
- counter++;
- }
- if ((char)input[i][0] == '/')
- {
- result = result / numberElements[counter];
- counter++;
- }
- if ((char)input[i][0] == '%')
- {
- result = result % numberElements[counter];
- counter++;
- }
- if ((char)input[i][0] == '-' || (char)input[i][0] == '+')
- {
- bigElements.Add(result);
- result = numberElements[counter];
- counter++;
- isThereSecondarySign = true;
- }
- if ((char)input[i][0] == '=' || (char)input[i][0] == ')')
- {
- bigElements.Add(result);
- result = bigElements[0];
- }
- }
- counter = 1;
- for (int i = 0; i < input.Length; i++)
- {
- if ((char)input[i][0] == '-')
- {
- result = result - bigElements[counter];
- counter++;
- }
- if ((char)input[i][0] == '+')
- {
- result = result + bigElements[counter];
- counter++;
- }
- }
- return result.ToString();
- }
- static void Main()
- {
- string input = Console.ReadLine();
- Console.WriteLine("{0:F3}", Convert.ToDouble(DoMath(FindElements(input))));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement