Understanding Calculator Creation in C#

Asked 2 years ago, Updated 2 years ago, 35 views

I am currently using C# to create a calculator, but it doesn't work well with Func.

What I want to do is to calculate continuously like 1*2*3.
I want to get an answer when I press the operator button.

At first, I put an equal in the operator button, but I was told that I could write short and concisely using Func.SUB and ADD have not been rewritten yet.The action of pressing the clear button is also being rewritten.

The following codes are affected:

namespace WindowsFormsApp1
{
    public partial class Form 1:Form
    {
        publicForm1(){
            InitializeComponent();
        }

        bool isFirst=true;
        decimal num1 = 0;

        private void Number_Click (object sender, EventArgse)
        {
            if(sign!=arthmatic.undefined&isFirst){
                num1 = Convert.ToDecimal(textDisplay.Text);
                textDisplay.Text=";
                isFirst=false;
            }
            if(textDisplay.TextLength==12){
                return;
            }
            textDisplay.Text=
               textDisplay.Text+(Button) sender).Text;
        }
        private void Dot_Click (object sender, EventArgse) {
            if(textDisplay.Text.IndexOf(".")>=0){
                return;
            }
            textDisplay.Text=textDisplay.Text+.";
        }

        US>enum arithmetical{
            undefined, ADD, SUB, MUL, DIV
        };
        ARITHMETIC sign = ARITHMETIC.undefined;

        private void ADD_Click (object sender, EventArgse) {
            if(sign!=arthmatic.undefined){
                decimal num2 = Convert.ToDecimal(textDisplay.Text);
                decimal Result = 0;
                Result = num1 + num2;
                textDisplay.Text=Result.ToString();
            }
            sign = arithmetical.ADD;
            isFirst = true;
        }
        private void SUB_Click (object sender, EventArgse)
        {
            if(sign!=arthmatic.undefined){
                decimal num2 = Convert.ToDecimal(textDisplay.Text);
                decimal Result = 0;
                Result = num1-num2;
                textDisplay.Text=Result.ToString();
            }
            sign =arthmic.SUB;
            isFirst = true;
        }
        private void MUL_Click (object sender, EventArgse) {
            Func<decimal,decimal,decimal>MUL=(num1,num2)=>{
                return num1*num2;
            };
            sign =arthmatic.MUL;
            isFirst = true;          
        }  

        private void DIV_Click (object sender, EventArgse) {
            Func<decimal,decimal,decimal>DIV=(num1,num2)=>{
                return num1/num2;
            }
;            sign =arthmic.DIV;
            isFirst = true;
        }
        private void Equal_Click (object sender, EventArgse) {
            decimal num2 = Convert.ToDecimal(textDisplay.Text);
            decimal Result = 0;
            try{
                switch(sign){
                    casearhythmic.ADD:
                        Result = num1 + num2;
                        break;
                    case arithmetic.SUB:
                        Result = num1-num2;
                        break;
                    casearhythmic.MUL:
                        Result = num1 * num2;
                        break;
                    casearhythmic.DIV:
                        Result = num1/num2;
                        break;
                }
            }
            catch(DividByZeroException){
                textDisplay.Text="0 does not break"; {
                    return;
                }
            }

            textDisplay.Text=Result.ToString();
            isFirst = true;
         if(textDisplay.TextLength>=13){
                textDisplay.Text = "Exceeded Digits";  

                This.button0.Enabled=false;
                This.button1.Enabled=false;
                This.button2.Enabled=false;
                This.button3.Enabled=false;
                This.button4.Enabled=false;
                This.button5.Enabled=false;
                This.button6.Enabled=false;
                This.button7.Enabled=false;
                This.button8.Enabled=false;
                This.button9.Enabled=false;
                This.buttonEq.Enabled=false;
                This.Dot.Enabled=false;
                This.ADD.Enabled=false;
                This.SUB.Enabled=false;
                This.MUL.Enabled=false;
                This.DIV.Enabled=false;
               }
        }
        private void Clear_Click (object sender, EventArgse) {

            This.button0.Enabled=true;
            This.button1.Enabled=true;
            This.button2.Enabled=true;
            This.button3.Enabled=true;
            This.button4.Enabled=true;
            This.button5.Enabled=true;
            This.button6.Enabled=true;
            This.button7.Enabled=true;
            This.button 8.Enabled = true;
            This.button9.Enabled=true;
            This.buttonEq.Enabled = true;
            This.Dot.Enabled = true;
            This.ADD.Enabled = true;
            This.SUB.Enabled = true;
            This.MUL.Enabled = true;
            This.DIV.Enabled = true;
        }
    }
}

c#

2022-09-29 20:15

1 Answers

The screen has one TextBox (textDisplay) for displaying results/input numbers, a numeric button from [0] to [9], and a button from [.][+][-][-][x][ ]][=][Clear].

This is how your code works right now:

View Operation TextBox    
[1] 1
[*] (no change)
[2] 2
[*] (no change)
[3] 3
[=] 6 (2*3 results displayed)

That's how it feels.I don't have WinForm running environment at hand, so please let me know if there are any errors.

Desired behavior is like this:

View Operation TextBox    
[1] 1
[*] (no change)
[2] 2
[*] 2 (1*2 results displayed)
[3] 3
[=]6(2*3 results are displayed…where `2` is the first calculation result)

That's what I said.

"For example, ""This is what happens when you do this kind of operation right now"" and ""I really want you to do this kind of operation"" are described as specifically as possible."

This answer may not be useful because I am writing a partial estimate, but if you could describe what you would like to do at this level, there would be many people who could write the answer you want.

Since your MUL_Click doesn't have any code to perform any calculations and display the results, don't you think your current behavior is a natural result?

When you press the operation key, you have to do the previous calculation, so you have to remember it somewhere.sign seems to have a field in your code, but sign has no meaning in calculating one piece before, so you should stop.

For example, it should not be Func (FUNC.If you use case sensitive language, be sure to have a good sense of it) I'll add these fields and methods.

private Func<decimal,decimal,decimal>pendingOperation=null;

        private void doPendingOperation(){
            if(pendingOperation!=null){
                try{
                    decimal num2 = Convert.ToDecimal(textDisplay.Text);
                    decimal result=pendingOperation(num1,num2);
                    textDisplay.Text=result.ToString();
                }
                catch(DividByZeroException){
                    textDisplay.Text="0 does not break";
                    return;
                }
                if(textDisplay.TextLength>=13){
                    textDisplay.Text = "Exceeded Digits";  

                    This.button0.Enabled=false;
                    This.button1.Enabled=false;
                    This.button2.Enabled=false;
                    This.button3.Enabled=false;
                    This.button4.Enabled=false;
                    This.button5.Enabled=false;
                    This.button6.Enabled=false;
                    This.button7.Enabled=false;
                    This.button8.Enabled=false;
                    This.button9.Enabled=false;
                    This.buttonEq.Enabled=false;
                    This.Dot.Enabled=false;
                    This.ADD.Enabled=false;
                    This.SUB.Enabled=false;
                    This.MUL.Enabled=false;
                    This.DIV.Enabled=false;
                }
            }
        }

Whenever you press the arithmetic button, call this method and update pendingOperation.

private void MUL_Click(object sender, EventArgse){
            doPendingOperation();
            Func<decimal,decimal,decimal>mul=(num1,num2)=>{a
                return num1*num2;
            };
            pendingOperation=mul;
            isFirst = true;          
        }  

        private void DIV_Click (object sender, EventArgse) {
            doPendingOperation();
            Func<decimal,decimal,decimal>div=(num1,num2)=>{
                return num1/num2;
            };
            pendingOperation=div;
            isFirst = true;
        }

When you press the equal button, you almost only need to call this method.

private void Equal_Click(object sender, EventArgse){
            doPendingOperation();
            pendingOperation = null;
            isFirst = true;
        }

Far from running it, I haven't checked if the compilation works, so you may need to make some minor corrections here and there, but try it.

Also, if you say, "This is not what I expect," edit your question and describe what exactly you want to do as much as possible.

Modify

I was able to confirm the operation under the condition you mentioned in the comment."I wrote ""Do not use sign"" in the above answer, but I left the reference to sign in Number_Click.

private void Number_Click (object sender, EventArgse)
        {
            if(isFirst)
            {
                var str1=textDisplay.Text=="?"0":textDisplay.Text;
                num1 = Convert.ToDecimal(str1);
                textDisplay.Text=";
                isFirst=false;
            }
            if (textDisplay.TextLength==12)
            {
                return;
            }
            textDisplay.Text=
               textDisplay.Text+(Button) sender).Text;
        }

If you correct it with …, I think it will not be impossible to calculate itself for now.Try it.

(As you may already know, I haven't done anything about ADD, SUB, or Clear.Please correct it yourself.)


2022-09-29 20:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.