using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; namespace calcluation { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { Calculator calc = new Calculator(this.textBox1.Text.Replace(" ", "")); calc.setvariables(textBox4.Text.ToString(), textBox5.Text.ToString(), textBox6.Text.ToString()); this.textBox2.Text = calc.PostFix(); } catch (Expressception ex) { MessageBox.Show(ex.Message); } } public class Expressception : Exception { public Expressception(string msg): base(msg) { } } public class Calculator { private string _expression; private Stack s; private string[] functionString = { "max", "min", "sum" }; private Queue variables; private int i = 0;//变量游标 //构造函数 public Calculator(string expression) { this._expression = expression; s = new Stack(); } public void setvariables(string a, string b, string c) { variables = new Queue(); variables.Enqueue(a); variables.Enqueue(b); variables.Enqueue(c); } //计算函数 public double Run() { // string[] functionString = { "max", "min","sum" }; string expression = PostFix(); //"10|20|max|3|5|min|-"; bool flag = true; ; string[] aryString = expression.Split('|'); foreach (string str in aryString) { for (int i = 0; i < functionString.Length; i++) { if (str.Equals(functionString[i])) { DoOperator(str); flag = false; break; } else flag = true; } if (flag == true) { if(char.IsNumber(str,0)) // if (IsNumber(str)) { double d = Convert.ToDouble(str.ToString()); AddOperands(d); } else if (char.IsLetterOrDigit(str,0)) { double d = Convert.ToDouble(variables.Dequeue()); AddOperands(d); i++; } else { DoOperator(str); } } } return (double)s.Pop(); } private bool IsNumber(string str) { if (str.Length > 1) { return true; } else { return Char.IsDigit(str[0]); } } private void AddOperands(double val) { s.Push(val); } private bool Get2Operands(out double left, out double right) { try { right = (double)s.Pop(); left = (double)s.Pop(); } catch (InvalidOperationException) { right = 0; left = 0; return false; } return true; } private void DoOperator(string op) { double left, right; bool result = Get2Operands(out left, out right); if (result) switch (op) { case "+": s.Push(left + right); break; case "-": s.Push(left - right); break; case "*": s.Push(left * right); break; case "max": s.Push(max(left, right)); break; case "min": s.Push(min(left, right)); break; case "/": if (right == 0.0) { s.Clear(); //Divide by 0! throw new Expressception("除数不能为零"); } else s.Push(left / right); break; case "^": s.Push(Math.Pow(left, right)); break; } else s.Clear(); } public double max(double left, double right) { if (left >= right) return left; else return right; } public double min(double left, double right) { if (left >= right) return right; else return left; } public string PostFix() { string str = this._expression + "#"; string tempc; char[] chars = str.ToCharArray(); Stack ts = new Stack(); ts.Push('#'); string str1 = ""; string tmpStr = ""; bool isNum = false; List<string> tmpfun = new List<string>(); //声明 //string[] tmpfun = {""}; //bool isvar = false; int k = 0; for(int i = 0;i<chars.Length;i++) { string c = chars[i].ToString(); if (Char.IsDigit(c,0)) { tmpStr += c.ToString(); isNum = true; } else if (char.IsLetter(c,0)) { int j = i; while (char.IsLetterOrDigit(c, 0)) { tmpStr += c.ToString(); c = chars[j + 1].ToString(); ++j; } i = j-1; for (int n = 0; n < functionString.Length; ++n) { if (tmpStr.Equals(functionString[n])) { tmpfun.Add("");//动态增长 tmpfun[k] = tmpStr; k++; tmpStr = ""; isNum = false; break; } else { isNum = true; }//判断为变量而非函数名 } //tmpStr += c.ToString(); //for (int j = 0; j < functionString.Length; ++j) //{ // if (tmpStr.Equals(functionString[j])) // { // tmpfun.Add("");//动态增长 // tmpfun[k] = tmpStr; // k++; // tmpStr = ""; // } // if (j == functionString.Length - 1&&tmpStr != "") { isNum = true; }//判断为变量而非函数名 //} } else { if (isNum) { str1 += tmpStr + "|"; tmpStr = ""; } isNum = false; if (c == ")") { for (tempc = Convert.ToString(ts.Pop()); tempc != "("; tempc = Convert.ToString(ts.Pop())) str1 += tempc.ToString() + "|"; k--; } else { for (tempc = Convert.ToString(ts.Pop()); Isp(Convert.ToString(tempc)) > Icp(Convert.ToString(c)); tempc = Convert.ToString(ts.Pop())) str1 += tempc.ToString() + "|"; ts.Push(tempc); if (c == ",") { ts.Push(tmpfun[k-1]); } else { ts.Push(c); } } } } return str1.Substring(0, str1.Length - 1); } private int Isp(string c) { int k; switch (c) { case "#": k = -3; break; case ",": k = 2; break; case "(": k = 0; break; case "^": k = 8; break; case "*": case "/": case "%": k = 6; break; case "+": case "-": k = 4; break; case ")": k = 10; break; case "max": k = 3; break; case "min": k = 3; break; default: //Unknown operator! throw new Expressception("不知道的操作符!"); } return k; } private int Icp(string c) { int k; switch (c) { case "#": k = -3; break; case ",": k = 1; break; case "(": k = 10; break; case "^": k = 7; break; case "*": case "/": case "%": k = 5; break; case "+": case "-": k = 3; break; case ")": k = 0; break; case "max": k = 2; break; case "min": k = 2; break; default: //Unknown operator! throw new Expressception("不知道的操作符!"); } return k; } } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { try { Calculator calc = new Calculator(this.textBox1.Text.Replace(" ", "")); calc.setvariables(textBox4.Text.ToString(), textBox5.Text.ToString(), textBox6.Text.ToString()); this.textBox3.Text = calc.Run().ToString(); } catch (Expressception ex) { MessageBox.Show(ex.Message); } } } }