Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Difetto su Programma sulla Bisezione?
Forum - C# / VB.NET - Difetto su Programma sulla Bisezione?

Avatar
ciaosimo (Normal User)
Newbie


Messaggi: 3
Iscritto: 28/12/2010

Segnala al moderatore
Postato alle 16:42
Venerdì, 22/04/2011
Sto realizzando un programma in VB.NET con OOP per calcolare le radici di un'equazione del tipo ax^3+bx^2+cx+d=0.

Posso avere tre casi:

- 1 intersezione con l'asse x
- 2 intersezioni con l'asse x
- 3 intersezioni con l'asse x

a parte questo, il programma funziona, però se qualcuno mi volesse DARE QUALCHE SUGGERIMENTO PER MIGLIORARLO, BEN VENGA! grazie

Codice sorgente - presumibilmente VB.NET

  1. Public Class FrmBisezione
  2.  
  3.     Private Sub CmdCalcola_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdCalcola.Click
  4.         LblX1.Text = ""
  5.         LblX2.Text = ""
  6.         LblX3.Text = ""
  7.  
  8.         Dim ObjBisezione As ClsBisezione
  9.  
  10.         ObjBisezione = New ClsBisezione
  11.  
  12.         If TxtA.Text <> "" And TxtB.Text <> "" And TxtC.Text <> "" And TxtD.Text <> "" Then
  13.             ObjBisezione.A = Convert.ToSingle(TxtA.Text)
  14.             ObjBisezione.B = Convert.ToSingle(TxtB.Text)
  15.             ObjBisezione.C = Convert.ToSingle(TxtC.Text)
  16.             ObjBisezione.D = Convert.ToSingle(TxtD.Text)
  17.         Else
  18.             MsgBox("Mancano dei dati!")
  19.         End If
  20.  
  21.         LblX1.Text = ObjBisezione.Calcola(-1, 1)
  22.         LblX2.Text = ObjBisezione.Calcola(Int(Convert.ToSingle(Rnd() * -15)), -1)
  23.         LblX3.Text = ObjBisezione.Calcola(1, Int((Convert.ToSingle(Rnd() * 15))))
  24.     End Sub
  25.  
  26. End Class



Codice sorgente - presumibilmente VB.NET

  1. Public Class ClsBisezione
  2.     Private mA, mB, mC, mD, Xm, l, m, Fl, Fm, FXm As Single
  3.     Public Sub New()
  4.         mA = mB = mC = mD = 1
  5.     End Sub
  6.     Public Property A() As Single
  7.         Get
  8.             Return mA
  9.         End Get
  10.         Set(ByVal Dato As Single)
  11.             If Dato <> 0 Or Dato = 0 Then
  12.                 mA = Dato
  13.             Else
  14.                 Err.Raise(13)
  15.             End If
  16.         End Set
  17.     End Property
  18.     Public Property B() As Single
  19.         Get
  20.             Return mB
  21.         End Get
  22.         Set(ByVal Dato As Single)
  23.             If Dato <> 0 Or Dato = 0 Then
  24.                 mB = Dato
  25.             Else
  26.                 Err.Raise(13)
  27.             End If
  28.         End Set
  29.     End Property
  30.     Public Property C() As Single
  31.         Get
  32.             Return mC
  33.         End Get
  34.         Set(ByVal Dato As Single)
  35.             If Dato <> 0 Or Dato = 0 Then
  36.                 mC = Dato
  37.             Else
  38.                 Err.Raise(13)
  39.             End If
  40.         End Set
  41.     End Property
  42.     Public Property D() As Single
  43.         Get
  44.             Return mD
  45.         End Get
  46.         Set(ByVal Dato As Single)
  47.             If Dato <> 0 Or Dato = 0 Then
  48.                 mD = Dato
  49.             Else
  50.                 Err.Raise(13)
  51.             End If
  52.         End Set
  53.     End Property
  54.     Public Function Calcola(ByRef l As Single, ByRef m As Single) As String
  55.  
  56.         Dim n As Byte = 50
  57.  
  58.         Do
  59.             Fl = A * (l ^ 3) + B * (l ^ 2) + C * l + D
  60.             Fm = A * (m ^ 3) + B * (m ^ 2) + C * m + D
  61.  
  62.             If Fl * Fm < 0 Then
  63.                 n = 50
  64.                 'MsgBox("I [" & Int(l) & ";" & Int(m) & "]")
  65.                 Exit Do
  66.             Else
  67.                 n -= 1
  68.                 If n = 0 Then
  69.                     Return "..."
  70.                     Exit Do
  71.                 End If
  72.             End If
  73.         Loop
  74.  
  75.         For i = 0 To 300
  76.  
  77.             Xm = (l + m) / 2
  78.             FXm = A * (Xm ^ 3) + B * (Xm ^ 2) + C * Xm + D
  79.  
  80.             If FXm = 0 Then
  81.                 Exit For
  82.             Else
  83.                 If FXm > 0 Then
  84.                     m = Xm
  85.                 Else
  86.                     l = Xm
  87.                 End If
  88.             End If
  89.  
  90.         Next
  91.  
  92.         Return Convert.ToString(Math.Round(Xm, 2))
  93.  
  94.     End Function
  95.  
  96. End Class




Ultima modifica effettuata da ciaosimo il 22/04/2011 alle 20:19
PM
Avatar
Thejuster (Admin)
Guru^2


Messaggi: 2306
Iscritto: 04/05/2008

Up
0
Down
V
Segnala al moderatore
Postato alle 17:43
Venerdì, 22/04/2011
http://www.pierotofy.it/pages/extras/forum/16/28972-legger ...

-modifica il titolo della discussione altrimenti credo che nessuno ti aiuterà.
perchè il topic và contro al regolamento.


https://mire.forumfree.it/ - Mire Engine
C# UI Designer
PM
Avatar
Phil93 (Normal User)
Pro


Messaggi: 85
Iscritto: 26/01/2011

Up
0
Down
V
Segnala al moderatore
Postato alle 0:05
Sabato, 23/04/2011
Usi una gestione degli errori obsoleta e basata su VB6. Ti consiglio di usare le eccezioni.
http://totemslair.org/guide/viewchapter.php?guida=vb&id=32/

Ti consiglio anche http://totemslair.org/guide/viewchapter.php?guida=vb&id=36 - Il Totem - 23/04/11 11:26
PM