Advertisement
ivan_yosifov

Enigmanation

Dec 9th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class Enigmanation
  5. {
  6.     static void Main()
  7.     {
  8.         string input = Console.ReadLine();
  9.         StringBuilder ready = new StringBuilder();
  10.  
  11.         int counter = 0;
  12.         while (true)
  13.         {
  14.             char ch = input[counter];
  15.             if (char.IsDigit(ch)) ready.Append(ch + " ");
  16.             else if (ch == '+' || ch == '-' || ch == '*' || ch == '%') ready.Append(ch + " ");
  17.             else // if (ch == '(')
  18.             {
  19.                 counter++;
  20.                 char next = input[counter];
  21.                 int result = int.Parse(next.ToString());
  22.                 while (true)
  23.                 {
  24.                     counter++;
  25.                     next = input[counter];
  26.                     if (next == '+' || next == '-' || next == '*' || next == '%')
  27.                     {
  28.                         counter++;
  29.                         int num = int.Parse(input[counter].ToString());
  30.                         switch (next)
  31.                         {
  32.                             case '+': result += num; break;
  33.                             case '-': result -= num; break;
  34.                             case '*': result *= num; break;
  35.                             case '%': result %= num; break;
  36.                         }
  37.                     }
  38.                     if (next == ')') break;
  39.                 }
  40.                 ready.Append(result + " ");
  41.             }
  42.             counter++;
  43.             if (input[counter] == '=')
  44.             {
  45.                 ready.Append("=");
  46.                 break;
  47.             }
  48.         }
  49.  
  50.         int count = 0;
  51.         for (int i = 0; i < ready.Length; i++)
  52.         {
  53.             if (ready[i] == ' ') count++;
  54.         }
  55.         string s = ready.ToString();
  56.         string[] str = s.Split(' ');
  57.  
  58.         long output = long.Parse(str[0]);
  59.  
  60.         for (int i = 1; str[i] != "="; i += 2)
  61.         {
  62.             char op = char.Parse(str[i]);
  63.             long num = long.Parse(str[i + 1]);
  64.             switch (op)
  65.             {
  66.                 case '+': output += num; break;
  67.                 case '-': output -= num; break;
  68.                 case '*': output *= num; break;
  69.                 case '%': output %= num; break;
  70.             }
  71.         }
  72.  
  73.         Console.WriteLine("{0:F3}", output);
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement