Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用字符串变量作为算术运算符_C#_C# 4.0 - Fatal编程技术网

C#使用字符串变量作为算术运算符

C#使用字符串变量作为算术运算符,c#,c#-4.0,C#,C# 4.0,我是C#新手,我正在尝试创建一个计算器。 我把一个算术运算符存储在一个字符串中,有没有办法用这个运算符来解这个方程 我的代码: else if (btn_name == "plus" || btn_name == "minus") { bol = true; try { if (solve == "") { string num1 = Screen.Text; first = float.Pa

我是C#新手,我正在尝试创建一个计算器。 我把一个算术运算符存储在一个字符串中,有没有办法用这个运算符来解这个方程

我的代码:

else if (btn_name == "plus" || btn_name == "minus")
{
    bol = true;
    try
    {
        if (solve == "")
        {
            string num1 = Screen.Text;
            first = float.Parse(num1);
            second = first + second;
            Screen.Text = second.ToString();
            Screen_upper.Text = upper_text + text + " + ";
        }
        else
        {
            second = first (**#I want to use the operator here#**) second;
        }
    }
    catch
    {
        Screen.Text = second.ToString();
        Screen_upper.Text = upper_text + text + " + ";
    }
    solve = "+";
}

不,我想你不能。检查btn_名称中的值,如果为“减”,则执行算术运算

if (btn_name == "minus")
{
    second = first - second;
}

您可以只使用
if
switch
运算符,并为每个
else/case
块分支逻辑:

switch(btn_name)
{
    case "plus":
        second = first + minus;
        break;
    case "minus":
        second = first - minus;
        break;
    case "div":
        second = first / minus;
        break;
}
但是阅读这段代码非常困难。另一种解决方案是使用预定义的操作员存储库。实现这一点的一种方法是使用简单和:

这可能会更好,因为您将拆分操作符初始化和使用,并使代码位清晰


另一个场景是为每个按钮使用不同的事件处理程序,并在处理程序内实现必要的逻辑。但是,在合并重复部分(数字解析和更新结果)之前,您需要重构代码,最终的解决方案与基于字典的解决方案几乎相同。

我认为最好的方法是使用一个字典,其中键作为操作名和委托值, 乙二醇

var dict=新字典(){
{“添加”,AddFunc},
{“负”,MinusFunc}
};
公共静态int AddFunc(int arg1,int arg2)
{
返回arg1+arg2;
}
公共静态int MinusFunc(int arg1,int arg2)
{
返回arg1-arg2;
}
调用这些方法非常简单,如
dict[“减号”](arg1,arg2)


当然,使用枚举而不是字符串更有意义,但你明白了

有一个实用程序提供泛型运算符作为方法。希望这将帮助您找到一个好的解决方案。也可以使用枚举而不是字符串来定义所有操作。我删除了以前的注释,因为它不清楚,我需要重新措辞。有很多方法可以实现这一点。然而,就直接将其转换为运算符而言,答案是“否”。这对你真的很有帮助。它显示了两种可能的方法,都有相关的结果。自定义类和带有逻辑开关的泛型是实现它的好方法。是的,我也这么做,但是如果我有30个操作,那么就意味着30个if-else块。这就是我想说的,但没有源代码。对我来说,听起来像是家庭作业。英语很难+1.
// it's class member, not a local variable 
Dictionary<string, Func<float, float, float>> operators = new Dictionary<string, Func<float, float, float>>();
operators.Add("plus", (first, second) => first + second);
operators.Add("minus", (first, second) => first - second);
operators.Add("div", (first, second) => first / second);
if (solve == "")
{
    string num1 = Screen.Text;
    first = float.Parse(num1);
    second = first + second;
    Screen.Text = second.ToString();
    Screen_upper.Text = upper_text + text + " + ";
}
else
{
    second = operators[btn_name](first, second);
}
var dict = new Dictionary<string, Func<int, int, int>> () {
    { "add", AddFunc },
    { "minus", MinusFunc }
};

public static int AddFunc(int arg1, int arg2)
{
    return arg1 + arg2;
}

public static int MinusFunc(int arg1, int arg2)
{
    return arg1 - arg2;
}