⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 基于编译原理的表达式计算器 使用算符优先算法实现了表达式计算器
💻 CS
📖 第 1 页 / 共 4 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsApplication1
{

    public partial class Form1 : Form
    {
        public string input;   //记录输入的表达式
        public int[] logo = new int[100];   //标记每个字符的类型
        public int n = 0;         //标记输入字符的数量
        public int[] zi = new int[100];        //记录每次输入的长度;
        public int countleft = 0;              //标记左括号的个数
        public int countright = 0;              //标记右括号的个数
        public int countlog = 0;               //textBox1用于显示表达式,textBox2用于显示计算过的表达式,textBox3用于显示表达式的计算结果
        public int countlogkey = 0;            //标记用于LOG(A,B)中“,”的个数,以便判断log中有无输入错误.
        public int t = 0;
        public char[] op = new char[21];
        public int keys = 0;

        public Form1()
        {

            InitializeComponent();

            textBox1.Text = input;

        }

        private void button1_Click(object sender, EventArgs e)         //.(小数点)      标记为1
        {
           
            label4.Text = "";
             input = textBox1.Text;
            input += "1";                                              //0到9           标记为2
            textBox1.Text = input;                                    //%(百分号)       标记为3
            logo[n] = 2;                                              //*,/,+,-         标记为4
                                                                      //^(请方号)       标记为5
            zi[n] = 1;                                                 // ( (左括号)    标记为6
            Lex();                                                   //)(右括号)       标记为7
        }                                                                //sin,cos,tan,asin,acos,atan,
        private void button2_Click(object sender, EventArgs e)        //log,lg,ln,sqrt  标记为8
        {
            input = textBox1.Text;                                   //,               标记为9
            input += "2";                                             //@ (负号)      标记为10
            textBox1.Text = input;                                    //# (结束符)     标记为11
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "3";
            textBox1.Text = input;
            logo[n] = 2;

            zi[n] = 1;
            Lex();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "4";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "5";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "6";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "7";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "8";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "9";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "0";
            textBox1.Text = input;
            logo[n] = 2;
            zi[n] = 1;
            Lex();

        }


        private void bpok_Click(object sender, EventArgs e)   //小数点键
        {
            label4.Text = "";
            input = textBox1.Text;
            input += ".";
            textBox1.Text = input;
            logo[n] = 1;
            zi[n] = 1;
            Lex();
        }

        private void badd_Click(object sender, EventArgs e)  //加号键
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "+";
            textBox1.Text = input;
            logo[n] = 4;
            zi[n] = 1;
            Lex();
        }

        private void bsub_Click(object sender, EventArgs e)  //减号键
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "-";
            textBox1.Text = input;
            logo[n] = 4;
            zi[n] = 1;
            Lex();
        }

        private void bmul_Click(object sender, EventArgs e)  //乘号键
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "*";
            textBox1.Text = input;
            logo[n] = 4;
            zi[n] = 1;
            Lex();
        }

        private void bdiv_Click(object sender, EventArgs e)  //除号键
        {
            label4.Text = "";
            input = textBox1.Text;
            input += "/";
            textBox1.Text = input;
            logo[n] = 4;
            zi[n] = 1;
            Lex();
        }

        private void bpercent_Click(object sender, EventArgs e)  //百分号键
        {
            label4.Text = "";
            input += "%";
            textBox1.Text = input;
            logo[n] = 3;
            zi[n] = 1;
            Lex();
        }

        private void bsquare_Click(object sender, EventArgs e)   //求方键
        {
            label4.Text = "";
            input += "^(";
            textBox1.Text = input;
            countleft = countleft + 1;
            logo[n] = 5;
            zi[n] = 1;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void conl_Click(object sender, EventArgs e)   //左括号键
        {
            label4.Text = "";
            input += "(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void conr_Click(object sender, EventArgs e)  //右括号键
        {
            label4.Text = "";
            input += ")";
            countright = countright + 1;
            textBox1.Text = input;
            logo[n] = 7;
            zi[n] = 1;
            Lex();
        }

        private void bsqrt_Click(object sender, EventArgs e)  //开平方键
        {
            label4.Text = "";
            input += "sqrt(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 4;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();

        }


        private void bcbrt_Click(object sender, EventArgs e)   //开立方键
        {
            label4.Text = "";
            input += "cbrt(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 4;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void bsin_Click(object sender, EventArgs e)   //三角函数sin键
        {
            label4.Text = "";
            input += "sin(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 3;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void bcos_Click(object sender, EventArgs e)  //三角函数cos键
        {
            label4.Text = "";
            input += "cos(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 3;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void btan_Click(object sender, EventArgs e)  //三角函数tan键
        {
            label4.Text = "";
            input += "tg(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 2;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void batan_Click(object sender, EventArgs e)   //三角函数atan键
        {
            label4.Text = "";
            input += "ctg(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 3;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void bacos_Click(object sender, EventArgs e)   //三角函数acos键
        {
            label4.Text = "";
            input += "acos(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 4;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void basin_Click(object sender, EventArgs e)   //三角函数asin键
        {
            label4.Text = "";
            input += "asin(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 4;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void batg_Click(object sender, EventArgs e)     //三角函数atg键
        {
            label4.Text = "";
            input += "atg(";
            countleft = countleft + 1;
            textBox1.Text = input;
            logo[n] = 8;
            zi[n] = 3;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void bactg_Click(object sender, EventArgs e)    //三角函数actg键
        {
            label4.Text = "";
            input += "actg(";
            countleft = countleft + 1;
            textBox1.Text = input;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -