Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 29
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    54

    [VB6] Risolvere equazioni in forma ax+b=0

    Ciao a tutti. Sto costruendo un programma che risolva equazioni. Per semplicità, voglio soffermarmi ora solo su quelle di primo grado.

    Per risolvere un'equazione in forma ax+b=0 sarebbe sufficiente inserire a e b con due input diversi. Ma se io volessi scrivere l'intera equazione in una sola stringa, ad esempio in una Textbox, come faccio ad estrapolare da quella stringa i due singoli valori di a e b?

    Mi è stato detto che devo usare un parser (che se ho ben capito è un "interprete"), ma non so proprio come. Grazie in anticipo per l'aiuto.

    PS: So che ho già aperto una discussione simile, ma a) è scritta da cani b) ha titolo sbagliato c) ha 0 risposte... perdonatemi =)

  2. #2
    Ti basterebbe leggere la stringa carattere per carattere per ricavarne i due valori, vedi dove c'è la x e i valori che la precedono (prima dello spazio) rappresentano la a, l'altro valore che poi trovi per esclusione è la b.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    54
    OK, quindi come faccio a leggerla carattere per carattere? =)

  4. #4
    se vuoi ho una routine che fa questo lavoro ...
    Mattia

  5. #5
    Public Function EvaluateExpr(ByVal expr As String) As Currency

    Const PREC_NONE = 11
    Const PREC_UNARY = 10 ' Not actually used.
    Const PREC_POWER = 9
    Const PREC_TIMES = 8
    Const PREC_DIV = 7
    Const PREC_INT_DIV = 6
    Const PREC_MOD = 5
    Const PREC_PLUS = 4

    Dim is_unary As Boolean
    Dim next_unary As Boolean
    Dim parens As Integer
    Dim Pos As Integer
    Dim expr_len As Integer
    Dim ch As String
    Dim lexpr As String
    Dim rexpr As String
    Dim Value As String
    Dim Status As Long
    Dim best_pos As Integer
    Dim best_prec As Integer

    ' Remove leading and trailing blanks.
    expr = Trim$(expr)
    expr_len = Len(expr)
    If expr_len = 0 Then Exit Function

    ' If we find + or - now, it is a unary operator.
    is_unary = True

    ' So far we have nothing.
    best_prec = PREC_NONE

    ' Find the operator with the lowest precedence.
    ' Look for places where there are no open
    ' parentheses.
    For Pos = 1 To expr_len
    ' Examine the next character.
    ch = Mid$(expr, Pos, 1)

    ' Assume we will not find an operator. In
    ' that case the next operator will not
    ' be unary.
    next_unary = False

    If ch = " " Then
    ' Just skip spaces.
    next_unary = is_unary
    ElseIf ch = "(" Then
    ' Increase the open parentheses count.
    parens = parens + 1

    ' An operator after "(" is unary.
    next_unary = True
    ElseIf ch = ")" Then
    ' Decrease the open parentheses count.
    parens = parens - 1

    ' An operator after ")" is not unary.
    next_unary = False

    ' If parens < 0, too many ')'s.
    If parens < 0 Then
    Err.Raise vbObjectError + 1001, _
    "EvaluateExpr", _
    "Parentesi in eccesso nella formula )'" & _
    expr & "'"
    End If
    ElseIf parens = 0 Then
    ' See if this is an operator.
    If ch = "^" Or ch = "*" Or _
    ch = "/" Or ch = "\" Or _
    ch = "%" Or ch = "+" Or _
    ch = "-" _
    Then
    ' An operator after an operator
    ' is unary.
    next_unary = True

    Select Case ch
    Case "^"
    If best_prec >= PREC_POWER Then
    best_prec = PREC_POWER
    best_pos = Pos
    End If

    Case "*", "/"
    If best_prec >= PREC_TIMES Then
    best_prec = PREC_TIMES
    best_pos = Pos
    End If

    Case "\"
    If best_prec >= PREC_INT_DIV Then
    best_prec = PREC_INT_DIV
    best_pos = Pos
    End If

    Case "%"
    If best_prec >= PREC_MOD Then
    best_prec = PREC_MOD
    best_pos = Pos
    End If

    Case "+", "-"
    ' Ignore unary operators
    ' for now.
    If (Not is_unary) And _
    best_prec >= PREC_PLUS _
    Then
    best_prec = PREC_PLUS
    best_pos = Pos
    End If
    End Select
    End If
    End If
    is_unary = next_unary
    Next Pos

    ' If the parentheses count is not zero,
    ' there's a ')' missing.
    If parens <> 0 Then
    Err.Raise vbObjectError + 1002, _
    "EvaluateExpr", "Missing ) in '" & _
    expr & "'"
    End If

    ' Hopefully we have the operator.
    If best_prec < PREC_NONE Then
    lexpr = Left$(expr, best_pos - 1)
    rexpr = Right$(expr, expr_len - best_pos)
    Select Case Mid$(expr, best_pos, 1)
    Case "^"
    EvaluateExpr = _
    EvaluateExpr(lexpr) ^ _
    EvaluateExpr(rexpr)
    Case "*"
    EvaluateExpr = _
    EvaluateExpr(lexpr) * _
    EvaluateExpr(rexpr)
    Case "/"
    EvaluateExpr = _
    EvaluateExpr(lexpr) / _
    EvaluateExpr(rexpr)
    Case "\"
    EvaluateExpr = _
    EvaluateExpr(lexpr) \ _
    EvaluateExpr(rexpr)
    Case "%"
    EvaluateExpr = _
    EvaluateExpr(lexpr) Mod _
    EvaluateExpr(rexpr)
    Case "+"
    EvaluateExpr = _
    EvaluateExpr(lexpr) + _
    EvaluateExpr(rexpr)
    Case "-"
    EvaluateExpr = _
    EvaluateExpr(lexpr) - _
    EvaluateExpr(rexpr)
    End Select
    Exit Function
    End If

    ' If we do not yet have an operator, there
    ' are several possibilities:
    '
    ' 1. expr is (expr2) for some expr2.
    ' 2. expr is -expr2 or +expr2 for some expr2.
    ' 3. expr is Fun(expr2) for a function Fun.
    ' 4. expr is a primitive.
    ' 5. It's a literal like "3.14159".

    ' Look for (expr2).
    If Left$(expr, 1) = "(" And Right$(expr, 1) = ")" Then
    ' Remove the parentheses.
    EvaluateExpr = EvaluateExpr(Mid$(expr, 2, expr_len - 2))
    Exit Function
    End If

    ' Look for -expr2.
    If Left$(expr, 1) = "-" Then
    EvaluateExpr = -EvaluateExpr( _
    Right$(expr, expr_len - 1))
    Exit Function
    End If

    ' Look for +expr2.
    If Left$(expr, 1) = "+" Then
    EvaluateExpr = EvaluateExpr( _
    Right$(expr, expr_len - 1))
    Exit Function
    End If

    ' Look for Fun(expr2).
    If expr_len > 5 And Right$(expr, 1) = ")" Then
    lexpr = LCase$(Left$(expr, 4))
    rexpr = Mid$(expr, 5, expr_len - 5)
    Select Case lexpr
    Case "sin("
    EvaluateExpr = Sin(EvaluateExpr(rexpr))
    Exit Function
    Case "cos("
    EvaluateExpr = Cos(EvaluateExpr(rexpr))
    Exit Function
    Case "tan("
    EvaluateExpr = Tan(EvaluateExpr(rexpr))
    Exit Function
    Case "sqr("
    EvaluateExpr = Sqr(EvaluateExpr(rexpr))
    Exit Function
    End Select
    End If

    ' See if it's a primitive.
    'On Error Resume Next
    'value = Primitives.Item(expr)
    'status = Err.Number
    'On Error GoTo 0
    'If status = 0 Then
    ' EvaluateExpr = CSng(value)
    ' Exit Function
    'End If

    ' It must be a literal like "2.71828".
    On Error Resume Next
    EvaluateExpr = CSng(expr)
    Status = Err.Number
    On Error GoTo 0
    If Status <> 0 Then
    Err.Raise Status, _
    "EvaluateExpr", _
    "Error evaluating '" & expr & _
    "' as a constant."
    End If
    End Function
    Mattia

  6. #6
    Nel tuo codice la usi come una funzione ... cioè :

    dim W_NUM as currency


    W_NUM = EvaluateExpr("((10+1+2)+10)*2")

    e voilà ! w_num CONTERRA' IL RISULTATO

    Credimi puoi far passare qualnque espressione matematica .

    Il dubbio è "ma perchè vb non ha una funzione simile ?" e bhoooo
    Mattia

  7. #7
    azz non mantiene l'identazione ... se vuoi ti mando uno zip !
    Mattia

  8. #8
    Originariamente inviato da broc
    OK, quindi come faccio a leggerla carattere per carattere? =)
    In questo modo, per esempio per trovare la x:

    codice:
                'La stringa non deve essere vuota altrimenti dà errore, quindi controllo che non lo sia.
                If Equazione <> "" Then
                    'Trovo la x nell'equazione.
                    For Carattere = 0 To Equazione.Length - 1
                        If Valore.Chars(Carattere) = "x" Then
                            MessageBox.Show("x trovata!")
                            Exit Sub
                        End If
                    Next Carattere
                End If

  9. #9
    Originariamente inviato da matt_vb6
    azz non mantiene l'identazione ... se vuoi ti mando uno zip !
    Non ti mantiene l'identazione perché non te lo riconosce come codice, devi metterlo tra queste:

    [-code]
    Stringhe di codice
    [-/code]

    Togliendoci il trattino che ho messo dopo che ho aperto le parentesi quadre perché altrimenti lo leggeva come comando e non lo visualizzava XD

  10. #10
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Originariamente inviato da Cavaliere Nero
    In questo modo
    Questo è un thread per VB6 non VB.NET
    No MP tecnici (non rispondo nemmeno!), usa il forum.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.