|
5 | 5 |
|
6 | 6 | namespace Demo |
7 | 7 | { |
8 | | - public delegate void Funny(); |
9 | 8 | class MainClass |
10 | 9 | { |
11 | | - public static void digest(Object del) |
12 | | - { } |
| 10 | + const string exitCommand = "exit"; |
| 11 | + static string currentLine; |
13 | 12 |
|
14 | | - |
15 | | - |
16 | | - public static void Test() |
17 | | - { |
18 | | - } |
19 | 13 | public static void Main(string[] args) |
20 | 14 | { |
21 | | - string line; |
22 | 15 | MathParser.MathParser mathParser = new MathParser.MathParser(); |
23 | 16 |
|
24 | 17 | SymbolManager symbolManager = new SymbolManager(); |
25 | 18 | symbolManager.SetTrigonometrySymbols(); |
26 | 19 |
|
27 | | - while ((line = Console.ReadLine()) != "exit") |
| 20 | + Console.WriteLine("Enter {0} to quit calculator.", exitCommand); |
| 21 | + |
| 22 | + while (ReadNextLine()) |
28 | 23 | { |
29 | 24 | try |
30 | 25 | { |
31 | | - ExpressionTree e = mathParser.Parse(line); |
32 | | - try |
33 | | - { |
34 | | - //e.Assign(); |
35 | | - } |
36 | | - catch (Exception e1) |
37 | | - { |
38 | | - Console.WriteLine("parse error: " + e1.Message); |
39 | | - } |
40 | | - |
41 | | - try |
42 | | - { |
43 | | - string detail = e.ToDebug(); |
44 | | - Console.Write(detail); |
45 | | - |
46 | | - object d = e.Evaluate(symbolManager); |
47 | | - Console.WriteLine(" = " + d); |
48 | | - } |
49 | | - catch (Exception e2) |
50 | | - { |
51 | | - Console.WriteLine("parse error: " + e2.Message); |
52 | | - } |
| 26 | + ExpressionTree expressionTree = mathParser.Parse(currentLine); |
53 | 27 |
|
| 28 | + string detail = expressionTree.ToDebug(); |
| 29 | + Value result = expressionTree.Evaluate(symbolManager); |
| 30 | + |
| 31 | + Console.WriteLine("{0} = {1}", detail, result); |
54 | 32 | } |
55 | | - catch (Exception fe) |
| 33 | + catch (Exception exception) |
56 | 34 | { |
57 | | - Console.WriteLine(fe.Message); |
| 35 | + Console.WriteLine("error: {0}", exception.Message); |
58 | 36 | } |
59 | 37 | } |
60 | 38 | } |
| 39 | + |
| 40 | + static bool ReadNextLine() |
| 41 | + { |
| 42 | + Console.Write("> "); |
| 43 | + currentLine = Console.ReadLine(); |
| 44 | + return currentLine != exitCommand; |
| 45 | + } |
61 | 46 | } |
62 | 47 | } |
0 commit comments