A
Arjen Logghe
Hello all,
I've written my first real C# program. I am posting this here, because
I would like your opinion. Is it good, bad, where can I improve etc.?
The project is a simple calculator, I've got the basics working and
I'll probably extend its functionality with a filelogger and
memorybuttons to make it more of a real calculator. I look forward
from hearing from you.
Regards,
A.Logghe.
Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Calculator
{
public partial class MyCalculator : Form
{
private string firstOperand = "";
private string secondOperand = "";
private bool isFirstChar = true;
private bool CommaChrUsed = false;
private int operandCount = 0;
private string operandType = "";
public MyCalculator()
{
InitializeComponent();
}
private void btnNumeral_Click(object sender, EventArgs e) //
buttons (0-9 + comma)
{
Button b = new Button();
b = (Button)sender;
if (operandCount == 0)
{
if (isFirstChar)
{
if (b.Text == "," || b.Text == "0")
{
firstOperand = "0,";
CommaChrUsed = true;
isFirstChar = false;
}
else
{
firstOperand += b.Text;
txtBoxDisplay.Text = firstOperand;
isFirstChar = false;
}
}
else //!isFirstChar
{
if (b.Text == "," && !CommaChrUsed)
{
firstOperand += b.Text;
txtBoxDisplay.Text = firstOperand;
CommaChrUsed = true;
}
if (b.Text != ",")
{
firstOperand += b.Text;
txtBoxDisplay.Text = firstOperand;
}
}
}
else //operandCount != 0
{
if (isFirstChar)
{
if (b.Text == "," || b.Text == "0")
{
secondOperand = "0,";
CommaChrUsed = true;
isFirstChar = false;
}
else
{
secondOperand += b.Text;
txtBoxDisplay.Text = secondOperand;
isFirstChar = false;
}
}
else //!isFirstChar
{
if (b.Text == "," && !CommaChrUsed)
{
secondOperand += b.Text;
txtBoxDisplay.Text = secondOperand;
CommaChrUsed = true;
}
if (b.Text != ",")
{
secondOperand += b.Text;
txtBoxDisplay.Text = secondOperand;
}
}
}
}
private void btnOperator_Click(object sender, EventArgs e) //
all the standard arithmetic operators + %-operator
{
Button b = new Button();
b = (Button)sender;
operandType = b.Text;
if (operandType == "+/-" || operandType == "sqrt" ||
operandType == "1/x") //plus these unary operators
operandCount = 1;
else operandCount = 2;
isFirstChar = true;
CommaChrUsed = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtBoxDisplay.Text = "0,";
firstOperand = "";
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
operandCount = 0;
}
private void btnEquals_Click(object sender, EventArgs e) // =
operator
{
if (operandCount > 1)
{
switch (operandType)
{
case "+":
{
double result = double.Parse(firstOperand)
+ double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "-":
{
double result = double.Parse(firstOperand)
- double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "*":
{
double result = double.Parse(firstOperand)
* double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "/":
{
double result =
double.Parse(firstOperand) / double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "%":
{
double result =
(double.Parse(firstOperand) / 100) * (double.Parse(secondOperand));
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
default:
{
MessageBox.Show("The chosen operator is
not a valid operator.", "Unknown operator");
btnClear_Click(sender, e);
}
break;
}
}
else if (operandCount == 1)
{
switch (operandType)
{
case "+/-":
{
double result = -
(double.Parse(firstOperand));
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "sqrt":
{
double result =
Math.Sqrt(double.Parse(firstOperand));
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "1/x":
{
double result = 1 /
double.Parse(firstOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
}
break;
default:
{
MessageBox.Show("The chosen operator is
not a valid operator.", "Unknown operator");
btnClear_Click(sender, e);
}
break;
}
}
}
}
}
I've written my first real C# program. I am posting this here, because
I would like your opinion. Is it good, bad, where can I improve etc.?
The project is a simple calculator, I've got the basics working and
I'll probably extend its functionality with a filelogger and
memorybuttons to make it more of a real calculator. I look forward
from hearing from you.
Regards,
A.Logghe.
Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Calculator
{
public partial class MyCalculator : Form
{
private string firstOperand = "";
private string secondOperand = "";
private bool isFirstChar = true;
private bool CommaChrUsed = false;
private int operandCount = 0;
private string operandType = "";
public MyCalculator()
{
InitializeComponent();
}
private void btnNumeral_Click(object sender, EventArgs e) //
buttons (0-9 + comma)
{
Button b = new Button();
b = (Button)sender;
if (operandCount == 0)
{
if (isFirstChar)
{
if (b.Text == "," || b.Text == "0")
{
firstOperand = "0,";
CommaChrUsed = true;
isFirstChar = false;
}
else
{
firstOperand += b.Text;
txtBoxDisplay.Text = firstOperand;
isFirstChar = false;
}
}
else //!isFirstChar
{
if (b.Text == "," && !CommaChrUsed)
{
firstOperand += b.Text;
txtBoxDisplay.Text = firstOperand;
CommaChrUsed = true;
}
if (b.Text != ",")
{
firstOperand += b.Text;
txtBoxDisplay.Text = firstOperand;
}
}
}
else //operandCount != 0
{
if (isFirstChar)
{
if (b.Text == "," || b.Text == "0")
{
secondOperand = "0,";
CommaChrUsed = true;
isFirstChar = false;
}
else
{
secondOperand += b.Text;
txtBoxDisplay.Text = secondOperand;
isFirstChar = false;
}
}
else //!isFirstChar
{
if (b.Text == "," && !CommaChrUsed)
{
secondOperand += b.Text;
txtBoxDisplay.Text = secondOperand;
CommaChrUsed = true;
}
if (b.Text != ",")
{
secondOperand += b.Text;
txtBoxDisplay.Text = secondOperand;
}
}
}
}
private void btnOperator_Click(object sender, EventArgs e) //
all the standard arithmetic operators + %-operator
{
Button b = new Button();
b = (Button)sender;
operandType = b.Text;
if (operandType == "+/-" || operandType == "sqrt" ||
operandType == "1/x") //plus these unary operators
operandCount = 1;
else operandCount = 2;
isFirstChar = true;
CommaChrUsed = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtBoxDisplay.Text = "0,";
firstOperand = "";
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
operandCount = 0;
}
private void btnEquals_Click(object sender, EventArgs e) // =
operator
{
if (operandCount > 1)
{
switch (operandType)
{
case "+":
{
double result = double.Parse(firstOperand)
+ double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "-":
{
double result = double.Parse(firstOperand)
- double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "*":
{
double result = double.Parse(firstOperand)
* double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "/":
{
double result =
double.Parse(firstOperand) / double.Parse(secondOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "%":
{
double result =
(double.Parse(firstOperand) / 100) * (double.Parse(secondOperand));
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
operandCount = 1;
isFirstChar = true;
CommaChrUsed = false;
}
break;
default:
{
MessageBox.Show("The chosen operator is
not a valid operator.", "Unknown operator");
btnClear_Click(sender, e);
}
break;
}
}
else if (operandCount == 1)
{
switch (operandType)
{
case "+/-":
{
double result = -
(double.Parse(firstOperand));
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "sqrt":
{
double result =
Math.Sqrt(double.Parse(firstOperand));
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
}
break;
case "1/x":
{
double result = 1 /
double.Parse(firstOperand);
txtBoxDisplay.Text = result.ToString();
firstOperand = txtBoxDisplay.Text;
secondOperand = "";
isFirstChar = true;
CommaChrUsed = false;
}
break;
default:
{
MessageBox.Show("The chosen operator is
not a valid operator.", "Unknown operator");
btnClear_Click(sender, e);
}
break;
}
}
}
}
}