简体   繁体   English

如何从c#中输入的字符串(a + b)获取运算符类型?

[英]how to get the Operator type from the string(a + b) Input in c#?

For example If the user gives input as a + b, process should be done as adding two variables that has been already declared as int a, int b 例如,如果用户将输入作为a + b进行输入,则应添加两个已经声明为int a,int b的变量来完成该过程

int a = 7;
int b = 8;
string formula = console.readline();

this is where I need Help if the user types a + b as formula we should addition on that a and b, User can use any binary operator, formula should get that operator from user input, Please help 这是我需要帮助的地方,如果用户键入a + b作为公式,我们应该在该值上加上a和b,用户可以使用任何二进制运算符,公式应从用户输入中获取该运算符,请提供帮助

You try to evaluate the math expression. 您尝试评估数学表达式。 I suggest to use NCalc 我建议使用NCalc

NCalc is a mathematical expressions evaluator in .NET. NCalc是.NET中的数学表达式评估器。 NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions. NCalc可以解析任何表达式并评估结果,包括静态或动态参数以及自定义函数。

You could do like this: 您可以这样:

int a = 7;
int b = 8;
string formula = "a+b";
formula=formula.Replace("a",a.ToString()).Replace("b",b.ToString());

var calc = new System.Data.DataTable();
Console.WriteLine(calc.Compute(formula, ""));

Made this a while ago to evaluate expressions like this 前一阵子来评估这样的表达式

private static double Calc(string expression)
{
    try
    {
        var table = new System.Data.DataTable();
        table.Columns.Add("expression", string.Empty.GetType(), expression);
        System.Data.DataRow row = table.NewRow();
        table.Rows.Add(row);
        return double.Parse((string)row["expression"]);
    }
    catch (Exception)
    {
        return 0;
    }
}

This code allows the user to enter two numbers and the operator he or she wants 此代码允许用户输入两个数字,并输入他或她想要的运算符

Console.WriteLine("Please enter the first number");
            int a = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the operator you want to use");
            string c = Console.ReadLine() ;

            Console.WriteLine("Enter the last number");
            int b = int.Parse(Console.ReadLine());

            string formula = "acb";
            formula = formula.Replace("a", a.ToString()).Replace("b", b.ToString()).Replace("c", c.ToString());

            var calc = new System.Data.DataTable();
            Console.WriteLine(calc.Compute(formula, ""));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C#类型名称字符串获取类型? - How to get a Type from a C# type name string? 如何从字符串c#中获取自定义类的类型 - How to get type of custom class from string c# 从html输入文本类型文本框中获取值作为字符串,并将其传递给在aspx中使用的C#哈希表 - Get value from html input text type textbox as string and pass it to C# hashtable being used in aspx 如何使用Split在C#中获取字符串中的字符和运算符 - How to get the character and operator in a string in C# using Split C#String:为什么字符串a == b运算符给出的答案不同于a.CompareTo(b)== 0? - C# String: why string a == b operator gives different answer than a.CompareTo(b) == 0? 如何从 C# 中的字符串解析数学运算符 - How can I parse math operator from string in c# 如何从C#中的文本框获取输入? - How to get input from textbox in C#? 如何溢出字符串以从AAA(ABC)获取ABC(ABC)(ABC:abc) - How to spilt a string to get ABC( A B C) from AAA( A B C) (ABC:abc) 如何使用C#从输入类型=“颜色”中获取颜色名称 - How to get the color's name from input type=“color” with C# C# 错误运算符 * 不能应用于“字符串”和“字符串”类型的操作数 - C# ERROR Operator * cannot be applied to operands of type 'string' and 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM