计算器(可用于表达式的值)

时间:2013-07-07 16:46:38
【文件属性】:

文件名称:计算器(可用于表达式的值)

文件大小:920B

文件格式:SLN

更新时间:2013-07-07 16:46:38

计算器

C#程序,巧用栈的基本操作!源代码如下: public FrmCalculator() { InitializeComponent(); } private string str = ""; private void FrmCalculator_Load(object sender, EventArgs e) { txtCalculate.Text = ""; } /* private void btn1_Click(object sender, EventArgs e) { str += "7"; txtCalculate.Text = str.ToString(); // txtCalculate.Text += Convert.ToString(7); } private void btn2_Click(object sender, EventArgs e) { str += "8"; txtCalculate.Text = str.ToString(); // txtCalculate.Text += Convert.ToString(8); } private void btn3_Click(object sender, EventArgs e) { str += "9"; txtCalculate.Text = str.ToString(); // txtCalculate.Text += "9"; } private void btn5_Click(object sender, EventArgs e) { str += "4"; txtCalculate.Text = str.ToString(); // txtCalculate.Text += "4"; } private void btn6_Click(object sender, EventArgs e) { str += "5"; txtCalculate.Text = str.ToString(); // txtCalculate.Text += "5"; } private void btn7_Click(object sender, EventArgs e) { str += "6"; txtCalculate.Text = str.ToString(); } private void btn9_Click(object sender, EventArgs e) { str += "1"; txtCalculate.Text = str.ToString(); } private void btn10_Click(object sender, EventArgs e) { str += "2"; txtCalculate.Text = str.ToString(); } private void btn11_Click(object sender, EventArgs e) { str += "3"; txtCalculate.Text = str.ToString(); } private void tn13_Click(object sender, EventArgs e) { str += "0"; txtCalculate.Text = str.ToString(); } private void btn4_Click(object sender, EventArgs e) { str += "+"; txtCalculate.Text = str.ToString(); } private void btn8_Click(object sender, EventArgs e) { str += "-"; txtCalculate.Text = str.ToString(); } private void btn12_Click(object sender, EventArgs e) { str += "*"; txtCalculate.Text = str.ToString(); } private void btn16_Click(object sender, EventArgs e) { str += "/"; txtCalculate.Text = str.ToString(); } private void btn14_Click(object sender, EventArgs e) { str += "("; txtCalculate.Text = str.ToString(); } private void btn15_Click(object sender, EventArgs e) { str += ")"; txtCalculate.Text = str.ToString(); } */ private void button2_Click(object sender, EventArgs e) { str = ""; txtCalculate.Text = ""; //txtCalculate.SelectionStart; txtCalculate.Focus(); } private char Precede(char x, char y) { char Ch = ' '; char[] ch = { '+', '-', '*', '/', '(', ')', '#' }; for (int i = 0; i < 7; i++) { if (x != ch[i]) Ch = 'E'; if (y != ch[i]) Ch = 'E'; } if (x == '+' || x == '-') { if (y == '+' || y == '-' || y == ')' || y == '#') Ch = '>'; else Ch = '<'; } else if (x == '*' || x == '/') { if (y == '(') Ch = '<'; else Ch = '>'; } else if (x == '(') { if (y == ')') Ch = '='; else Ch = '<'; } else if (x == ')') Ch = '>'; else if (x == '#') { if (y == '#') Ch = '='; else Ch = '<'; } return Ch; } private double Operate(double a, char theta, double b) { switch (theta) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } private bool In(char ch) { bool b; if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '#') b = true; else b = false; return b; } private double Getnum(string s, ref int i) { double x = 0; double l = 0; int j = i, k; while (s[j] != '\0' && !In(s[j])) { if (s[j] == '.') { k = j; break; } else j++; } k = j; double d = 10; while (s[i] != '\0' && !In(s[i])) { if (i < k) x = x * 10 + s[i] - '0'; else if (i == k) { i++; continue; } else { l += ((s[i] - '0') / d); d *= 10; } i++; } // i--;s return x + l; } private double EvaluateExpression(string s) { s = s.Insert(0, "#"); //字符串变量的insert属性用于将一段字符串插入到指定位置 s = s.Insert(s.Length, "#"); //append属性将一段字符串插入到已知字符串的末尾 Stack OPTR = new Stack(); OPTR.Push(s[0]); Stack OPND = new Stack(); for (int i = 0; i < s.Length; i++) { while (s[i] != '#' || OPTR.Peek() != '#') { if (!In(s[i])) { // int m=s[i]-'0'; OPND.Push(Getnum(s, ref i)); // i++; } else { switch (Precede(OPTR.Peek(), s[i])) { case '<': OPTR.Push(s[i]); i++; break; case '=': OPTR.Pop(); i++; break; case '>': char theta; theta = OPTR.Peek(); OPTR.Pop(); double b = OPND.Peek(); OPND.Pop(); double a = OPND.Peek(); OPND.Pop(); double c = Operate(a, theta, b); OPND.Push(c); break; } } } } return OPND.Peek(); } private void btnEqual_Click(object sender, EventArgs e) { double d = EvaluateExpression(txtCalculate.Text); txtCalculate.AppendText("\r\n" + Convert.ToString(d)); txtCalculate.Focus(); } private void btnClose_Click(object sender, EventArgs e) { Close(); } private int n = 0; private void btn(object sender, EventArgs e) { //str += (sender as Button).Text; //txtCalculate.Text = str; n = txtCalculate.SelectionStart; str = str.Insert(n, (sender as Button).Text); txtCalculate.Text = str; n++; txtCalculate.SelectionStart = n; txtCalculate.Focus(); }


网友评论