国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

使用字符串解析的方式完成計算器的設(shè)計思路

2019-11-14 14:00:05
字體:
供稿:網(wǎng)友

聲明:

一個正確的計算串是格式良好的,如:{ln(10)+5*[cos(2π+1/4π)+sin(1+2+3π)]}。

計算是將一系列不相關(guān)的數(shù)據(jù)聯(lián)系或整合為一個數(shù)字,這就是計算的意義。

正文:

正如事例所示,一個正確的計算串包括以下幾種格式: 數(shù)字(0~9)、字母(a~z)、希臘字符(π Pai)、括號({}、[]、())、四則運算符(+、-、*、/)。

那么按照正常人類的思考方式,去吧字符串進行優(yōu)先級區(qū)分,將優(yōu)先級最高的一段串計算為數(shù)值,循環(huán)如此知道沒有優(yōu)先級最高的操作符的時候(僅剩下一個數(shù)),計算完成,返回結(jié)果。

不完整功能代碼如下

//聲明:目前所有操作符都是單字符型        //根據(jù)合理的字符串,返回值        PRivate decimal getValue(string str)        {            decimal result = 0;//存儲要返回的結(jié)果            int OperatorIndex = -1;//存儲當(dāng)前操作符在字符串中的索引            string operatorType = "";//存儲當(dāng)前操作符的類型            operatorIndex = getOperatorIndex(str);            operatorType = getOperatorType(operatorIndex, str);            int operator0Index = -1;//存儲當(dāng)前操作符前一個操作符0在字符串中的索引            //string operator0Type = "";//存儲當(dāng)前操作符的前一個操作符0的類型            operator0Index = getOperator0Index(str, operatorIndex);            //operator0Type = getOperatorType(operator0Index, str);            int operator1Index = -1;//存儲當(dāng)前操作符后一個操作符1在字符串中的索引            //string operator1Type = "";//存儲當(dāng)前操作符的后一個操作符1的類型            operator1Index = getOperator1Index(str, operatorIndex);            //operator1Type = getOperatorType(operator1Index, str);            double num1 = 0.1000000000000000000001;//存儲第一個操作數(shù)的值            double num2 = 0.2000000000000000000002;//存儲第二個操作數(shù)的值            num1 = getNum1(operatorIndex, operator0Index, str);            num2 = getNum2(operatorIndex, operator1Index, str);            result = Convert.ToDecimal(getResult(num1, num2, operatorType));            return result;        }        //目前字符串以數(shù)字開頭,找到優(yōu)先操作符,并返回在字符串中的索引        private int getOperatorIndex(string str)        {            int operatorIndex = -1;//存儲第一個操作符所在的索引            for (int i = 0; i < str.Length; i++)            {                switch (str[i])                {                    case '*':                        {                            operatorIndex = i;                        } break;                    case '/':                        {                            operatorIndex = i;                        } break;                    case '+':                        {                            operatorIndex = i;                        } break;                    case '-':                        {                            operatorIndex = i;                        } break;                }            }            return operatorIndex;        }        //根據(jù)優(yōu)先操作符索引,操作符所在字符串,返回前一個操作符的索引,有則返回索引,無則返回0        private int getOperator0Index(string str, int operatorIndex)        {            int operator0Index = -1;//存儲前一個操作符所在的索引            for (int i = operatorIndex - 1; i >= 0; i--)            {                switch (str[i])                {                    case '*':                        {                            operator0Index = i;                        } break;                    case '/':                        {                            operator0Index = i;                        } break;                    case '+':                        {                            operator0Index = i;                        } break;                    case '-':                        {                            operator0Index = i;                        } break;                }            }            if (operator0Index == -1)            {                operator0Index = 0;            }            return operator0Index;        }        //根據(jù)優(yōu)先操作符索引,操作符所在字符串,返回后一個操作符的索引,有則返回索引,無則返回字符串最后有一位的索引        private int getOperator1Index(string str, int operatorIndex)        {            int operator1Index = -1;//存儲后一個操作符所在的索引            for (int i = operatorIndex + 1; i < str.Length - 1; i++)            {                switch (str[i])                {                    case '*':                        {                            operator1Index = i;                        } break;                    case '/':                        {                            operator1Index = i;                        } break;                    case '+':                        {                            operator1Index = i;                        } break;                    case '-':                        {                            operator1Index = i;                        } break;                }            }            if (operator1Index == -1)            {                operator1Index = str.Length - 1;            }            return operator1Index;        }        //根據(jù)操作符的索引,返回操作符的類型        private string getOperatorType(int operatorIndex, string str)        {            string operatorType = "";            if (operatorIndex != 0 || operatorIndex != str.Length)            {                operatorType = str[operatorIndex].ToString();            }            return operatorType;        }        //根據(jù)當(dāng)前操作符的索引,前一個操作符的索引,操作符所在字符串,返回當(dāng)前操作符前一個操作數(shù)        private double getNum1(int OperatorIndex, int Operator0Index, string str)        {            double num1 = 0.10000000000000000000001;            num1 = Convert.ToDouble(str.Substring(Operator0Index, OperatorIndex - Operator0Index));            return num1;        }        //根據(jù)當(dāng)前操作符的索引,后一個操作符的索引,操作符所在字符串,返回當(dāng)前操作符后一個操作數(shù)        private double getNum2(int OperatorIndex, int Operator1Index, string str)        {            double num2 = 0.200000000000000000000002;            num2 = Convert.ToDouble(str.Substring(OperatorIndex + 1, Operator1Index - OperatorIndex));            return num2;        }        //根據(jù)兩個操作數(shù)和一個操作符,返回結(jié)果        private double getResult(double num1, double num2, string operatorType)        {            double result = 0.000000000000000000000000001;            switch (operatorType)            {                case "+":                    {                        result = num1 + num2;                    } break;                case "-":                    {                        result = num1 - num2;                    } break;                case "*":                    {                        result = num1 * num2;                    } break;                case "/":                    {                        result = num1 / num2;                    } break;            }            return result;        }        private void btn_Empty_Click(object sender, EventArgs e)        {            textBox1.Text = "";        }        private void btn_Back_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);        }

 在此希望有感興趣的同行,非同行提出建議或增加功能,不要忘了,聯(lián)系我。

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖西县| 三河市| 峨边| 海丰县| 唐山市| 德化县| 五常市| 重庆市| 三门县| 屯昌县| 滨州市| 民和| 义乌市| 青田县| 屯留县| 牡丹江市| 永仁县| 紫金县| 会理县| 云安县| 团风县| 桦川县| 凤翔县| 兖州市| 潞城市| 洞口县| 中卫市| 辽源市| 福安市| 镇雄县| 仙游县| 湘阴县| 大邑县| 灯塔市| 泗水县| 夏邑县| 禹州市| 龙泉市| 阳谷县| 兴国县| 阳谷县|