C#での電卓作成について
現在C#で電卓を作成しているのですが、Funcを使用しても上手に動きません。
やりたいこととしては、1*2*3
のような、連続した計算になります。
演算子ボタンを押したとき、答えが出るようにしたいです。
初めは演算子ボタンの中にイコールの処理を入れたのですが、Funcを使えば短く簡潔に書けると言われました。SUBとADDはまだ書き換えていません。クリアボタンを押したときの処理も書き換え中です。
以下が該当のコードになります。
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1(){
InitializeComponent();
}
bool isFirst = true;
decimal num1 = 0;
private void Number_Click(object sender, EventArgs e)
{
if (sign != arithmetic.undefined && isFirst) {
num1 = Convert.ToDecimal(textDisplay.Text);
textDisplay.Text = "";
isFirst = false;
}
if (textDisplay.TextLength == 12) {
return;
}
textDisplay.Text =
textDisplay.Text + ((Button)sender).Text;
}
private void Dot_Click(object sender, EventArgs e) {
if (textDisplay.Text.IndexOf(".") >= 0) {
return;
}
textDisplay.Text = textDisplay.Text + ".";
}
enum arithmetic{
undefined, ADD, SUB, MUL, DIV
};
arithmetic sign = arithmetic.undefined;
private void ADD_Click(object sender, EventArgs e){
if (sign != arithmetic.undefined) {
decimal num2 = Convert.ToDecimal(textDisplay.Text);
decimal Result = 0;
Result = num1 + num2;
textDisplay.Text = Result.ToString();
}
sign = arithmetic.ADD;
isFirst = true;
}
private void SUB_Click(object sender, EventArgs e)
{
if (sign != arithmetic.undefined) {
decimal num2 = Convert.ToDecimal(textDisplay.Text);
decimal Result = 0;
Result = num1 - num2;
textDisplay.Text = Result.ToString();
}
sign = arithmetic.SUB;
isFirst = true;
}
private void MUL_Click(object sender, EventArgs e){
Func<decimal, decimal, decimal> MUL = (num1, num2) => {
return num1 * num2;
};
sign = arithmetic.MUL;
isFirst = true;
}
private void DIV_Click(object sender, EventArgs e){
Func<decimal,decimal,decimal> DIV = (num1,num2) => {
return num1 / num2;
}
; sign = arithmetic.DIV;
isFirst = true;
}
private void Equal_Click(object sender, EventArgs e){
decimal num2 = Convert.ToDecimal(textDisplay.Text);
decimal Result = 0;
try {
switch (sign) {
case arithmetic.ADD:
Result = num1 + num2;
break;
case arithmetic.SUB:
Result = num1 - num2;
break;
case arithmetic.MUL:
Result = num1 * num2;
break;
case arithmetic.DIV:
Result = num1 / num2;
break;
}
}
catch(DivideByZeroException) {
textDisplay.Text = "0では割れません";{
return;
}
}
textDisplay.Text = Result.ToString();
isFirst = true;
if (textDisplay.TextLength >= 13) {
textDisplay.Text = "桁数超過";
this.button0.Enabled = false;
this.button1.Enabled = false;
this.button2.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = false;
this.button5.Enabled = false;
this.button6.Enabled = false;
this.button7.Enabled = false;
this.button8.Enabled = false;
this.button9.Enabled = false;
this.buttonEq.Enabled = false;
this.Dot.Enabled = false;
this.ADD.Enabled = false;
this.SUB.Enabled = false;
this.MUL.Enabled = false;
this.DIV.Enabled = false;
}
}
private void Clear_Click(object sender, EventArgs e){
this.button0.Enabled = true;
this.button1.Enabled = true;
this.button2.Enabled = true;
this.button3.Enabled = true;
this.button4.Enabled = true;
this.button5.Enabled = true;
this.button6.Enabled = true;
this.button7.Enabled = true;
this.button8.Enabled = true;
this.button9.Enabled = true;
this.buttonEq.Enabled = true;
this.Dot.Enabled = true;
this.ADD.Enabled = true;
this.SUB.Enabled = true;
this.MUL.Enabled = true;
this.DIV.Enabled = true;
}
}
}