您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 计算器(可用于表达式的值)
  所属分类: C
  开发工具:
  文件大小: 920byte
  下载次数: 0
  上传时间: 2010-06-17
  提 供 者: HelloWo********
 详细说明: 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(); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 计算器
 输入关键字,在本站1000多万海量源码库中尽情搜索: