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
原创粉丝点击