您好,登錄后才能下訂單哦!
以下是我用c#寫的一個圖形化的計算器,這是關鍵代碼
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; //數據存儲區 private CalCulatorStack OptrStack; //運算符棧 private CalCulatorStack NumStack; //運算數棧 private ArrayList temp_List; //分離數據臨時變量存儲區 private string StrOptr = "+-*/()#"; //計算支持的運算符 private int[,] OptrReation = new int[7, 7]{ //存儲操作數關系 {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> /// 分離數據存儲區的運算數與運算符, /// 并將其插入到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> /// 運算函數 /// </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> /// 將字符轉換為數字 /// </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; } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。