以下是我用c#寫的一個圖形化的計算器,這是關(guān)鍵代碼
成都創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、靈石網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城開發(fā)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為靈石等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Collections; namespace Calculator { public partial class CalCulator : Form { private enum OptrNum //枚舉類型定義,方便比較運算符的大小 { LessThan, //小于 Equal, //等于 GreaterThan, //大于 Error //錯誤 }; private string temp_textBoxView; //數(shù)據(jù)存儲區(qū) private CalCulatorStack OptrStack; //運算符棧 private CalCulatorStack NumStack; //運算數(shù)棧 private ArrayList temp_List; //分離數(shù)據(jù)臨時變量存儲區(qū) private string StrOptr = "+-*/()#"; //計算支持的運算符 private int[,] OptrReation = new int[7, 7]{ //存儲操作數(shù)關(guān)系 {1,1,-1,-1,-1,1,1}, // + {1,1,-1,-1,-1,1,1}, // - {1,1,1,1,-1,1,1}, // * {1,1,1,1,-1,1,1}, // / {-1,-1,-1,-1,-1,0,2}, // ( {1,1,1,1,2,1,1}, // ) {-1,-1,-1,-1,-1,2,0}}; // # public CalCulator() { this.StartPosition = FormStartPosition.CenterScreen; OptrStack = new CalCulatorStack(); NumStack = new CalCulatorStack(); temp_List = new ArrayList(); InitializeComponent(); temp_textBoxView = string.Empty; } /// <summary> /// ButtonOne點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonOneClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "1"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonTwo點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonTwoClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "2"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonThree點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonThreeClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "3"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonFour點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonFourClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "4"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonFive點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonFiveClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "5"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonSix點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonSixClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "6"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonSeven點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonSevenClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "7"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonEight點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonEightClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "8"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonNine點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonNineClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "9"; textBoxView.Text = temp_textBoxView; } /// <summary> /// ButtonZero點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonZeroClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "0"; textBoxView.Text = temp_textBoxView; } /// <summary> /// PriorBracketButton點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PriorBracketButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "("; textBoxView.Text = temp_textBoxView; } /// <summary> /// NextBracketButton點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NextBracketButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += ")"; textBoxView.Text = temp_textBoxView; } /// <summary> /// 加號點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddButtonClick(object sender,EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "+"; textBoxView.Text = temp_textBoxView; } /// <summary> /// 減號點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SubButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "-"; textBoxView.Text = temp_textBoxView; } /// <summary> /// 乘號點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MulButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "*"; textBoxView.Text = temp_textBoxView; } /// <summary> /// 除號點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DivButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "/"; textBoxView.Text = temp_textBoxView; } /// <summary> /// 清除點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClrButtonClick(object sender,EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView = string.Empty; } /// <summary> /// 退格點擊事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DelButtonClick(object sender,EventArgs e) { if (temp_textBoxView == string.Empty) return; else { string temp; temp = temp_textBoxView.Substring(0, temp_textBoxView.Length - 1); temp_textBoxView = temp; textBoxView.Text = temp_textBoxView; } } /// <summary> /// 求值事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EuqButtonClick(object sender, EventArgs e) { if (temp_textBoxView == string.Empty) return; else { InitTempList(); OptrStack.Push(temp_List[0]); temp_List.RemoveAt(0); object temp; temp = temp_List[0]; temp_List.RemoveAt(0); while (Convert.ToChar(Convert.ToInt32(temp)) != '#' || Convert.ToChar(OptrStack.GetTop()) != '#') { if (IsOptr(Convert.ToChar(Convert.ToInt32(temp))) == false) { NumStack.Push(Convert.ToDouble(temp)); temp = temp_List[0]; temp_List.RemoveAt(0); } else { switch (Precede(Convert.ToChar(OptrStack.GetTop()), Convert.ToChar(temp))) { case OptrNum.LessThan: OptrStack.Push(Convert.ToChar(temp)); temp = temp_List[0]; temp_List.RemoveAt(0); break; case OptrNum.Equal: OptrStack.Pop(); temp = temp_List[0]; temp_List.RemoveAt(0); break; case OptrNum.GreaterThan: char temp_optr; double a, b,c; temp_optr = Convert.ToChar(OptrStack.Pop()); b = Convert.ToDouble(NumStack.Pop()); a = Convert.ToDouble((NumStack.Pop())); c = Operate(a, temp_optr, b); NumStack.Push(c); break; case OptrNum.Error: MessageBox.Show(this, "運算錯誤,請檢查輸入是否正確!"); break; } } } textBoxView.Text = Convert.ToString(NumStack.Pop()); temp_textBoxView = string.Empty; temp_List.Clear(); OptrStack.Clear(); NumStack.Clear(); } } /// <summary> /// 加載事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmLoad(object sender, EventArgs e) { buttonOne.Click += new EventHandler(ButtonOneClick); buttonTwo.Click += new EventHandler(ButtonTwoClick); buttonThree.Click += new EventHandler(ButtonThreeClick); buttonFour.Click += new EventHandler(ButtonFourClick); buttonFive.Click += new EventHandler(ButtonFiveClick); buttonSix.Click += new EventHandler(ButtonSixClick); buttonSeven.Click += new EventHandler(ButtonSevenClick); buttonEight.Click += new EventHandler(ButtonEightClick); buttonNine.Click += new EventHandler(ButtonNineClick); buttonZero.Click += new EventHandler(ButtonZeroClick); PriorBracketButton.Click += new EventHandler(PriorBracketButtonClick); NextBracketButton.Click += new EventHandler(NextBracketButtonClick); AddButton.Click += new EventHandler(AddButtonClick); SubButton.Click += new EventHandler(SubButtonClick); MulButton.Click += new EventHandler(MulButtonClick); DivButton.Click += new EventHandler(DivButtonClick); ClrButton.Click += new EventHandler(ClrButtonClick); DelButton.Click += new EventHandler(DelButtonClick); EuqButton.Click += new EventHandler(EuqButtonClick); } /// <summary> /// 分離數(shù)據(jù)存儲區(qū)的運算數(shù)與運算符, /// 并將其插入到temp_list等待下一步計算 /// </summary> private void InitTempList() { temp_textBoxView += "#"; char[] Temp = temp_textBoxView.ToCharArray(); double Num = 0, COUNT = 10; int i; temp_List.Add('#'); for (i = 0; i < Temp.Length; i++) { if (IsOptr(Temp[i]) == true) { if (Num != 0) { temp_List.Add(Num); Num = 0; } temp_List.Add(Temp[i]); } else { Num = ConvertToInt32(Temp[i]) + Num * COUNT; } } } /// <summary> /// 判斷元素是否為操作符 /// </summary> /// <param name="temp"></param> /// <returns></returns> private bool IsOptr(char temp) { if (temp == '+' || temp == '-' || temp == '*' || temp == '/' || temp == '(' || temp == ')'||temp=='#') return true; else return false; } /// <summary> /// 比較兩個運算符的大小 /// </summary> /// <param name="prior_Optr"></param> /// <param name="next_Optr"></param> /// <returns></returns> private OptrNum Precede(char prior_Optr,char next_Optr) { OptrNum optr_num=OptrNum.Error; int i = StrOptr.IndexOf(prior_Optr); int j = StrOptr.IndexOf(next_Optr); if (OptrReation[i, j] == 1) optr_num = OptrNum.GreaterThan; if (OptrReation[i, j] == -1) optr_num = OptrNum.LessThan; if (OptrReation[i, j] == 0) optr_num = OptrNum.Equal; if (OptrReation[i, j] == 2) optr_num = OptrNum.Error; return optr_num; } /// <summary> /// 運算函數(shù) /// </summary> /// <param name="a"></param> /// <param name="Optr"></param> /// <param name="b"></param> private double Operate(double a,char Optr,double b) { if (Optr == '+') return a + b; if (Optr == '-') return a - b; if (Optr == '*') return a * b; if (Optr == '/') return a / b; else return 0; } /// <summary> /// 將字符轉(zhuǎn)換為數(shù)字 /// </summary> /// <param name="temp"></param> /// <returns></returns> private int ConvertToInt32(char temp) { if (temp == '0') return 0; if (temp == '1') return 1; if (temp == '2') return 2; if (temp == '3') return 3; if (temp == '4') return 4; if (temp == '5') return 5; if (temp == '6') return 6; if (temp == '7') return 7; if (temp == '8') return 8; if (temp == '9') return 9; else return 0; } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:C#實現(xiàn)計算器-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://jinyejixie.com/article12/ccsidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站策劃、云服務(wù)器、小程序開發(fā)、軟件開發(fā)、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容