【问题标题】:how to do basic math calculations with a list of numbers and operators如何使用数字和运算符列表进行基本数学计算
【发布时间】:2013-03-22 13:07:06
【问题描述】:

我有一个包含 5 个数字的小数类型列表:{10, 9, 100,73,3457}。我在这些数字之间还有另外 4 个字符串类型运算符的列表:* 乘法 - 减法 / 除法 + 加法。

我怎样才能得到字符串的结果?我不知道如何强加运算符优先级。

10 * 9 - 100 / 73 + 3457 = ???

这是我的代码,我以完全错误的方式处理问题,应该怎么做?

static List<decimal> numbers = new List<decimal>();
static List<string> operators = new List<string>();

foreach (decimal number in numbers)
{
    foreach (string operatorz in operators)
    {
        resultnumber = 
    }
}

【问题讨论】:

  • 你真的需要计算 20 次的结果,还是循环可选?
  • 谷歌“skeet misc 实用程序” - 隐藏在其中的一个奇妙的数学运算符库可以很好地与通用概念配合使用。可能无法解决您的问题,但绝对“有用”。
  • 程序员应该使用这样的库或现成的工具:stackoverflow.com/questions/4620437/… 如果你想自己编写胆量,你必须研究这里定义的一些算法:stackoverflow.com/questions/1437964/…,询问对于堆栈溢出来告诉您键入哪些字母违背了练习的目的,您可能根本不这样做。结果是一样的。
  • 这个练习有什么意义?您是否正在尝试学习如何构建解析器?

标签: c# list


【解决方案1】:

希望我得到了你想要的,这将使所有数字交叉连接到它们自身,然后进入运算符。您已经列出了二元运算符,所以我想每个运算符都需要两个操作数。结果行数为 = numbers.Length * numbers.Length * operators.Length

void Main()
{
   var numbers = new[] { 10, 9, 100, 73, 3457 };
   var operators = new [] { "*", "+", "/", "-" };


    var r = from n1 in numbers
            from n2 in numbers
            from op in operators
            select string.Format("{0} {1} {2} = {3}",
                                  n1, op, n2,   Perform(n1, op, n2));

}

public int Perform(int val1, string @operator, int val2)
{
    //main core if your question, consider to extract operators to outer level
    var operators = new Dictionary<string, Func<int, int, int>>
    {
        {"+", (v1, v2) => v1 + v2},
        {"/", (v1, v2) => v1 / v2},
        {"*", (v1, v2) => v1 * v2},
        {"-", (v1, v2) => v1 - v2},
    };

    return operators[@operator](val1, val2);
}

结果是

10 * 10 = 100 
10 + 10 = 20 
10 / 10 = 1 
10 - 10 = 0 
10 * 9 = 90 
....

【讨论】:

    【解决方案2】:

    不完全清楚你想要什么。但是假设这些运算符位于数字之间,并解析为本地运算符优先级:简单的方法是编写脚本。您的运算符优先级将是本地的。

    Scripting

    private static void Main(string[] args)
    {
        var numbers = new[]{ 10, 9, 100, 73, 3457 };
        var ops = new[] { "*","-","/","+" };
    
        if (numbers.Length != ops.Length + 1)
            throw new Exception("TODO");
    
        string statement = numbers[0].ToString();
    
        for (int i = 0; i < ops.Length; i++)
        {
            statement += string.Format(" {0} {1}", ops[i], numbers[i + 1]);
        }
    
        Console.WriteLine("Statement: " + statement);
    
        var method = String.Format(@"int Product() {{ return {0}; }} ", statement);
    
        Console.WriteLine("Method: " + method);
    
    
        var Product = CSScript.Evaluator.CreateDelegate(method);
        int result = (int)Product();
    
        Console.WriteLine(result);
    }
    

    结果:

    Statement: 10 * 9 - 100 / 73 + 3457
    Method: int Product() { return 10 * 9 - 100 / 73 + 3457; }
    3546
    Press any key to continue . . .
    

    【讨论】:

      【解决方案3】:

      http://msdn.microsoft.com/fr-fr/library/s53ehcz3(v=VS.80).aspx

      您可以使用自己的类来处理计算

      【讨论】:

        猜你喜欢
        • 2017-08-07
        • 1970-01-01
        • 2017-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多