public ClientProcessor(MathsProccessor.MathsProccessorClient client, ICalcIO calcIO, ICalcInputParser parser) { this.client = client; this.calcIO = calcIO; this.parser = parser; operationsCount = 0; clientID = Guid.NewGuid().ToString(); mathOperations = new Dictionary <char, Func <Arguments, Result> > { ['='] = (args) => client.Set(args), ['+'] = (args) => client.Add(args), ['-'] = (args) => client.Sub(args), ['*'] = (args) => client.Mul(args), ['/'] = (args) => client.Div(args), }; systemOperations = new Dictionary <char, Func <bool> > { ['q'] = () => false, ['#'] = () => { var input = parser.ReadInt(x => x > 0 && x <= operationsCount); var result = client.Jmp(new Arguments() { ID = clientID, Input = input }).Value; calcIO.WriteLine($"[#{++operationsCount}] = {result}"); return(true); }, }; }
public ProcessorStorageFilesWork(IMathBuffer maths, ICalcIO calcIO, IOperationsHistory operationsHistory, IExpressionParser mathExpressionParser, IPathReader filePathReader, ICalcInputParser inputParser) { Maths = maths; CalcIO = calcIO; OperationsHistory = operationsHistory; MathExpressionParser = mathExpressionParser; FilePathReader = filePathReader; InputParser = inputParser; }
public bool Run(IProcessorStorage storage) { ICalcIO calcIO = storage.CalcIO; IMathBuffer mathBuffer = storage.Maths; mathBuffer.TempValue = storage.InputParser.ReadDouble(); mathBuffer.AccValue += mathBuffer.TempValue; mathBuffer.SaveAccValue(); return(true); }
public bool Run(IProcessorStorage storage) { ICalcIO calcIO = storage.CalcIO; IMathBuffer mathBuffer = storage.Maths; int input = storage.InputParser.ReadInt(x => (x > 0 && x <= mathBuffer.Values.Count)); mathBuffer.TempValue = input; mathBuffer.AccValue = mathBuffer.Values[input - 1]; mathBuffer.SaveAccValue(); return(true); }
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); }
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 bool Run(IProcessorStorage storage) { ICalcIO calcIO = storage.CalcIO; IMathBuffer mathBuffer = storage.Maths; double input = storage.InputParser.ReadDouble(); while (input == 0) { calcIO.WriteLine(new DivideByZeroException().Message); input = (calcIO as ICalcInputParser).ReadDouble(); } mathBuffer.TempValue = input; mathBuffer.AccValue /= input; mathBuffer.SaveAccValue(); return(true); }
private static void Main(string[] args) { _programRunning = true; _clientCalcIO = new ConsoleCalcIO(); var ipPoint = new IPEndPoint(IPAddress.Parse(args[0]), int.Parse(args[1])); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(ipPoint); } catch (Exception e) { _clientCalcIO.WriteLine($"Can't connect to server {ipPoint}\nError: {e.Message}"); _clientCalcIO.ReadString(); return; } _networkCalcIO = new TCPCalcIO(socket, _clientCalcIO); _readMessagesThread = new Thread(new ThreadStart(ReadServerMessages)); _readMessagesThread.Start(); while (_programRunning) { string input = _clientCalcIO.ReadString().ToLower(); if (input == "q") { _programRunning = false; _networkCalcIO.Disconnect(); return; } if (_programRunning) { _networkCalcIO.Write(input); } } }
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); }
public bool Run(IProcessorStorage storage) { ICalcIO calcIO = storage.CalcIO; List <string> history = null; if (storage is IProcessorStorageFilesWork ext) { history = ext.OperationsHistory.Data; } else { throw new ArgumentException(); } using (var file = new StreamWriter(new PathReader().Read(calcIO), false)) { // Operations buffer history.ForEach(expression => file.WriteLine(expression)); } return(true); }
private static void Main(string[] args) { _programRunning = true; _consoleCalcIO = new ConsoleCalcIO(); _clientCalcIO = new List <TCPCalcIO>(); _clientThreads = new List <Thread>(); var ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(args[0])); _listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _consoleCalcIO.WriteLine("Server started at port " + args[0]); _listenSocket.Bind(ipPoint); _listenSocket.Listen(10); _connectThread = new Thread(new ThreadStart(ConnectionsThread)); _connectThread.Start(); while (_programRunning) { _consoleCalcIO.ReadString(); } }
public TCPCalcIO(Socket handler, ICalcIO consoleCalcIO) { Handler = handler; _consoleCalcIO = consoleCalcIO; }
public CalcInputParser(ICalcIO calcIO) { _calcIO = calcIO; }
public ProcessorStorage(IMathBuffer maths, ICalcIO calcIO, ICalcInputParser inputParser) { Maths = maths; CalcIO = calcIO; InputParser = inputParser; }