实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Collections; namespace SeriesOfCalculationCalculator { public partial class FrmSeriesOfCalculationCalculator : Form { private Operation oper; //操作类型. private Operands operandsStored; //存储操作数容器. private Operators operators; //存储操作符容器. private bool hasOperator = false; //标志是否最后字符为"操作符". private bool isCalculate = false; //标志是否已计算结果. private bool hasPoint = false; //标志是否已加"."号. public FrmSeriesOfCalculationCalculator() { InitializeComponent(); } //插入操作符. public void InsertOperator(string strOperator) { //已有操作符. if (hasOperator) //替换末尾的操作符. txtShow.Text = txtShow.Text.Substring(0, txtShow.Text.Length - 1) strOperator; else txtShow.Text = txtShow.Text.Insert(txtShow.Text.Length, strOperator); //在末尾添加操作符. hasOperator = true; //标志此时有未纳入的操作符. } //判断是否有非法值. public double[] GetDoubles(string[] strInputs) { double[] douOut = new double[strInputs.Length]; //存储转换后的值. bool hasInvalid = false; //标志是否有非法值. for (int i = 0; i < strInputs.Length; i ) { if (!double.TryParse(strInputs[i], out douOut[i])) { hasInvalid = true; //标志有非法值. break; } } if (hasInvalid) //有非法值时返回空. douOut = null; return douOut; } /// <summary> /// 计算两个栈中操作数的运算. /// </summary> /// <param name="operOperand">存储操作数容器"2".</param> /// <param name="operStored">存储操作数容器.</param> /// <param name="operOperator">存储操作符容器.</param> /// <returns>容器操作数的运算结果.</returns> private double CalculateStackOperands(Operands operOperand, Operators operOperator) { double result = 0; //临时求和结果. int count = operOperator.CountAll; //操作符容器长度. for (int i = 0; i < count; i ) //从后往前运算. result = OperationFactory.GetOperation(operOperator.PopOperator()).Operate(result, operOperand.PopOperand()); return result operOperand.PopOperand(); //操作数容器还有一个数. } //设置追加或重置文本. private void SetNumber(string strInput) { if (txtShow.Text.Trim().Equals("0") || isCalculate) txtShow.Text = strInput; //重置文本. else txtShow.Text = strInput; //追加文本. hasOperator = false; //标志此时操作符已被纳入. isCalculate = false; //标志此时未计算结果. } //"清零"键. private void btn_clear_Click(object sender, EventArgs e) { txtShow.Text = "0"; isCalculate = false; //标志没有可运算. hasOperator = false; //标志没有操作符. hasPoint = false; //标志没有"."号. } //数字键"0". private void btn_0_Click(object sender, EventArgs e) { SetNumber(this.btn_0.Text); } //数字键"1". private void btn_1_Click(object sender, EventArgs e) { SetNumber(this.btn_1.Text); } //数字键"2". private void btn_2_Click(object sender, EventArgs e) { SetNumber(this.btn_2.Text); } //数字键"3". private void btn_3_Click(object sender, EventArgs e) { SetNumber(this.btn_3.Text); } //数字键"4". private void btn_4_Click(object sender, EventArgs e) { SetNumber(this.btn_4.Text); } //数字键"5". private void btn_5_Click(object sender, EventArgs e) { SetNumber(this.btn_5.Text); } //数字键"6". private void btn_6_Click(object sender, EventArgs e) { SetNumber(this.btn_6.Text); } //数字键"7". private void btn_7_Click(object sender, EventArgs e) { SetNumber(this.btn_7.Text); } //数字键"8". private void btn_8_Click(object sender, EventArgs e) { SetNumber(this.btn_8.Text); } //数字键"9". private void btn_9_Click(object sender, EventArgs e) { SetNumber(this.btn_9.Text); } private void btn_plus_minus_Click(object sender, EventArgs e) { } //逐一清除. private void btn_backspace_Click(object sender, EventArgs e) { //长度大于1. string textInput = txtShow.Text.Trim(); //输入框的文本. if (1 < textInput.Length) { if (textInput.EndsWith(".")) hasPoint = false; //表示已删"."号. txtShow.Text = txtShow.Text.Substring(0, txtShow.Text.Trim().Length - 1); } else if (1 == textInput.Length) { //只有一位数. txtShow.Text = "0"; hasPoint = false; //标志无"."号可用. } } private void Form1_Load(object sender, EventArgs e) { operandsStored = new Operands(); //实例化操作数容器. operators = new Operators(); //实例化操作符容器. } //加法运算. private void btn_add_Click(object sender, EventArgs e) { InsertOperator(this.btn_add.Text); } //减法运算. private void btn_subtract_Click(object sender, EventArgs e) { InsertOperator(this.btn_subtract.Text); } //乘法运算. private void btn_multiply_Click(object sender, EventArgs e) { InsertOperator(this.btn_multiply.Text); } //除法运算. private void btn_divide_Click(object sender, EventArgs e) { InsertOperator(this.btn_divide.Text); } //计算结果. private void btn_calculate_Click(object sender, EventArgs e) { try { string[] strSplited = new Regex(@"(\ |\-|\*|\/|%)").Split(txtShow.Text.Trim().TrimEnd(' ', '-', '*', '/')); //分割待计算表达式. string[] strOperators = new string[strSplited.Length / 2]; //总长度除2,取整为操作符的个数. string[] strOperands = new string[strSplited.Length - strOperators.Length]; //总长度减操作符长度为操作数的个数. int operatorIndex = 0; //记录存储字符型"操作符"的下标. int operandIndex = 0; //记录存储字符型"操作数"的下标. for (int i = 0; i < strSplited.Length; i ) { if (i % 2 == 0) strOperands[operandIndex ] = strSplited[i]; //偶数下标为操作数,其他为操作符. else strOperators[operatorIndex ] = strSplited[i]; } //无非法操作数. if (null != GetDoubles(strOperands)) { double[] douOprands = GetDoubles(strOperands); //所有的操作数数组. operandsStored.PushOperand(douOprands[0]); //操作数栈先存储第一个数. for (int i = 0; i < strOperators.Length; i ) { //遍历长度为操作数的个数. oper = OperationFactory.GetOperation(strOperators[i]); //操作类型. string OperationTypeName = oper.GetType().Name; //操作类型姓名. //操作类型不是" "和"减". if (OperationTypeName != "OperationAdd" && OperationTypeName != "OperationSubtract") { //如果是高级操作符,将刚导入操作数栈的数Pop,并与下一个操作数计算再Push到栈中. operandsStored.PushOperand(oper.Operate(operandsStored.PopOperand(), douOprands[i 1])); } else { operators.PushOperator(strOperators[i]); //操作符压栈. operandsStored.PushOperand(douOprands[i 1]); //操作数直接压栈. } } isCalculate = true; txtShow.Text = Convert.ToString(CalculateStackOperands(operandsStored, operators)); } else { //有非法操作数. txtShow.Text = "无穷大!"; } } catch (System.Exception ex) { txtShow.Text = ex.Message; } finally { } } //取余. private void btn_mod_Click(object sender, EventArgs e) { InsertOperator(this.btn_mod.Text); } private void btn_divideByOne_Click(object sender, EventArgs e) { try { txtShow.Text = Convert.ToString(OperationFactory.GetOperation(this.btn_divideByOne.Text).Operate(1, double.Parse(txtShow.Text))); } catch (System.Exception ex) { txtShow.Text = ex.Message; } } private void btn_point_Click(object sender, EventArgs e) { if (!txtShow.Text.Trim().EndsWith(".") && !hasPoint) { txtShow.Text = "."; hasPoint = true; //表示已加"."号. } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论