运行时报"索引超出数组界限",如何解决!!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace wordanylysis
{
    public partial class wordanalysis : Form
    {
        public wordanalysis()
        {
            InitializeComponent();
        }

        private void okbutton_Click(object sender, EventArgs e)
        {
            //得到字符数组
            char[] getch = textToCharArray();
            //将得到的字符数组转换成分析后的单词数组
            string[] stringArray = charArrayToStringArray(getch);
            string[,] twoStringArray = stringArrayToTwoStringArray(stringArray);
            printString(twoStringArray );
        }

        private void clearbutton_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
        }

        private void exitbutton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private char[] textToCharArray()
        {
            string stringTemp;
            stringTemp = this.textBox1.Text;
            char[] getch = stringTemp.ToCharArray();//要处理的字符都在getch这个数组中。
            return getch;
        }
        private void stringToArrayString(ref string[] stringArrange, string st)
        {

            if (stringArrange[0] == "")
            {
                stringArrange[0] = st;
            }
            else
            {
                string[] oldA = stringArrange;//刚得到的字符串
                int i = oldA.Length + 1;
                stringArrange = new string[i];//申请一个长一个的字符数组。
                oldA.CopyTo(stringArrange, 0);//将先前的字符数组考到现在这个数组中。
                stringArrange[stringArrange.Length - 1] = st;
            }
        }
        private void oneArrayToTwo(ref string[,] twoArray, ref int j)
        {

            string[,] tempArray = twoArray;
            twoArray = new string[2, j + 2];
            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < j + 1; y++)
                {
                    twoArray[x, y] = tempArray[x, y];
                }
            }
            j = j + 1;
        }
        private string[,] stringArrayToTwoStringArray(string[] stringArray)
        {
            //存放单词标识后的结果。
            string[,] twoArray = new string[2, 1];
            //单词的标志
            int j = 0;


            //每循环一次,把一个单词归于一类,即前面加上一个数字。
            for (int i = 0; i < stringArray.Length; i++)
            {
                //保留字 1
                if (stringArray[i] == "main"
                || stringArray[i] == "int"
                || stringArray[i] == "float"
                || stringArray[i] == "printf"
                || stringArray[i] == "if"
                || stringArray[i] == "for"
                || stringArray[i] == "while"
                || stringArray[i] == "do"
                || stringArray[i] == "return"
                || stringArray[i] == "break"
                || stringArray[i] == "continue")
                {
                    twoArray[0, j] = "1";
                    twoArray[1, j] = stringArray[i];
                    this.oneArrayToTwo(ref twoArray, ref j);
                }
                //运算符 2
                else
                    if (stringArray[i] == "+"
                    || stringArray[i] == "-"
                    || stringArray[i] == "*"
                    || stringArray[i] == "/"
                    || stringArray[i] == ">"
                    || stringArray[i] == "<"
                    || stringArray[i] == ">="
                    || stringArray[i] == "<="
                    || stringArray[i] == "!="
                    || stringArray[i] == "=="
                    || stringArray[i] == "++"
                    || stringArray[i] == "--"
                    || stringArray[i] == "=")
                    {
                        twoArray[0, j] = "2";
                        twoArray[1, j] = stringArray[i];
                        this.oneArrayToTwo(ref twoArray, ref j);
                    }
                    //分隔符 3
                    else
                        if (stringArray[i] == "("
                        || stringArray[i] == ")"
                        || stringArray[i] == "{"
                        || stringArray[i] == "}"
                        || stringArray[i] == "["
                       // || stringArray[i] == "]"
                        //|| stringArray[i] == ","
                       // || stringArray[i] == ";"
                       // || stringArray[i] == ":"
                       // || stringArray[i] == "/""
                       // || stringArray[i] == "/*"
                        /*|| stringArray[i] == "*"*/)
                        {
                            twoArray[0, j] = "3";
                            twoArray[1, j] = stringArray[i];
                            this.oneArrayToTwo(ref twoArray, ref j);
                        }
                        //数字 4
                        else
                            if (stringArray[i].ToCharArray()[0] >= '0' &&
                            stringArray[i].ToCharArray()[0] <= '9')
                            {
                                twoArray[0, j] = "4";//数字
                                twoArray[1, j] = stringArray[i];
                                this.oneArrayToTwo(ref twoArray, ref j);
                            }
                            //其它 5(变量等)
                            else
                            {
                                twoArray[0, j] = "5";
                                twoArray[1, j] = stringArray[i];
                                this.oneArrayToTwo(ref twoArray, ref j);
                            }
            }
            return twoArray;
        }
        private void printString(string[,] twoStringArray)
        {
            //提示说明
            this.textBox2.Text = "1 -> 保留字" + "/r/n" +
            "2 -> 运算符" + "/r/n" +
            "3 -> 分隔符" + "/r/n" +
            "4 -> 数字 " + "/r/n" +
            "5 -> 其它" + "/r/n";
            //输出二维数组中的数据
            for (int x = 0; x <twoStringArray.Length / 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    this.textBox2.Text = this.textBox2.Text + twoStringArray[y, x] + " ";
                    if (y == 1)
                    {
                        this.textBox2.Text = this.textBox2.Text + "/r/n";
                    }
                }
            }
        }
        private string[] charArrayToStringArray(char[] getch)
        {
            string[] stringArrange={""} ;//用这个字符串数组存放词法分析后得到的单词。
            char charTemp;
            string stringSave = "";// 存放一个分析得到的单词
             //一次循环得到一个单词。
            for(int i = 0;i < getch.Length;i++)
            {
                charTemp = getch[i];
                //begin with character
                if (charTemp >= 'a' && charTemp <= 'z' || charTemp >='A' && charTemp <= 'Z')
                {
                    stringSave = charTemp.ToString();
                    i = i + 1;
                    int test = 0;//判断循环是否结束,1 为结束。
                    while (test == 0)
                    {
                        charTemp = getch[i];
                        if (charTemp >= 'a' && charTemp <= 'z' || charTemp >= 'A' && charTemp <= 'Z' )
                        {
                            stringSave = stringSave + charTemp.ToString();
                            i = i + 1;
                        }
                        else
                            test = 1;
                    }

                }
                else
                    stringToArrayString(ref stringArrange, stringSave);

                stringSave = "";
                if (charTemp >= '0' && charTemp <= '9' )
                {
                    stringSave = charTemp.ToString();
                    i = i + 1;
                    int test = 0;//判断循环是否结束,1 为结束。
                    while (test == 0)
                    {
                        charTemp = getch[i];
                        if (charTemp >= '0' && charTemp <= '9')
                        {
                            stringSave = stringSave + charTemp.ToString();
                            i = i + 1;
                        }
                        else
                            test = 1;
                    }

                }
                else
                    stringToArrayString(ref stringArrange, stringSave);

                stringSave = "";
                if( charTemp == '(' ||charTemp == ')'||charTemp == '{'||charTemp == '}'||charTemp == '[' )
                {
                    stringSave = charTemp.ToString();
                    i = i + 1;
                    int test = 0;//判断循环是否结束,1 为结束。
                    while (test == 0)
                    {
                        charTemp = getch[i];
                        if (charTemp == '(' || charTemp == ')' || charTemp == '{' || charTemp == '}' || charTemp == '[')
                        {
                            stringSave = stringSave + charTemp.ToString();
                            i = i + 1;
                        }
                        else
                            test = 1;
                    }

                }
                else
                    stringToArrayString(ref stringArrange, stringSave);

           }
           return stringArrange;

    }
    }
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页