Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<pre>using System;
using System.Collections.Generic;

class Calculator
{
    public string FirstNumber = "0";
    public string SecondNumber = "0";
    public int IntFirstNumber;
    public int IntSecondNumber;
    public string Operator = "";
    public string OperatorType = "";
    public bool isOperator = false;
    public string ShowNumber = "";
    public string UserInput = "";
    public int Result;
    public double Calculate()
    {
        string Plus = "+";
        string Subtract = "-";
        string Multiply = "*";
        string Devide = "/";

        while (0 < 1)
        {
            Console.WriteLine("Enter any Value or Operator");
            string UserInput = Console.ReadLine();
            ShowNumber = ShowNumber + UserInput;
            List<string> TotalInfo = new List<string>();

            TotalInfo.Add(ShowNumber);
            foreach (var item in TotalInfo)
            {

                if (FirstNumber == "0")
                {
                    FirstNumber = UserInput;
                    IntFirstNumber = int.Parse(FirstNumber);
                }
                else if (UserInput == "+" || UserInput == "-" || UserInput == "*" || UserInput == "/")
                {
                    OperatorType = UserInput;
                    if (Operator == Plus)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                    else if (UserInput == Subtract)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                    else if (UserInput == Multiply)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                    else if (UserInput == Devide)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                }
                else if (UserInput == "=")
                    break;

                else
                {
                    SecondNumber = UserInput;

                    IntSecondNumber = int.Parse(SecondNumber);
                }

            }
            if (UserInput == "=")
            {
                if (OperatorType == "+")
                {
                    Result = IntFirstNumber + IntSecondNumber;
                }
                else if (OperatorType == "*")
                {
                    Result = IntFirstNumber * IntSecondNumber;
                }
                else if (OperatorType == "-")
                {
                    Result = IntFirstNumber - IntSecondNumber;
                }
                else if (OperatorType == "/")
                {
                    Result = IntFirstNumber / IntSecondNumber;

                }

                {
                    Console.WriteLine(Result);
                    Console.ReadLine();
                    IntFirstNumber = Result;
                }

            }
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Calculator obj = new Calculator();
        obj.Calculate();
    }
}


What I have tried:

C#
<pre>using System;
using System.Collections.Generic;

class Calculator
{
    public string FirstNumber = "0";
    public string SecondNumber = "0";
    public int IntFirstNumber;
    public int IntSecondNumber;
    public string Operator = "";
    public string OperatorType = "";
    public bool isOperator = false;
    public string ShowNumber = "";
    public string UserInput = "";
    public int Result;
    public double Calculate()
    {
        string Plus = "+";
        string Subtract = "-";
        string Multiply = "*";
        string Devide = "/";

        while (0 < 1)
        {
            Console.WriteLine("Enter any Value or Operator");
            string UserInput = Console.ReadLine();
            ShowNumber = ShowNumber + UserInput;
            List<string> TotalInfo = new List<string>();

            TotalInfo.Add(ShowNumber);
            foreach (var item in TotalInfo)
            {

                if (FirstNumber == "0")
                {
                    FirstNumber = UserInput;
                    IntFirstNumber = int.Parse(FirstNumber);
                }
                else if (UserInput == "+" || UserInput == "-" || UserInput == "*" || UserInput == "/")
                {
                    OperatorType = UserInput;
                    if (Operator == Plus)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                    else if (UserInput == Subtract)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                    else if (UserInput == Multiply)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                    else if (UserInput == Devide)
                    {
                        OperatorType = UserInput;
                        isOperator = true;
                    }
                }
                else if (UserInput == "=")
                    break;

                else
                {
                    SecondNumber = UserInput;

                    IntSecondNumber = int.Parse(SecondNumber);
                }

            }
            if (UserInput == "=")
            {
                if (OperatorType == "+")
                {
                    Result = IntFirstNumber + IntSecondNumber;
                }
                else if (OperatorType == "*")
                {
                    Result = IntFirstNumber * IntSecondNumber;
                }
                else if (OperatorType == "-")
                {
                    Result = IntFirstNumber - IntSecondNumber;
                }
                else if (OperatorType == "/")
                {
                    Result = IntFirstNumber / IntSecondNumber;

                }

                {
                    Console.WriteLine(Result);
                    Console.ReadLine();
                    IntFirstNumber = Result;
                }

            }
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Calculator obj = new Calculator();
        obj.Calculate();
    }
}
Posted
Updated 14-Apr-21 23:03pm

It is not a straightforward task, since you need a bit of parsing. Have a look at Shunting-yard algorithm - Wikipedia[^].
 
Share this answer
 
with infix, or postfix, notation: without some form of delimiter, like (), to mark expression boundaries, you can not implement recursive operator precedence handling/parsing.

how would you parse this ?
1 + 5 * 10 / 4 - 23 * 4 / 5

compare to this:
(((1 + 5) * 10 / 4) - 23 * 4) / 5

fortunately for you :), a zillion other programmers have had to implement this: [^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900