Advertisement
tane_superior

Untitled

Jun 10th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9.  
  10.     static void Main(string[] args)
  11.     {
  12.         string exp = Console.ReadLine();
  13.         Console.WriteLine(Calculate(exp));
  14.     }
  15.  
  16.  
  17.  
  18.     private static int Calculate(string exp)
  19.     {
  20.         if (exp.ToUpper() != exp)
  21.             throw new ArgumentException("niga u rarted");
  22.  
  23.         StringBuilder endgame = new StringBuilder(exp);
  24.         int bracketToggle = default(int);
  25.         char operationToggle = default(char);
  26.         int pointerMemory = default(int);
  27.         string[] numberTemp = new string[2] { "", "" };
  28.  
  29.         for (int i = 0; i < exp.Length; i++)
  30.         {
  31.             if (operationToggle != default(char) && !int.TryParse(exp[i].ToString(), out int crutch4) && numberTemp[0] != "")
  32.             {
  33.                 var temp = numberTemp[0] + operationToggle.ToString() + numberTemp[1];
  34.                 endgame.Replace(temp, Calculate(temp + "&").ToString());
  35.  
  36.                 operationToggle = default(char);
  37.                 numberTemp = new string[2] { "", "" };
  38.             }
  39.  
  40.             switch (exp[i])
  41.             {
  42.                 case '(':
  43.                     bracketToggle++;
  44.                     pointerMemory = endgame.ToString().IndexOf('(');
  45.                     break;
  46.                 case ')':
  47.                     if (bracketToggle == 0)
  48.                         throw new ArgumentException("nigga u rarrted");
  49.  
  50.                     int occurenceCount = 0;
  51.                     for (int j = 0; j < bracketToggle; j++)
  52.                         occurenceCount = endgame.ToString().IndexOf(')', occurenceCount);
  53.  
  54.                     var length = occurenceCount + 1 < endgame.ToString().Length ? occurenceCount + 1 : occurenceCount;
  55.                     endgame.Replace(endgame.ToString().Substring(pointerMemory, length), Calculate(endgame.ToString().Substring(pointerMemory + 1, length - 2)).ToString());
  56.                     bracketToggle--;
  57.                     break;
  58.                 case '+':
  59.                     if (bracketToggle == 0)
  60.                     {
  61.                         int[] crutch2;
  62.                         if (exp[exp.Length - 1] == '&')
  63.                         {
  64.                             exp = exp.Substring(0, exp.Length - 1);
  65.                             crutch2 = Array.ConvertAll(exp.Split('+'), int.Parse);
  66.                             return (crutch2[0] + crutch2[1]);
  67.                         }
  68.                         else operationToggle = '+';
  69.                     }
  70.                     break;
  71.                 case '-':
  72.                     if (bracketToggle == 0)
  73.                     {
  74.                         int[] crutch2;
  75.                         if (exp[exp.Length - 1] == '&')
  76.                         {
  77.                             exp = exp.Substring(0, exp.Length - 1);
  78.                             crutch2 = Array.ConvertAll(exp.Split('-'), int.Parse);
  79.                             return (crutch2[0] - crutch2[1]);
  80.                         }
  81.                         else
  82.                         {
  83.                             if (!int.TryParse(exp[i - 1].ToString(), out int crutch3))
  84.                                 numberTemp[0] = "0";
  85.                             else operationToggle = '-';
  86.                         }
  87.                     }
  88.                     break;
  89.                 case '*':
  90.                     if (bracketToggle == 0)
  91.                     {
  92.                         int[] crutch2;
  93.                         if (exp[exp.Length - 1] == '&')
  94.                         {
  95.                             exp = exp.Substring(0, exp.Length - 1);
  96.                             crutch2 = Array.ConvertAll(exp.Split('*'), int.Parse);
  97.                             return (crutch2[0] * crutch2[1]);
  98.                         }
  99.                         else operationToggle = '*';
  100.                     }
  101.                     break;
  102.                 case '/':
  103.                     if (bracketToggle == 0)
  104.                     {
  105.  
  106.                         int[] crutch2;
  107.                         if (exp[exp.Length - 1] == '&')
  108.                         {
  109.                             exp = exp.Substring(0, exp.Length - 1);
  110.                             crutch2 = Array.ConvertAll(exp.Split('/'), int.Parse);
  111.                             return (crutch2[0] / crutch2[1]);
  112.                         }
  113.                         else operationToggle = '/';
  114.  
  115.                     }
  116.                     break;
  117.                 case char x when int.TryParse(x.ToString(), out int y):
  118.                     if (bracketToggle == 0)
  119.                     {
  120.                         if (operationToggle == default(char))
  121.                             numberTemp[0] = numberTemp[0] == "" ? x.ToString() : numberTemp[0] + x.ToString();
  122.                         else numberTemp[1] = numberTemp[1] == "" ? x.ToString() : numberTemp[1] + x.ToString();
  123.                     }
  124.                     break;
  125.                 default:
  126.                     throw new ArgumentException("nigga");
  127.             }
  128.         }
  129.  
  130.         if (bracketToggle != 0)
  131.             throw new ArgumentException("cmon nigga");
  132.  
  133.         if (operationToggle != default(char))
  134.         {
  135.             var temp = numberTemp[0] + operationToggle.ToString() + numberTemp[1];
  136.             endgame.Replace(temp, Calculate(temp + "&").ToString());
  137.  
  138.             operationToggle = default(char);
  139.             numberTemp = new string[2] { "", "" };
  140.         }
  141.  
  142.         if (int.TryParse(endgame.ToString(), out int nigger))
  143.             return nigger;
  144.         else return Calculate(endgame.ToString());
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement