I am having trouble with this program - I have the frm code and I create a class to calculate. The program runs and accepts input - however, when I press equals it is displaying the 1st operand. So, 5 + 3 = 5, 1 + 8 = 1. Etc. Can anyone shed some light on why this is happening? I have been in circles for days on this one trying different ways to get this completed, yet, have not been able to figure it out.
frmCalculator
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 Project_3_A_Create_a_basic_calculator
{
public partial class frmCalculator : Form
{
//Calculator calc;
//variables for storing input, operands, and result
//string input = string.Empty;
//string operand1 = string.Empty;
//string operand2 = string.Empty;
//char operation;
//double result = 0.0;
public frmCalculator()
{
InitializeComponent();
}
private string displayString;
private decimal displayValue;
private bool newValue;
private bool decimalEntered;
private Calculator calc = new Calculator();
private void frmCalculator_Load(object sender, System.EventArgs e)
{
displayValue = 0;
displayString = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
private void btnNumber_Click (object sender, System.EventArgs e)
{
if (newValue)
{
displayString = "";
newValue = false;
}
displayString += ((Button)sender).Tag.ToString();
displayValue = Convert.ToDecimal(displayString);
txtDisplay.Text = displayValue.ToString();
}
private void btnBackSpace_Click(object sender, EventArgs e)
{
if (displayString.Length > 1)
{
displayString = displayString.Substring(0, displayString.Length - 1);
displayValue = Convert.ToDecimal(displayString);
txtDisplay.Text = displayValue.ToString();
}
else
{
displayString = "";
displayValue = 0;
txtDisplay.Text = displayValue.ToString();
}
}
private void btnClear_Click (object sender, System.EventArgs e)
{
calc.Clear();
displayString = "";
displayValue = 0;
txtDisplay.Text = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
private void btnDecimal_Click(object sender, EventArgs e)
{
if (newValue)
{
displayString = "0";
newValue = false;
}
if (!decimalEntered)
{
displayString += ".";
displayValue = Convert.ToDecimal(displayString);
txtDisplay.Text = displayValue.ToString();
decimalEntered = true;
}
}
private void btnSign_Click(object sender, EventArgs e)
{
displayValue = -displayValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnAdd_Click (object sender, EventArgs e)
{
calc.Add(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnSubtract_Click(object sender, EventArgs e)
{
calc.Subtract(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnMultiply_Click(object sender, EventArgs e)
{
calc.Multiply(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnDivide_Click(object sender, EventArgs e)
{
calc.Divide(displayValue);
newValue = true;
decimalEntered = false;
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnSqrt_Click(object sender, EventArgs e)
{
calc.SquareRoot(displayValue);
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
private void btnReciprocal_Click(object sender, EventArgs e)
{
try
{
calc.Reciprocal(displayValue);
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
}
catch (DivideByZeroException)
{
displayValue = 0;
txtDisplay.Text = "Can't divide by zero";
newValue = true;
decimalEntered = false;
}
}
private void btnEquals_Click(object sender, System.EventArgs e)
{
try
{
if (newValue)
calc.Equals();
else
calc.Equals(displayValue);
displayValue = calc.CurrentValue;
txtDisplay.Text = displayValue.ToString();
newValue = true;
decimalEntered = false;
}
catch (DivideByZeroException)
{
displayValue = 0;
txtDisplay.Text = "Can't divide by zero";
newValue = true;
decimalEntered = false;
}
}
}
}
and the calculator class
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Project_3_A_Create_a_basic_calculator
{
public class Calculator
{
public Decimal displayValue;
public Decimal currentValue;
private Decimal Operand1;
private Decimal Operand2;
private bool Operand1Add = false;
private bool Operand2Add = false;
private string Operation = "";
public Calculator()
{
Clear();
}
private void AddOperand(Decimal Operand)
{
if (Operand1Add)
{
Operand1 = Operand;
Operand1Add = true;
}
else
{
Operand2 = Operand;
Operand2Add = true;
currentValue = Operand2;
}
}
public void Add(Decimal op)
{
this.AddOperand(op);
Operation = "Addition";
}
public void Reciprocal(Decimal op1)
{
this.AddOperand(op1);
Operation = "Reciprocal";
}
public void Subtract(Decimal op2)
{
this.AddOperand(op2);
Operation = "Subtract";
}
public void Multiply(Decimal op5)
{
this.AddOperand(op5);
Operation = "Multiply";
}
public void SquareRoot(Decimal op3)
{
this.AddOperand(op3);
Operation = "Sqrt";
}
public void Divide(Decimal op4)
{
this.AddOperand(op4);
Operation = "Divide";
}
public void Clear()
{
currentValue = 0;
displayValue = 0;
Operand1 = 0;
Operand2 = 0;
}
public void Equals()
{
switch (Operation)
{
case "Addition":
currentValue = Operand1 + Operand2;
break;
case "Reciprocal":
currentValue = 1 / Operand1;
break;
case "Subtraction":
currentValue = Operand1 - Operand2;
break;
case "Multiply":
currentValue = 1 * Operand1;
break;
case "Divide":
currentValue = Operand1 / Operand2;
break;
default:
currentValue = 0;
break;
}
}
public void Equal(Decimal displayValue)
{
currentValue = displayValue;
}
public decimal CurrentValue
{
get
{
return currentValue;
}
}
}
}
Your AddOperant
logic has a bug in it
private void AddOperand(Decimal Operand)
{
if (Operand1Add) // if(true) then set to true?
{
Operand1 = Operand;
Operand1Add = true;
}
else
{
Operand2 = Operand;
Operand2Add = true;
currentValue = Operand2;
}
}
Can you spot it from the hint above?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.