Saber las diferencias entre numeros

(1/2) > >>

luis456:
Estoy tratando de averiguar las diferencias entre una gran base de numeros entre si,tratare de explicarlo :) tengo en un txt una lista de numeros

numeros en el txt que debera de leer el codigo en forma descendente

02 15 28 50 45 60
10 20 35 40 60 80
11 40 49 50 77 99
20 25 33 48 60 88
30 36 44 55 77 95


Bien lo que se nesecita es saber las diferencias entre ellos

02 15 28  50  45  60 <--primer numero de la lista

10 20 35  40  60  80 <--segundo numero comparar con el primero

+8 +5 +7 -10 +15 +20 <--resultados


10  20  35  40  60 80 <-se toma el segundo numero  compara con el  tercer numero
11  40  49  50  77 99 <- com compara al igual que el anterior y haci con todos los numeros hasta acabar la muestra

+1 +20 +14 +10 +17 +19 < resultados


en fin en el txt resultante lo unico que tiene que estar son los resultados

+8 +5  +7  -10 +15 +20  <--resultados
+1 +20 +14 +10 +17 +19

Tengo este codigo amabilididad por elektro


Código
Public Class Form1
 
   Private Shadows Sub Load() Handles MyBase.Load
 
       Dim sb As New System.Text.StringBuilder
       Dim Values As New List(Of Integer()) From
           {
               ({1I, 20, 1I}),
               ({7I, 22, 4I}),
               ({8I, 27, 11}),
               ({20, 29, 17}),
               ({23, 33, 22}),
               ({39, 46, 31})
           }
 
       For Each ValueCol() As Integer In Values
 
           Dim Value1 As Integer = ValueCol(0)
           Dim Value2 As Integer = ValueCol(1)
           Dim Value3 As Integer = ValueCol(2)
 
           Dim Diff1 As String = GetDifferenceExpression(Value1, Value2)
           Dim Diff2 As String = GetDifferenceExpression(Value2, Value3)
 
 
           sb.AppendLine(String.Format("{0} -> {1} = {2}",
                         Value1.ToString("00"), Value2.ToString("00"), Diff1))
 
           sb.AppendLine(String.Format("{0} -> {1} = {2}",
                         Value2.ToString("00"), Value3.ToString("00"), Diff2))
 
 
           sb.AppendLine()
 
       Next ValueCol
 
       MessageBox.Show(sb.ToString)
 
       Application.Exit()
 
 
   End Sub
 
   Private Sub Form1_Load(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles MyBase.Load
       Try
           Dim Archivo As System.IO.FileStream
           ' crea un archivo vacio prueba.txt  
           Archivo = System.IO.File.Create("c:\asi\Prueba.txt")
           ' error  
       Catch oe As Exception
           MsgBox(oe.Message, MsgBoxStyle.Critical)
       End Try
   End Sub
 
   ' Get Difference expression
   ' ( By Elektro )
   '
   ' Usage Examples:
   ' MessageBox.Show(GetDifferenceExpression(5, 10))
   '
   ''' <summary>
   ''' Obtiene la expresión aritmética suma diferencia / descanso entre dos valores.
   ''' </summary>
   ''' <param name="Value1">El primer valor.</param>
   ''' <param name="Value2">El segundo valor.</param>
   ''' <returns>La expresión aritmética.</returns>
   Friend Function GetDifferenceExpression(ByVal Value1 As Long, ByVal Value2 As Long) As String
 
       Select Case Value2
 
           Case Is > Value1 ' Value2 es más grande que Valor1.
               Return String.Format("+{0}", CStr(Value2 - Value1))
 
           Case Is < Value1 ' Value2 es inferior a Valor1.
               Return String.Format("-{0}", CStr(Value1 - Value2))
 
           Case Else ' Value2 es igual a Valor1.
               Return "0"
 
       End Select
 
   End Function
 
End Class



Luis



Eleкtro:
El siguiente código asume que todas las combinaciones tienen la misma cantidad de números, y que en el archivo de texto de donde quieres sacar las combinaciones, hay un número par de combinaciones para ir sacándolas de dos en dos, como me comentaste.

con este ya van 100 ejemplos que te muestro Luis,
saludos!

Código
Public Class TestForm
 
   ' Aquí guardas los dos combos, y la diferencia de la resta entre ambos.
   Public NotInheritable Class ComboDiff
 
       ''' <summary>
       ''' Gets or sets the first combination.
       ''' </summary>
       ''' <value>The first combination.</value>
       Public Property First As IEnumerable(Of Integer)
 
       ''' <summary>
       ''' Gets or sets the second combination.
       ''' </summary>
       ''' <value>The second combination.</value>
       Public Property Second As IEnumerable(Of Integer)
 
       ''' <summary>
       ''' Gets the combination difference.
       ''' </summary>
       ''' <value>The combination difference.</value>
       Public ReadOnly Property Difference As IEnumerable(Of Integer)
           Get
               Dim comdoDiff As New List(Of Integer)
               For i As Integer = 0 To (First.Count - 1)
                   comdoDiff.Add(Me.Second(i) - Me.First(i))
               Next i
               Return comdoDiff
           End Get
       End Property
 
   End Class
 
   Private Sub Test() Handles MyBase.Shown
 
       Dim comboDiffs As New List(Of ComboDiff)
       Dim txtFilePath As String = "C:\Archivo.txt"
       Dim txtLines As IEnumerable(Of String) = IO.File.ReadAllText(txtFilePath).
                                                        Split({ControlChars.NewLine},
                                                              StringSplitOptions.RemoveEmptyEntries)
 
       For i As Integer = 0 To (txtLines.Count - 1) Step 2
 
           Dim comboDiff As New ComboDiff
           With comboDiff
               .First = From value As String In txtLines(i).Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
                        Select Integer.Parse(value)
 
               .Second = From value As String In txtLines(i + 1).Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
                         Select Integer.Parse(value)
           End With
           comboDiffs.Add(comboDiff)
 
       Next i
 
       For Each comboDiff As ComboDiff In comboDiffs
           Dim sb As New System.Text.StringBuilder
           With sb
               .AppendLine(String.Format("Primer combo : {0}", String.Join(",", comboDiff.First)))
               .AppendLine(String.Format("Segundo combo: {0}", String.Join(",", comboDiff.Second)))
               .AppendLine(String.Format("Diferencia   : {0}", String.Join(",", comboDiff.Difference)))
           End With
           MessageBox.Show(sb.ToString, "", MessageBoxButtons.OK)
       Next comboDiff
 
   End Sub
 
End Class

luis456:
Gracias mil de nuevo elektro no que haria sin ti :) lo estoy probando y me da este error


Error1   No se puede encontrar el evento 'Shown'.



luis

Eleкtro:
Cita de: luis456 en 17 Febrero 2015, 14:59 pm

Error1   No se puede encontrar el evento 'Shown'.

Es porque el nombre de tu formulario será "Form1", no "TestForm", al no existir un Form con ese nombre no existen sus eventos... modifica el nombre a "Form1" en el código de arriba y ya.

Saludos

luis456:
Era eso  :( mil gracias lo estoy probando :)


luis

Navegación

[0] Índice de Mensajes

[#] Página Siguiente