方法中的C#计算器 [英] C# calculator in method

查看:101
本文介绍了方法中的C#计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个计算器并将运行它的代码放在方法Operations()中,当单击 - 减去例如它将查看Operations()内部然后查看if btnMinus.click并将运行那部分。我还需要这样做+,/,*,%但是一旦我得到了一些帮助,我应该能够弄明白。



我试图创建一个参数我很困惑如何做到这一点,因为我的书解释为Minus => 100.用户输入Left和Right值。如何让它通过方法Operations()中的代码的减号部分?



public frmActors()

{

InitializeComponent();

}



//把你的方法放在这里

private void操作()

{



//代码所有操作都将使用。

decimal dLeft = 0.0m;

十进制dRight = 0.0m;

十进制dAnswer = 0.0m;

string szLeft =;

string szRight =;

string szAnswer =;

string szEquation =;



szLeft = txtLeft.Text;

szRight = txtRight.Text;



dLeft = Convert.ToDecimal(szLeft);

dRight = Convert.ToDecimal(szRight);







//减号

{

if(b tnMinus.Click

)dAnswer = dLeft - dRight;



szAnswer = dAnswer.ToString();



szEquation = szLeft + - + szRight +=+ szAnswer;

}



< br $>














lblAnswer.Text =;

lblAnswer.Text = szEquation;

















}

private void btnPercent_Click(object sender,EventArgs e)

{















} < br $> b $ b





private void btnDivide_Click(object sender,EventArgs e)

{

十进制dLeft = 0.0m;

十进制dRight = 0.0m;

十进制dAnswer = 0.0m;

string szLeft =;

string szRight =;

string szAnswer =;

string szEquation = ;



szLeft = txtLeft.Text;

szRight = txtRight.Text;



dLeft = Convert.ToDecimal(szLeft);

dRight = Convert.ToDecimal(szRight);



dAnswer = dLeft / dRight;



szAnswer = dAnswer.ToString();



szEquation = szLeft +/ + szRight +=+ szAnswer;



lblAnswer.Text =;

lblAnswer.Text = szEquation;





}



private void btnMultiply_Click(object sender,EventArgs e)

{

十进制dLeft = 0.0m;

十进制dRight = 0.0m;

十进制dAnswer = 0.0m;

string szLeft =;

string szRight =;

string szAnswer =;

string szEquation =;



szLeft = txtLeft.Text;

szRight = txtRight.Text;



dLeft = Convert.ToDecimal(szLeft);

dRight = Convert.ToDecimal(szRight);



dAnswer = dLeft * dRight;



szAnswer = dAnswer.ToString();



szEquation = szLeft +*+ szRight +=+ szAnswer;



lblAnswer.Text =;

lblAnswer.Text = szEquation;





}



private void btnMinus_Click(object sender,EventArgs e)

{

操作();





}



private void btnPlus_Click(object发件人,EventArgs e)

{

十进制dLeft = 0.0 m;

十进制dRight = 0.0m;

十进制dAnswer = 0.0m;

string szLeft =;

string szRight =;

string szAnswer =;

string szEquation =;



szLeft = txtLeft.Text;

szRight = txtRight.Text;



dLeft = Convert.ToDecimal(szLeft);

dRight = Convert.ToDecimal(szRight);



dAnswer = dLeft + dRight;



szAnswer = dAnswer.ToString();



szEquation = szLeft +++ szRight +=+ szAnswer;



lblAnswer.Text =;

lblAnswer.Text = szEquation;



}

}

}



我的尝试:



我尝试使用btnMinus.click创建参数,使其能够通过一组代码。

我需要做+,*,/和百分比,这也就是我需要在方法中做的原因。我只是不确定如何把它放在那里调用它。

I am trying to build a calculator and put the code that runs it inside a method Operations() and when the - minus is clicked for example it will look inside Operations() and then see the if btnMinus.click and will run that part of it. I also need to do this +, /, *, percent but once I get some help on this I should be able to figure it out.

I am attempting to create a parameter I am confused at how to do this as my book explains it as Minus => 100. The user enters a Left and a Right value. How would I get it to go through the Minus part of the code in the method Operations()?

public frmActors()
{
InitializeComponent();
}

//Put Your method here
private void Operations()
{

//Code all operations will use.
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);



//Minus
{
if (btnMinus.Click
)dAnswer = dLeft - dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " - " + szRight + " = " + szAnswer;
}









lblAnswer.Text = "";
lblAnswer.Text = szEquation;








}
private void btnPercent_Click(object sender, EventArgs e)
{







}



private void btnDivide_Click(object sender, EventArgs e)
{
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);

dAnswer = dLeft / dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " / " + szRight + " = " + szAnswer;

lblAnswer.Text = "";
lblAnswer.Text = szEquation;


}

private void btnMultiply_Click(object sender, EventArgs e)
{
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);

dAnswer = dLeft * dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " * " + szRight + " = " + szAnswer;

lblAnswer.Text = "";
lblAnswer.Text = szEquation;


}

private void btnMinus_Click(object sender, EventArgs e)
{
Operations();


}

private void btnPlus_Click(object sender, EventArgs e)
{
decimal dLeft = 0.0m;
decimal dRight = 0.0m;
decimal dAnswer = 0.0m;
string szLeft = "";
string szRight = "";
string szAnswer = "";
string szEquation = "";

szLeft = txtLeft.Text;
szRight = txtRight.Text;

dLeft = Convert.ToDecimal(szLeft);
dRight = Convert.ToDecimal(szRight);

dAnswer = dLeft + dRight;

szAnswer = dAnswer.ToString();

szEquation = szLeft + " + " + szRight + " = " + szAnswer;

lblAnswer.Text = "";
lblAnswer.Text = szEquation;

}
}
}

What I have tried:

I have tried creating a parameter by using the btnMinus.click to enable it to go through that set of code.
I am needing to do +, *, /, and percent also that is why I need to do it inside a method. I am just unsure how after I put it in there to call it.

推荐答案

这是你的具体问题的解决方案。

我我不讨论,如果这是最合乎逻辑的做法,但只是提供一个答案,你试图以你的方式完成。



你的代码不起作用的一个原因是btnMinus.Click不是你需要的,它没有告诉你按钮是否被点击,但它是一个事件处理程序,包含按下按钮时调用的回调方法。 InitializeComponent像这样添加回调:

Here is a solution to your specific question.
I am not discussing, if this is the most logical way of doing this, but just provide an answer to what you try to accomplish in your way.

One of the reason your code does not work is that btnMinus.Click is not what you need, it is not telling you if the button was clicked, but it is an event handler that contains the callback method that gets called when the button is pressed. InitializeComponent adds the callback like this:
this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click);



这就像解释一样。

所以你需要一种不同的方式告诉Operations()要执行哪些操作。这里提出了一种方法(我只是将减号操作代码作为示例,您应该能够实现其他操作):


This much just as explanation.
So you need a different way to tell Operations() which operation to perform. One way is propsed here (I just put the minus operation code as example, you should be able to implement the other operations):

        /// <summary>
        /// Define all operations you want to support in this enumeration
        /// </summary>
        enum Ops
            {
                Plus,
                Minus,
                Mult,
                Div,
                Percent
            };

        /// <summary>
        /// Do all work in this single method.
        /// </summary>
        /// <param name="op">The operation to perform.</param>
        void Operations(Ops op)
        {
            decimal dLeft = 0.0m;
            decimal dRight = 0.0m;
            decimal dAnswer = 0.0m;
...
            switch (op)
            {
                case Ops.Minus:
                    dAnswer = dLeft - dRight;
                    break;
...
            }
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            Operations(Ops.Minus);
        }
...


这篇关于方法中的C#计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆