Example #1
0
        public static void Main0(string[] args)
        {
//			Calculte cal=new Calculte ();
//			string str=cal.GetString ();
//			string strcal=cal .BackSort (str);
//			double result=cal .Calculator (strcal );

//------------------------------------------------------------------------------

//----------------------主程序---------------------------------------------------
            char op = '0';

            while (op != 'q')
            {
                CalString cal = new CalString("");
                cal.UIGetString();



                SortBlock2 sort = new SortBlock2();
                sort.GetCalString(cal);

                double result = sort.GetResult();
                if (sort.IsError())
                {
                    Console.WriteLine(sort.ShowInfo());
                }
                Console.Write(cal.GetString() + "=" + result.ToString() + '\n' + "按q退出,其他键继续" + '\n');
                SortBlock2.Wrong = false;
                SortBlock2.error.ErrorMessage = "";
                op = Console.ReadKey().KeyChar;
            }

//----------------------------------------------------------------------------------------------
//          cos(sin(20+30-20)*20+50)*4/2^2+1/2+cos(sin(20+30-20)*20+50)*4/2^2+1/2
//          pow(sin(30)*4,2)
//          SortBlock2 Release模式下用时32
//			int i=cal.FindOp(0,'(');
//			int k=cal.FindBrackets(i);
//			string str=cal.FromToEnd(i,k);
//			Console.WriteLine(str);

//			其实C#自带的就已经很屌了 Console.WriteLine(3+30*3*(3-4));

            /**
             * 测试找括号
             */
//			char []opts={'(',')' };
//			string str="(1+2)+1*(2+3)";
//			CalString scal=new CalString(str);
//			int [] ars=scal.FindAllElements(scal.FindOps,opts);
//			foreach (int element in ars) {
//				Console.WriteLine(element);
//			}

            /*
             * 测试逗号查找
             */
//		    char []opts={','};
//			string str="pow(1,1,2,3)";
//			CalString scal=new CalString(str);
//			int [] ars=scal.FindAllElements(opts);
//			foreach (int element in ars) {
//				Console.WriteLine(element);
//			}

//			Console.WriteLine(Math.Log(Math.E));

            /*
             * 测试FuncName的FindFunName方法
             */
//			Console.WriteLine(FuncName.FindName("cos"));
//				Console.WriteLine(FuncName.CheckFun("cos",1));
            //测试FindOp
//			int c=scal.FindOp(0,opts);
//			Console.WriteLine(c);
//			测试时间
            //不改730多
            //加一个方法 改成NoGetResult 740多
            //用Paraller 1300多
            //Paraller 干for循环只要410多
//				char op = '0';
//			while (op != 'q') {
//				CalString cal = new CalString("");
//				cal.UIGetString();
//			    Stopwatch sw=new Stopwatch();
//			    sw.Start();
//
//
//				double result=0;
////				for(int i=0;i<10000;i++)
////				{
////					  sort = new SortBlock2();
////				    sort.GetCalString(cal);
////
////					result = sort.NoGetResult();
////				}
////
//				Parallel.For( 0,10000,(i)=>
//				{
//				    SortBlock2 sort= new SortBlock2();
//				    sort.GetCalString(cal);
//
//					result = sort.NoGetResult();
//				             });
////				if (sort.IsError()) {
////					Console.WriteLine(sort.ShowInfo());
////				}
//				Console.Write(cal.GetString() + "=" + result.ToString() + '\n' + "按q退出,其他键继续" + '\n');
//				sw.Stop();
//				long time=sw.ElapsedMilliseconds;
//				Console.WriteLine(time);
//				op = Console.ReadKey().KeyChar;
//			}
//
//
//
//
//



            Console.ReadKey();
        }
Example #2
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);
        }