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

📄 form1.cs

📁 基于编译原理的表达式计算器 使用算符优先算法实现了表达式计算器
💻 CS
📖 第 1 页 / 共 4 页
字号:
            logo[n] = 8;
            zi[n] = 4;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

        private void blog_Click(object sender, EventArgs e)   //log键  log(a,b) a表示log的底
        {
            
            input += "log(";
            countleft = countleft + 1;
            countlog = countlog + 1;
            textBox1.Text = input;
            label4.Text = "log的输入形式为:log(a,b)  b为底数";
            logo[n] = 8;
            zi[n] = 3;
            n = n + 1;
            logo[n] = 6;
            zi[n] = 1;
            Lex();
        }

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

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

        private void button11_Click(object sender, EventArgs e)  //用与log中的分割键,如log(a,b)
        {
            label4.Text = "";
            input += ",";
            countlogkey = countlogkey + 1;
            textBox1.Text = input;
           
            logo[n] = 9;
            zi[n] = 1;
            Lex();

        }

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

        private void b12_Click(object sender, EventArgs e)  //等号键()
        {
            int i;
            string[] str = new string[100];
            t = 0;
            input = textBox1.Text;
            label4.Text = "";
            if (countright == countleft)
            {
                if (checkBox1.Checked == false)
                {
                   
                        input += "#";                      //作为表达式结束标志
                        logo[n] = 11;
                        zi[n] = 1;
                        YuFa(input, n);
                        str[t] = textBox1.Text + "=" + textBox3.Text;
                   
                       
                    

                }
                else if (checkBox1.Checked == true)
                {
                    if (radioButton1.Checked == false && radioButton2.Checked == false && radioButton3.Checked == false && radioButton4.Checked == false)
                        label4.Text = "请选择初始进制!!";
                    else if (radioButton9.Checked == false && radioButton6.Checked == false && radioButton7.Checked == false && radioButton8.Checked == false)
                        label4.Text = "请选择转换的进制!!";
                    else
                    {
                        Change();
                        if (radioButton1.Checked == true && radioButton7.Checked == true)    //表示出进制转换功能进制的改变情况在显示板显示
                            str[t] = textBox1.Text + "[16]" + "=" + textBox3.Text + "[2]";
                        else if (radioButton1.Checked == true && radioButton6.Checked == true)
                            str[t] = textBox1.Text + "[16]" + "=" + textBox3.Text + "[8]";
                        else if (radioButton1.Checked == true && radioButton8.Checked == true)
                            str[t] = textBox1.Text + "[16]" + "=" + textBox3.Text + "[10]";
                        else if (radioButton1.Checked == true && radioButton9.Checked == true)
                            str[t] = textBox1.Text + "[16]" + "=" + textBox3.Text + "[16]";
                        if (radioButton3.Checked == true && radioButton7.Checked == true)
                            str[t] = textBox1.Text + "[8]" + "=" + textBox3.Text + "[2]";
                        else if (radioButton3.Checked == true && radioButton6.Checked == true)
                            str[t] = textBox1.Text + "[8]" + "=" + textBox3.Text + "[8]";
                        else if (radioButton3.Checked == true && radioButton8.Checked == true)
                            str[t] = textBox1.Text + "[8]" + "=" + textBox3.Text + "[10]";
                        else if (radioButton3.Checked == true && radioButton9.Checked == true)
                            str[t] = textBox1.Text + "[8]" + "=" + textBox3.Text + "[16]";
                        if (radioButton4.Checked == true && radioButton7.Checked == true)
                            str[t] = textBox1.Text + "[2]" + "=" + textBox3.Text + "[2]";
                        else if (radioButton4.Checked == true && radioButton6.Checked == true)
                            str[t] = textBox1.Text + "[2]" + "=" + textBox3.Text + "[8]";
                        else if (radioButton4.Checked == true && radioButton8.Checked == true)
                            str[t] = textBox1.Text + "[2]" + "=" + textBox3.Text + "[10]";
                        else if (radioButton4.Checked == true && radioButton9.Checked == true)
                            str[t] = textBox1.Text + "[2]" + "=" + textBox3.Text + "[16]";
                        if (radioButton2.Checked == true && radioButton7.Checked == true)
                            str[t] = textBox1.Text + "[10]" + "=" + textBox3.Text + "[2]";
                        else if (radioButton2.Checked == true && radioButton6.Checked == true)
                            str[t] = textBox1.Text + "[10]" + "=" + textBox3.Text + "[8]";
                        else if (radioButton2.Checked == true && radioButton8.Checked == true)
                            str[t] = textBox1.Text + "[10]" + "=" + textBox3.Text + "[10]";
                        else if (radioButton2.Checked == true && radioButton9.Checked == true)
                            str[t] = textBox1.Text + "[10]" + "=" + textBox3.Text + "[16]";
                    }
                }
                for (i = 0; i <= t; i++)
                {
                    textBox2.Text = textBox2.Text + Environment.NewLine + str[i];
                }
                t = t + 1;
                for (i = 0; i <= n; i++)
                {
                    logo[i] = 0;
                    zi[i] = 0;
                }

                // label4.Text = "";
                input = "";
                countleft = 0;
                countright = 0;
                countlog = 0;
                countlogkey = 0;
                n = 0;
            }
            else
                label4.Text = "括号不匹配,请检查括号!";
        }

        private void bbs_Click(object sender, EventArgs e)    //退格键Backspace
        {
            label4.Text = "";
            if (input.Length > 0 && n > 0)
            {
                string newinput;
                newinput = "";
                newinput = textBox1.Text.Substring(0, textBox1.Text.Length - zi[n - 1]);
                if (logo[n - 1] == 6)
                    countleft = countleft - 1;
                if (logo[n - 1] == 7)
                    countright = countright - 1;
                if (n - 2 >= 0)
                {
                    if (zi[n - 2] > 1)
                    {
                        // n = n - 1;
                        newinput = textBox1.Text.Substring(0, newinput.Length - zi[n - 2]);
                        //logo[n] = 0;
                        logo[n - 1] = 0;
                        n = n - 1;
                    }
                }
                logo[n] = 0;
                //if (n > 0)
                n = n - 1;
                label4.Text = "";
                input = newinput;
                textBox1.Text = input;

            }


        }

        private void bc_Click(object sender, EventArgs e)  //清空键C
        {
            int i;
            string str;
            str = "";
           // textBox1.Enabled = true;
            textBox1.Text = str;
            for (i = 0; i <= n; i++)
            {
                logo[i] = 0;
                zi[i] = 0;
            }
            textBox3.Text = "";
            label4.Text = "";
            input = "";
            // zi = null;
            //logo = null;
            //  logo = null;
            countleft = 0;
            countright = 0;
            countlog = 0;
            countlogkey = 0;
            n = 0;
            keys = 1;
        }



        //总体的词法分析函数
        private void Lex()
        {
          
            textBox3.Clear();
            if (n == 0)
            {
                if (logo[n] != 2 && logo[n] != 6 && logo[n] != 8 && logo[n] != 10)
                {label4.Text = "输入错误,请重新输入!";
                    MessageBox.Show("输入错误,请重新输入!");
                }

                n = n + 1;
            }
            else
            {
                if (logo[n] == 1) S1();
                if (logo[n] == 2) S2();
                if (logo[n] == 3) S3();
                if (logo[n] == 4) S4();
                if (logo[n] == 7) S7();
                if (logo[n] == 9) S9();
                if (logo[n] == 10) S10();
                if (logo[n] == 6)
                {
                    if (logo[n - 1] == 5) S5();
                    else if (logo[n - 1] == 8) S8();
                    else S6();
                }
            }

        }



        private void S1()       //函数S1判断小数点输入是否正确
        {
            if (logo[n - 1] == 1 || logo[n - 1] > 2)
            {
                label4.Text = "输入错误,小数点前必须为数字!";
                MessageBox.Show("输入错误,小数点前必须为数字!");
            }

            n = n + 1;
        }

        private void S2()       //函数S2判断数字输入是否正确
        {
            if (logo[n - 1] == 3 || logo[n - 1] == 7 || logo[n - 1] == 8)
            {
                label4.Text = "输入错误,该数字输入错误!";
                MessageBox.Show("输入错误,该数字输入错误!");
            }

            n = n + 1;
        }


        private void S3()      //函数S3判断百分号输入是否正确
        {
            if (logo[n - 1] != 2 && logo[n - 1] != 7)
            {
                label4.Text = "输入错误,百分号输入错误!";
                MessageBox.Show("输入错误,百分号输入错误!");
            }

            n = n + 1;
        }

        private void S4()     //函数S4判断运算符输入是否正确
        {
            if (logo[n - 1] != 2 && logo[n - 1] != 3 && logo[n - 1] != 7)
            {
                label4.Text = "输入错误,运算符输入错误!";
                MessageBox.Show( "输入错误,运算符输入错误!");
            }

            n = n + 1;
        }


        private void S5()      //函数S5判断求方符输入是否正确
        {
            if (n - 2 >= 0)
            {
                if (logo[n - 2] != 2 && logo[n - 2] != 3 && logo[n - 2] != 7)
                {
                    label4.Text = "输入错误,求方号输入错误!";
                    MessageBox.Show("输入错误,求方号输入错误!");
                }
            }
            n = n + 1;
        }

        private void S6()     //函数S6判断左括号输入是否正确
        {
            if (logo[n - 1] != 4 && logo[n - 1] != 6 && logo[n - 1] != 9 && logo[n - 1] != 10)
            {
                label4.Text = "输入错误,左括号输入错误!";
                MessageBox.Show("输入错误,左括号输入错误!");

            }
            if (countleft < countright)
            {
                label4.Text = "括号输入不正常!";
                MessageBox.Show("括号输入不正常!");
                
            }

            n = n + 1;
        }

        private void S7()  //函数S7判断右括号输入是否正确
        {
            if (logo[n - 1] != 2 && logo[n - 1] != 3 && logo[n - 1] != 7)
            {
                label4.Text = "输入错误,右括号输入错误!";
                MessageBox.Show("输入错误,右括号输入错误!");

            }
            if (countleft < countright)
            {
                label4.Text = "括号输入不正常!";
                MessageBox.Show("括号输入不正常!");
            }
            n = n + 1;
        }

        private void S8()  //函数S8判断函数输入是否正确
        {
            if (n - 2 >= 0)
            {
                if (logo[n - 2] != 4 && logo[n - 2] != 6 && logo[n - 2] != 9 && logo[n - 2] != 10 && n - 1 != 0)
                {
                    label4.Text = "输入错误," + input.Substring(input.Length - 1 - zi[n - 1], zi[n - 1] + 1) + "输入错误!";
                    MessageBox.Show("输入错误," + input.Substring(input.Length - 1 - zi[n - 1], zi[n - 1] + 1) + "输入错误!");
                  
                }
            }
            n = n + 1;
        }


        private void S9()  //函数S9判断分割符输入是否正确
        {
            if (logo[n - 1] != 2 && logo[n - 1] != 3 && logo[n - 1] != 7)
            {
                label4.Text = "输入错误,分割符输入错误!";
                MessageBox.Show("输入错误,分割符输入错误!");
            }
            if (countlog < countlogkey)
            {
                label4.Text = "分割符用于LOG,其表达为log(a,b)!";
                MessageBox.Show("分割符用于LOG,其表达为log(a,b)!");
            }
            n = n + 1;

⌨️ 快捷键说明

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