找传奇、传世资源到传世资源站!

C# 计算器(加减乘除等算法)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing 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; //表示已加"."号. } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复