Separated calculator logic to RPN class and repl to REPL. Added error position pointer and unfinished expression detector

This commit is contained in:
Dmitriy Shishkov 2021-08-08 21:14:53 +03:00
parent 8234e92ce1
commit 7ef2c86ce6
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060

View File

@ -7,31 +7,58 @@ namespace calculator
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
if (args.Length == 0) if (args.Length == 0)
{ {
string input = readREPL(); string input = REPL.read();
while (input != null) while (input != null)
{ {
writeREPL(evaluate(input)); handleExpression(input);
input = readREPL(); input = REPL.read();
} }
} }
else else
{ handleExpression(args[0]);
writeREPL(evaluate(args[0]));
}
} }
static private double evaluate(string s) static private void handleExpression(string s)
{ {
try try
{ {
string[] expr = s.Split(); RPN calc = new RPN(s);
Stack<double> st = new Stack<double>(); REPL.write(calc.evaluate());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
for (int i = 0; i < expr.Length; i++) class RPN
{
Stack<double> st = new Stack<double>();
string[] tokens;
public RPN(string s)
{
tokens = s.Split();
}
public RPN(string[] ss)
{
tokens = new string[ss.Length];
ss.CopyTo(tokens, 0);
}
public double evaluate()
{
for (int i = 0; i < tokens.Length; i++)
{
try
{ {
object val = convertOne(expr[i]); object val = convertOne(i);
if (val is double number) if (val is double number)
st.Push(number); st.Push(number);
@ -59,36 +86,57 @@ namespace calculator
st.Push(Math.Pow(a, b)); st.Push(Math.Pow(a, b));
break; break;
default: default:
throw new Exception("Got unknown operator"); throw new Exception("got unknown operator");
} }
} }
catch (InvalidOperationException) catch (InvalidOperationException)
{ {
throw new Exception(oper + " operation requires two operands"); throw new Exception("operator requires 2 arguments");
} }
} }
else throw new Exception("Stack corrupted");
} }
catch (Exception e)
{
throw new Exception(positionPointer(i, 2) + ' ' + e.Message);
}
}
return st.Pop(); if (st.Count != 1) throw new Exception("Expression is not finished");
}
catch (Exception e) return st.Pop();
{
Console.WriteLine(e.Message);
Environment.Exit(1);
return -1;
}
} }
static private object convertOne(string s) private string positionPointer(int pos, int gap)
{ {
string prepend = "";
for (int i = 1; i <= gap && pos - i >= 0; i++)
prepend = tokens[pos - i] + ' ' + prepend;
if (pos > gap) prepend = "... " + prepend;
string append = " ";
for (int i = 1; i <= gap && pos + i < tokens.Length; i++)
append += tokens[pos + i] + ' ';
if (pos < tokens.Length - 2) append += "...";
string arrow = "^".PadLeft(prepend.Length + 1);
return prepend + tokens[pos] + append + '\n' + arrow;
}
private object convertOne(int i)
{
string s = tokens[i];
double res; double res;
if (Double.TryParse(s, out res)) return res; if (Double.TryParse(s, out res)) return res;
else if (s.Length == 1) return s[0]; else if (s.Length == 1) return s[0];
else throw new Exception("Got multiple characters operator"); else throw new Exception("got multiple characters operator");
} }
}
static private string readREPL() class REPL
{
static public string read()
{ {
Console.Write(">> "); Console.Write(">> ");
string input = Console.ReadLine(); string input = Console.ReadLine();
@ -107,7 +155,7 @@ namespace calculator
} }
} }
static private void writeREPL<T>(T o) static public void write<T>(T o)
{ {
Console.WriteLine(o); Console.WriteLine(o);
} }