您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
以下是我用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。