具有SplitStrings的C#计算器 [英] Calculator in C# with SplitStrings

查看:92
本文介绍了具有SplitStrings的C#计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hej在这里,
我只是C#中的新手,对于火车,我想创建一个Console-Calculator.我只有一个简单的可以1 + 1和1-1的游戏,依此类推,但是现在我想要一个更大的游戏,它可以观察到嗯,它在英语中被称为点到短跑规则"吗?我不知道抱歉,不过是一个例子->

我简单的计算器就是这样->

2 + 2 * 2 = 8

但我希望他像->
一样工作
2 + 2 * 2 = 6

希望你们知道我的意思;)

因此,尽管 t是将计算分为数字和单数形式的最佳方法.

这已经可以用了,但是我不知道该怎么做才能得到计算结果:X

希望你们能帮助我.我已经准备好的继承人代码:

Hej there,
Iam just new in C# and for train I want to create a Console-Calculator. I just got a simple one that can 1+1 and 1-1 and so on but now i want a bigger one that observes the hmm is it called Dot to Dash rule in English? I don´t know sorry but an example ->

My simple Calculator is working like this ->

2+2*2 = 8

but i want him to work like ->

2+2*2 = 6

Hope you guys know what i mean ;)

So i thought it would be the best way to split up the calculation in numbers and sings.

This works allready but i don´t know what I should do right now to get the result of a calculation :X

Hope you guys can help me. Heres my Code that i wrote allready:

{
    public static void Main()
    {
        int count = 0;

        Console.WriteLine("Insert your Calculation");
        string calculation = Console.ReadLine();
        string[] splitnumbers = calculation.Split(new Char[] { ''+'', ''-'', ''*'', ''/'' });
        string[] splitsigns = calculation.Split(new Char[] { ''0'', ''1'', ''2'', ''3'', ''4'', ''5'', ''6'', ''7'', ''8'', ''9'' });
        foreach (string s in splitnumbers)
        {
            count++;
            if (s.Trim() != "")
                Console.WriteLine(s);
            Console.WriteLine(splitsigns[count]);

            Console.WriteLine("For exit press Enter");
            Console.Read();
        }
    }
}



[已将OP评论从答案中移出]
好吧,我现在只想走另一条路...



[Moved OP comment from answer]
Okay now i just want to go another way...

for (int i = 0; i < splitnumbers.Length - 1; i++)
{
    int firstNumber = int.Parse(splitnumbers[i].ToString());
    int secondNumber = int.Parse(splitnumbers[i + 1].ToString());
    string operatorString = splitsigns[i + 1].ToString();

}


但是现在呢?以为我必须得到这个结果或水木清华.并计算出下一步,例如5 + 6 * 7!?


But what now? Thought i have to get the result of this or smth. and denn calculate the next step in for example 5+6*7!?

推荐答案

您想用Google搜索计算器:您需要实现某种运算符堆栈因此您可以使用运算符优先级.

基本上,您不能再从右至左处理运算符:您必须认识到"*"的优先级比"+"高,并首先对其进行评估.去吧,谷歌:那里有很多东西! :laugh:
You want to google for calculators: you need to implement an operator stack of some kind so you can use operator precedence.

Basically, you can''t process operators Right-To-Left any more: you must recognise that "*" has a higher precedence than "+" and evaluate it first. Go, Google: there is a lot out there! :laugh:




您想要的是一个数学表达式解析器.去谷歌搜索数学表达式解析器C#",您会发现很多信息.以下是一些我认为很有帮助的链接:
带有插件的可扩展数学表达式解析器 [ http://www.c-sharpcorner.com/uploadfile/patricklundin/mathexpparser12062005062213am/mathexpparser.aspx [ ^ ]
http://muparser.sourceforge.net/ [ ^ ]
Hi,

what you want is a mathematical expression parser. Go and google for "mathematical expression parser c#" and you will find lots of information. Here are some links I found to be informative:
An extensible math expression parser with plug-ins[^]
http://www.c-sharpcorner.com/uploadfile/patricklundin/mathexpparser12062005062213am/mathexpparser.aspx[^]
http://muparser.sourceforge.net/[^]


这篇关于具有SplitStrings的C#计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆