Problème de code de puissance (x^y)

Contenu du snippet

bonsoir à tous. j'ai un petit exercice de calculatrice que je dois rendre le plus tôt possible. ci dessous se trouve le code, mais la fonction puissance ne fonctionne pas correctement. je dois rendre dans 2 heures. si quelqu'un peut m'aider ca me fera plaisir. je suis connecté sur donromys@hotmail.com pour 1h30min. Merci.

Source / Exemple :


Option Explicit
Dim operation As Boolean
Dim puiss As Double
Dim x As Double
Dim y As Integer
Dim i As Integer
Dim Sign As String
Dim Last As Double
Dim Comma As Boolean
Dim taxe As Integer
Private Sub Command1_Click()
Last = 0
   operation = False
    Sign = ""
   Comma = False
    Me.ecran.Text = "0"
End Sub

Private Sub effacer_Click()
Last = 0
    operation = False
    Sign = ""
    Comma = False
    Me.ecran.Text = "0"
    ecran.SetFocus
End Sub

Private Sub fact_Click()
Dim n As Double
Dim i As Double
Dim f As Double
f = 1
n = ecran.Text
If ecran.Text = 0 Then
ecran.Text = "1"
End If
For i = 1 To n Step 1
f = i * f
ecran.Text = f
Next i
End Sub

Private Sub factorielle_Click()
Dim n As Double
Dim i As Double
Dim f As Double
f = 1
n = ecran.Text
If ecran.Text = 0 Then
ecran.Text = "1"
End If
For i = 1 To n Step 1
f = i * f
ecran.Text = f
Next i
End Sub

Private Sub fermer_Click()
End
End Sub

Private Sub C_Click()
    Last = 0
    operation = False
    Sign = ""
    Comma = False
    Me.ecran.Text = "0"
    ecran.SetFocus
End Sub

Private Sub num_Click(Index As Integer)
    If operation = True Then
        ecran.Text = "0"
        operation = False
    End If
    If ecran.Text = "0" Then ecran.Text = ""
    Me.ecran.Text = Me.ecran.Text & Index
End Sub

Private Sub ecran_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End If
    If Me.ecran.Text = "0" Then Me.ecran.Text = ""
End Sub

Private Sub retour_Click()
If Len(Me.ecran.Text) - 1 <> 0 Then
        Me.ecran.Text = Left(Me.ecran.Text, Len(Me.ecran.Text) - 1)
    Else
        ecran.Text = "0"
    End If
End Sub

Private Sub virgule_Click()
    If operation = True Then
        Me.ecran.Text = "0"
        operation = False
    End If
    If Comma <> True Then
        Me.ecran.Text = Me.ecran.Text & ","
        Comma = True
    End If
End Sub

Private Sub cmd_operation_Click(Index As Integer)
    If Index = 4 Then
    Sign = "%"
    Call Validate
    End If
    If Index = 5 Then
    'Sign = "="
       Call Validate
    End If
    If Sign <> "" Then
        Call Validate
    End If
    Last = Me.ecran.Text
        Select Case Index
            Case 0:
                Sign = "+"
            Case 1:
                Sign = "-"
            Case 2:
                Sign = "*"
            Case 3:
                Sign = "/"
            Case 5:
                Sign = "="
                Call Validate
                Exit Sub
            Case 6:
                Sign = "+/-"
            Case 7:
            Sign = "x^y"
                x = Last
                ecran.Text = "0"
                ecran.SetFocus
             Exit Sub
        End Select
    Me.ecran.Text = Last
    operation = True
    Comma = False
End Sub
Public Function Validate()
    Select Case Sign
        Case "+"
            Last = Last + Me.ecran.Text
        Case "-"
            Last = Last - Me.ecran.Text
        Case "*"
            Last = Last * Me.ecran.Text
        Case "/"
            Last = Last / Me.ecran.Text
        Case "%"
            Last = Last - (Last * Me.ecran.Text) / 100
        Case "+/-"
            Last = -Last
        Case "x^y"
         If ecran.Text < 0 Then
           MsgBox "Valeur saisie incorrecte; Veuillez saisir une valeur superieur ou egale à 0"
           End If
           If ecran.Text = 0 Then
           Last = 1
           End If
           If ecran.Text >= 1 Then
           y = ecran.Text
           puiss = x
           For i = y To 1 Step -1
           puiss = puiss * x
           Next i
           'Do While y <= 1
           'puiss = puiss * x
           'y = y - 1
          ' Loop
           ecran.Text = puiss
           End If
     End Select
    Me.ecran.Text = Last
    operation = True
    Sign = ""
    Comma = False
End Function

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.