-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfrmTest.cs
More file actions
120 lines (110 loc) · 4.01 KB
/
frmTest.cs
File metadata and controls
120 lines (110 loc) · 4.01 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
都昌数值表达式引擎 DCSoft.Expression
南京都昌信息科技有限公司 2018年 版权所有
公司网址 http://www.dcwriter.cn
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace DCSoft.Expression
{
/// <summary>
/// 测试功能窗口
/// </summary>
[System.Runtime.InteropServices.ComVisible(false)]
public partial class frmTest : Form
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmTest());
}
}
public frmTest()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cboExpression.Items.Add("SIN(99+123)");
cboExpression.Items.Add("A*99.1+MAX(-11,-10)");
cboExpression.Items.Add("FIND('7048dcb034d94ce6bfd750c3f1672096',[zz])>=0");
cboExpression.Items.Add("SUM(A1:B3)");
cboExpression.Items.Add("[A1]-[B2]");
cboExpression.Items.Add("A+B+C+在三+是+213");
cboExpression.Items.Add("A+B*(C+D*(E-F))-999");
cboExpression.Items.Add("-A+B+C+D");
cboExpression.Items.Add("A+B*C-D");
cboExpression.Items.Add("10+8>-9");
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
DCTokenList tokens = new DCTokenList(cboExpression.Text);
StringBuilder result = new StringBuilder();
for (int iCount = 0; iCount < tokens.Count; iCount++)
{
result.AppendLine(iCount.ToString("00") + ":" + tokens[iCount].ToString());
}
DCExpression exp = new DCExpression(cboExpression.Text);
result.AppendLine(exp.ToDebugString());
MyContext c = new MyContext();
object vresult = exp.Eval(c);
result.AppendLine("运算结果:" + vresult);
txtResult.Text = result.ToString();
}
private class MyContext : IDCExpressionContext
{
public object ExecuteFunction(string name, object[] parameters)
{
name = name.ToUpper();
try
{
switch (name)
{
case "SIN": return Math.Sin(Convert.ToDouble(parameters[0]));
case "MAX":
return Math.Max(
Convert.ToDouble(parameters[0]),
Convert.ToDouble(parameters[1]));
default:
MessageBox.Show("不支持的函数" + name);
return 0;
}
}
catch (System.Exception ext)
{
MessageBox.Show("执行函数" + name + " 错误:" + ext.Message);
return 0;
}
}
public object GetVariableValue(string name)
{
name = name.ToUpper();
switch (name)
{
case "A": return 1.5;
case "B": return 2.3;
case "C": return -99;
case "D": return 998;
case "E": return 123.5;
case "F": return 3.1415;
default:
MessageBox.Show("不支持的参数名:" + name);
return 0;
}
}
}
}
}