Created
September 15, 2014 15:48
-
-
Save yukkuribemani/c67ec107f263c1fbaca2 to your computer and use it in GitHub Desktop.
C#+Windows Formで作るダメな電卓例(改善版) ref: http://qiita.com/yukkuribemani/items/ddf25a9534b5e58c6ad9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace calc | |
{ | |
public partial class Form1 : Form | |
{ | |
private bool Form_Write_Permit = true; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void btnNumber_Click(object sender, EventArgs e) | |
{ | |
if (Form_Write_Permit == false) | |
{ | |
return; | |
} | |
textBoxCalc.Text += ((Button)sender).Text; | |
} | |
private void Op_Click(object sender, EventArgs e) | |
{ | |
if (input_check() != 1) | |
{ | |
return; | |
} | |
textBoxCalc.Text += ((Button)sender).Text; | |
} | |
private void point_Click(object sender, EventArgs e) | |
{ | |
if(input_check() != 1) | |
{ | |
return; | |
} | |
textBoxCalc.Text += '.'; | |
} | |
private void Clear_Click(object sender, EventArgs e)//式の初期化 | |
{ | |
textBoxCalc.Text = String.Empty; | |
Form_Write_Permit = true; | |
} | |
private void eq_Click(object sender, EventArgs e)//式の計算を行う | |
{ | |
char Operator = ' '; | |
string Form_Num_Copy = string.Empty; | |
double Form_Check; | |
double Left_Value = 0.0; //演算子で式を分けた時の左と右の数値 | |
double Right_Value = 0.0; | |
bool First_Found = true; // 初回の計算かどうか判定 | |
// フォーム内に数値以外もしくは空のときに"="を押しても何もしない | |
if (double.TryParse(textBoxCalc.Text, out Form_Check) || textBoxCalc.Text == "") | |
{ | |
return; | |
} | |
for (int i = 0; i < textBoxCalc.Text.Length;i++) //フォームから1文字ずつ読みだす | |
{ | |
char Char_Temp = textBoxCalc.Text[i]; | |
if (Char.IsDigit(Char_Temp) == true || Char_Temp == '.') //数値もしくは小数点の場合 | |
{ | |
Form_Num_Copy += Char_Temp; //数値のコピーをとっておく | |
if(i == textBoxCalc.Text.Length - 1)//数値挿入時点で式が終了するか調べる | |
{ | |
Right_Value = double.Parse(Form_Num_Copy); | |
Left_Value = calc(Left_Value, Right_Value, Operator); | |
Form_Num_Copy = string.Empty; | |
} | |
} | |
else if (Char_Temp == '+' || Char_Temp == '-' || Char_Temp == '*' || Char_Temp == '/')//いずれかの演算子の場合 | |
{ | |
if(First_Found == true)//初めて演算子を見つけたとき | |
{ | |
Left_Value = double.Parse(Form_Num_Copy);//1回目のLeft_Valueの代入 | |
Form_Num_Copy = string.Empty; | |
First_Found = false; | |
} | |
else // 2回目以降の演算 | |
{ | |
Right_Value = double.Parse(Form_Num_Copy); | |
Left_Value = calc(Left_Value, Right_Value, Operator); | |
Form_Num_Copy = string.Empty; | |
} | |
Operator = Char_Temp; | |
} | |
} | |
textBoxCalc.Text = Left_Value.ToString(); | |
Form_Write_Permit = false;//計算後に数値を入力しても受け付けないようにする | |
} | |
private double calc(double x, double y,char op) | |
{ | |
double ans = 0.0; | |
switch(op) | |
{ | |
case '+': | |
ans = x + y; | |
break; | |
case '-': | |
ans = x - y; | |
break; | |
case '*': | |
ans = x * y; | |
break; | |
case '/': | |
ans = x / y; | |
break; | |
} | |
return ans; | |
} | |
// 入力内容のチェック | |
private int input_check() | |
{ | |
if (textBoxCalc.Text == string.Empty) | |
{ | |
return -1; | |
} | |
else if (textBoxCalc.Text[textBoxCalc.Text.Length - 1] == '+' || textBoxCalc.Text[textBoxCalc.Text.Length - 1] == '-' || | |
textBoxCalc.Text[textBoxCalc.Text.Length - 1] == '*' || textBoxCalc.Text[textBoxCalc.Text.Length - 1] == '/' || | |
textBoxCalc.Text[textBoxCalc.Text.Length - 1] == '.') | |
{ | |
return -1; | |
} | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment