QuadraticFormulaForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _14_QuadraticFormula
{
public partial class QuadraticFormulaForm : Form
{
//RootsOfQuadraticEquation()
public void RootsOfQuadraticEquation(double a, double b, double c,
out double dResult1, out double dResult2)
{
dResult1 = (-b + Math.Sqrt((b * b) - (4 * a * c))) / (2 * a);
dResult2 = (-b - Math.Sqrt((b * b) - (4 * a * c))) / (2 * a);
}
public QuadraticFormulaForm()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
//변수 선언
int a = int.Parse(txtA.Text);
int b = int.Parse(txtB.Text);
int c = int.Parse(txtC.Text);
//이전 값 초기화
double dResult1, dResult2 = 0;
lblResult.Text = "";
//RootsOfQuadraticEquation() 호출
RootsOfQuadraticEquation(a, b, c, out dResult1, out dResult2);
//판별식에 따른 결과 출력
//D > 0
if ((b * b) - (4 * a * c) > 0)
lblResult.Text = dResult1.ToString() + ", " + dResult2.ToString();
//D == 0
else if ((b * b) - (4 * a * c) == 0)
lblResult.Text = dResult1.ToString();
//D < 0
else if ((b * b) - (4 * a * c) < 0)
lblResult.Text = "허근";
}
}
}

Windows Forms 앱으로 만듦
ax^2 + bx + c = 0에서

<name 속성>
a값을 입력받는 textbox: txtA
b값을 입력받는 textbox: txtB
c값을 입력받는 textbox: txtC
입력을 확인받아 실행하는 button: btnOk
결과(여기서는 해)를 출력하는 label: lblResult

카테고리:

최초작성:

댓글남기기