[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

c�digo-fonte aberto



Oi pessoal (especialmente o Saldanha que come�ou a publicar c�digo-fonte de
seus programas)

Devido a concorr�ncia do meu amigo Saldanha na programa��o, terei de dar a
tacada de volta, abro portanto o c�digo-fonte mais esperado do ano: c�lculo
se um n�mero � primo e se n�o, qual sua decomposi��o em primos (c�digo para
visual basic 5, n�o testado em outras vers�es do vb. Direito de c�pia
permanece com o autor ... bl�bl�bl�)



Sub Optimize()
'==============================================================
'Processo do Optimiza��o da resposta
'==============================================================
    If Text2.Text = "� primo" Then Exit Sub
Dim i, XCopy, Answ, Count, where, where2, what
    XCopy = Text2.Text
UpMe:
    where = InStr(1, XCopy, ";")
    what = Left(XCopy, where)
    'While InStr(1, XCopy, what) = 1
    While Left(XCopy, Len(what)) = what
        Count = Count + 1
        XCopy = Right(XCopy, Len(XCopy) - Len(what))
    Wend
    Answ = Answ & Left(what, Len(what) - 1) & "^" & Count & ";"
    Count = 0
    If Not XCopy = "" Then GoTo UpMe
    Text2.Text = Answ
    While InStr(1, Text2, "^1;")
        where = InStr(1, Text2, "^1;") - 1
        Text2 = Mid(Text2, 1, where) & Mid(Text2, where + 3, Len(Text2) -
where - 2)
    Wend
End Sub
'==============================================================
'Decomp�e um n�mero em seus fatores primos
'==============================================================
Private Sub Command1_Click()
    If Len(Text1) > 4 Then
        Answ = MsgBox("Este c�lculo pode demorar v�rios minutos ou at�
horas. Deseja continuar?", vbOKCancel, "Cuidado...")
        If Answ = 2 Then Exit Sub
    ElseIf Len(Text1) = 0 Then Exit Sub
    End If

On Error GoTo MyErr
Dim X, i, XCopy, CCopy, si, hwtims
    XCopy = Text1
    CCopy = Text1
    For i = 2 To Text1 / 2
HeyMan:
    If XCopy = 0 Or XCopy = 1 Then GoTo NextI
        XCopy = XCopy / i
        If InStr(XCopy, ",") = 0 Then
            X = X & i & ";"
            CCopy = XCopy
            GoTo HeyMan
        Else
            XCopy = CCopy
        End If
    Next i
NextI:
    Text2 = X
    If X = "" Then Text2 = "� primo"
    Text2.Visible = True
    Optimize
    Exit Sub
MyErr:
    MsgBox "H� um erro. Verifique seus n�meros"
    Exit Sub
End Sub


Como podes ver, Saldanha, tenho l� umas cartas na manga. Ali�s, obrigado
pela dica do MuPAD, tem me ajudado bastante. Infelizmente n�o mexo com C,
ainda.
Quem sabe vc. publica mais C que eu publico mais c�digos do vb...

Paremos de brigar. Esta � uma lista pac�fica...


Abra�o,


Benjamin Hinrichs