-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
55 lines (51 loc) · 1.31 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
namespace Calculator
{
class MainClass
{
public static void Main (string[] args)
{
while (true) {
Console.Write (">>> ");
string expression = Console.ReadLine ();
try {
ShuntingYard parser = new ShuntingYard (Lexer.Tokenize (expression));
Queue<Token> postfixNotation = parser.PostfixTokens;
Evaluator evaluator = new Evaluator (postfixNotation);
Console.WriteLine (evaluator.Evaluate ());
} catch (Exception) {
}
}
}
public static void TestCases()
{
string[] expressions = {
"min(((-((42))) * sin( pi/2 + pi))*2,64)",
"sin(pi/2) +4",
"+(42)",
"3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3",
"4 + 2",
"3.1415 * -3.1415",
"sin ( 1.570795 )",
"sin ( abs ( -4 * 3 ^ 2 ) + 1 )",
"sin ( pi / 2 )",
"min ( pi , pi / 2 )",
"max ( pi , pi / 2 )",
"3*2+42.14-3",
"sin(pi/2) * 3 +2",
"-(42) * 2",
"avg(2, 3)",
"sqrt(9)",
"vavg(4, 3, 2, 6, 4) + 100",
"cos(pi/2)"
};
foreach (string expression in expressions) {
ShuntingYard parser = new ShuntingYard (Lexer.Tokenize (expression));
Queue<Token> postfixNotation = parser.PostfixTokens;
Evaluator evaluator = new Evaluator (postfixNotation);
Console.WriteLine (evaluator.Evaluate ());
}
}
}
}