Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static void Main(string[] args)
- {
- string exp = Console.ReadLine();
- Console.WriteLine(Calculate(exp));
- }
- private static int Calculate(string exp)
- {
- if (exp.ToUpper() != exp)
- throw new ArgumentException("niga u rarted");
- StringBuilder endgame = new StringBuilder(exp);
- int bracketToggle = default(int);
- char operationToggle = default(char);
- int pointerMemory = default(int);
- string[] numberTemp = new string[2] { "", "" };
- for (int i = 0; i < exp.Length; i++)
- {
- if (operationToggle != default(char) && !int.TryParse(exp[i].ToString(), out int crutch4) && numberTemp[0] != "")
- {
- var temp = numberTemp[0] + operationToggle.ToString() + numberTemp[1];
- endgame.Replace(temp, Calculate(temp + "&").ToString());
- operationToggle = default(char);
- numberTemp = new string[2] { "", "" };
- }
- switch (exp[i])
- {
- case '(':
- bracketToggle++;
- pointerMemory = endgame.ToString().IndexOf('(');
- break;
- case ')':
- if (bracketToggle == 0)
- throw new ArgumentException("nigga u rarrted");
- int occurenceCount = 0;
- for (int j = 0; j < bracketToggle; j++)
- occurenceCount = endgame.ToString().IndexOf(')', occurenceCount);
- var length = occurenceCount + 1 < endgame.ToString().Length ? occurenceCount + 1 : occurenceCount;
- endgame.Replace(endgame.ToString().Substring(pointerMemory, length), Calculate(endgame.ToString().Substring(pointerMemory + 1, length - 2)).ToString());
- bracketToggle--;
- break;
- case '+':
- if (bracketToggle == 0)
- {
- int[] crutch2;
- if (exp[exp.Length - 1] == '&')
- {
- exp = exp.Substring(0, exp.Length - 1);
- crutch2 = Array.ConvertAll(exp.Split('+'), int.Parse);
- return (crutch2[0] + crutch2[1]);
- }
- else operationToggle = '+';
- }
- break;
- case '-':
- if (bracketToggle == 0)
- {
- int[] crutch2;
- if (exp[exp.Length - 1] == '&')
- {
- exp = exp.Substring(0, exp.Length - 1);
- crutch2 = Array.ConvertAll(exp.Split('-'), int.Parse);
- return (crutch2[0] - crutch2[1]);
- }
- else
- {
- if (!int.TryParse(exp[i - 1].ToString(), out int crutch3))
- numberTemp[0] = "0";
- else operationToggle = '-';
- }
- }
- break;
- case '*':
- if (bracketToggle == 0)
- {
- int[] crutch2;
- if (exp[exp.Length - 1] == '&')
- {
- exp = exp.Substring(0, exp.Length - 1);
- crutch2 = Array.ConvertAll(exp.Split('*'), int.Parse);
- return (crutch2[0] * crutch2[1]);
- }
- else operationToggle = '*';
- }
- break;
- case '/':
- if (bracketToggle == 0)
- {
- int[] crutch2;
- if (exp[exp.Length - 1] == '&')
- {
- exp = exp.Substring(0, exp.Length - 1);
- crutch2 = Array.ConvertAll(exp.Split('/'), int.Parse);
- return (crutch2[0] / crutch2[1]);
- }
- else operationToggle = '/';
- }
- break;
- case char x when int.TryParse(x.ToString(), out int y):
- if (bracketToggle == 0)
- {
- if (operationToggle == default(char))
- numberTemp[0] = numberTemp[0] == "" ? x.ToString() : numberTemp[0] + x.ToString();
- else numberTemp[1] = numberTemp[1] == "" ? x.ToString() : numberTemp[1] + x.ToString();
- }
- break;
- default:
- throw new ArgumentException("nigga");
- }
- }
- if (bracketToggle != 0)
- throw new ArgumentException("cmon nigga");
- if (operationToggle != default(char))
- {
- var temp = numberTemp[0] + operationToggle.ToString() + numberTemp[1];
- endgame.Replace(temp, Calculate(temp + "&").ToString());
- operationToggle = default(char);
- numberTemp = new string[2] { "", "" };
- }
- if (int.TryParse(endgame.ToString(), out int nigger))
- return nigger;
- else return Calculate(endgame.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement