C#练习:实现基本计算器功能,考虑运算符优先级

设计思路:

1 用StringBuilder类变量StrData,动态保存用户输入的数据及运算符
2 以加减运算符为分割符,对字符串进行分割,得到字符串数组StrDataArr[]
3 遍历字符串数组StrDataArr,查找其中含有乘除运算的项,对该项进行计算
4 经过乘除计算之后,数组StrDataArr存储的实际结果为加减运算符的操作数,再进行加减运算

主要方法:
StringBuilder类:动态存储数据
string.Split()方法:根据运算符优先级多次分割处理数据
string.Contains()方法:判断是否包含特定字符

Double.Parse()方法:将字符串形式的数字转换为数字(可简单理解为去掉双引号)


代码实现:

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 Frm_Main : Form
    {
        StringBuilder StrData = new StringBuilder(); //数据及运算符保存

        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            calcResult.Clear();
            calcProcess.Clear();
        }

        public void DataProcess(string data)
        {
            calcProcess.Text = calcProcess.Text + data;
            StrData.Append(data);
        }
 
        //0
        private void button16_Click(object sender, EventArgs e)
        {
            DataProcess("0");
        }
        //1
        private void button12_Click(object sender, EventArgs e)
        {
            DataProcess("1");
        }
        
        //2
        private void button11_Click(object sender, EventArgs e)
        {
            DataProcess("2");
        }
        //3
        private void button10_Click(object sender, EventArgs e)
        {
            DataProcess("3");
        }
        //4
        private void button5_Click(object sender, EventArgs e)
        {
            DataProcess("4");
        }
        //5
        private void button6_Click(object sender, EventArgs e)
        {
            DataProcess("5");
        }
        //6
        private void button7_Click(object sender, EventArgs e)
        {
            DataProcess("6");
        }
        //7
        private void button1_Click(object sender, EventArgs e)
        {
            DataProcess("7");
        }
        //8
        private void button2_Click(object sender, EventArgs e)
        {
            DataProcess("8");
        }
        //9
        private void button3_Click(object sender, EventArgs e)
        {
            DataProcess("9");
        }
        //小数点
        private void button15_Click(object sender, EventArgs e)
        {
            DataProcess(".");
        }

        //除
        private void button4_Click(object sender, EventArgs e)
        {
            DataProcess("÷");
        }
        //乘
        private void button8_Click(object sender, EventArgs e)
        {
            DataProcess("×");
        }
        //减
        private void button9_Click(object sender, EventArgs e)
        {
            DataProcess("-");
        }
        //加
        private void button13_Click(object sender, EventArgs e)
        {
            DataProcess("+");
        }

        //回退
        private void button18_Click(object sender, EventArgs e)
        {
            StrData.Remove(StrData.Length-1,1);
            calcProcess.Text = StrData.ToString();
        }

        //clear
        private void button20_Click(object sender, EventArgs e)
        {
            calcResult.Clear();
            calcProcess.Clear();
            StrData.Clear();

        }

        double CalcData(string s)
        {
            char[] splitor = { '×', '÷' };
            string[] strArr = s.Split(splitor);
            double tempData=0; 
            int i = 0;

            tempData = Double.Parse(strArr[0]); 
            foreach(char temps in s)
            {
                switch (temps)
                {
                    case '÷':
                        tempData = tempData / Double.Parse(strArr[i+1]);
                        i++;
                        break;
                    case '×':
                        tempData = tempData * Double.Parse(strArr[i + 1]);
                        i++;
                        break;
                    default:
                        break;
                }
            }
            return tempData;

        }

        //等于
        private void button14_Click(object sender, EventArgs e)
        {
            char[] SplitSymbol = { '+', '-' };
            string StrDataTemp = StrData.ToString(); ;
            string[] StrDataArr= StrDataTemp.Split(SplitSymbol);
            double ResultData = 0;
            int j = 0;

            //先以加减运算符为分隔符分割字符串,处理分割后数组中的乘除运算
            for (int i=0; i< StrDataArr.Length;i++)
            {
                if(StrDataArr[i].Contains("×")|| StrDataArr[i].Contains("÷"))
                {
                    StrDataArr[i] = CalcData(StrDataArr[i]).ToString();
                }
            }

            //加减运算
            ResultData= Double.Parse(StrDataArr[0]);
            foreach (char temps in StrDataTemp)
            {
                switch (temps)
                {
                    case '-':
                        ResultData = ResultData - Double.Parse(StrDataArr[j + 1]);
                        j++;
                        break;
                    case '+':
                        ResultData = ResultData + Double.Parse(StrDataArr[j + 1]);
                        j++;
                        break;
                    default:
                        break;
                }
            }

            calcResult.Text = ResultData.ToString();

        }

        private void calcResult_TextChanged(object sender, EventArgs e)
        {

        }

        private void calcProcess_TextChanged(object sender, EventArgs e)
        {

        }
       
    }

}


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