public Matrix ConvertToMatrix() { string tempExpress = strExpress.Substring(1, strExpress.Length - 2); string[] rows = tempExpress.Split(';'); string[] firstRow = rows[0].Trim().Split(new char[] { ' ', ',' }); int col = 0; for (int i = 0; i < firstRow.Length; i++) { if (string.IsNullOrEmpty(firstRow[i])) { } else { col++; } } int row = rows.Length; double[,] data = new double[row, col]; try { for (int i = 0; i < row; i++) { string[] singlerow = rows[i].Trim().Split(new char[] { ' ', ',' }); int column = 0; for (int j = 0; column < col; j++) { if (j < singlerow.Length) { if (string.IsNullOrEmpty(singlerow[j])) { continue; } string str = new ExpData(singlerow[j].Trim()).GetValueEx(); data[i, column] = double.Parse(str); } else { data[i, column] = 0; } column++; } } } catch (ExpError e) { throw new AssignedError(e.Message + "\nAssignmentError::矩阵转换出错\n"); } Matrix matrix = new Matrix(data); cd = matrix; return(matrix); }
public bool CreateData(string name, CalData value) { bool right = true; if (name != "pi" && name != "e") { inputs.Add(new TempData(name, value)); } else { right = false; } return(right); }
public BlockData ConverToBlock() { function.FuncString funstring = new function.FuncString(strExpress); int leftBracket = funstring.FindOp(0, '('); int rightBracket = funstring.FindOp(0, ')'); int commaSymbol = funstring.FindOp(0, ','); string[] ab = strExpress.Split(','); double a = 0; double b = 0; try { string strA = new ExpData(ab[0].Substring(1)).GetValueEx(); string strB = new ExpData(ab[1].Substring(0, ab[1].Length - 1)).GetValueEx(); a = double.Parse(strA); b = double.Parse(strB); } catch (ExpError e) { throw new AssignedError(e.Message + "\nAssignmentError::区间转换出错" + "\n"); } BlockData bd = new BlockData(a, b); bool left; bool right; if (leftBracket != -1 && leftBracket < commaSymbol) { left = false; } else { left = true; } if (rightBracket != -1 && rightBracket > commaSymbol) { right = false; } else { right = true; } bd.SetBlockBounderAvailable(left, right); cd = bd; return(bd); }
public DataType GetDataType() { DataType dt = new DataType(); function.FuncString funstring = new function.FuncString(strExpress); int leftSquareBracket = funstring.FindOp(0, '['); int rightSquareBracket = funstring.FindOp(0, ']'); int leftBracket = funstring.FindOp(0, '('); int rightBracket = funstring.FindOp(0, ')'); int commaSymbol = funstring.FindOp(0, ','); int semicolon = funstring.FindOp(0, ';'); if (commaSymbol == -1 && semicolon == -1) { dt = DataType.EXP; cd = new ExpData(strExpress); (cd as ExpData).GetValueFinal(); return(dt); } if (semicolon != -1) { dt = DataType.MATRIX; ConvertToMatrix(); return(dt); } if (commaSymbol != -1) { dt = DataType.BLOCK; ConverToBlock(); return(dt); } return(dt); }
public void Run() { Console.WriteLine("Hello World! type: help() to know how to use"); while (InputData != "exit") { GetDataToInput(); // PushToShow("进入run程序"); funstring.SetString(InputData); int Extype = funstring.AnalyseEquation(); if (Extype == 1 || Extype == 4) //没有等号 判断为命令或求值之类 // PushToShow("不是存值"); { AnalyseInputG(Extype); continue; } //保存起来 string funs = funstring.GetFuncName(); CalData caldata = null; switch (Extype) { case 2: DealAssignment da = new DealAssignment(funstring.GetFuncExpress()); if (tdc.CheckName(funs)) { DataType getdt = new DataType(); try { getdt = da.GetDataType(); } catch (ExpError e) { PushToShow(e.Message); continue; } if (getdt == DataType.EXP) { try { ((ExpData)(da.GetCalData())).GetValueFinal(); //防止循环调用SimpleCal--> ReplaceParamers--> GetValueEx-->SimpleCal } catch (AssignedError e) { PushToShow(e.Message); continue; } } } try { caldata = da.GetCalData(); } catch (AssignedError e) { PushToShow(e.Message); continue; } break; case 3: caldata = new FuncData(funstring.GetFuncExpress(), funstring.GetParamers()); break; default: break; } if (CheckDuplicationName(funs)) { tdc.Remove(tdc.FindPosition(funs)); } if (funstring.CheckFunName(funs)) { PushToShow("Variable Name not acceptable!"); continue; } if (tdc.CreateData(funstring.GetFuncName(), caldata)) { PushToShow("数据已保存"); } else { PushToShow("Not accept input!"); } #region 之前写的不好就改了 // int a = funstring.FindOp(0, '='); // int b = funstring.FindOp(0, '('); // if (a != -1) { // // // if (b > a) { // funstring.AnalyseEquation(); //// funstring.ShowParamers(); // if(CheckDuplicationName()) continue; // // tdc.CreateData(funstring.GetFuncName(), funstring.GetFuncExpress(), funstring.GetParamers()); // } else { // funstring.AnalyseVariable(); //// funstring.ShowParamers(); // if(CheckDuplicationName()) continue; // tdc.CreateData(funstring.GetFuncName(), funstring.GetFuncExpress()); // } // } else { // // } #endregion } }
// 2*3e void AnalyseInputG(int blacket) { if (blacket == 4) //有括号的 { if (IsOrder()) { // PushToShow("进入命令程序"); Command cm = new Command(); var funs = funstring.GetFuncExpressTemp(); switch (funstring.GetFuncName()) { case "plot": string[] paramers = funstring.GetParamersData(); FuncString funcs = FindFuncInTDC(paramers[0]); double left = 0; double right = 1; try { left = double.Parse(paramers[1]); right = double.Parse(paramers[2]); } catch { if (tdc.CheckName(paramers[1])) { BlockData bd = (BlockData)tdc.GetData(paramers[1]).GetCalData(); if (bd != null) { left = bd.GetXStart(); right = bd.GetXEnd(); } } } if (funcs != null) { new Thread(new ThreadStart(() => cm.PlotFunc(funcs, 6, 1000, left, right))).Start(); // new Thread(new ThreadStart(()=>cm.PlotFuncParallel(funcs, 6, 1000, -10, 10))).Start(); } else { PushToShow("Wrong!"); } break; case "list": #region 处理list bool needJudge = true; DataType dt = DataType.NONE; switch (funs) { case "exp": dt = DataType.EXP; break; case "matrix": dt = DataType.MATRIX; break; case "block": dt = DataType.BLOCK; break; case "func": dt = DataType.FUNC; break; default: needJudge = false; break; } TempData td; for (int i = 1; i < tdc.GetLength(); i++) { td = tdc.GetData(i); bool show = false; if (td != null) { CalData cd = td.GetCalData(); if (cd.CalType == dt) { show = true; } if (!needJudge || show) { PushToShow(td.ToString()); } } } #endregion break; case "beep": int times = 10; int.TryParse(funs, out times); if (times == 0) { times = 10; } for (int i = 0; i < times; i++) { PushToShow("倒计时" + (times - i - 1) + "s"); Console.Beep(1000 * 7, 500); Console.Beep(500, 500); } Console.Beep(30000, 1000); break; case "matrix": ConsoleCal.UIController uc = new ConsoleCal.UIController(); uc.Run(); break; case "clc": if (string.IsNullOrEmpty(funs)) { tdc.RemoveAll(); PushToShow("已清空存储数据"); } else { if (tdc.CheckName(funs)) { tdc.Remove(tdc.FindPosition(funs)); PushToShow("clear " + funs); } else { PushToShow("fail to clear.Can't find " + funs); } } break; case "exit": Environment.Exit(0); break; case "clear": Console.Clear(); break; //----------------------这个GetFuncExpressTemp最多返回""不会返回null case "help": cm.Help(funstring.GetFuncExpressTemp()); break; } return; } if (tdc.CheckName(funstring.GetFuncName())) { PushToShow("该函数存在数据中"); if (funstring.GetParamers() != null) { PushToShow("进入函数求值过程"); FuncString funs = FindFuncInTDC(funstring.GetFuncName()); double result = GetFuncResult(funs, new FuncString(InputData).GetParamersData()); PushToShow("结果:" + result); } else { PushToShow("没有输入参数"); } return; // PushToShow("这是啥:" + tdc.GetData(funstring.GetFuncName()).GetValue().ToString()); } { var str = SimpleCal(InputData); if (str == null) { PushToShow("没有此函数"); } else { PushToShow(str); } return; } } if (blacket == 1) //没有括号 Name即是计算的表达式 { var Name = funstring.GetFuncName(); CheckDataType(Name); } }
//f(x,y)=x+y+t /// <summary> /// 替换已知变量,fstr是一个字符串表达式:x+y 不是f=x+y , /// 没有变量是会原串返回的 /// 里面找未知数时会调用底层 CalString 的函数,里面已经自动排除sin,cos,avg等 函数名了 /// </summary> /// <param name="fstr">含参表达式</param> /// <param name="ps">排除变量</param> /// <returns>返回FuncExpressTemp为替换后的string /// 或含有未知变量返回null</returns> public static string ReplaceParamers(string fstr, string[] ps = null) { string[] strarr = null; FuncString fs = new FuncString(fstr); // fs.SetFuncExpressTemp(fstr); fs.SetFuncExpress(fstr); //为向下兼容不出错 FuncCal fc = new FuncCal(); if (ps == null) { strarr = fs.FindUnKnown(); } else { strarr = fs.FindUnKnown(ps); } fs.SetParamers(strarr); //为向下兼容不出错,不明觉厉 if (strarr.Length != 0) { PushToShow("含有未知数 尝试替换已知变量"); } string[] paramersdata = new string[strarr.Length]; fc.SetFuncString(fs); for (int i = 0; i < strarr.Length; i++) { if (tdc.CheckName(strarr[i])) { CalData datatemp = tdc.GetData(strarr[i]).GetCalData(); switch (datatemp.CalType) { case DataType.BLOCK: { PushToShow("Type not match!"); } break; case DataType.EXP: { ExpData ed = (ExpData)datatemp; paramersdata[i] = ed.GetValueEx(); PushToShow("提取" + strarr[i] + "的值:" + paramersdata[i]); break; } case DataType.MATRIX: { Matrix matrix = (Matrix)datatemp; PushToShow(matrix.ToString()); } break; } // if (datatemp.CalType == DataType.EXP) { // ExpData ed = (ExpData)datatemp; // paramersdata[i] = ed.GetValueEx(); // PushToShow("提取" + strarr[i] + "的值:" + paramersdata[i]); // } // // else // PushToShow("Type not match!"); continue; } PushToShow("未发现变量" + strarr[i] + "还未定义其值\n退出"); return(null); } if (paramersdata.Length != 0) { fc.DealFuncstring(paramersdata); } else { fs.SetFuncExpressTemp(fstr); } return(fc.GetFuncString().GetFuncExpressTemp()); }
public void SetValue(CalData value) { this.Value = value; }
public TempData(string name, CalData value) { this.Name = name; this.Value = value; }