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

📄 form1.cs

📁 PDA构造
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace PDAProject
{
    public partial class Form1 : Form
    {
        string[] b = new string[20];
        int s = 0;
        string stackLink = "";
        int times=0;
        int marquee = 0;
        //List list1 = new List();
        string sentenceLeft = "";
        List<string> list1 = new List<string>();
        List<string> list2 = new List<string>();
        public Form1()
        {
            InitializeComponent();

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //******************变量声明************************
            int theFlag = 1;
            string gram = ""; 
            string language = "";
            string sentence = "";
            string specificGram = "";
            string temp = "";
            int place = 0;
            int LinkPlace = 0;
            
           // int flag = 0;
            //******************算法过程************************
            gram = gramDec.Text.Trim();
            language = languages.Text.Trim();
            sentence = sentences.Text.Trim();
            //split分割文法
            string [] gramSplit=gram.Split(';');
            //for(int j = 0;j<gramSplit.Length;j++){

            //}
            //建立一个堆栈--用于
            string[] a = new string[20];
            a[0]="S";
            a[1]="#";
            for (int i = 0; i < sentence.Length-1;i++ )
            {

                stackLink = "";
                int flag = 0;
                string induce = "";
                induce = a[0]+"->"+sentence[i];
                for (int j = 0; j < gramSplit.Length; j++)
                {
                    if (gramSplit[j].StartsWith(induce)) {
                        specificGram = gramSplit[j];//找到匹配的文法
                        if (induce.Equals(specificGram))
                        {
                            temp = "";
                            flag = 1;
                        }
                        else {
                            temp = specificGram.Substring(induce.Length);// specificGram.Length - induce.Length
                            flag = 1;
                        }
                        break;
                    }
                    
                }
                if (flag == 0)
                {
                    theFlag = 0;
                    break;
                }
                else
                {
                    int l1 = temp.Length;//temp = ""时候的长度是多少?
                    if (l1 == 0)
                    {
                        for (int j = 0; j < a.Length-1; j++)
                        {
                            a[j] = a[j + 1];
                        }
                    }
                    else if (l1 == 1)
                    {
                        a[0] = temp.Substring(0, 1);//a[0] = temp.Substring(1, 1);
                    }
                    else
                    {
                        //数组中#后移动 l1-1位
                        for (int j = 0; j < a.Length; j++)
                        {
                            if (a[j].Equals("#"))
                            {
                                place = j;
                                break;
                            }
                        }
                        //后移
                        for (int j = place; j >= 1; j--)
                        {
                            a[j + l1 - 1] = a[j];
                        }
                        // 将temp中的内容放入数组,即压栈
                        for (int j = 0; j < l1; j++)
                        {
                            a[j] = temp.Substring(j, 1);
                        }
                    }
                    //link
                    for (int j = 0; j < a.Length; j++)
                    {
                        if (a[j] == null)
                        {
                            LinkPlace = j;
                            break;
                        }
                    }
                    for (int t = LinkPlace - 1; t >= 0; t--)
                    {
                        stackLink = stackLink + a[t];
                    }
                    list1.Add(stackLink);
                }
                sentenceLeft = sentence.Substring(i+1);
                list2.Add(sentenceLeft);
            }
            if (theFlag == 0)
            { }
            else
            {
                string s = sentence.Substring(sentence.Length - 1, 1);
                if (a[0].Equals(sentence.Substring(sentence.Length - 1, 1)))
                { theFlag = 1; }
                else
                { theFlag = 0; }
            }
            //******************判断结果************************
            if(theFlag==1){
                label4.Text="是句子";
            }
            else{
                label4.Text="不是句子";
            }
        }
//*****************************************************************************************************************************//////////////////////
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval =Int32.Parse(minitueSetting.Text)*1000;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer1.Enabled = false ;
            //if (times < list1.Count)
            //{
            //    timer1.Enabled = true;
            //}
            if (marquee == 0)
            {
                label10.Visible = false;
                marquee = 1;
            }
            else {
                label10.Visible = true;
                marquee = 0;
            }
            //获取堆栈内容
                //stackContent.Text = (string)list1[times];
                listBox1.Items.Add((string)list1[times]);

                //获取剩余语句内容
                //leftSentences.Text = (string)list2[times];
                listBox2.Items.Add((string)list2[times]);
            //
                if (!(list1.Count - 1 == times))
                {
                    if (listBox1.GetItemText(times) == "#")
                    {
                        //timer1.Enabled = false;
                        //timer1.Interval = 1000000;
                        label10.Text = "动态演示完毕";
                    }
                    else
                    {
                        if (times < list1.Count)
                        {
                            times++;
                        }
                    }
                }
                else {
                    
                }
                
        }

        private void label11_Click(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

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