Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Enigmanation
- {
- static double CalcBrackets(string expr)
- {
- char[] expression = expr.ToCharArray();
- double result = expression[0] - '0';
- for (int i = 2; i < expression.Length; i += 2)
- {
- if (expression[i - 1] == '+') result += (expression[i] - '0');
- else if (expression[i - 1] == '-') result -= (expression[i] - '0');
- else if (expression[i - 1] == '%') result %= (expression[i] - '0');
- else if (expression[i - 1] == '*') result *= (expression[i] - '0');
- }
- return result;
- }
- static void Main()
- {
- string entry = Console.ReadLine();
- char[] input = entry.ToCharArray();
- List<string> list = new List<string>();
- double result = 0;
- for (int i = 0; i < input.Length; i++)
- {
- if (input[i] == '(')
- {
- int endInBracketIndex = entry.IndexOf(')', i);
- string insideBrackets = entry.Substring(i + 1, endInBracketIndex - i - 1);
- list.Add(CalcBrackets(insideBrackets).ToString());
- i = endInBracketIndex;
- }
- if (input[i] == ')') i += 1;
- list.Add(input[i].ToString());
- }
- result = double.Parse(list[0]);
- for (int i = 2; i < list.Count; i++)
- {
- if (list[i - 1] == "+") result += double.Parse(list[i]);
- else if (list[i - 1] == "-") result -= double.Parse(list[i]);
- else if (list[i - 1] == "%") result %= double.Parse(list[i]);
- else if (list[i - 1] == "*") result *= double.Parse(list[i]);
- }
- Console.WriteLine("{0:f3}", result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement