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

📄 arthmexp.cs

📁 关于CMM语言的解释器
💻 CS
📖 第 1 页 / 共 2 页
字号:

            int num = 0;
            while (exp.Contains(leftBracket))
            {
                string exp_inbracket = ""; //括号内的表达式
                string tmpstr = exp.Remove(0, exp.IndexOf(leftBracket));
                grammerAnalysis.GetBracketContent(ref tmpstr, ref exp_inbracket);
                codeArray.Add(exp_inbracket);

                //替换
                exp = exp.Replace(leftBracket + exp_inbracket + rightBracket,
                                    replaceStr + Convert.ToString(num));
                num++;
            }
        }

        //还原括号部分。参数leftBracket只能是'('或者'['(辅助方法)
        private static void DecodeBracket(ref string exp, ArrayList codeArray,
            string leftBracket, string replaceStr)
        {
            string rightBracket;
            if (leftBracket.Equals("(")) rightBracket = ")";
            else if (leftBracket.Equals("[")) rightBracket = "]";
            else return;

            ArrayList expWords = wordAnalysis.GetWordLst(exp);
            exp = "";
            foreach (string expWord in expWords)
            {
                string addStr = expWord;
                int replaceStrIndex = addStr.IndexOf(replaceStr);
                if (replaceStrIndex >= 0)
                {
                    int num = Convert.ToInt32(addStr.Substring(replaceStrIndex + replaceStr.Length));


                    addStr = addStr.Replace(replaceStr + Convert.ToString(num),
                        leftBracket + (string)codeArray[num] + rightBracket);    //还原
                }
                exp += addStr;
            }
        }

        public static GrammerAnalysis grammerAnalysis = new GrammerAnalysis();
        public static WordAnalysis wordAnalysis = new WordAnalysis();
        //错误
        public const int ERROR = -1;
        //标识符
        public const int ID = -12;
        //数组标识符
        public const int ARR_ID = -13;
        //整数
        public const int INTG = -14;
        //实数
        public const int REAL = -15;
        //算术表达式
        public const int ARTHM_EXP = -46;
        //代替已经规约为因子的部分
        const string FACTOR = ":factor";
        //代替括号部分
        const string BRC = ":bracket";
        //代替数组的括号部分
        const string ARR_IDX = ":arr_index";
    }

    //项
    class Term : ArthmExp
    {

        private string sign = "";//符号
        public string Sign
        {
            get { return sign; }
            set { sign = value; }
        }

        public Term()
        {
            this.Con = "";
        }

        public Term(string initCon)
        {
            this.Con = initCon;
        }


        public Factor[] GetFactor()
        {
            return ArthmExp.GetFactor(this);
        }

        //获得项的值(整数)
        public new bool GetValue(ArrayList IDLst, ArrayList ValueLst, ref int Value)
        {
            int result = 1;
            Factor[] factors = GetFactor();

            foreach (Factor factor in factors)
            {
                int fValue = 0;
                if (!factor.GetValue(IDLst, ValueLst, ref fValue))
                    return false;

                if (factor.Sign.Equals("*"))
                    result = result * fValue;
                else if (factor.Sign.Equals("/"))
                {
                    try
                    {
                        result = result / fValue;
                    }
                    catch (Exception excp)
                    {
                        return false; //除数为0,不能计算其值
                    }
                }
                else
                    return false; //错误的符号
            }

            Value = result;
            return true;
        }

        //获得项的值(实数)
        public new bool GetValue(ArrayList IDLst, ArrayList ValueLst, ref double Value)
        {
            double result = 1.0;
            Factor[] factors = GetFactor();

            foreach (Factor factor in factors)
            {
                double fValue = 0.0;
                if (!factor.GetValue(IDLst, ValueLst, ref fValue))
                {
                    int fIValue = 0; //实型项也可能含整型因子
                    if (factor.GetValue(IDLst, ValueLst, ref fIValue))
                        fValue = (double)fIValue;
                    else
                        return false;
                }

                if (factor.Sign.Equals("*"))
                    result = result * fValue;
                else if (factor.Sign.Equals("/"))
                {
                    try
                    {
                        result = result / fValue;
                    }
                    catch (Exception excp)
                    {
                        return false; //除数为0,不能计算其值
                    }
                }
                else
                    return false; //错误的符号
            }

            Value = result;
            return true;
        }
    }

    //因子
    class Factor : Term
    {
        public Factor()
        {
            this.Con = "";
        }

        public Factor(string initCon)
        {
            this.Con = initCon;
        }

        public bool isFactor()
        {
            return (Factor.GetFactorType(this.Con) != ERROR);
        }

        public int GetFactorType()
        {
            return Factor.GetFactorType(this.Con);
        }

        //获得因子类型
        public static int GetFactorType(string factor)
        {
            if (factor.Length == 0) //空字符串不是因子
                return ERROR;
            ArrayList words = wordAnalysis.GetWordLst(factor);
            int ftype = wordAnalysis.GetWordType((string)words[0]);

            if (words.Count == 1 && (ftype == INTG || ftype == REAL || ftype == ID))
            {
                return ftype;  //单个整数,实数或标识符
            }

            else if (ftype == ID && words[1].Equals("[") && words[words.Count - 1].Equals("]"))
            {
                string exp = "";
                for (int i = 2; i < words.Count - 1; i++)
                    exp += words[i] + " ";
                if (ArthmExp.IsArthmExp(exp))
                    return ARR_ID;  //单个数组变量
            }

            else if (words[0].Equals("(") && words[words.Count - 1].Equals(")"))
            {
                string exp = "";
                if (grammerAnalysis.GetBracketContent(ref factor, ref exp)
                    && factor.Length == 0
                    && ArthmExp.IsArthmExp(exp))
                    return ARTHM_EXP;  //(表达式)
            }

            return ERROR;
        }


        //获得整数型的因子的值
        public new bool GetValue(ArrayList IDLst, ArrayList ValueLst, ref int Value)
        {
            if (GetFactorType() == INTG) //整数
            {
                Value = Convert.ToInt32(this.Con);
                return true;
            }
            else if (GetFactorType() == ID)  //普通变量
            {
                foreach (Variable var in IDLst)
                {
                    if (var.VID.Equals(this.Con))
                    {
                        return var.GetValue(ValueLst, ref Value);
                    }
                }

                return false;  //未声明的ID
            }
            else if (GetFactorType() == ARR_ID)  //数组变量
            {
                int arrIndex = 0;
                string factorCon = this.Con;
                int brcIdx = factorCon.IndexOf("[");

                string arrId = factorCon.Substring(0, brcIdx).Trim();
                string arrIndexStr = factorCon.Substring(brcIdx + 1,
                    factorCon.Length - brcIdx - 2);
                ArthmExp arrIndexExp = new ArthmExp(arrIndexStr);

                if (!arrIndexExp.GetValue(IDLst, ValueLst, ref arrIndex))
                    return false; //数组下标不合法

                foreach (Variable var in IDLst)
                {
                    if (var.VID.Equals(arrId))
                    {
                        return var.GetValue(ValueLst, arrIndex, ref Value);
                    }
                }

                return false;  //未声明的ID
            }
            else if (GetFactorType() == ARTHM_EXP)  //(表达式)
            {
                string expStr = this.Con.Substring(1, this.Con.Length - 2);
                ArthmExp exp = new ArthmExp(expStr);
                return exp.GetValue(IDLst, ValueLst, ref Value);
            }

            else  //若非以上类型
                return false;
        }
        //获得实数类型的因子的值
        public new bool GetValue(ArrayList IDLst, ArrayList ValueLst, ref double Value)
        {
            if (GetFactorType() == REAL) //实数
            {
                Value = Convert.ToDouble(this.Con);
                return true;
            }
            else if (GetFactorType() == ID)  //普通变量
            {
                foreach (Variable var in IDLst)
                {
                    if (var.VID.Equals(this.Con))
                    {
                        return var.GetValue(ValueLst, ref Value);
                    }
                }

                return false; //未声明的ID
            }
            else if (GetFactorType() == ARR_ID)  //数组变量
            {
                int arrIndex = 0;
                string factorCon = this.Con;
                int brcIdx = factorCon.IndexOf("[");

                string arrId = factorCon.Substring(0, brcIdx).Trim();
                string arrIndexStr = factorCon.Substring(brcIdx + 1,
                    factorCon.Length - brcIdx - 2);
                ArthmExp arrIndexExp = new ArthmExp(arrIndexStr);

                if (!arrIndexExp.GetValue(IDLst, ValueLst, ref arrIndex))
                    return false; //数组下标不合法

                foreach (Variable var in IDLst)
                {
                    if (var.VID.Equals(arrId))
                    {
                        return var.GetValue(ValueLst, arrIndex, ref Value);
                    }
                }

                return false;  //未声明的ID
            }
            else if (GetFactorType() == ARTHM_EXP)  //(表达式)
            {
                string expStr = this.Con.Substring(1, this.Con.Length - 2);
                ArthmExp exp = new ArthmExp(expStr);
                return exp.GetValue(IDLst, ValueLst, ref Value);
            }

            else  //若非以上类型
                return false;
        }
    }
}

⌨️ 快捷键说明

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