public bool Run(IProcessorStorage storage) { IMathBuffer mathBuffer = storage.Maths; List <string> history = null; IPathReader pathReader = null; IExpressionParser expressionParser = null; if (storage is IProcessorStorageFilesWork ext) { history = ext.OperationsHistory.Data; pathReader = ext.FilePathReader; expressionParser = ext.MathExpressionParser; } else { throw new ArgumentException(); } var newOperBuffer = new List <string>(); var valBuffer = new List <double>(); ICalcIO calcIO = storage.CalcIO; using (var file = new StreamReader(pathReader.Read(calcIO))) { string expression, rawExpression; while ((rawExpression = expression = file.ReadLine()) != null) { newOperBuffer.Add(expression); double result = expressionParser.Parse(ref expression, valBuffer, calcIO); if (double.IsNaN(result)) { calcIO.Write("Parse error!\n"); return(true); } calcIO.Write($"[#{ valBuffer.Count }] { rawExpression } = { (rawExpression != expression ? $"{ expression } = " : "") }{ result }\n"); } } mathBuffer.Values = valBuffer; mathBuffer.AccValue = valBuffer.Last(); history.Clear(); history.AddRange(newOperBuffer); return(true); }
private static void ReadServerMessages() { while (_programRunning) { _clientCalcIO.Write(_networkCalcIO.ReadString()); } }
public void Start() { calcIO.WriteLine( "Usage:\n" + " when first symbol on line is ‘>’ – enter operand(number)\n" + " when first symbol on line is ‘@’ – enter operation\n"); RunMathOperation(mathOperations['=']); bool running = true; while (running) { string input; do { calcIO.Write("@: "); input = calcIO.ReadString().Trim(); } while (input.Length > 1 || string.IsNullOrWhiteSpace(input)); if (mathOperations.TryGetValue(input[0], out var mathFunc)) { RunMathOperation(mathFunc); continue; } if (systemOperations.TryGetValue(input[0], out var sysFunc)) { running = sysFunc(); } } }
public string Read(ICalcIO calcIO) { Directory.CreateDirectory(FolderPath); var stringBuilder = new StringBuilder("List of existing files:\n"); foreach (string file in Directory.GetFiles(FolderPath, "*.txt")) { var str = file.Replace(FolderPath, ""); stringBuilder.Append(str.Substring(0, str.LastIndexOf(".txt"))); stringBuilder.Append("\n"); } calcIO.Write(stringBuilder.Append("Enter name of file:\n").ToString()); var name = calcIO.ReadString(); return(FolderPath + (string.IsNullOrWhiteSpace(name) ? "_" : name) + ".txt"); }
public double ReadDouble(Predicate <double> valueCorrectnessPredicate = null) { double result; _calcIO.Write("> "); string input; while (!double.TryParse(input = _calcIO.ReadString(), out result) || !(valueCorrectnessPredicate?.Invoke(result) ?? true)) { _calcIO.Write("Wrong input. Try again.\n> "); } return(result); }
public double Parse(ref string expression, List <double> valBuffer, ICalcIO calcIO) { foreach (KeyValuePair <string, string> pair in replaceDictionary) { expression = expression.Replace(pair.Key, pair.Value); } MatchCollection outFuncMatches = Regex.Matches(expression, @"Out\(-?\d*\)"); foreach (Match match in outFuncMatches) { string old = match.Value; if (!int.TryParse(old.Replace("Out(", "").Replace(")", ""), out int index)) { break; } if (index < 0) { index = valBuffer.Count + index + 1; } expression = expression.Replace(old, "" + valBuffer[index - 1]); } double result = double.NaN; try { result = (double)System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(new ParameterExpression[0], typeof(double), expression).Compile()?.DynamicInvoke(); valBuffer.Add(result); } catch (System.Linq.Dynamic.Core.Exceptions.ParseException e) { calcIO.Write(e.Message + "\n"); } return(result); }