2次方程式
今回は難しいことはしません。原点に戻って2次方程式を解の公式で求めるプログラムです。
#console
Dim a As Double , b As Double , c As Double , d As Double
Input a,b,c
d = (b * b - 4 * a *c)
If d = 0 Then
Print -b / 2 * a
Else If d > 0 Then
Print (-b + Sqr(d)) / 2 * a
Print (-b - Sqr(d)) / 2 * a
Else
Print "実数ではない"
End If
? 1,-1,-6
3
-2
? 4,-4,1
8
? 1,2,3
実数ではない
これだけでは寂しいので、グラフを書いてみます。
#console
Dim a As Double , b As Double , c As Double , d As Double
Input a,b,c
d = (b * b - 4 * a *c)
If d = 0 Then
Print -b / 2 * a
Else If d > 0 Then
Print (-b + Sqr(d)) / 2 * a
Print (-b - Sqr(d)) / 2 * a
Else
Print "実数ではない"
End If
'グラフを描く
Dim hWnd As HWND , hDC As HDC , hPen As HPEN
Dim p[20] As POINTAPI'座標データを入れる
Dim x As Long
For x=-10 To 10
p[x+10].x = 320 + x * 5
p[x+10].y = 200 - a*x*x + b * x + c * 5
Next
hWnd = GetForegroundWindow()
SetWindowPos(hWnd , NULL , 0, 0, 640 , 480 , SWP_NOMOVE)
hDC = GetDC(hWnd)
hPen = CreatePen(PS_SOLID , 2 , RGB(0,255,0))
SelectObject(hDC , hPen)
Polyline(hDC , p , 21)
DeleteObject(hPen)
ReleaseDC(hWnd , hDC)
Input a
