表單調用簡單,未設計華麗外觀與高階設定。主要代碼為「計算」按鈕 Click 事件處理方法:
private void btnExecute_Click(object sender, EventArgs e)
{
if (this.rtbInput.Text.Trim().Replace("\n", "").Length == 0)
{ this.rtbOutput.Text = "輸入的運算式不能為空,請重新輸入。"; }
else
{
string strSource; int intTotalIndex = 0;
this.rtbOutput.Text = ""; string[] strLines;
this.trvSyntaxTree.Nodes.Clear();
if (this.rtbInput.SelectedText.Trim().Length == 0)
strSource = this.rtbInput.Text;
else
{ strSource = this.rtbInput.SelectedText; intTotalIndex = this.rtbInput.SelectionStart; }
if (this.chkAllowMultiLine.Checked)
strLines = strSource.Split(new char[] { '\n' });
else
strLines = new string[] { strSource.Replace("\n", "") };
foreach (string Line in strLines)
{
if (Line.Trim().Length != 0)
{
try
{
TokenRecord TokenTop = myAnalyse.Analyse(Line);
this.rtbOutput.Text += TokenTop.GetValueString() + "\n";
this.LoadSyntaxTree(TokenTop);
}
catch (Exception ex)
{
this.rtbOutput.Text += "發生錯誤\n" + ex.Message + "\n";
if (ex is SyntaxException)
{
SyntaxException myException = (SyntaxException)ex;
this.ActiveControl = this.rtbInput;
this.rtbInput.Select(myException.Index + intTotalIndex, myException.Length);
}
return;
}
}
intTotalIndex += Line.Length + 1;
}
}
}