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();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值