首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

仿 SQL 查詢分析器的 C# 計算器(十六):計算按鈕事件處理、多行運算式與選取區塊運算

     編輯:關於C語言

表單調用簡單,未設計華麗外觀與高階設定。主要代碼為「計算」按鈕 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;
        }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved