Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CoolCalculator
- {
- class MathValue
- {
- private ArrayList args = new ArrayList();
- public MathValue(string input)
- {
- Console.WriteLine("Parsing " + input);
- int paranthesisLevel = 0;
- int paranthesisStart = -1;
- int numberStart = -1;
- int functionNameStart = -1;
- for(int i = 0; i < input.Length; i++)
- {
- char currChar = input[i];
- if(currChar == '(')
- {
- if(paranthesisLevel==0)
- {
- paranthesisStart = i;
- if (numberStart != -1)
- {
- args.Add(Double.Parse(input.Substring(numberStart, i - numberStart)));
- numberStart = -1;
- }
- else if(functionNameStart != -1)
- {
- args.Add(input.Substring(functionNameStart, i - functionNameStart));
- functionNameStart = -1;
- }
- }
- paranthesisLevel++;
- }
- else if(currChar == ')')
- {
- paranthesisLevel--;
- if(paranthesisLevel==0)
- {
- args.Add(new MathValue(input.Substring(paranthesisStart+1, i-paranthesisStart-1)));
- paranthesisStart = 0;
- }
- }
- else if(paranthesisLevel!=0)
- {
- //do nothing, we let other math values do that parsing...
- }
- else if(isAllowedOperator(currChar))
- {
- if(numberStart!=-1)
- {
- args.Add(Double.Parse(input.Substring(numberStart, i - numberStart)));
- numberStart = -1;
- }
- args.Add(currChar);
- }
- else if(isNumeric(currChar) || currChar == Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
- {
- if(functionNameStart != -1)
- {
- throw new Exception("Fuck you no loose numbers next to a function you wanker");
- }
- if(numberStart==-1)
- {
- numberStart = i;
- }
- }
- else
- {
- //function call
- if(functionNameStart == -1)
- {
- functionNameStart = i;
- }
- }
- }
- if(numberStart != -1)
- {
- //Parse remaining numbers
- args.Add(Double.Parse(input.Substring(numberStart, input.Length - numberStart)));
- }
- Console.WriteLine("Done parsing!");
- }
- private bool isAllowedOperator(char a)
- {
- return a == '*' || a == '/' || a == '+' || a == '-';
- }
- private bool isNumeric(char a)
- {
- return a == '1' || a == '2' || a == '3' || a == '4' || a == '5' || a == '6' || a == '7' || a == '8' || a == '9' || a == '0';
- }
- private double doFunction(string funcName, double arg)
- {
- switch(funcName)
- {
- case "sqrt":
- return Math.Sqrt(arg);
- }
- throw new Exception("Unknown function");
- return -1;
- }
- public double solve()
- {
- //First iteration, handle * and /
- for (int i = 0; i < args.Count; i++)
- {
- if (args[i].GetType() == typeof(char) && ((char)args[i] == '/' || (char)args[i] == '*'))
- {
- double leftSideVal = 0.0;
- double rightSideVal = 0.0;
- //Get left and right side
- if (args[i - 1].GetType() == typeof(double))
- {
- leftSideVal = (double)args[i - 1];
- }
- else if(args[i-1].GetType() == typeof(MathValue))
- {
- leftSideVal = ((MathValue)args[i - 1]).solve();
- }
- //Left side can't have a function string, that would be weird
- if (args[i + 1].GetType() == typeof(double))
- {
- rightSideVal = (double)args[i + 1];
- }
- else if (args[i + 1].GetType() == typeof(MathValue))
- {
- rightSideVal = ((MathValue)args[i + 1]).solve();
- }
- else
- {
- //Right side can have a function string
- rightSideVal = doFunction((string)args[i + 1], ((MathValue)args[i + 1]).solve());
- }
- //Calculate and set value
- if((char)args[i] == '/')
- {
- args[i - 1] = leftSideVal / rightSideVal;
- }
- else if((char)args[i] == '*')
- {
- args[i - 1] = leftSideVal * rightSideVal;
- }
- //Remove stuff we have calculated
- args.RemoveAt(i);
- args.RemoveAt(i);
- //Jump back a bit
- i -= 2;
- }
- }
- //We have divided and multiplied everything, sum it up
- double sum = 0;
- char opcode = '+';
- for (int i = 0; i < args.Count; i++)
- {
- if(args[i].GetType() == typeof(double))
- {
- if(opcode == '+')
- {
- sum += (double)args[i];
- }
- else if(opcode == '-')
- {
- sum -= (double)args[i];
- }
- else
- {
- throw new Exception("Illegal opcode");
- }
- opcode = ' '; //Swallow the opcode
- }
- else if(args[i].GetType() == typeof(MathValue))
- {
- if (opcode == '+')
- {
- sum += ((MathValue)args[i]).solve();
- }
- else if (opcode == '-')
- {
- sum -= ((MathValue)args[i]).solve();
- }
- else
- {
- throw new Exception("Illegal opcode");
- }
- opcode = ' '; //Swallow the opcode
- }
- else if(args[i].GetType() == typeof(string))
- {
- if (opcode == '+')
- {
- sum += doFunction((string)args[i], ((MathValue)args[i + 1]).solve());
- i++;
- }
- else if (opcode == '-')
- {
- sum -= doFunction((string)args[i], ((MathValue)args[i + 1]).solve());
- i++;
- }
- else
- {
- throw new Exception("Illegal opcode");
- }
- opcode = ' '; //Swallow the opcode
- }
- else
- {
- opcode = (char)args[i];
- }
- }
- return sum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement