日期:2014-05-18 浏览次数:21069 次
// Calc.cs - 表达式计算器
// 编译方法: csc /t:winexe Calc.cs VBExpression.cs
using System;
using System.Windows.Forms;
using Skyiv.Util;
namespace Skyiv
{
class Calc : Form
{
TextBox tbxA1, tbxA2, tbxA3;
Calc()
{
Text = "表达式计算器";
StartPosition = FormStartPosition.CenterScreen;
Width = 400;
Height = 200;
Label lblA1 = new Label();
lblA1.Text = "表达式(&E)";
lblA1.Parent = this;
lblA1.Top = 23;
lblA1.Left = 10;
lblA1.AutoSize = true;
tbxA1 = new TextBox();
tbxA1.Parent = this;
tbxA1.Top = 20;
tbxA1.Left = 80;
tbxA1.Width = 300;
tbxA1.BorderStyle = BorderStyle.FixedSingle;
Label lblA2 = new Label();
lblA2.Text = "自变量(&X)";
lblA2.Parent = this;
lblA2.Top = 48;
lblA2.Left = 10;
lblA2.AutoSize = true;
tbxA2 = new TextBox();
tbxA2.Parent = this;
tbxA2.Top = 45;
tbxA2.Left = 80;
tbxA2.Width = 300;
tbxA2.BorderStyle = BorderStyle.FixedSingle;
Button btnA3 = new Button();
btnA3.Text = "计算(&C)";
btnA3.Parent = this;
btnA3.Top = 70;
btnA3.Left = 10;
btnA3.Width = 62;
btnA3.Click += new EventHandler(Calc_Clicked);
tbxA3 = new TextBox();
tbxA3.Parent = this;
tbxA3.Top = 70;
tbxA3.Left = 80;
tbxA3.Width = 300;
tbxA3.BorderStyle = BorderStyle.FixedSingle;
tbxA3.ReadOnly = true;
TextBox tbxA4 = new TextBox();
tbxA4.Text = @"
表达式使用 Visual Baisc 语法,可带一个的自变量(x)
可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数
例子:x * cos(x * pi / sqrt(25 * 6^4)) + log(E^10)";
tbxA4.Parent = this;
tbxA4.Top = 95;
tbxA4.Left = 10;
tbxA4.Width = 370;
tbxA4.Height = 65;
tbxA4.BorderStyle = BorderStyle.None;
tbxA4.Multiline = true;
tbxA4.ReadOnly = true;
}
void Calc_Clicked(object sender, EventArgs ea)
{
(sender as Control).Enabled = false;
try
{
double x = 0;
if (tbxA2.Text.Trim().Length != 0)
{
try
{
x = double.Parse(tbxA2.Text);
}
catch
{
try