计算器程序无法正常运行 [英] Calculator Program in not working properly

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

问题描述

嗨...我正在尝试用c#生成计算器程序.此处的操作"="仅适用于两个数字.如果连续给出三个数字,则该数字不起作用... plzzz帮帮我.

Hi... I''m trying to generate calculator program in c#. Here the operations "=" is working only for two numbers. If three numbers were given continuously, its not working... plzzz help me.

public partial class _Default : System.Web.UI.Page
{
    static bool plus;
    static bool minus;
    static bool multiply;
    static bool division;
   // static bool equal;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn0_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn0.Text;
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn1.Text;
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn2.Text;
    }
    protected void btn3_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn3.Text;
    }
    protected void btn4_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn4.Text;
    }
    protected void btn5_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn5.Text;
    }
    protected void btn6_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn6.Text;
    }
    protected void btn7_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn7.Text;
    }
    protected void btn8_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn8.Text;
    }
    protected void btn9_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btn9.Text;
    }
    protected void btndot_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = txtdisplay.Text + btndot.Text;
    }
    protected void btnclr_Click(object sender, EventArgs e)
    {
        txtdisplay.Text = string.Empty;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
     //bool plus=false  ;
    if (txtdisplay.Text == "")
    {
        return;
    }
    else
    {
        plus = true;
        minus = false;
        multiply = false;
        division = false;
        Session["firstNo"] = txtdisplay.Text;
        txtdisplay.Text = "";
    }

    }
   // bool minus = false ;
    protected void btnMinus_Click(object sender, EventArgs e)
    {
        if (txtdisplay.Text == "")
        {
            return;
        }
        else
        {
           minus = true;
           plus = false;
           multiply = false;
           division = false;
            Session["firstNo"] = txtdisplay.Text;
            txtdisplay.Text = "";
        }
    }
   // bool multiply = false ;
    protected void btnMul_Click(object sender, EventArgs e)
    {
        if (txtdisplay.Text == "")
        {
            return;
        }
        else
        {
           multiply  = true;
           plus = false;
           minus = false;
           division = false;
            Session["firstNo"] = txtdisplay.Text;
            txtdisplay.Text = "";
        }
    }
    //bool division = false;
    protected void btnDiv_Click(object sender, EventArgs e)
    {
        if (txtdisplay.Text == "")
        {
            return;
        }
        else
        {
           division = true;
           plus = false;
           minus = false;
                multiply =false ;
            Session["firstNo"] = txtdisplay.Text;
            txtdisplay.Text = "";
        }
    }
    protected void btnEqual_Click(object sender, EventArgs e)
    {
      /*  bool plus=true  ;
        bool minus=true  ;
        bool multiply=true  ;
        bool division=true ;*/
    //   bool equal = true;
        if (plus)
        {
            decimal  sum = Convert.ToDecimal (txtdisplay.Text) + Convert.ToDecimal (Session["firstNo"]);
            txtdisplay.Text = sum.ToString();
        }
        else if (minus)
        {
            decimal  diff = Convert.ToDecimal (Session["firstNo"]) - Convert.ToDecimal (txtdisplay.Text);
            txtdisplay.Text = diff.ToString();
        }
        else if (multiply)
        {
            decimal  mul = (Convert.ToDecimal (txtdisplay.Text)) * (Convert.ToDecimal (Session["firstNo"]));
            txtdisplay.Text = mul.ToString();
        }
        else if (division)
        {
          decimal  div = Convert.ToDecimal (Session["firstNo"]) / Convert.ToDecimal(txtdisplay.Text);
            txtdisplay.Text = div.ToString();
        }
        return;
    }

}



[edit]主题:问题已转入正文,主题已简短描述.
添加了代码块以保留格式
-OriginalGriff [/edit]



[edit]Subject: Question moved into body, subject shortened to a brief description.
Code Block added to preserve formatting
- OriginalGriff[/edit]

推荐答案

请原谅我一些离题的话题:我从来不知道谁需要那些带有几乎不能执行任何操作的按钮的计算器.

想看看我的吗? http://www.sakryukov.org/freeware/calculator/ [
不,我不会理解你的所有练习……

—SA
Please forgive me for some off-topic: I never understood who needs all those calculators with buttons which can do almost nothing.

Want to take a look at mine? http://www.sakryukov.org/freeware/calculator/[^].

No server-side, "View Page Source" and get full source code!

No, I won''t understand all your exercises…

—SA


您可以采取一些措施来改善此问题,并且应该采取以下措施解决您的问题:

首先:用一个通用处理程序替换所有单独的按钮单击处理程序:

There are a few things you could do to improve this, and one you should do to fix your problem:

Firstly: replace all your separate button Click handlers with a common handler:

protected void NumberButton_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        txtdisplay.Text = txtdisplay.Text + b.Text;
        }
    }

这会使您的代码更短,更易于阅读且更可靠!
然后,设置一个名为SetOperation的例程:

That makes your code a lot shorter, easier to read, and reliable!
Then, set up a routine called SetOperation:

private void SetOperation(ref bool op)
   {
    if (txtdisplay.Text != "")
       {
       multiply  = false;
       plus = false;
       minus = false;
       division = false;
       op = true;
       Session["firstNo"] = txtdisplay.Text;
       txtdisplay.Text = "";
       }
   }

并在每个操作按钮中按以下键调用:

And call that in each operation button press:

protected void btnAdd_Click(object sender, EventArgs e)
   {
   SetOperation(ref plus);
   }
   protected void btnMinus_Click(object sender, EventArgs e)
   {
   SetOperation(ref minus);
   }

以此类推.
其次:当您按下操作键时,首先需要检查是否有待处理的操作! IE.如果plus为true,或者minus为true,或者...
否则,您只是丢掉以前的工作...
提示:您可以检查并调用大部分btnEqual_Click吗?

And so on.
Secondly: When you press a operation key, you first need to check if there is a pending operation! I.e. If plus is true, or minus is true, or ...
Otherwise, you just throw away your previous work...
Hint: Could you check and call most of btnEqual_Click?


这篇关于计算器程序无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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