winfrom计算器,使用栈实现算法
来源:互联网 发布:数控车床新代系统编程 编辑:程序博客网 时间:2024/06/05 03:53
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Calculator{ class Expression { private Stack<Char> operateStack; private Stack<Double> numStack; private StringBuilder sb; private bool isPriority = false; public bool isZore = false; public string errorMessage = null; public Expression() { operateStack = new Stack<Char>(); numStack = new Stack<Double>(); } /* 将字符串翻译成数字和操作符,并进行压栈处理 */ public double pase(string expression) { sb = new StringBuilder(expression); Console.WriteLine(sb.ToString()); while (this.sb.Length > 0) { //获取数字的整数部分 char c = sb.ToString().ElementAt(0); sb.Remove(0, 1); double num = 0; bool isExistNum = false; while (c >= '0' && c <= '9') { num = num * 10 + c - '0'; isExistNum = true; if (this.sb.Length > 0) { c = sb.ToString().ElementAt(0); sb.Remove(0, 1); } else { break; } } if (c.Equals('.')) { //获取数字的小数部分 double bit = 1; c = sb.ToString().ElementAt(0); sb.Remove(0, 1); while (c >= '0' && c <= '9') { bit = bit * 10; num = num + (c - '0') / bit; isExistNum = true; if (this.sb.Length > 0) { c = sb.ToString().ElementAt(0); sb.Remove(0, 1); } else { break; } } if (isExistNum) //如果是数字,则将其压入数字栈 { this.numStack.Push(num); //Console.WriteLine("压栈num:" + num); //Console.WriteLine(numStack.Peek()); if (isPriority) { cal(); isPriority = false; } //Console.WriteLine("压栈:" + this.numStack.ToArray().ToString()); //若整个表达式解析完成,退出 if (this.sb.Length == 0 && c >= '0' && c <= '9') break; } } else { //如果是整数,则将其压入数字栈 if (isExistNum) this.numStack.Push(num); if (isPriority) { cal(); isPriority = false; } //若整个表达式解析完成,退出 if (this.sb.Length == 0 && c >= '0' && c <= '9') break; } //判断 运算符 if (this.sb.Length > 0 && (c < '0' || c > '9')) { this.operateStack.Push(c); if (c == '*' || c == '/') { isPriority = true; } //Console.WriteLine(this.operateStack.ToString()); continue; } } while (numStack.Count > 1) { cal(); } return this.numStack.Pop(); } private void cal() { //Console.WriteLine("执行结束cal()..."); double num1 = (double)this.numStack.Pop(); double num2 = (double)this.numStack.Pop(); char op = this.operateStack.Pop(); double result = 0; //Console.WriteLine("num1:"+num1); //Console.WriteLine("num2:" + num2); switch (op) { case '+': result = num1 + num2; break; case '-': result = num2 - num1; break; case '*': result = num1 * num2; break; case '/': { if (num1 == 0) { isZore = true; errorMessage = "您输入有误,除数为零"; } result = num2 / num1; break; } } this.numStack.Push(result); } public void Clear() { this.numStack.Clear(); } public bool isEmpty() { bool b = false; if (this.numStack.Count == 0) { b = true; } return b; } }}
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;namespace Calculator{ public partial class Form1 : Form { private string str; private Expression ex; public Form1() { InitializeComponent(); ex = new Expression(); } private void Form1_Load(object sender, EventArgs e) { } private void button17_Click(object sender, EventArgs e) { if (!textBox1.Text.Equals("")) { textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1); str = textBox1.Text; } } private void button11_Click(object sender, EventArgs e) { string c = str.Substring(0,1); if (c.Equals("*") || c.Equals("/")) { MessageBox.Show("您输入的有误,请您重新输入..."); str = ""; textBox1.Text = ""; return; } double str1 = ex.pase(textBox1.Text); if (ex.isZore) { //MessageBox.Show("对不起,您输入有误。(除数不能为零)"); MessageBox.Show(ex.errorMessage.ToString()); textBox1.Text = ""; str = ""; return; } else { textBox1.Text = str1.ToString(); } textBox1.Text = ""; textBox1.Text = str1.ToString(); str = textBox1.Text; } /// <summary> /// 用于清除计算器所有输入的内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button16_Click(object sender, EventArgs e) { textBox1.Text = ""; str = ""; if (!ex.isEmpty()) { ex.Clear(); } } /// <summary> /// 功能:(只用来获取数字按钮) /// 1、获取按钮所输入数字 /// 2、将所输入数字进行连接 /// 3、将所连接成的字符串显示在TextBox中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void number(object sender, EventArgs e) { Button button = (Button)sender; str = str + button.Text; textBox1.Text = str; } }}
0 0
- winfrom计算器,使用栈实现算法
- JAVA计算器算法实现
- Winfrom 使用WCF 实现双工通讯
- Winfrom编写简单的计算器
- MFC使用双栈实现简单计算器
- 使用JSF实现计算器
- java 实现简单计算器算法
- 计算器算法的简单实现
- 使用递归下降算法分析数学表达式 -- 基于堆栈的计算器实现算法
- 使用Python实现计算器功能
- 使用状态模式实现计算器
- Python使用wxPython实现计算器
- 【栈】实现高级计算器
- 栈实现计算器
- 用栈实现计算器
- 用栈实现计算器
- 计算器(栈实现)
- 栈实现计算器
- StrictMode 详解
- PHP面向对象基础笔记三
- 【D3相关资料整理】持续更新中......
- zynq ap soc
- 允许ubuntu下mysql远程连接
- winfrom计算器,使用栈实现算法
- Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel
- hadoop集群搭建之一(Hadoop环境安装)
- STM32 可编程电压监测器(PVD)
- jquery获取select下拉框的值的集中案例
- 【c语言】编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现
- Visual Studio 2013 C++ 常用插件
- 移除某个图像的缓存
- oracle Hierarchical Queries connect by