Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.612
  • Views Posts: 542.381
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

VB.NET - FUNZIONE PER IL CALCOLO DELLE CONGRUENZE LINEARI (Codice)

VB.NET - FUNZIONE PER IL CALCOLO DELLE CONGRUENZE LINEARI (Codice)



 

Oggi vi voglio mettere a disposizione una funzione per il calcolo delle Congruenza Lineari che si denotato con questo simbolo "" .

Le congruenze sono un linguaggio alternativo per trattare la divisibilità fra interi :

 

Definizione. Siano a e b due interi e m un intero strettamente positivo, detto modulo.

Si dice che a e b sono congruenti modulo m, in simboli

a º b  (mod m)

se m divide a-b .


Qui di seguito troverete sia il codice Sorgente e il file eseguibile.


      CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET


Public Class Form1

    Dim ax As Integer

    Dim b_ As Integer

    Dim m As Integer


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        MsgBox("Dovete inserire i seguenti paramatri per costruire una congruenza lineare ax -= b (mod m) : " & vbNewLine _

               & "1) Parametro ax" & vbNewLine _

               & "2) Parametro b " & vbNewLine _

               & "3) Parametro m " & vbNewLine _

               & "Esempio : 14x -= 6 (mod 5) , ax = 14 ; b = 6 ; m = 5", vbInformation, "ATTENZIONE!!!")

        ax = InputBox("Inserisci il primo valore ax : = ", "Inserisci il valore di a ", "14")

        b_ = InputBox("Inserisci il primo valore b : = ", "Inserisci il valore di a ", "6")

        m = InputBox("Inserisci il primo valore mod : = ", "Inserisci il valore di a ", "5")


        'ax = 14

        'b_ = 6

        'm = 5

        box1.Text = ax

        box2.Text = b_

        box3.Text = m


        Congruenza(ax, b_, m)

      


    End Sub


    Function MCD(a As Integer, b As Integer) As Integer

       While (a <> b)

            If (a > b) Then

                a = a - b

            Else

                 b = b - a

            End If

        End While

        Return a

    End Function


    Function MaxComuneDivisore (Primo As Long, Secondo As Long) As Integer

        Dim Temp As Long

        Primo = Math.Abs(Primo)

        Secondo = Math.Abs(Secondo)

        Temp = Primo Mod Secondo

        Do While Temp > 0

            Primo = Secondo

            Secondo = Temp

            Temp = Primo Mod Secondo

        Loop

        MCD = Secondo

    End Function


    Private Sub Congruenza(ByVal a As Integer, ByVal b As Integer, ByVal n As Integer)

        

        Dim d As Integer = MaxComuneDivisore(a, n)

        Dim resto As Integer = 0

        Dim inv, sol As New Integer


        If a <> 0 Then


            Select Case a

                Case Is < 0

                    While (a < 0)

                        a = a + n

                    End While

                Case Is > n

                    While (a > n)

                        a = a - n

                    End While

            End Select


            Select Case b

                Case Is < 0

                    While (b < 0)

                        b = b + n

                    End While

                Case Is >= n

                    While (b >= n)

                        b = b - n

                    End While

            End Select


            resto = b Mod d

            If resto = 0 Then

                a = a / d

                b = b / d

                n = n / d

                Debug.Print("La Congruenza e' compatibile e risulta che : ")

                Debug.Print("La congruenza data e' equivalente ax -= b (mod m) => " & box1.Text & "x -=" & box2.Text & "(mod " & box3.Text & ")")

                Testo.Text = Testo.Text & "La Congruenza è compatibile e risulta che : " & vbNewLine

                Testo.Text = Testo.Text & "La congruenza data è equivalente ax -= b (mod m) => " & box1.Text & "x -= " & box2.Text & "(mod " & box3.Text & ")" & vbNewLine


                Dim k As Integer = 1

                While (((1 + n * k) Mod a) <> 0)

                    k = k + 1

                End While

                inv = (1 + n * k) / a

                sol = inv * b

                If sol > n Then

                    sol = (inv * b) Mod n

                Else

                    sol = inv * b

                End If


                Debug.Print("Segue che ")

                Debug.Print("Le soluzioni sono date da x = " & sol & " + " & n & "K che va da 0 <= K <= " & d - 1)

                Testo.Text = Testo.Text & "Segue che " & vbNewLine & "Le soluzioni sono date da x = " & sol & " + " & n & "K che va da 0 <= K <= " & d - 1 & vbNewLine

                Dim risultato As Integer = 0


                For i As Integer = 0 To d - 1

                    risultato = sol + i * (m / d)

                    Debug.Print("Le soluzioni sono date da x(" & i & ") = " & risultato)

                    Testo.Text = Testo.Text & "Le soluzioni sono date da x(" & i & ") = " & risultato & vbNewLine

                Next

            Else

                Debug.Print("La congruena non è compatibile")

                Testo.Text = Testo.Text & "La congruena non è compatibile"

            End If



        End If


    End Sub



    Private Sub btnCalcolaCongruenza_Click(sender As System.Object, e As System.EventArgs) Handles btnCalcolaCongruenza.Click

        Testo.Clear()

        Congruenza(box1.Text, box2.Text, box3.Text)


    End Sub


    Private Sub box1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles box1.KeyPress

        If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then

            e.Handled = True

        End If

    End Sub


    

    Private Sub box2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles box2.KeyPress

        If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then

            e.Handled = True

        End If

    End Sub


    Private Sub box3_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles box3.KeyPress

        If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then

            e.Handled = True

        End If

    End Sub

     End Class



Potete scaricare il file d'esempio :


Password :  "ImaginSystem" 
Link File Download : Download Congruenza Lineare 


By ImaginSystems & Queen Gin   
Categoria: VB.NET
martedì, 30 ott 2012 Ore. 18.36

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003