C#实现计算器-创新互联
以下是我用c#写的一个图形化的计算器,这是关键代码
成都创新互联专注于企业营销型网站、网站重做改版、灵石网站定制设计、自适应品牌网站建设、H5建站、商城开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为灵石等各大城市提供网站开发制作服务。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; } ////// ButtonOne点击事件 /// /// /// private void ButtonOneClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "1"; textBoxView.Text = temp_textBoxView; } ////// ButtonTwo点击事件 /// /// /// private void ButtonTwoClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "2"; textBoxView.Text = temp_textBoxView; } ////// ButtonThree点击事件 /// /// /// private void ButtonThreeClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "3"; textBoxView.Text = temp_textBoxView; } ////// ButtonFour点击事件 /// /// /// private void ButtonFourClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "4"; textBoxView.Text = temp_textBoxView; } ////// ButtonFive点击事件 /// /// /// private void ButtonFiveClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "5"; textBoxView.Text = temp_textBoxView; } ////// ButtonSix点击事件 /// /// /// private void ButtonSixClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "6"; textBoxView.Text = temp_textBoxView; } ////// ButtonSeven点击事件 /// /// /// private void ButtonSevenClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "7"; textBoxView.Text = temp_textBoxView; } ////// ButtonEight点击事件 /// /// /// private void ButtonEightClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "8"; textBoxView.Text = temp_textBoxView; } ////// ButtonNine点击事件 /// /// /// private void ButtonNineClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "9"; textBoxView.Text = temp_textBoxView; } ////// ButtonZero点击事件 /// /// /// private void ButtonZeroClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "0"; textBoxView.Text = temp_textBoxView; } ////// PriorBracketButton点击事件 /// /// /// private void PriorBracketButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "("; textBoxView.Text = temp_textBoxView; } ////// NextBracketButton点击事件 /// /// /// private void NextBracketButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += ")"; textBoxView.Text = temp_textBoxView; } ////// 加号点击事件 /// /// /// private void AddButtonClick(object sender,EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "+"; textBoxView.Text = temp_textBoxView; } ////// 减号点击事件 /// /// /// private void SubButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "-"; textBoxView.Text = temp_textBoxView; } ////// 乘号点击事件 /// /// /// private void MulButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "*"; textBoxView.Text = temp_textBoxView; } ////// 除号点击事件 /// /// /// private void DivButtonClick(object sender, EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView += "/"; textBoxView.Text = temp_textBoxView; } ////// 清除点击事件 /// /// /// private void ClrButtonClick(object sender,EventArgs e) { textBoxView.Text = string.Empty; temp_textBoxView = string.Empty; } ////// 退格点击事件 /// /// /// 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; } } ////// 求值事件 /// /// /// 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(); } } ////// 加载事件 /// /// /// 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); } ////// 分离数据存储区的运算数与运算符, /// 并将其插入到temp_list等待下一步计算 /// 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; } } } ////// 判断元素是否为操作符 /// /// ///private bool IsOptr(char temp) { if (temp == '+' || temp == '-' || temp == '*' || temp == '/' || temp == '(' || temp == ')'||temp=='#') return true; else return false; } /// /// 比较两个运算符的大小 /// /// /// ///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; } /// /// 运算函数 /// /// /// /// 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; } ////// 将字符转换为数字 /// /// ///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; } } }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:C#实现计算器-创新互联
文章链接:http://lswzjz.com/article/ccsidc.html