公式例如 a+{[(b+c)*(c+d)]+ [(b-c)*(c-d)]}*2
我将变量替换了,有些变量还是负数,请问如何解决这样的问题。
请给一个字符串计算的类的源码。谢谢

解决方案 »

  1.   

     a+{[(b+c)*(c+d)]+   [(b-c)*(c-d)]}*2 
    ===========================
    这是字符串么?
      

  2.   

    参考这个:
    http://www.cnblogs.com/skyiv/archive/2007/08/21/Calc.html
      

  3.   

     1// SuperCalc.cs - 超级计算器
     2// 编译方法: csc /t:winexe SuperCalc.cs VBExpression.cs
     3
     4using System;
     5using System.Windows.Forms;
     6using Skyiv.Util;
     7
     8namespace Skyiv
     9{
    10  class Calc : Form
    11  {
    12    TextBox tbxA1;
    13    TextBox tbxA3;
    14
    15    Calc()
    16    {
    17      Text              = "Super Calculator";
    18      StartPosition     = FormStartPosition.CenterScreen;
    19      Width             = 300;
    20      Height            = 300;
    21
    22      tbxA1             = new TextBox();
    23      tbxA1.Parent      = this;
    24      tbxA1.Multiline   = true;
    25      tbxA1.WordWrap    = false;
    26      tbxA1.Dock        = DockStyle.Fill;
    27      tbxA1.BorderStyle = BorderStyle.FixedSingle;
    28
    29      Panel pnlA1       = new Panel();
    30      pnlA1.Parent      = this;
    31      pnlA1.Height      = 22;
    32      pnlA1.Dock        = DockStyle.Top;
    33
    34      tbxA3             = new TextBox();
    35      tbxA3.Parent      = pnlA1;
    36      tbxA3.Dock        = DockStyle.Fill;
    37      tbxA3.BorderStyle = BorderStyle.FixedSingle;
    38      tbxA3.ReadOnly    = true;
    39
    40      Button btnA3      = new Button();
    41      btnA3.Text        = "&Calculate";
    42      btnA3.Parent      = pnlA1;
    43      btnA3.Width       = 80;
    44      btnA3.Dock        = DockStyle.Left;
    45      btnA3.Click      += new EventHandler(Calc_Clicked);
    46    }
    47
    48    void Calc_Clicked(object sender, EventArgs ea)
    49    {
    50      (sender as Control).Enabled = false;
    51      try
    52      {
    53        tbxA3.Text = (new Expression(tbxA1.Text)).Compute().ToString();
    54      }
    55      catch (Exception ex)
    56      {
    57        MessageBox.Show(ex.Message, "Error");
    58      }
    59      finally
    60      {
    61        (sender as Control).Enabled = true;
    62      }
    63    }
    64
    65    [STAThread]
    66    static void Main(string [] args)
    67    {
    68      Application.Run(new Calc());
    69    }
    70  }
    71} 1// VBExpression.cs - 动态生成数学表达式并计算其值
     2// 表达式使用 Visual Baisc 语法
     3// 可使用 pi、e 等常量,sin、cos、tan、log、sqrt 等函数
     4
     5using System;
     6using System.CodeDom.Compiler;
     7using Microsoft.VisualBasic;
     8using System.Reflection;
     9using System.Text;
    10using System.Globalization;
    11
    12namespace Skyiv.Util
    13{
    14  sealed class Expression
    15  {
    16    object instance;
    17    MethodInfo method;
    18
    19    public Expression(string expression)
    20    {
    21      if (expression.ToUpper(CultureInfo.InvariantCulture).IndexOf("RETURN") < 0)
    22      {
    23        expression = "Return " + expression.Replace(Environment.NewLine, " ");
    24      }
    25      string className = "Expression";
    26      string methodName = "Compute";
    27      CompilerParameters p = new CompilerParameters();
    28      p.GenerateInMemory = true;
    29      CompilerResults cr = new VBCodeProvider().CompileAssemblyFromSource
    30      (
    31        p,
    32        string.Format
    33        (
    34          @"Option Explicit Off
    35          Option Strict Off
    36          Imports System, System.Math, Microsoft.VisualBasic
    37          NotInheritable Class {0}
    38          Public Function {1} As Double
    39          {2}
    40          End Function
    41          End Class",
    42          className, methodName, expression
    43        )
    44      );
    45      if(cr.Errors.Count > 0)
    46      {
    47        string msg = "Expression(\"" + expression + "\"): \n";
    48        foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
    49        throw new Exception(msg);
    50      }
    51      instance = cr.CompiledAssembly.CreateInstance(className);
    52      method = instance.GetType().GetMethod(methodName);
    53    }
    54
    55    public double Compute()
    56    {
    57      return (double)method.Invoke(instance, null);
    58    }
    59  }
    60}
      

  4.   

    应该是不能用大括号跟中括号的吧,全部只能用小括号!
    a+(((b+c)*(c+d))+((b-c)*(c-d)))*2 
      

  5.   

    a=1:b=2:c=3:d=4
    a+(((b+c)*(c+d))+((b-c)*(c-d)))*2把这些内容放到文本框就好了。
      

  6.   

    这样:a=1:b=2:c=3:d=4 
    retrun a+(((b+c)*(c+d))+((b-c)*(c-d)))*2