0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

電卓

0
Last updated at Posted at 2026-04-02

'''
using System;
using System.Linq;
using System.Windows.Forms;
namespace calculator_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizeBox = false; // タイトルバーの最大化の削除 todo:プロパティにないかな
this.MinimizeBox = false; // タイトルバーの最小化の削除
}
/// --------------------------------------
/// グローバル変数
/// --------------------------------------
private bool isNewInput = true; // 新しい数字の入力の場合のフラグ
private int maxDigits = 20; // 最大桁数
private decimal? left = null; // 左辺
private decimal? right = null; // 右辺
private decimal memory = 0m; // メモリ
private enum ErrorCode
{
None,
OverFlow,
DivideByZero,
}
private enum Operator
{
Plus,
Minus,
Times,
Divide,
Equal
}
///


/// 数字ボタンクリック時の処理
///

///
private void NumberButtonClick(string num)
{
// 桁数チェック
if (!IsDigitLengthWithinLimit())
{
return;
}
if (isNewInput)
{ // 一番最初に数字が押された時
if (mainTxtBox.Text == "0")
{
mainTxtBox.Text = num;
}
else if (mainTxtBox.Text == "00")
{
mainTxtBox.Text = "0";
}
else
{
mainTxtBox.Text += num;
}
}
else
{ // すでにボタンが押され、文字列結合する
mainTxtBox.Text = num;
isNewInput = false;
}
decimal displayNum = ParseDisplay();
// 3桁区切り
mainTxtBox.Text = FormatWithComma(displayNum);
}
///
/// 演算子ボタンクリック字の処理
///

/// 演算子
private void OperationButtonClick(Operator ope)
{
isNewInput = true; // 新しく入力を受け付けれるようにする
decimal mainTxt = ParseDisplay();
if (testLabel.Text != "")
{
// 計算を行う
right = mainTxt;
ErrorCode errorCode = ExecuteOperation();
if (errorCode == ErrorCode.DivideByZero)
{
Error(errorCode);
return;
}
mainTxtBox.Text = FormatWithComma((decimal)left);
}
else
{
// まだ計算はしない
left = mainTxt;
}
testLabel.Text = left.ToString() + " " + OperatorToSymbol(ope);
mainTxtBox.Text = "0";
}
///
/// 四則演算処理
/// left 演算子 rghtの結果をleftに入れる
///

///
private ErrorCode ExecuteOperation()
{
if (testLabel.Text.Contains("+"))
{
left += right;
}
else if (testLabel.Text.Contains("-"))
{
left -= right;
}
else if (testLabel.Text.Contains("×"))
{
left *= right;
}
else if (testLabel.Text.Contains("÷"))
{
if (right == 0m)
return ErrorCode.DivideByZero;
left /= right;
}
else
{
left = right;
}
return ErrorCode.None;
}
///
/// 3桁区切りにする処理
///

private string FormatWithComma(decimal value)
{
if (value % 1 == 0)
return value.ToString("#,0");
else
return value.ToString("#,0.####################");
}
///
/// 最大桁数を超えているか
///

/// 最大桁数越え:false, 最大桁数以内:true
private bool IsDigitLengthWithinLimit()
{
string[] charDelete = new string[] { ",", ".", "-" };
string txtBox = mainTxtBox.Text;
// 記号などの文字列を除く
foreach (var c in charDelete)
{
txtBox = txtBox.Replace(c, "");
}
if (maxDigits <= txtBox.Length)
{
// 桁数あふれ
return false;
}
else
{
return true;
}
}
///
/// 表示→decimal変換(カンマ除去)
///

///
private decimal ParseDisplay()
{
return decimal.Parse(mainTxtBox.Text.Replace(",", ""));
}
///
/// 演算子のenumからその演算子記号を返す
///

///
///
private string OperatorToSymbol(Operator ope)
{
switch (ope)
{
case Operator.Plus: return "+";
case Operator.Minus: return "-";
case Operator.Times: return "×";
case Operator.Divide: return "÷";
default: return "";
}
}
///
/// エラーが起こった時の処理
///

///
private void Error(ErrorCode code)
{
switch (code)
{
case ErrorCode.DivideByZero:
mainTxtBox.Text = "ゼロ除算エラー";
break;
case ErrorCode.OverFlow:
mainTxtBox.Text = "桁数エラー(演算結果の整数桁が大きすぎ)";
break;
default:
break;
}
includeTaxBtn.Enabled =
excludeTaxBtn.Enabled =
mClearBtn.Enabled =
changeWarekiBtn.Enabled =
mPlusBtn.Enabled =
mMinusBtn.Enabled =
mRecallBtn.Enabled =
zeroBtn.Enabled =
zerozeroBtn.Enabled =
oneBtn.Enabled =
twoBtn.Enabled =
threeBtn.Enabled =
fourBtn.Enabled =
fiveBtn.Enabled =
sixBtn.Enabled =
sevenBtn.Enabled =
eightBtn.Enabled =
nineBtn.Enabled =
pointBtn.Enabled =
plusAndMinusBtn.Enabled =
timesBtn.Enabled =
plusBtn.Enabled =
divideBtn.Enabled =
minusBtn.Enabled =
equalBtn.Enabled = false;
}
///
/// エラー後のボタン復活処理
///

private void ResetButton()
{
includeTaxBtn.Enabled =
excludeTaxBtn.Enabled =
mClearBtn.Enabled =
changeWarekiBtn.Enabled =
mPlusBtn.Enabled =
mMinusBtn.Enabled =
mRecallBtn.Enabled =
zeroBtn.Enabled =
zerozeroBtn.Enabled =
oneBtn.Enabled =
twoBtn.Enabled =
threeBtn.Enabled =
fourBtn.Enabled =
fiveBtn.Enabled =
sixBtn.Enabled =
sevenBtn.Enabled =
eightBtn.Enabled =
nineBtn.Enabled =
pointBtn.Enabled =
plusAndMinusBtn.Enabled =
timesBtn.Enabled =
plusBtn.Enabled =
divideBtn.Enabled =
minusBtn.Enabled =
equalBtn.Enabled = true;
}
#region イベントハンドラ
#region 数字のボタン
private void zeroBtn_Click(object sender, EventArgs e)
{
string num = "0";
NumberButtonClick(num);
}
private void zerozeroBtn_Click(object sender, EventArgs e)
{
string num = "00";
NumberButtonClick(num);
}
private void oneBtn_Click(object sender, EventArgs e)
{
string num = "1";
NumberButtonClick(num);
}
private void twoBtn_Click(object sender, EventArgs e)
{
string num = "2";
NumberButtonClick(num);
}
private void threeBtn_Click(object sender, EventArgs e)
{
string num = "3";
NumberButtonClick(num);
}
private void fourBtn_Click(object sender, EventArgs e)
{
string num = "4";
NumberButtonClick(num);
}
private void fiveBtn_Click(object sender, EventArgs e)
{
string num = "5";
NumberButtonClick(num);
}
private void sixBtn_Click(object sender, EventArgs e)
{
string num = "6";
NumberButtonClick(num);
}
private void sevenBtn_Click(object sender, EventArgs e)
{
string num = "7";
NumberButtonClick(num);
}
private void eightBtn_Click(object sender, EventArgs e)
{
string num = "8";
NumberButtonClick(num);
}
private void nineBtn_Click(object sender, EventArgs e)
{
string num = "9";
NumberButtonClick(num);
}
#endregion
#region クリア系
///
/// AC
///

///
///
private void allClearBtn_Click(object sender, EventArgs e)
{
mainTxtBox.Text = "0";
left = null;
right = null;
isNewInput = true;
testLabel.Text = "";
ResetButton();
}
///
/// C
///

///
///
private void clearBtn_Click(object sender, EventArgs e)
{
mainTxtBox.Text = "0";
ResetButton();
}
#endregion
#region 演算系
///
/// 足し算
///

///
///
private void plusBtn_Click(object sender, EventArgs e)
{
OperationButtonClick(Operator.Plus);
}
///
/// 引き算
///

///
///
private void minusBtn_Click(object sender, EventArgs e)
{
OperationButtonClick(Operator.Minus);
}
///
/// 掛け算
///

///
///
private void timesBtn_Click(object sender, EventArgs e)
{
OperationButtonClick(Operator.Times);
}
///
/// 割り算
///

///
///
private void divideBtn_Click(object sender, EventArgs e)
{
OperationButtonClick(Operator.Divide);
}
///
/// イコールボタンクリック時の処理
///

///
///
private void equalBtn_Click(object sender, EventArgs e)
{
right = ParseDisplay();
ErrorCode errorCode = ExecuteOperation();
if (errorCode == ErrorCode.DivideByZero)
{
Error(errorCode);
return;
}
mainTxtBox.Text = FormatWithComma((decimal)left);
right = null; // いらない気がする
testLabel.Text = "";
}
#endregion
#region メモリ関係
///
/// M+
///

///
///
private void mPlusBtn_Click(object sender, EventArgs e)
{
decimal mainTxt = ParseDisplay();
memory += mainTxt;
isNewInput = true;
mainTxtBox.Text = "0";
}
///
/// M-
///

///
///
private void mMinusBtn_Click(object sender, EventArgs e)
{
decimal mainTxt = ParseDisplay();
memory -= mainTxt;
isNewInput = true;
mainTxtBox.Text = "0";
}
///
/// MR
///

///
///
private void mRecallBtn_Click(object sender, EventArgs e)
{
mainTxtBox.Text = FormatWithComma((decimal)memory);
}
///
/// MC
///

///
///
private void mClearBtn_Click(object sender, EventArgs e)
{
memory = 0;
}
#endregion
#region 付加系
///
/// .
///

///
///
private void pointBtn_Click(object sender, EventArgs e)
{
bool hasPoint = mainTxtBox.Text.Contains('.');
if (hasPoint)
{
return;
}
mainTxtBox.Text += ".";
pointBtn.Enabled = true;
}
///
/// +/-
///

///
///
private void plusAndMinusBtn_Click(object sender, EventArgs e)
{
if (mainTxtBox.Text.StartsWith("-"))
{
mainTxtBox.Text = mainTxtBox.Text.Substring(1);
}
else
{
mainTxtBox.Text = "-" + mainTxtBox.Text;
}
}
#endregion
#region キーボードからの読み取り系
///
/// 数字のボタン
///

///
///
private void Calculator_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.D0:
case (char)Keys.NumPad0:
zeroBtn.PerformClick();
break;
case (char)Keys.D1:
case (char)Keys.NumPad1:
oneBtn.PerformClick();
break;
case (char)Keys.D2:
case (char)Keys.NumPad2:
twoBtn.PerformClick();
break;
case (char)Keys.D3:
case (char)Keys.NumPad3:
threeBtn.PerformClick();
break;
case (char)Keys.D4:
case (char)Keys.NumPad4:
fourBtn.PerformClick();
break;
case (char)Keys.D5:
case (char)Keys.NumPad5:
fiveBtn.PerformClick();
break;
case (char)Keys.D6:
case (char)Keys.NumPad6:
sixBtn.PerformClick();
break;
case (char)Keys.D7:
case (char)Keys.NumPad7:
sevenBtn.PerformClick();
break;
case (char)Keys.D8:
case (char)Keys.NumPad8:
eightBtn.PerformClick();
break;
case (char)Keys.D9:
case (char)Keys.NumPad9:
nineBtn.PerformClick();
break;
default:
break;
}
}
///
/// 記号・演算子・Enterボタン
///

///
///
private void Calculator_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Oemplus || e.KeyCode == Keys.Add)
{
plusBtn.PerformClick();
}
else if (e.KeyCode == Keys.OemMinus || e.KeyCode == Keys.Subtract)
{
minusBtn.PerformClick();
}
else if (e.KeyCode == Keys.Multiply || (e.Shift && e.KeyCode == Keys.D8))
{
timesBtn.PerformClick();
}
else if (e.KeyCode == Keys.Divide || e.KeyCode == Keys.OemQuestion)
{
divideBtn.PerformClick();
}
else if (e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal)
{
pointBtn.PerformClick();
}
else if (e.KeyCode == Keys.Enter)
{
equalBtn.PerformClick();
}
}
#endregion
#region チェックボックス変更時系
///
///
///

///
///
private void changeTaxRateMode_CheckedChanged(object sender, EventArgs e)
{
if (changeTaxRateMode.Checked)
{
Console.WriteLine("チェックついたよ");
}
else
{
Console.WriteLine("チェックが外れたよ");
}
}
///
///
///

///
///
private void changeWarekiMode_CheckedChanged(object sender, EventArgs e)
{
}
#endregion
#endregion
}
}

'''

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?