STATE에 따라 버튼 이벤트가 다르게 동작해야 하기 때문에 어떻게 할까 고민하다 아래 4가지로 정의해서 구현해봄
IDLE
COMPLETE_FIRST_NUMBER
OPERATOR_INSERTED
COMPLETE_SECONDE_NUMBER
지금 C#문법 배우는 중이고 일단 돌아가는거 보자 하고 막 만들어서 컨트롤 이름도 안바꿈..
namespace cal
{
public partial class Form1 : Form
{
enum state_t
{
IDLE,
COMPLETE_FIRST_NUMBER,
OPERATOR_INSERTED,
COMPLETE_SECOND_NUMBER
}
state_t currentState = state_t.COMPLETE_FIRST_NUMBER;
public Form1()
{
InitializeComponent();
this.label1.AutoSize = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (currentState == state_t.COMPLETE_FIRST_NUMBER)
{
this.label1.Text += "0";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "0";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else if(currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "0";
}
else
{
this.label2.Text = "";
this.label3.Text = "";
}
}
private void button5_Click(object sender, EventArgs e)
{
if(currentState == state_t.IDLE)
{
this.label1.Text = "1";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "1";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "1";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button6_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "2";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "2";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "2";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button7_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "3";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "3";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "3";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button12_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "4";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "4";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "4";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button11_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "5";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "5";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "5";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button10_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "6";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "6";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "6";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button13_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "7";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "7";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "7";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button14_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "8";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "8";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "8";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button15_Click(object sender, EventArgs e)
{
if (currentState == state_t.IDLE)
{
this.label1.Text = "9";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_FIRST_NUMBER;
}
else if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text += "9";
}
else if (currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "9";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else
{
}
}
private void button3_Click(object sender, EventArgs e)
{
this.label1.Text = "0";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.IDLE;
}
private void button1_Click(object sender, EventArgs e)
{
if(currentState == state_t.IDLE)
{
this.label1.Text = "0.";
this.label2.Text = "";
this.label3.Text = "";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else if(currentState == state_t.OPERATOR_INSERTED)
{
this.label1.Text = "0.";
currentState = state_t.COMPLETE_SECOND_NUMBER;
}
else if(currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
if (!this.label1.Text.Contains("."))
{
this.label1.Text += ".";
}
}
else
{
}
}
private void label1_TextChanged(object sender, EventArgs e)
{
if(this.label1.Text.EndsWith("."))
{
string numStr = this.label1.Text.Replace(",", "").Replace(".", "");
if (decimal.TryParse(numStr, out decimal num))
{
string formattedStr = string.Format("{0:#,##0}", num);
this.label1.Text = formattedStr + ".";
}
}
else if (this.label1.Text.Contains('.'))
{
string numStr = this.label1.Text.Replace(",", "");
string[] delimeter = { "." };
string[] parts = numStr.Split(delimeter, 2, StringSplitOptions.RemoveEmptyEntries);
if (decimal.TryParse(parts[0], out decimal num))
{
string formattedStr = string.Format("{0:#,##0}", num);
this.label1.Text = formattedStr + "." + parts[1];
}
}
else
{
string numStr = this.label1.Text.Replace(",", "");
if(decimal.TryParse(numStr, out decimal num))
{
string formattedStr = string.Format("{0:#,##0}", num);
this.label1.Text = formattedStr;
}
}
Graphics g = this.label1.CreateGraphics();
SizeF textSize = g.MeasureString(this.label1.Text, this.label1.Font);
if(this.label1.Width < textSize.Width)
{
this.label1.Text = this.label1.Text.Substring(0, this.label1.Text.Length - 1);
}
}
private void button8_Click(object sender, EventArgs e)
{
if(currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.OPERATOR_INSERTED || currentState == state_t.IDLE)
{
this.label2.Text = "+";
this.label3.Text = this.label1.Text;
currentState = state_t.OPERATOR_INSERTED;
}
else if(currentState == state_t.COMPLETE_SECOND_NUMBER )
{
double first;
double second;
double result = 0;
double.TryParse(this.label1.Text, out second);
double.TryParse(this.label3.Text, out first);
result = first + second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
this.label3.Text = this.label1.Text;
this.label2.Text = "+";
currentState = state_t.OPERATOR_INSERTED;
}
else
{
}
}
private void button9_Click(object sender, EventArgs e)
{
if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.OPERATOR_INSERTED || currentState == state_t.IDLE)
{
this.label2.Text = "-";
this.label3.Text = this.label1.Text;
currentState = state_t.OPERATOR_INSERTED;
}
else if (currentState == state_t.COMPLETE_SECOND_NUMBER)
{
double first;
double second;
double result = 0;
double.TryParse(this.label1.Text, out second);
double.TryParse(this.label3.Text, out first);
result = first - second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
this.label3.Text = this.label1.Text;
this.label2.Text = "-";
currentState = state_t.OPERATOR_INSERTED;
}
else
{
}
}
private void button16_Click(object sender, EventArgs e)
{
if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.OPERATOR_INSERTED || currentState == state_t.IDLE)
{
this.label2.Text = "×";
this.label3.Text = this.label1.Text;
currentState = state_t.OPERATOR_INSERTED;
}
else if (currentState == state_t.COMPLETE_SECOND_NUMBER)
{
decimal first;
decimal second;
decimal result = 0;
decimal.TryParse(this.label1.Text, out second);
decimal.TryParse(this.label3.Text, out first);
result = first * second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
this.label3.Text = this.label1.Text;
this.label2.Text = "×";
currentState = state_t.OPERATOR_INSERTED;
}
else
{
}
}
private void button17_Click(object sender, EventArgs e)
{
if (currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.OPERATOR_INSERTED || currentState == state_t.IDLE)
{
this.label2.Text = "÷";
this.label3.Text = this.label1.Text;
currentState = state_t.OPERATOR_INSERTED;
}
else if (currentState == state_t.COMPLETE_SECOND_NUMBER)
{
decimal first;
decimal second;
decimal result = 0;
decimal.TryParse(this.label1.Text, out second);
decimal.TryParse(this.label3.Text, out first);
result = first / second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
this.label3.Text = this.label1.Text;
this.label2.Text = "÷";
currentState = state_t.OPERATOR_INSERTED;
}
else
{
}
}
private void button4_Click(object sender, EventArgs e)
{
decimal first;
decimal second;
decimal result = 0;
decimal.TryParse(this.label1.Text, out second);
decimal.TryParse(this.label3.Text, out first);
if (this.label2.Text == "+")
{
this.label3.Text = this.label3.Text + " + " + this.label1.Text;
this.label2.Text = "=";
result = first + second;
if(Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
}
else if (this.label2.Text == "-")
{
this.label3.Text = this.label3.Text + " - " + this.label1.Text;
this.label2.Text = "=";
result = first - second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
}
else if (this.label2.Text == "×")
{
this.label3.Text = this.label3.Text + " × " + this.label1.Text;
this.label2.Text = "=";
result = first * second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
}
else if (this.label2.Text == "÷")
{
this.label3.Text = this.label3.Text + " ÷ " + this.label1.Text;
this.label2.Text = "=";
result = first / second;
if (Math.Floor(result) == result)
{
this.label1.Text = string.Format("{0:#,##0}", result);
}
else
{
this.label1.Text = string.Format("{0:#,##0.########################}", result);
}
}
else
{
this.label2.Text = "=";
this.label3.Text = this.label1.Text;
}
currentState = state_t.IDLE;
}
private void button18_Click(object sender, EventArgs e)
{
if(currentState == state_t.COMPLETE_FIRST_NUMBER || currentState == state_t.COMPLETE_SECOND_NUMBER)
{
this.label1.Text = this.label1.Text.Substring(0, this.label1.Text.Length - 1);
if(this.label1.Text == "")
{
this.label1.Text = "0";
}
}
}
}
}
'프로그래밍 > C# (WinForms)' 카테고리의 다른 글
C#, LINQ ] Enumerable 클래스, Range(), Select(), Where(), ToArray() 메서드 (0) | 2023.04.09 |
---|---|
WinForms ] Label vs TextBox 컨트롤 (0) | 2023.04.09 |
C#, WinForms ] label의 너비를 초과하는 글자 자르기 (0) | 2023.02.25 |
C# ] decimal 과 double 의 차이 (1) | 2023.02.25 |
C#, WinForms ] MessageBox 속성 (0) | 2023.02.25 |