Can you improve this, don't like brute force

Discussion in 'Programmer's Corner' started by Felo, Apr 18, 2012.

  1. Felo

    Thread Starter Member

    Feb 20, 2012
    91
    13
    Hi, this is my first thread so be kind!!

    I've made a very brute force algorithm in vb.net to decompose a number and achive the following.

    1. I can have any number from 0 to 999999, the leght of this number is fixed at 6 figures.
    2. In order to send this number, or any within, there are two registers that have a value of multiples of 50 and 1. (for example if I want to send number 41, then I would have to send registerA that has a value of 1, 41 times since 41 it's not multiple of 50, instead if I were to send number 51, then I would send one registerB, wich has a value of 50, and one registerA, wich has a value of 1)

    So, I have two available numbers (50 or 1) to complete any number from 1 to 999999.

    this is the code that i've implemented in VB.NET this is actually working flawlessly, but my guts says that there has to be a better way, I know that it may be a pain to go trough to, but this is really itching me.

    please feel free to ask, don't be shy now...

    here we go
    Code ( (Unknown Language)):
    1.     Public Sub transfiere_creditos()
    2.         Dim uni, dec, cen, udm, ddm, cdm, xes, nes, lon As Integer
    3.         Dim opa As Double
    4.  
    5.         lon = lbl_creditos.Text.Length()
    6.         uni = lbl_creditos.Text.Substring(lon - 1, 1)
    7.         lon = lon - 1
    8.         If lon > 0 Then
    9.             dec = lbl_creditos.Text.Substring(lon - 1, 1)
    10.             lon = lon - 1
    11.             If lon > 0 Then
    12.                 cen = lbl_creditos.Text.Substring(lon - 1, 1)
    13.                 lon = lon - 1
    14.                 If lon > 0 Then
    15.                     udm = lbl_creditos.Text.Substring(lon - 1, 1)
    16.                     lon = lon - 1
    17.                     If lon > 0 Then
    18.                         ddm = lbl_creditos.Text.Substring(lon - 1, 1)
    19.                         lon = lon - 1
    20.                         If lon > 0 Then
    21.                             cdm = lbl_creditos.Text.Substring(lon - 1, 1)
    22.                             lon = lon - 1
    23.                         End If
    24.                     End If
    25.                 End If
    26.             End If
    27.         End If
    28.  
    29.         'decode xes
    30.         xes = uni
    31.         'decode nes
    32.         If dec > 0 Then
    33.             opa = dec * 10
    34.             opa = opa / 50
    35.         End If
    36.         If opa < 1 Then
    37.             opa = dec * 10
    38.             xes = xes + opa
    39.             opa = 0
    40.         End If
    41.         If opa = 1 Then
    42.             nes = 1
    43.         End If
    44.         If opa > 1 Then
    45.             nes = 1
    46.             opa = dec * 10
    47.             opa = opa - 50
    48.             xes = xes + opa
    49.         End If
    50.         'decode centenas
    51.         If IsNumeric(cen) = True Then
    52.             If cen > 0 Then
    53.                 opa = cen * 100
    54.                 opa = opa / 50
    55.                 nes = nes + opa
    56.             End If
    57.         End If
    58.         'decode udm
    59.         If IsNumeric(udm) = True Then
    60.             If udm > 0 Then
    61.                 opa = udm * 1000
    62.                 opa = opa / 50
    63.                 nes = nes + opa
    64.             End If
    65.         End If
    66.         'decode ddm
    67.         If IsNumeric(ddm) = True Then
    68.             If ddm > 0 Then
    69.                 opa = ddm * 10000
    70.                 opa = opa / 50
    71.                 nes = nes + opa
    72.             End If
    73.         End If
    74.         'decode cdm
    75.         If IsNumeric(cdm) = True Then
    76.             If cdm > 0 Then
    77.                 opa = cdm * 100000
    78.                 opa = opa / 50
    79.                 nes = nes + opa
    80.             End If
    81.         End If
    82.         'Label5.Text = xes
    83.         'Label6.Text = nes
    84.         lbl_creditos.Text = "0"
    85.         Do Until xes = 0
    86.             SendKeys.SendWait("x")
    87.             xes = xes - 1
    88.         Loop
    89.  
    90.         Do Until nes = 0
    91.             SendKeys.SendWait("n")
    92.             nes = nes - 1
    93.         Loop
    94.     End Sub
     
  2. panic mode

    Senior Member

    Oct 10, 2011
    1,321
    304
    lookup modulo operator...

    Code ( (Unknown Language)):
    1.  
    2. X=179  ' set some value
    3.  
    4. X50 = X \ 50   ' result is 3
    5. X1 = X mod 50 ' result is 29
     
    Last edited: Apr 18, 2012
Loading...