C#作业——表达式计算器+小数+运算符优先级

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public String expression;
        public class op
        {
            public char symbol;//符号
            public int grade;//优先级
            public op(char sym)
            {
                symbol = sym;
                if ((symbol == '+') || (symbol == '-'))
                    grade = 0;
                else
                    grade = 1;
            }

            public double calculate(double p1,double p2)
            {
                if (symbol == '+')
                return p1 + p2;
                else if (symbol == '-')
                return p1 - p2;
                else if (symbol == '*')
                return p1 * p2;
                else if (symbol == '/')
                    return p1 / p2;
                else
                    return 0;
               
            }

        }

        public class datanum
        {
            public double number;
            public bool isdeleted = false;
            public datanum(double num)
            {
                number = num;
            }
            public void delete()
            {
                isdeleted = true;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            expression = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = expression + "=" + Result(expression);
        }
        private double Result(String expression)
        {
            int length = expression.Length;
            char[] a = expression.ToCharArray(0, length);
            datanum[] data = new datanum[10];
            op[] oper = new op[10];
            int datacount = 0, opercount = 0;
            double temp = 0;
            int dicimal = -1;
            double tempdi = 1;
            for (int m = 0; m < length; m++)
            {

                if (Char.IsDigit(a[m]) == true)
                {

                    if (dicimal == -1)
                    {
                        temp *= 10;
                        temp += a[m] - '0';
                    }
                    else
                    {
                        dicimal++;
                        for (int q = 0; q < dicimal; q++)
                            tempdi = tempdi * 0.1;
                        temp = temp + (a[m] - '0') * tempdi;
                        tempdi = 1;
                    }

                }
                else if (a[m] == '.')
                {
                    dicimal = 0;
                }
                else
                {
                    dicimal = -1;
                    tempdi = 1;
                    data[datacount] = new datanum(temp);
                    datacount++;
                    temp = 0;
                    oper[opercount] = new op(a[m]);
                    opercount++;
                }
            }

            data[datacount] = new datanum(temp);//最后一个数
            datacount++;

            
            double n=0;
            int temp1;
            int tempgrade = 1;
            int[] lossdata = new int[10];
            while(tempgrade>=0)
            {
            for (temp1 = 0; temp1 < opercount; temp1++)
            {
                if (oper[temp1].grade == tempgrade)
                {
                    int pa = temp1;
                    int pb = temp1 + 1;
                    while(data[pa].isdeleted==true)
                    {
                        pa--;
                    }
                     while(data[pb].isdeleted==true)
                     {
                        pb++;
                     }

                    data[pa].number=oper[temp1].calculate(data[pa].number, data[pb].number);
                    n = data[pa].number;
                    data[pb].delete();

                }

            }
            tempgrade--;
        }
            return n;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
    }
}

你可能感兴趣的:(C#)