【问题标题】:Calculator, clear window when second variable is typed after chosen operator计算器,在选择运算符后键入第二个变量时清除窗口
【发布时间】:2015-03-25 14:24:07
【问题描述】:

所以我有这个计算器http://gyazo.com/589156935eec141c3aedf83b9f960d29(声望不够抱歉)

当我输入 [1] 然后输入 [2] 时,显示屏显示 [12]

如果我按下一个运算符,例如 [+],数字 12 仍应显示在显示屏中。

但是,如果我现在开始输入新数字,那么旧的数字应该会从显示中删除。但我无法让它工作。

我的表格:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Miniräknare
{
    public partial class Form1 : Form
    {
        Miniräknare miniräknare;

        public Form1()
        {
            InitializeComponent();

            miniräknare = new Miniräknare(0, 0, "", 0, false);
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.doEquals();
        }

        private void btnNum1_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("1", tbxWindow.Text);             
        }

        private void btnNum2_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("2", tbxWindow.Text);
        }

        private void btnNum3_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("3", tbxWindow.Text);
        }

        private void btnNum4_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("4", tbxWindow.Text);

        }

        private void btnNum5_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("5", tbxWindow.Text);
        }

        private void btnNum6_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("6", tbxWindow.Text);
        }

        private void btnNum7_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("7", tbxWindow.Text);
        }

        private void btnNum8_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("8", tbxWindow.Text);
        }

        private void btnNum9_Click(object sender, EventArgs e)
        {

            tbxWindow.Text = miniräknare.getOperand("9", tbxWindow.Text);
        }

        private void btnNum0_Click(object sender, EventArgs e)
        {

            if (tbxWindow.Text != "") tbxWindow.Text = miniräknare.getOperand("0", tbxWindow.Text);

        }

        private void btnOperatorDivision_Click(object sender, EventArgs e)
        {

        }

        private void btnOperatorTimes_Click(object sender, EventArgs e)
        {

        }

        private void btnOperatorPlus_Click(object sender, EventArgs e)
        {
            miniräknare.Op = "+";




        }

        private void btnOperatorMinus_Click(object sender, EventArgs e)
        {
            miniräknare.Op = "-";
            miniräknare.Change = true;
        }        

        private void btnDecimal_Click(object sender, EventArgs e)
        {
            tbxWindow.Text = miniräknare.getOperand(",", tbxWindow.Text);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

        }

        private void btnSin_Click(object sender, EventArgs e)
        {

        }

        private void btnCos_Click(object sender, EventArgs e)
        {

        }

        private void btnTan_Click(object sender, EventArgs e)
        {

        }

        private void btnSquared_Click(object sender, EventArgs e)
        {

        }

        private void btnModulus_Click(object sender, EventArgs e)
        {

        }

        private void btnExponential_Click(object sender, EventArgs e)
        {

        }

        private void btnlogarithm_Click(object sender, EventArgs e)
        {

        }

        private void btn1OverX_Click(object sender, EventArgs e)
        {

        }

        private void btnLn_Click(object sender, EventArgs e)
        {

        }

        private void btnPi_Click(object sender, EventArgs e)
        {

        }

        private void btnMemoryClear_Click(object sender, EventArgs e)
        {

        }

        private void btnMemoryRecall_Click(object sender, EventArgs e)
        {

        }

        private void btnMemorySave_Click(object sender, EventArgs e)
        {

        }        
    }
}

我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Miniräknare
{
    class Miniräknare
    {
        private double first;
        private double second;
        private string op; 
        private double memory; 
        private bool change;

        public Miniräknare(double first, double second, string op, double memory, bool change)
        {
            this.first = 0;
            this.second = 0;
            this.op = "";
            this.memory = 0;
            this.change = false;
        }

        public double First
        {
            get {return first; }
            set { first = value; }
        }

        public double Second
        {
            get { return second; }
            set { second = value; }
        }

        public string Op
        {
            get { return op; }
            set { op = value; }
        }

        public double Memory
        {
            get { return memory; }
            set { memory = value; }
        }

        public bool Change
        {
            get { return change; }
            set { change = value; }
        }

        public string getOperand(string t, string textBox)
        {
            textBox = textBox + t;
            if (t.Equals(","))
            {
                change = true;
                second = double.Parse(textBox);
            }
            else if (op.Equals(""))
            {
                if (!change)
                {
                    textBox = "";
                    change = true;
                    textBox = textBox + t;
                }
                first = double.Parse(textBox);
            }
            else
            {
                if (!change)
                {
                    textBox = "";
                    change = true;
                    textBox = textBox + t;
                }
                second = double.Parse(textBox);
            }
            return textBox;
        }

       /* public string calculateAnswer()
        {


        } */

        public string doEquals()
        {
            if (op == "-" ) return (first - second).ToString();





            else return null;



        }        
    }
}

【问题讨论】:

  • 1.从问题中删除所有空事件处理程序。 2. 学习使用调试器并逐步执行代码。
  • 3.使用自动属性而不是字段(这只是一个样式问题,但可以提高代码的可读性)。
  • 声誉或没有声誉,请不要使用指向外部资源的链接来记​​录您的问题。 StackOverflow 问题应该是完全独立的,因为指向其他页面(甚至是 StackOverflow 中的页面)的链接可能会更改甚至消失,从而使它们与您发布的问题无关。请阅读stackoverflow.com/help/mcve,了解为什么以及如何提供一个好的、minimalcomplete 代码示例来清楚地说明您的问题。
  • 另一个设计建议:不要使用 atm 的所有代码,而是使用按钮的 Tag 属性,将它们全部链接到同一个事件处理程序并调用 tbxWindow.Text = miniräknare.getOperand(((Button)sender).Tag as String, tbxWindow.Text);
  • 此外,您的构造函数将参数作为输入,但您不会将输入参数分配给类的变量。

标签: c# class operators calculator


【解决方案1】:

在按下“+”按钮后的以下块中,“更改”为真,输入第二个数字的第一个数字时跳过该块。

    else
    {
        if (!change)
        {
            textBox = "";
            change = true;
            textBox = textBox + t;
        }
        second = double.Parse(textBox);
    }

由于您在 getOperand 方法的开头指定了 textBox,它将返回结合了您在屏幕上已有的内容和新字符的值。

public string getOperand(string t, string textBox)
{
    textBox = textBox + t;

这应该可以解决问题:

public string getOperand(string t, string textBox)
{


    if (t.Equals(","))
    {
        textBox = textBox + t;
        change = true;
        second = double.Parse(textBox);
    }
    else if (Op.Equals(""))
    {
        textBox = textBox + t;
        if (!change)
        {
            textBox = "";
            change = true;
            textBox = textBox + t;
        }
        first = double.Parse(textBox);
    }
    else
    {
        if (!change)
        {
            textBox = textBox + t;
        }
        else
        {
            textBox = t;
            change = false;
        }
        second = double.Parse(textBox);
    }
    return textBox;
}

【讨论】:

  • 谢谢这个工作,几乎:/如果我想在第二个数字中输入更长的输入,超过一个字符,它会自动删除旧的(非常明显)。
  • else 代码块中添加change = false; 以允许额外输入
  • @AzeyZ - 一次一个问题
  • @AlexG 感谢您的宝贵时间,但不幸的是,我实际上已经得到了该代码 ^^。嗯,其中一个问题与我现在修复的另一部分代码有关。
【解决方案2】:

我知道这不是https://codereview.stackexchange.com/,这并不能回答所提出的问题(已经是answered),并且可能被标记为离题,但想显示各种 cmets 中建议的更改一种有序的方式,只是为了帮助您改善您的编码(当前和未来)体验。

可以对Miniräknare 类进行的更改(添加注释以进行解释):

public class Miniräknare
{
  public Miniräknare()
  {
    // Have a default constructor that sets all the default properties
    First = 0;
    Second = 0;
    Op = "";
    Memory = 0;
    Change = false;
  }

  public Miniräknare(double first, double second, string op, double memory, bool change)
  {
    // If you have a constructor with parameters, use the parameters to set your properties
    First = first;
    Second = second;
    Op = op;
    Memory = memory;
    Change = change;
  }

  // Use automatic properties, this improves readability and less confusion (As per D Stanley in comments)
  public double First { get; set; }
  public double Second { get; set; }
  public string Op { get; set; }
  public double Memory { get; set; }
  public bool Change { get; set; }

  public string getOperand(string t, string textBox)
  {
    // Apply changes as per the accepted answer
    textBox = textBox + t;
    if (t.Equals(","))
    {
      Change = true;
      Second = double.Parse(textBox);
    }
    else if (Op.Equals(""))
    {
      if (!Change)
      {
        textBox = "";
        Change = true;
        textBox = textBox + t;
      }
      First = double.Parse(textBox);
    }
    else
    {
      if (!Change)
      {
        textBox = "";
        Change = true;
        textBox = textBox + t;
      }
      Second = double.Parse(textBox);
    }
    return textBox;
  }

  public string doEquals()
  {
    if (Op == "-") return (First - Second).ToString();
    else return null;
  }
}

Form1 的变化:

现在像这样实例化您的 miniräknare 变量,因为您使用原始输入参数将它们设置为默认值。

miniräknare = new Miniräknare();

用这个单一的事件处理程序替换你所有的btnNum1_ClickbtnNum9_Click 事件处理程序,以提高代码的可读性和体积(检查代码中的其他 cmets):

private void btnNumber_Click(object sender, EventArgs e)
{
  // !! Remember !! to set the Tag value of each of your buttons to their corresponding values
  // Then change btnNum1 to btnNum9's Click events to btnNumber_Click
  // Additionally you can also just use ((Button)sender).Text if their text values will never change
  // You could even do this with your operators, unless you have specific code for the button (like you have for btnNum0)
  tbxWindow.Text = miniräknare.getOperand(((Button)sender).Tag as String, tbxWindow.Text);
}

【讨论】:

  • 感谢您的建议!但是我意识到如果我希望在单击运算符后在第二个数字中输入更长的数字(如 113),这将不起作用。现在它不会接受超过一个字符的数字。
  • 忘记在其他评论中添加您,因此将在此处添加:问题已解决 ^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 2013-11-22
  • 2020-04-11
  • 2017-09-27
  • 1970-01-01
相关资源
最近更新 更多