Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i perform 1+2+3*4/5 in xamarin form calculator

What I have tried:

C#
        string Operand1 = "";
        bool isoperatorselected=false; 
        private void Button_Clicked(object sender, EventArgs e)
        {
            var Button =  (Button) sender;
            string Input = Button.Text;
            if (isoperatorselected)
            {
                Operand1 = Operand1 + Input;
                resultText.Text = Operand1;
            }
           else
            {
                Operand2 =  Input;
               resultText.Text = Operand2;
          }
        }
        private void Operator_Clicked(object sender, EventArgs e)
        {
            var Button = (Button)sender;
            string Input = Button.Text;
            isoperatorselected = true;
            if ("+"==Input)
            {
                Operator = "+";
            }
            else if ("-"==Input)
            {
                Operator = "-";
            }
            else if ("*"==Input)
            {
                Operator = "*";
            }
            else if ("/"==Input)
            {
                Operator = "/";
            }
        }
        private void Clear_Clicked(object sender, EventArgs e)
        {
            this.resultText.Text = "";
            this.Input = string.Empty;
            this.Operand1 = string.Empty;
            this.Operand2 = string.Empty;

        }
        private void Equal_Clicked(object sender, EventArgs e)
        {
            Operand2 = Input;
            double FirstNumber;
            double SecondNumber;
            double.TryParse(Operand1, out FirstNumber);
            double.TryParse(Operand1, out SecondNumber);

            if (Operator=="+")
            {
                result = FirstNumber + SecondNumber;
                resultText.Text = result.ToString();
            }
            else if(Operator=="-")
            {
                result = FirstNumber - SecondNumber;
                resultText.Text = result.ToString();
            }
            else if(Operator=="*")
            {
                result = FirstNumber * SecondNumber;
                resultText.Text = result.ToString();
            }
            else if(Operator=="/")
            {
                if(SecondNumber!=0)
                {
                    result = FirstNumber / SecondNumber;
                    resultText.Text = result.ToString();
                }
            }
        }
    }
}
Posted
Updated 21-Apr-21 19:34pm
v2
Comments

1 solution

You are going to have to be rather more clever than that - that's just your "value operator value equals" code which doesn't really cut it when you start working with multiple operators.

And ... what if I type "2 * 3 + 4" and "4 + 2 * 3"?
Operator precedence rules say I should get the same result for both: 10.
But unless you add that to your code you will get 10 and 18!

What I'd suggest is that - if you must go down the "button for each possible input" route - you need to be a lot more "clever" about what you are doing and eitehr convret the user input into Reverse Polish notation[^] and stack it internally, or store your operators as "tokens" as they are entered and process them "properly" once Equals is pressed.

Me? I'd throw the lot away and do something a bit more Windows "user freindly" like Microsoft Mathematics[^] (Though the latest version has ... um ... bloated somewhat from teh svelte original!)
You can get it free from MS: Download Microsoft Mathematics 4.0 from Official Microsoft Download Center[^]
 
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