public static object EvaluateInput(string input) { if (input.Contains("=="))//comparison operator { string[] parts = input.Split("=="); object original = Parse.ParseAnything(parts[0]); //compare all of the following to the original object for (int i = 1; i < parts.Length; i++) { //if not equal, return now if (!Function.compare(original, Parse.ParseAnything(parts[i]))) { return(false); } } //if it made it this far, it is equal return(true); } else if (input.Contains('=')) //assignment operator { string[] parts = input.Split('='); if (parts.Length > 2) { PrintError("You can only use one assignment operator (=) in one line. (Input.EvaluateInput)"); return(null); } string name = parts[0].Trim(); object value = Parse.ParseAnything(parts[1]); Sandbox.SetVariable(name, value); return(value); } else { return(Parse.ParseAnything(input)); } }
private static void Main(string[] args) { Console.Title = "Mathematical Sandbox"; Console.CursorVisible = false; Console.SetWindowSize(150, 45); Console.TreatControlCAsInput = false; saveData = SaveData.Load(); saveData.Update(); Sandbox.LoadVariables(saveData); //if the background has a color, this will color it all that color Output.Clear(); Intro(); MainLoop(); Output.Clear(); Sandbox.SaveVariables(saveData); saveData.Save(); }
private static void Settings() { Clear(); string[] options = new string[] { "Text Color", "Background Color", "Debug Mode", "Delete All Variables", "Reset Settings", "Back" }; int optionsChoice = EnterChoiceIndex(options); while (optionsChoice >= 0) { string choice = options[optionsChoice]; switch (choice) { case "Text Color": case "Background Color": bool foreColor = choice.CompareTo("Text Color") == 0; string[] colors = Enum.GetNames(typeof(ConsoleColor)); int colorIndex = 0; string currentColor = Enum.GetName(typeof(ConsoleColor), foreColor ? saveData.ForeColor : saveData.BackColor); for (int i = 0; i < colors.Length; i++) { if (colors[i].CompareTo(currentColor) == 0) { colorIndex = i; } } colorIndex = EnterChoiceIndex(colors, colorIndex); if (colorIndex >= 0) { ConsoleColor cc = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colors[colorIndex]); //set based on whichever one the user is setting, and do not set it if it is the same color as the opposite //otherwise you cannot see anything if (foreColor && saveData.BackColor != cc) { saveData.ForeColor = cc; } else if (!foreColor && saveData.ForeColor != cc) { saveData.BackColor = cc; } saveData.Update(); } break; case "Debug Mode": saveData.DebugMode = EnterChoiceIndex(new string[] { "On", "Off" }, "Debug Mode", saveData.DebugMode ? 0 : 1) == 0; break; case "Delete All Variables": if (EnterYesNo("Are you sure you want to delete all saved variables?")) { Sandbox.ClearVariables(); } break; case "Reset Settings": if (EnterYesNo("Are you sure you want to reset all settings?")) { saveData.ResetSettings(); saveData.Update(); } break; case "Back": return; } optionsChoice = EnterChoiceIndex(options, optionsChoice); } }
private static void UseFunction(string funcChoice) { //get the function itself System.Reflection.MethodInfo mi = Function.GetFunctionFromFullName(funcChoice); string funcName = mi.Name; //get the parameters so we know what to ask System.Reflection.ParameterInfo[] pis = mi.GetParameters(); object[] args = new object[pis.Length]; Clear(); //LINE 1: Preview of function //LINE 2: Instructions //LINE 3: Input int argsEntered = 0; while (argsEntered < args.Length) { Type t = pis[argsEntered].ParameterType; Console.SetCursorPosition(0, 0); PrintPartialFunction(funcName, args); Console.SetCursorPosition(0, 1); Console.Write(string.Format("Enter a {0}.{1}", t.Name.ToLower(), new string(' ', 30)));//write the instructions Console.SetCursorPosition(0, 2); object input = null; if (t == typeof(string)) { input = EnterString(); } else if (t == typeof(MathNet.Symbolics.SymbolicExpression)) { input = EnterExpression(); } else if (t.IsArray) { input = ParseDoubleArray(EnterString()); } else if (t == typeof(int)) { input = EnterInt(); } else if (t == typeof(double)) { input = EnterDouble(); } else if (t == typeof(bool)) { input = EnterBool(); } else { input = EnterString(); } args[argsEntered++] = input; Console.SetCursorPosition(0, 2); Console.Write(new string(' ', input.ToString().Length + 1)); } //input is done, print the output info and then go into sanbox mode Clear(); object output = Function.EvaluateFunction(funcName, args); Sandbox.SetVariable("ans", output); PrintPartialFunction(funcName, args); PrintLine(); Print(string.Format("ans = {0}", output)); Print("Use ans to refer to your answer."); PrintLine(2); Sandbox.SandboxLoop(false); }
public static void clear() { Sandbox.ClearScreen(); }
//example: sin ( max ( 2 , 3 ) / 3 * pi ) //output: 2 3 max 3 / pi * sin private static string[] GetTokens(string input) { List <string> tokens = new List <string>(); StringBuilder current = new StringBuilder(); char lastChar = '\n'; for (int i = 0; i < input.Length; i++) { char c = input[i]; if (c == STR_OPEN_CLOSE) { int end = FindNextSTRChar(input, i + 1); tokens.Add(input.Substring(i, end - i + 1)); i = end; continue; } if (c == ARR_OPEN) { int end = FindNextCloseIndex(input, i + 1, ARR_OPEN, ARR_CLOSE); tokens.Add(input.Substring(i, end - i + 1)); i = end; continue; } if (c == EXPR_OPEN) { int end = FindNextCloseIndex(input, i + 1, EXPR_OPEN, EXPR_CLOSE); tokens.Add(input.Substring(i, end - i + 1)); i = end; continue; } //ignore whitespace if (char.IsWhiteSpace(c)) { continue; } if (char.IsLetterOrDigit(c) || c == '.' || (c == '-' && IsOperator(lastChar)))//letter, number or decimal point { current.Append(c); } else { //must be an operator or something if it got this far if (current.Length > 0) { //add the current thing to the string thing = current.ToString(); current.Clear(); //try to replace with variable value thing = Function.TryReplaceConstant(Sandbox.TryReplaceVariable(thing)); //if it has a number at the front, it's a number if (char.IsDigit(thing[0])) { tokens.Add(thing); } else { //otherwise, it's a function, so add the amount of parameters at the end //this is used to evaluate functions that have overrides thing += CountUntilNext(input, i, ARG_PAR, FUNC_CLOSE) + 1; tokens.Add(thing); } } //add commas tokens.Add(c.ToString()); } lastChar = c; } //get the last part if (current.Length > 0) { tokens.Add(current.ToString()); } return(tokens.ToArray()); }