1
\$\begingroup\$

I have this calculator. The user is asked to provide three numbers as well as the numeric operation they wish to perform. Based on this input, a certain case will be executed. The calculator is supposed to cover the basic arithmetic options such as + - * / as well as that "dividing or multiplying numbers are stronger actions than adding or subtracting numbers", if that makes sense (sorry, I am not a native speaker).

I think it works, but the code is clumsy and way too long. I feel like there must be a better way, but I don't really know how to improve the code as I am new to this.

This is the code:

using System;

namespace Program
{
    class Taschenrechner2
    {
        public static void Main()
        {
            double eingabe1, eingabe2, eingabe3; string rechenoperator1, rechenoperator2;

            Console.WriteLine("Geben Sie die erste Zahl ein.");
            eingabe1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Geben Sie die erste Rechenoperation ein.");
            rechenoperator1 = Console.ReadLine();

            Console.WriteLine("Geben Sie die zweite Zahl ein.");
            eingabe2 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Geben Sie die zweite Rechenoperation ein.");
            rechenoperator2 = Console.ReadLine();

            Console.WriteLine("Geben Sie die dritte Zahl ein.");
            eingabe3 = Convert.ToDouble(Console.ReadLine());

            if (rechenoperator1 == "-" && rechenoperator2 == "-")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 - eingabe2 - eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "+" && rechenoperator2 == "+")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 + eingabe2 + eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "+" && rechenoperator2 == "-")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 + eingabe2 - eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "-" && rechenoperator2 == "+")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 - eingabe2 + eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "*" && rechenoperator2 == "+")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 * eingabe2) + eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "+" && rechenoperator2 == "*")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 + eingabe2) * eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "-" && rechenoperator2 == "*")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 - eingabe2) * eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "*" && rechenoperator2 == "-")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 * eingabe2) - eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "*" && rechenoperator2 == "*")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 * eingabe2 * eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "*" && rechenoperator2 == "/")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 * eingabe2) / eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "/" && rechenoperator2 == "*")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 / eingabe2) * eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "-" && rechenoperator2 == "/")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 - (eingabe2 / eingabe3)));
                Console.ReadKey();
            }
            if (rechenoperator1 == "/" && rechenoperator2 == "-")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 / eingabe2) - eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "+" && rechenoperator2 == "/")
            {
                Console.WriteLine("Das Ergebnis ist: " + (eingabe1 + (eingabe2 / eingabe3)));
                Console.ReadKey();
            }
            if (rechenoperator1 == "/" && rechenoperator2 == "+")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 / eingabe2) + eingabe3));
                Console.ReadKey();
            }
            if (rechenoperator1 == "/" && rechenoperator2 == "/")
            {
                Console.WriteLine("Das Ergebnis ist: " + ((eingabe1 / eingabe2) / eingabe3));
                Console.ReadKey();
            }
        }
    }
}

I have tried to teach myself how to program multiple times, but every time I didn't manage to get further than the basics... This time I hope to get further, so any help would be appreciated!

\$\endgroup\$
1
  • \$\begingroup\$ dividing or multiplying numbers are stronger actions than adding or subtracting numbers "precedence" is word you want. It means "priority of action" As in: "multiplication takes precedence over addition" \$\endgroup\$
    – radarbob
    Nov 18 '21 at 0:50
1
\$\begingroup\$

Welcome to the site! The best way to learn is to experiment and have fun!

The key to simplifying code is to find patterns. This will be easier to find if we sort all the if statements by operation like the following:

if (rechenoperator1 == "+" && rechenoperator2 == "+") { ... }
if (rechenoperator1 == "+" && rechenoperator2 == "-") { ... }
if (rechenoperator1 == "+" && rechenoperator2 == "*") { ... }
if (rechenoperator1 == "+" && rechenoperator2 == "/") { ... }
if (rechenoperator1 == "-" && rechenoperator2 == "+") { ... }
if (rechenoperator1 == "-" && rechenoperator2 == "-") { ... }
if (rechenoperator1 == "-" && rechenoperator2 == "*") { ... }
if (rechenoperator1 == "-" && rechenoperator2 == "/") { ... }
if (rechenoperator1 == "*" && rechenoperator2 == "+") { ... }
if (rechenoperator1 == "*" && rechenoperator2 == "-") { ... }
if (rechenoperator1 == "*" && rechenoperator2 == "*") { ... }
if (rechenoperator1 == "*" && rechenoperator2 == "/") { ... }
if (rechenoperator1 == "/" && rechenoperator2 == "+") { ... }
if (rechenoperator1 == "/" && rechenoperator2 == "-") { ... }
if (rechenoperator1 == "/" && rechenoperator2 == "*") { ... }
if (rechenoperator1 == "/" && rechenoperator2 == "/") { ... }

See the pattern? I hope so :-)

What can we do with this? First, we can create a function like the following to take care of the first operation:

public static double Berechnungsteil(double eingabe1, double eingabe2, string operation)
{
    switch (operation)
    {
        case "+":
            return eingabe1 + eingabe2;
        case "-":
            return eingabe1 - eingabe2;
        case "*":
            return eingabe1 * eingabe2;
        case "/":
            return eingabe1 / eingabe2;
        default:
            return 0;
    }
}

This can then be used to calculate the first part like so:

double resultFirstPart = Berechnungsteil(eingabe1, eingabe2, rechenoperator1);

To calculate the final result we can take resultFirstPart and call `Berechnungsteil' again:

double result = Berechnungsteil(resultFirstPart, eingabe3, rechenoperator2);

To get the correct order of operations, we can simply check if the first operation is * or /.

Putting it all together:

using System;

namespace Program
{
    class Taschenrechner2
    {
        public static double Berechnungsteil(double eingabe1, double eingabe2, string operation)
        {
            switch (operation)
            {
                case "+":
                    return eingabe1 + eingabe2;
                case "-":
                    return eingabe1 - eingabe2;
                case "*":
                    return eingabe1 * eingabe2;
                case "/":
                    return eingabe1 / eingabe2;
                default:
                    return 0;
            }
        }

        public static void Main()
        {
            Console.WriteLine("Geben Sie die erste Zahl ein.");
            double eingabe1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Geben Sie die erste Rechenoperation ein.");
            string rechenoperator1 = Console.ReadLine();

            Console.WriteLine("Geben Sie die zweite Zahl ein.");
            double eingabe2 = double.Parse(Console.ReadLine());

            Console.WriteLine("Geben Sie die zweite Rechenoperation ein.");
            string rechenoperator2 = Console.ReadLine();

            Console.WriteLine("Geben Sie die dritte Zahl ein.");
            double eingabe3 = double.Parse(Console.ReadLine());

            if (rechenoperator1 == "*" || rechenoperator1 == "/")
            {
                double resultFirstPart = Berechnungsteil(eingabe1, eingabe2, rechenoperator1);
                Console.WriteLine("Das Ergebnis ist: " + Berechnungsteil(resultFirstPart, eingabe3, rechenoperator2));
            }
            else
            {
                double resultFirstPart = Berechnungsteil(eingabe2, eingabe3, rechenoperator2);
                Console.WriteLine("Das Ergebnis ist: " + Berechnungsteil(resultFirstPart, eingabe1, rechenoperator1));
            }
        }
    }
}
\$\endgroup\$
8
  • \$\begingroup\$ Thank you, I wouldn't have thought of that myself. How can I improve my way of thinking in that regard? Any project ideas maybe or tips? \$\endgroup\$
    – morloq
    Nov 18 '21 at 8:31
  • \$\begingroup\$ @morloq, It's all about breaking down problems into manageable parts. Get out the whiteboard and ask yourself: what are the logical steps that I need to do to make this work. Implementing PEMDAS math in code is supprising difficult and I know a couple of decent programmers that would not have gotten as far as you did :-). Project ideas: simple games like tick tack toe, checkers, card games, etc. If you are feeling brave create your own calculator that can work on strings like the following (3 * 3 + 4 * 4) ^ .5 \$\endgroup\$
    – jdt
    Nov 18 '21 at 13:02
  • \$\begingroup\$ Thanks, I think Ill go with tack tack toe. Is it bad for the learning process to google if you are stuck or do not know how to implement something? For instance, I know what tick tack toe is and how it should work but I have no idea where to start and how to even generate the graphic. \$\endgroup\$
    – morloq
    Nov 18 '21 at 13:04
  • \$\begingroup\$ @morloq, not at all. \$\endgroup\$
    – jdt
    Nov 18 '21 at 13:06
  • \$\begingroup\$ @morloq, here is something to get you started TIO \$\endgroup\$
    – jdt
    Nov 20 '21 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.