求c#高手帮助,c#设计的计算器怎样实现先乘除后加减
比如:2+3*5 怎么让程序自动判别先乘除后加减啊?程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Calculator { public partial class Form1 : Form { //定义一个控制标志 int flag=-1; //存储第一个操作数 int num1,num2; //存储最后的结果 int results; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } //当用户按下0-9数字键时触发 private void num_Click(object sender, EventArgs e) { Button a = (Button)sender; txtOutput.Text = txtOutput.Text + a.Text; } //当用户按下运算符时触发 private void operator_Click(object sender, EventArgs e) { Button b = (Button)sender; if (b.Text == "+") { if (flag == -1) { num1 = int.Parse(txtOutput.Text); } else { switch(flag) { case 0: num1 = num1 + int.Parse(txtOutput.Text); break; case 1: num1 = num1 - int.Parse(txtOutput.Text); break; case 2: num1 = num1 *int.Parse(txtOutput.Text); break; case 3: num1 = num1 / int.Parse(txtOutput.Text); break; } }; if (num2==-1) { num1 = 0; } txtOutput.Text = ""; flag = 0; } if (b.Text == "-") { if (flag == -1) { num1 = int.Parse(txtOutput.Text); } else { switch (flag) { case 0: num1 = num1 + int.Parse(txtOutput.Text); break; case 1: num1 = num1 - int.Parse(txtOutput.Text); break; case 2: num1 = num1 * int.Parse(txtOutput.Text); break; case 3: num1 = num1 / int.Parse(txtOutput.Text); break; } }; txtOutput.Text = ""; flag = 1; } if (b.Text == "*") { if (flag == -1) { num1 = int.Parse(txtOutput.Text); } else { switch (flag) { case 0: num1 = num1 + int.Parse(txtOutput.Text); break; case 1: num1 = num1 - int.Parse(txtOutput.Text); break; case 2: num1 = num1 * int.Parse(txtOutput.Text); break; case 3: num1 = num1 / int.Parse(txtOutput.Text); break; } } txtOutput.Text = ""; flag = 2; } if (b.Text == "/") { if (flag == -1) { num1 = int.Parse(txtOutput.Text); } else { switch (flag) { case 0: num1 = num1 + int.Parse(txtOutput.Text); break; case 1: num1 = num1 - int.Parse(txtOutput.Text); break; case 2: num1 = num1 * int.Parse(txtOutput.Text); break; case 3: num1 = num1 / int.Parse(txtOutput.Text); break; } } txtOutput.Text = ""; flag = 3; } if (b.Text == "C") { txtOutput.Text = ""; num1 = 0; txtOutput.Focus(); } if (b.Text == "=") { if (flag == 0) { results = num1 + int.Parse(txtOutput.Text); flag = -1; } if (flag == 1) { results = num1 - int.Parse(txtOutput.Text); flag = -1; } if (flag == 2) { results = num1 * int.Parse(txtOutput.Text); flag = -1; } if (flag == 3) { results = num1 / int.Parse(txtOutput.Text); flag = -1; } txtOutput.Text = results.ToString(); } } private void btnDivide_Click(object sender, EventArgs e) { } } }