From 8234e92ce19cd24a96dac78fd43d31dc3cfd5bd8 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sun, 8 Aug 2021 19:03:15 +0300 Subject: [PATCH] Separated expression string processing to its own method and created basic REPL --- Program.cs | 130 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 45 deletions(-) diff --git a/Program.cs b/Program.cs index b8d1a4f..f946490 100644 --- a/Program.cs +++ b/Program.cs @@ -8,60 +8,76 @@ namespace calculator static void Main(string[] args) { if (args.Length == 0) - Console.WriteLine("REPL is not ready yet. Please, provide mathematic expression as an argument"); - else - try + { + string input = readREPL(); + while (input != null) { - string[] expr = args[0].Split(); - Stack st = new Stack(); + writeREPL(evaluate(input)); + input = readREPL(); + } + } + else + { + writeREPL(evaluate(args[0])); + } + } - for (int i = 0; i < expr.Length; i++) + static private double evaluate(string s) + { + try + { + string[] expr = s.Split(); + Stack st = new Stack(); + + for (int i = 0; i < expr.Length; i++) + { + object val = convertOne(expr[i]); + + if (val is double number) + st.Push(number); + else if (val is char oper) { - object val = convertOne(expr[i]); - - if (val is double number) - st.Push(number); - else if (val is char oper) + try { - try + double b = st.Pop(); + double a = st.Pop(); + switch (oper) { - double b = st.Pop(); - double a = st.Pop(); - switch (oper) - { - case '+': - st.Push(a + b); - break; - case '-': - st.Push(a - b); - break; - case '*': - st.Push(a * b); - break; - case '/': - st.Push(a / b); - break; - case '^': - st.Push(Math.Pow(a, b)); - break; - default: - throw new Exception("Got unknown operator"); - } - } - catch (InvalidOperationException) - { - throw new Exception(oper + " operation requires two operands"); + case '+': + st.Push(a + b); + break; + case '-': + st.Push(a - b); + break; + case '*': + st.Push(a * b); + break; + case '/': + st.Push(a / b); + break; + case '^': + st.Push(Math.Pow(a, b)); + break; + default: + throw new Exception("Got unknown operator"); } } - else throw new Exception("Stack corrupted"); + catch (InvalidOperationException) + { + throw new Exception(oper + " operation requires two operands"); + } } + else throw new Exception("Stack corrupted"); + } - Console.WriteLine(st.Pop()); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - } + return st.Pop(); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + Environment.Exit(1); + return -1; + } } static private object convertOne(string s) @@ -71,5 +87,29 @@ namespace calculator else if (s.Length == 1) return s[0]; else throw new Exception("Got multiple characters operator"); } + + static private string readREPL() + { + Console.Write(">> "); + string input = Console.ReadLine(); + + switch (input) + { + case "exit": + case "exit()": + case "quit": + case "quit()": + case "q": + case "": + return null; + default: + return input; + } + } + + static private void writeREPL(T o) + { + Console.WriteLine(o); + } } }