Click here to Skip to main content
14,455,745 members
Rate this:
Please Sign up or sign in to vote.
See more:
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)
            if (s.Trim() != "")

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

[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();


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!?
Updated 22-Jan-20 1:33am
Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
fjdiewornncalwe 23-Feb-11 9:57am
Go - Go - Googlicious - Go
Rate this:
Please Sign up or sign in to vote.

Solution 4


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[^][^][^]
Espen Harlinn 25-Feb-11 6:58am
Excellent advice, my 5
Rate this:
Please Sign up or sign in to vote.

Solution 7

Well you need to go back to theory, Read Postfix notation as this is the preciese way of doing what you want to do. Read This STACK and POSTFIX Notation[^]

Once you grab the idea than a simple google search will give you tons of examples implemented in almost all languages, the choice is yours.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100