C# 我想用一个按钮C做数学运算#

C# 我想用一个按钮C做数学运算#,c#,C#,我想用一个按钮和文本框进行数学运算,当输入文本框操作如25+5+-10+7并按下按钮显示结果时 我正试着这样做,但我做不到。 在这段代码中,我为textbox中的每个字符做了一个标记,我想在firstvalue参数中存储25,在op参数中存储+,在secondvalue参数中存储5 并使操作25+5=30并将其存储在firstvalue参数中,将+-存储在op中,将10存储在secondvalue中并进行操作,将其存储在firstvalue中…依此类推,直到字符串长度。 但我不知道在哪里以及如何

我想用一个按钮和文本框进行数学运算,当输入文本框操作如25+5+-10+7并按下按钮显示结果时 我正试着这样做,但我做不到。 在这段代码中,我为textbox中的每个字符做了一个标记,我想在firstvalue参数中存储25,在op参数中存储+,在secondvalue参数中存储5 并使操作25+5=30并将其存储在firstvalue参数中,将+-存储在op中,将10存储在secondvalue中并进行操作,将其存储在firstvalue中…依此类推,直到字符串长度。 但我不知道在哪里以及如何存储第二个值

private void button1_Click(object sender, EventArgs e)
    {
        string allValue = textBox1.Text;
        int firstValue=0;
        int secondValue=0;
        string first = string.Empty;
        string op = string.Empty;


        foreach (char item in allValue)
        {
            if (char.IsNumber(item))
            {
                first += item;

            }

            else if(item=='+' || item=='-' || item=='*' || item=='/')
            {
                firstValue = Int32.Parse(first);
                op += item;
                first = "";

                switch (op)
                {
                    case "+":
                        firstValue = firstValue + secondValue;
                        break;
                    case "-":
                        firstValue = firstValue - secondValue;
                        break;
                    case "+-":
                        firstValue = firstValue +- secondValue;
                        break;
                    case "*":
                        firstValue = firstValue * secondValue;
                        break;
                    case "/":
                        firstValue = firstValue / secondValue;
                        break;

                }
            }
        }


        MessageBox.Show(firstValue.ToString());
    }
你可以使用图书馆。您可以这样使用它:

NCalc.Expression e = new NCalc.Expression(textBox1.Text);
double value = e.Evaluate();
// Next, do something with your evaluated value.

如果要从头到尾解析字符串并在解析过程中进行计算,则在拥有两个操作数之前,无法在两个值之间进行计算。这意味着您必须等待找到第二个运算符(或字符串的结尾),直到可以对第一个运算符进行计算

解析数字时,将其放入
secondValue
变量中。如果它是第一个值,则只需将其移动到
firstValue
并继续获取另一个值,否则使用根据上一个运算符得到的两个值进行计算

要包含最后一次计算,可以在字符串中的最后一个字符之外循环一个步骤。这允许您的循环在最后一个值之后再运行一次,您可以进行最后一次计算。不过,它还需要一些检查,这样您就不会真正尝试从字符串之外的位置读取

要处理负值,应将其包含在值中,而不是使用
+-
运算符。否则,您还需要
-
*-
/-
运算符

示例(未测试):

private void按钮1\u单击(对象发送者,事件参数e){
string allValue=textBox1.Text;
int firstValue=0;
int secondValue=0;
string second=string.Empty;
string op=string.Empty;
对于(int i=0;i 0){
开关(op){
大小写“+”:第一个值+=第二个值;中断;
大小写“-”:第一个值-=第二个值;中断;
案例“*”:第一个值*=第二个值;中断;
大小写“/”:firstValue/=secondValue;中断;
}
}否则{
第一个值=第二个值;
}
如果(i

注意:这严格从左到右计算,不处理优先级,例如,
1+2*3
被评估为
(1+2)*3
,而不是通常使用的
1+(2*3)

从头开始做所有事情并不容易。在我的简化回答中,我假定您的运算符仅为
+-*/
(包括它们的优先级)

参考:

首先,您应该解析输入字符串。。。我的方式是

public enum TokenType
{
    Operator,
    Operand
}

public class Token
{
    public string Value { get; set; }
    public TokenType Type { get; set; }

    public override string ToString()
    {
        return Type + "   " + Value;
    }
}

IEnumerable<Token> Parse(string input)
{
    return Regex.Matches(input, @"(?<value>\d+)|(?<type>[\+\-\*\/\(\)])").Cast<Match>()
                .Select(m => m.Groups["value"].Success
                        ? new Token() { Value = m.Groups["value"].Value, Type = TokenType.Operand }
                        : new Token() { Value = m.Groups["type"].Value, Type = TokenType.Operator });
}
现在可以测试代码了

int r1 = Eval("25 - 5");
int r2 = Eval("25 - 5*2");
int r3 = Eval("25 - 5 + 3*2");
int r4 = Eval("25/5 + 7*3");
int r5 = Eval("25/5 + 7*3 + 2");
int r6 = Eval("25/5 + 7*3 * 2");

这对你有用吗?正如前面的回答中所提到的,这不考虑操作顺序,并且有许多其他错误检查可能需要完成

using System.Text.RegularExpressions;

private void button1_Click(object sender, EventArgs e)
{
    MatchCollection values = Regex.Matches(textBox1.Text, "[0-9]+");
    MatchCollection operations = Regex.Matches(textBox1.Text, @"[\/\*\+-]");
    if (values.Count == operations.Count + 1)
    {
        int total = int.Parse(values[0].Value);
        if (operations.Count > 0)
        {
            for (int index = 1; index < values.Count; index++)
            {
                int value = int.Parse(values[index].Value);
                switch (operations[index - 1].Value)
                {
                    case "+":
                        total += value;
                        break;
                    case "-":
                        total -= value;
                        break;
                    case "/":
                        total /= value;
                        break;
                    case "*":
                        total *= value;
                        break;
                }

            }
        }
        MessageBox.Show(total.ToString());
    }
}
使用System.Text.regular表达式;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
MatchCollection值=Regex.Matches(textBox1.Text,“[0-9]+”);
MatchCollection operations=Regex.Matches(textBox1.Text,@“[\/\*\+-]”;
if(values.Count==operations.Count+1)
{
int total=int.Parse(值[0].Value);
如果(operations.Count>0)
{
for(int index=1;index
有些库(可能还有在线示例)会使这个问题变得非常简单。NCalc库就是其中之一:这种方法似乎有很多问题。。。主要是一个foreach循环,它盲目地循环,这意味着它不需要计算操作的顺序。你应该一步一步地做事情,确保每一位都正常工作,而不是继续添加代码直到出现错误。此外,这也不是你应该问的问题,所以:你必须正确理解问题,只包括代码的相关部分(不是期望有人调试它的所有内容)。除此之外,我认为这是一个很好的练习来适应C(甚至学习编程)。使用图书馆?一般来说,是用.NET吗?为了什么?如果你想要的是简单的(只是加/减),你可以自己做。如果有困难,就不会有任何图书馆。。。。。。。数学解析可能变得非常复杂。库用于简单(和通用)任务。虽然这取决于你。这不是一个有效的答案。甚至连“我应该如何解析一个等式”的有效答案都没有?这可能是对“我几乎不懂任何编程,不想学习,并且对立即将两个数字相加感兴趣,我该怎么做?”的回答。记住推荐一个库是离题的,那么,你认为推荐一个库(给一个甚至没有要求这样的东西的人;甚至没有明确定义最终目标的人)是离题的吗?很抱歉,这个库确实解决了我的问题,但我想通过编程来解决它
int r1 = Eval("25 - 5");
int r2 = Eval("25 - 5*2");
int r3 = Eval("25 - 5 + 3*2");
int r4 = Eval("25/5 + 7*3");
int r5 = Eval("25/5 + 7*3 + 2");
int r6 = Eval("25/5 + 7*3 * 2");
using System.Text.RegularExpressions;

private void button1_Click(object sender, EventArgs e)
{
    MatchCollection values = Regex.Matches(textBox1.Text, "[0-9]+");
    MatchCollection operations = Regex.Matches(textBox1.Text, @"[\/\*\+-]");
    if (values.Count == operations.Count + 1)
    {
        int total = int.Parse(values[0].Value);
        if (operations.Count > 0)
        {
            for (int index = 1; index < values.Count; index++)
            {
                int value = int.Parse(values[index].Value);
                switch (operations[index - 1].Value)
                {
                    case "+":
                        total += value;
                        break;
                    case "-":
                        total -= value;
                        break;
                    case "/":
                        total /= value;
                        break;
                    case "*":
                        total *= value;
                        break;
                }

            }
        }
        MessageBox.Show(total.ToString());
    }
}