함수 오버로딩을 통하여 간단한 사칙 계산기 구현.생성자 함수를 이용하여 값을 초기화 하고 소멸자를 사용할 것[ Source ]// 왠만한 시스템 코드 및 form 코드는 생략하였습니다. public class Calc { private int numA,numB,reSum,reSub,reMux; private double reDiv;
public Calc()
{
numA = 0;
numB = 0;
reSum = 0;
reSub = 0;
reMux = 0;
reDiv = 0;
}
~Calc()
{
MessageBox.Show("여러분과 저만이 진정 롹 뭬니아~","Thank you!!!");
}
public int funcSum(int num1, int num2)
{
numA = num1;
numB = num2;
reSum = numA + numB;
return reSum;
}
public int funcSub(int num1, int num2)
{
numA = num1;
numB = num2;
reSub = numA - numB;
return reSub;
}
public int funcMux(int num1, int num2)
{
numA = num1;
numB = num2;
reMux = numA * numB;
return reMux;
}
public double funcDiv(int num1, int num2)
{
numA = num1;
numB = num2;
reDiv= numA / numB;
return reDiv;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button3_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
textBox5.Text = null;
textBox6.Text = null;
}
private void button1_Click(object sender, System.EventArgs e)
{
int num1, num2;
Calc hnd_Cal = new Calc();
num1 = Convert.ToInt32(textBox1.Text);
num2 = Convert.ToInt32(textBox2.Text);
textBox3.Text = num1 + " + " + num2 + " = " +hnd_Cal.funcSum(num1,num2).ToString();
textBox4.Text = num1 + " - " + num2 + " = " +hnd_Cal.funcSub(num1,num2).ToString();
textBox5.Text = num1 + " * " + num2 + " = " + hnd_Cal.funcMux(num1, num2).ToString();
textBox6.Text = num1 + " / " + num2 + " = " + hnd_Cal.funcDiv(num1, num2).ToString();
}
}
}
댓글
댓글 쓰기