示例#1
0
        private void SortString(string str)
        {        //这个是核心
            if (str[0] == '-')
            {
                data[da] = new Data();
                data[da].SetNum(0);
                da++;
            }
            for (int i = 0; i < str.Length; i++)              //开始
            {
                type = GetType(str[i].ToString());
                if (type == 0)
                {
                    SortBlock2G.error.ErrorMessage += "包含未知字符!\n";
                    errorMessage += "包含未知字符!\n";
                    this.Wrong    = true;
                }
                if (strtype != type)
                {
                    if (strNum != "")
                    {
                        PutNumToData();
                    }


                    if (strFun != "")
                    {
                        i = PutFuncToData(i);                        //返回另一个括号
                        if (i == -1)
                        {
                            stackchop.message.ErrorMessage += "括号不对\n";
                            Wrong = true;
                            return;
                        }
                        type = strtype;
                        continue;
                    }
                }

                if (str[i] <= '9' && str[i] >= '0' || str[i] == '.')                  //数字
                {
                    strNum += str[i];
                    strtype = 1;
                }
                else
                {
                    if (strcal.CheckOption(str[i].ToString()))                      //处理单目运算符
                    {
                        strtype = 3;
                        if (i + 1 < str.Length && str[i] == '(' && str[i + 1] == '-')                          //处理负数的问题
                        {
                            data[da] = new Data();
                            data[da].SetNum(0);
                            da++;
                        }
                        if (str[i] == '(' && (i > 0 && (str[i - 1] <= '9' && str[i - 1] >= '0')))                          //处理3(x+2)的问题
                        {
                            Option opti = new Option('*');
                            stackchop.Push(opti);                                     //这一句话修改之后就好了可以计算3*4e
                        }
                        if (i + 1 < str.Length && str[i] == '+' && str[i + 1] == '-') //处理3+x(x<0)的问题
                        {
                            data[da] = new Data();
                            data[da].SetNum(0);
                            da++;
                        }
                        push = false;                        //还没有压入
                        Option opt = new Option(str[i]);
                        bool   k   = PutOptionToData(opt);   //强制分离出来但增加了全局变量
                        if (k)
                        {
                            continue;
                        }
                    }                    //处理运算符完毕
                    else
                    {
                        strFun += str[i];
                        strtype = 2;

//						stackchop .message .ErrorMessage +="include unkown characters\n";
//					    SortBlock2G.error.ErrorMessage+="include unkown character\n";
//					    errorMessage+="Include Unknown 字符";
//					    Wrong=true;//其实这句话可以不要,不要就可以 时间6+你好9=15 而不出错;
                    }
                }
            }            //for循环完成一次
            if (strNum != "")
            {
                PutNumToData();
            }

            while (stackchop.IsNotEmpty())
            {
                data[da] = new Data();
                data[da].SetOption(stackchop.Pop());
                da++;
            }
        }
示例#2
0
        private void SortStringFormer(string str)
        {        //这个是核心呐
            Option opti = new Option('#');

            stackchop.SetBottom(opti);
            int  change = 0;
            bool first  = true;

            //pow(1,2)
            for (int i = 0; i < str.Length; i++)              //开始

            {
                if (strtype != change)
                {
                    if (strNum != "")
                    {
                        PutNumToData();
                    }


                    if (strFun != "")
                    {
                        i = PutFuncToData(i);//返回另一个括号
                        if (i == -1)
                        {
                            stackchop.message.ErrorMessage += "括号不对\n";
                            Wrong = true;
                            return;
                        }
                        change = strtype;
                        continue;
                    }
                }

                if (str[i] <= '9' && str[i] >= '0' || str[i] == '.')        //数字
                {
                    strNum += str[i];
                    strtype = 1;
                }
                else
                {
                    if (strNum != "")
                    {
                        PutNumToData();            //这一段要不要去掉
                    }

                    if (strcal.CheckOption(str[i].ToString()))
                    {
                        strtype = 3;
                        if (str[i] == '(' && str[i + 1] == '-')
                        {
                            data[da] = new Data();
                            data[da].SetNum(0);
                            da++;
                        }


                        bool   push = false;                     //还没有压入
                        Option opt  = new Option(str [i]);
                        //压入堆栈,返回-1
                        //弹出堆栈,返回1
                        if (!stackchop.IsNotEmpty())
                        {
                            stackchop.Push(opt);
                            push  = true;
                            first = true;                          //为了能嵌套 这个地方加了个东西,不知道会不会引入其他问题
                            continue;
                        }
                        while (stackchop.IsNotEmpty())
                        {
                            int rank = opt.CompareTo(stackchop.TopElement());
                            if (rank == -1)
                            {
                                stackchop.Push(opt);
                                push = true; break;
                            }
                            else if (rank == 1)
                            {
                                data[da] = new Data();
                                data[da].SetOption(stackchop.Pop());
                                da++;
                            }
                            else if (rank == 2)
                            {
                                stackchop.Pop();
                                push = true; break;
                            }
                            else if (rank == 0)
                            {
                                break;
                            }
                        }                        //
                        if (!push)
                        {
                            stackchop.Push(opt);
                        }
                    }
                    else
                    {
                        strFun += str[i];
                        strtype = 2;
//						stackchop .message .ErrorMessage +="include unkown characters\n";
//					      Wrong=true;//其实这句话可以不要,不要就可以 时间6+你好9=15 而不出错;
                    }
                }

                if (first)
                {
                    change = strtype;
                    first  = false;
                }
            }            //for循环一次
            if (strNum != "")
            {
                PutNumToData();
            }

            while (stackchop.IsNotEmpty())
            {
                data[da] = new Data();
                data[da].SetOption(stackchop.Pop());
                da++;
            }
        }