Beispiel #1
0
        public double CalFunc(string str, double[]  add)
        {
            //无参数数量限制
            switch (str)
            {
            case "sum": return(Sum(add));

            case "avg": return(Average(add));
            }
            //有限制
            if (!FuncName.CheckFun(str, add.Length))
            {
                SortBlock2G.error.ErrorMessage += "函数: " + str + "参数个数错误,应为" + FuncName.options[FuncName.FindName(str)].GetFunNum() + "\n";
                Wrong = true;

                return(0);
            }


            switch (add.Length)
            {
            case 1: return(CalFunc(str, add[0]));

            case 2: return(CalFunc(str, add[0], add[1]));

            default: SortBlock2G.error.ErrorMessage += "该函数参数个数错误 \n"; Wrong = true; return(0);
            }
        }
Beispiel #2
0
 {    //差一个3!之类的 阶乘 运算符  /finished
     public Calculator()
     {
         SetRad(false);
         FuncName.AddFunc("exp", 1);
         FuncName.AddFunc("sum", 0);
         FuncName.AddFunc("avg", 0);
         FuncName.AddFunc("ln", 1);
         FuncName.AddFunc("lg", 1);
         FuncName.AddFunc("asin", 1);
         FuncName.AddFunc("acos", 1);
         FuncName.AddFunc("atan", 1);
     }
Beispiel #3
0
        private int PutFuncToData(int i)
        {
            int         k          = 0;
            SortBlock2G sortblock2 = new SortBlock2G();
            FuncName    fn         = new FuncName();

            fn.SetFuncName(strFun);
            strFun = "";
            if ((new Calculator()).CheckFunName(fn.GetFuncName()))
            {
                k = strcal.FindBrackets(i + 1);                //只是简单的换个函数就可以实现多重嵌套函数了
                if (k == -1)
                {
                    SortBlock2G.error.ErrorMessage += "括号不成对\n";
                    errorMessage += "括号不成对\n";
                    this.Wrong    = true;
                    return(-1);
                }
                string stri = strcal.FromToEnd(i, k - 1);

                //此处应该提取一个专门把函数里面的参数弄出来的类或方法
                CalString str = new CalString(stri);
//				int[] split = str.FindAllElements(new char[]{ ',' });
                int[] split = str.FindOpOutOfBrackets(',');

                split = str.PushToHead <int>(split, -1);
                string[] strsplit = new string[20];                //保存分割后的字符串
                for (int j = 0; j < split.Length - 1; j++)
                {
                    strsplit[j] = str.FromToEnd(split[j], split[j + 1] - 1);
                }

                int count = 0;
                strsplit[split.Length - 1] = str.FromToEnd(split[split.Length - 1], str.GetLength() - 1);



                double[] res = new double[split.Length];                //保存结果
                foreach (var element in strsplit)
                {
//					Console.WriteLine(element);
                    if (element == null)
                    {
                        break;
                    }
                    CalString   strpart   = new CalString(element);
                    SortBlock2G sortblock = new SortBlock2G();
                    sortblock.GetCalString(strpart);
                    res[count] = sortblock.GetResult();
                    if (sortblock.Wrong)
                    {
                        sortblock2.Wrong         = true;
                        sortblock2.errorMessage += sortblock.errorMessage;
                    }
                    count++;
                }
                double resu = 0;
                resu = sortblock2.calculator.CalFunc(fn.GetFuncName(), res);
                if (sortblock2.Wrong || sortblock2.calculator.Wrong)
                {
                    this.Wrong    = true;
                    errorMessage += sortblock2.errorMessage;
                }
                data[da] = new Data();
                data[da].SetNum(resu);
                da++;
            }
            else
            {
                stackchop.message.ErrorMessage += "include unkown characters\n";
                SortBlock2G.error.ErrorMessage += fn.GetFuncName().Length > 1 ? "Include UnKnown 字符串" : "Include UnKnown 字符";
                errorMessage += fn.GetFuncName().Length > 1 ? "Include UnKnown 字符串\n" : "Include UnKnown 字符\n";
                Wrong         = true;        //其实这句话可以不要,不要就可以 时间6+你好9=15 而不出错;
                return(i);
            }
            return(k);
        }
Beispiel #4
0
 public void SetFun(FuncName a)
 {
     this.fun = a;
     TypeN    = 3;
 }
Beispiel #5
0
 /// <summary>
 /// 为后续改的 避免没有初始化Calculator的时候发现未定义函数 解决输入检查的问题
 /// </summary>
 public bool CheckFunName(string strfuncname)
 {
     return(FuncName.CheckFunName(strfuncname));
 }
Beispiel #6
0
        private int PutFuncToData(int i)
        {
            //stackchop.Pop();//在这里Pop一下就好了。。尽量不要这样
            //1.2.3修改一下就已经不用了,哈哈
            int        k          = 0;
            SortBlock2 sortblock2 = new SortBlock2();
            FuncName   fn         = new FuncName();

            fn.SetFuncName(strFun);
            strFun = "";
            if ((new Calculator()).CheckFunName(fn.GetFuncName()))
            {
//				 k=strcal.FindOp(i,')');
                k = strcal.FindBrackets(i + 1);            //只是简单的换个函数就可以实现多重嵌套函数了
//				Console.WriteLine("i={0}   k={1}",i,k);
                if (k == -1)
                {
                    SortBlock2.error.ErrorMessage += "括号不成对\n"; SortBlock2.Wrong = true;
                    return(-1);
                }
                string stri = strcal.FromToEnd(i, k - 1);

                CalString str   = new CalString(stri);
                int[]     split = str.FindAllElements(new char[] { ',' });

//				 if (split.Length!=0) {
                split = str.PushToHead <int>(split, -1);
                string [] strsplit = new string[20];                     //保存分割后的字符串
                for (int j = 0; j < split.Length - 1; j++)
                {
                    strsplit[j] = str.FromToEnd(split[j], split[j + 1] - 1);
                }

                int count = 0;
                strsplit[split.Length - 1] = str.FromToEnd(split[split.Length - 1], str.GetLength() - 1);
                double [] res = new double[split.Length];                     //保存结果
                foreach (var element in strsplit)
                {
                    if (element == null)
                    {
                        break;
                    }
                    CalString  strpart   = new CalString(element);
                    SortBlock2 sortblock = new SortBlock2();
                    sortblock.GetCalString(strpart);
                    res[count] = sortblock.GetResult();
//				        FuncName.InnerSetString();
                    count++;
                }
                double resu = 0;
                resu = sortblock2.calculator.CalFunc(fn.GetFuncName(), res);



                //pow(1+2*3+4,5-5+6,8,96,9-5,55)
//				    string str1=str.FromToEnd(-1,split-1);
//				    string str2=str.FromToEnd(split,str.GetLength()-1);
//				    Console.WriteLine(str1+"      "+str2);

//				 }//if 逗号存在 结束


//				 sortblock2.GetCalString(str);
//				 double re=sortblock2.GetResult();
//
//
//				 re=sortblock2.calculator.CalFunc(FuncName.GetFunc(),re);
                //PutNumToData();
                data[da] = new Data();
                data[da].SetNum(resu);
                da++;
            }
            else
            {
                stackchop.message.ErrorMessage += "include unkown characters\n";
                Wrong = true;          //其实这句话可以不要,不要就可以 时间6+你好9=15 而不出错;
                return(i);
            }
            return(k);
        }