1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| Imports system.console
Module Module1
Function CALOPERANDE(ByVal pval1 As Integer, ByVal poper As Char, ByVal pval2 As Integer) As Integer
Select Case poper
Case "+"
Return pval1 + pval2
Case "-"
Return pval1 - pval2
Case "*"
Return pval1 * pval2
Case "/"
If pval1 = 0 Then
Return 0
Else
Return pval1 / pval2
End If
End Select
End Function
Sub ADDTOARRAY(ByRef pArray() As String, ByVal pvaleur As String) 'gestion de tableau de façon dynamique
If IsNothing(pArray) Then 'si le tableau pArray est vide
ReDim pArray(0) 'redimentionner le tableau pArray
Else
ReDim Preserve pArray(pArray.Length) 'redimentionner le tableau pArray sans l'initialiser
End If
pArray(pArray.Length - 1) = pvaleur 'Numéro be cellule du tableau.-1 car un tableau commence par l'indice zéro
End Sub
Function SPLIT(ByVal poperation As String) As String()
Dim lIndexprev, lIndexNext As Integer 'avec Next:index suivant
Dim loperateur, loperande As String
Dim loperation As String = poperation.Replace(" ", String.Empty) 'le string.Empty envoie un vide
Dim ltabterme() As String
lIndexprev = 0 'Index précédent
lIndexNext = loperation.IndexOfAny("+-*/()", lIndexprev) 'Rechercher dans l'importe quel chaine (.IndexofAny)
While lIndexNext > -1
loperateur = loperation.Substring(lIndexNext, 1)
If lIndexNext > lIndexprev Then
loperande = loperation.Substring(lIndexprev, lIndexNext - lIndexprev) 'extrait de la sous chaine dans la chaine principale
Else
loperande = String.Empty
End If
If loperande.Trim <> String.Empty Then
ADDTOARRAY(ltabterme, loperande)
End If
lIndexprev = lIndexNext + 1 'on avance à l'indice suivant'
lIndexNext = loperation.IndexOfAny("+-*/()", lIndexprev)
If lIndexNext = -1 AndAlso lIndexprev < loperation.Length Then
loperande = loperation.Substring(lIndexprev, loperation.Length - lIndexprev)
ADDTOARRAY(ltabterme, loperande)
End If
End While
Return ltabterme
End Function |
Partager