IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Vos contributions VB6 Discussion :

Exercice : Source d'une Calculatrice en VB6 [Sources]


Sujet :

Vos contributions VB6

  1. #1
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 038
    Points
    20 038
    Par défaut Exercice : Source d'une Calculatrice en VB6
    Bonjour ce Week-end Pascal s'annonçant pluvieux

    Je vous propose, un petite exercice ..

    la création d'un calculatrice en VB6..

    Pour ouvrir le BAL, voici ma première création :

    http://bbil.developpez.com/sources/vb6/calcVB6-01/




    Elle est simple ,pas forcément pas terminée,.. , un peu tordue dans la gestion des flags de mémorisation ....Mais en gros elle fonctionne...


    Je vous propose d'utiliser cette discussion

    soit pour commenter ce premier source..
    soit pour proposer vos créations ..





    Les meilleures créations trouverons leur place dans notre page source ..




    à vos claviers

  2. #2
    Expert éminent
    Avatar de bidou
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mai 2002
    Messages
    3 055
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Transports

    Informations forums :
    Inscription : Mai 2002
    Messages : 3 055
    Points : 7 962
    Points
    7 962
    Par défaut
    Amusons nous alors.

    Un clone de la calculette 'standard' windows, la aussi largement améliorable

    http://bidou.developpez.com/outils/calc.frm


  3. #3
    Modérateur
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 077
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 077
    Points : 17 197
    Points
    17 197
    Par défaut
    Pour faire vivre un peu cette discussion.
    La calculatrice

    La page d'aide


    Soyez tolérant, ce source je l’avais fait en prévision du passage a l’EURO, pour les commerciaux de là où je travaillais, je n’ai volontairement pas retouché le source, bien qu’il y aurai des améliorations à y faire, au vue de ce que j’ai appris depuis grâce à notre site préféré (DVP).
    Tout ceci pour bien faire voir que l’on peut évoluer dans sa façon d’écrire du code au cours des années qui passent.
    Fichiers attachés Fichiers attachés
    Soyez sympa, pensez -y
    Balises[CODE]...[/CODE]
    Balises[CODE=NomDuLangage]...[/CODE] quand vous mettez du code d'un autre langage que celui du forum ou vous postez.
    Balises[C]...[/C] code intégré dans une phrase.
    Balises[C=NomDuLangage]...[/C] code intégré dans une phrase quand vous mettez du code d'un autre langage que celui du forum ou vous postez.
    Le bouton en fin de discussion, quand vous avez obtenu l'aide attendue.
    ......... et pourquoi pas, pour remercier, un pour celui/ceux qui vous ont dépannés.
    👉 → → Ma page perso sur DVP ← ← 👈

  4. #4
    Membre expert
    Avatar de Delbeke
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    2 675
    Détails du profil
    Informations personnelles :
    Âge : 70
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 2 675
    Points : 3 696
    Points
    3 696
    Par défaut
    Personellement, j'ai toujours regrété qu'il n'existe pas de calculatrice programmable(Un comble pour un ordinateur). C'est un projet que j'avais abordé il y a bien lngtemps et que je n'ai jamais fini, mais il avait produit mon composant DiEval6.ocx. Seul rescapé de mes divagations d'alors.
    En général, on ne demande de conseils que pour ne pas les suivre ou, si on les a suivis, reprocher à quelqu'un de les avoir donnés
    (ALEXANDRE DUMAS)

    N'hésitez pas à visiter ma page de contributions

  5. #5
    Membre habitué
    Inscrit en
    Mai 2005
    Messages
    125
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 125
    Points : 128
    Points
    128
    Par défaut
    c'est bien cette discussion. elle m'a permis de sortir les vieux projets du carton.

    ce n'est pas vraiment une calculatrice. mais peut servir comme telle. on peut lui faire faire autre chose.

    Note: faut que MSscriptcontrol soit installé

    Fichiers attachés Fichiers attachés

  6. #6
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 038
    Points
    20 038
    Par défaut
    merci medkarim pour ta contribution j'ai rajouté une image pour donnez des idées à d'autres contributeurs ...

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2010
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2010
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    petite participation de ma part.

    La calculatrice la plus SIMPLE qu'il puisse exister, c'est d'ailleurs le tout premier programme que j'ai développé à l'école cette année !




    Et ici une version un peu plus améliorée mais faites pas attention au design, je m'en fou un peu de ça dans mes débuts, j'essaie que le code soit impeccable




    Voili voilou
    Fichiers attachés Fichiers attachés

  8. #8
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Août 2011
    Messages : 1
    Points : 1
    Points
    1
    Par défaut PArticiper
    J'ai moi aussi un peut travailler sur une calculette. C'est pas encore parfait, mais je compte beaucoup sur votre aide et vos critiques pour la parfaire.
    Fichiers attachés Fichiers attachés

  9. #9
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 038
    Points
    20 038
    Par défaut
    Citation Envoyé par nathanscott Voir le message
    J'ai moi aussi un peut travailler sur une calculette. C'est pas encore parfait, mais je compte beaucoup sur votre aide et vos critiques pour la parfaire.
    Il s'agit d'une version .Net tu devrais poste ton code la partie "Téléchargez" de VB.Net ici :

    http://vb.developpez.com/telecharger...rie/324/Divers

  10. #10
    Membre éprouvé
    Avatar de Montor
    Homme Profil pro
    Autre
    Inscrit en
    Avril 2008
    Messages
    879
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : Autre

    Informations forums :
    Inscription : Avril 2008
    Messages : 879
    Points : 963
    Points
    963
    Par défaut
    C'est un peut trop tard pour moi mais rien empeche de mettre ma contribution "il s'agit d'un interpréteur d'expression "
    bienque on ne utilise ce genre de code que dans rare cas.. créer un calculateur reste une excercice de style rien de plus

    classe CLexer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    Option Explicit
    Private Declare Function RtlMoveMemory Lib "kernel32" (ByVal Dest As Long, _
              ByVal Source As Long, ByVal iCount As Long) As Long
    Private Declare Function GetLocaleInfoA Lib "kernel32" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As Long, ByVal cchData As Long) As Long
    Private Declare Function GetThreadLocale Lib "kernel32" () As Long
    Const LOCALE_SDECIMAL = &HE
    Public Enum TokenType
       tkInvalid = 1000
       tkNone = 0
       tkWhite = 1
       tkNumber = 2
       tkIdent = 3
       tkOpPow = 4
       tkOpenExp = 5
       tkCloseExp = 6
       tkOpenBra = 7
       tkCloseBra = 8
       tkOpNot = 9
       OpTermBegin = 100
         tkOpOr = 101
         tkOpXor = 102
         tkOpPlus = 103
         tkOpMinus = 104
       OpTermEnd = 105
       OpFacBegin = 200
         tkOpMul = 201
         tkOpMod = 202
         tkOpDivF = 203
         tkOpDiv = 204
         tkOpAnd = 205
         tkOpShl = 206
         tkOpShr = 207
       OpFacEnd = 208
    End Enum
     
    Private DecimalSep As Long
    Private fCode As String
    Private fPtr As Long
    Private fLen As Long
    Private fPos As Long
    Private fCopyStart As Long
    Private Char As Long
    Public CurrToken As TokenType
     
    Private Sub Class_Initialize()
       'Récupère le séparateur décimal
       If GetLocaleInfoA(GetThreadLocale, LOCALE_SDECIMAL, VarPtr(DecimalSep), 4) = 0 Then
            DecimalSep = 44
       End If
    End Sub
     
    Private Function GetChar(ByVal Idx As Long) As Long
       If (Idx < fLen) And (Idx >= 0) Then
         RtlMoveMemory VarPtr(GetChar), fPtr + Idx * 2, 1
       End If
       Char = GetChar
    End Function
     
    Private Function IdentType(ByVal AIdent As String) As TokenType
          Select Case AIdent
             Case "MOD": IdentType = tkOpMod
             Case "DIV": IdentType = tkOpDiv
             Case "AND": IdentType = tkOpAnd
             Case "OR":  IdentType = tkOpOr
             Case "XOR": IdentType = tkOpXor
             Case "NOT": IdentType = tkOpNot
             Case "SHL": IdentType = tkOpShl
             Case "SHR": IdentType = tkOpShr
          Case Else
             IdentType = tkIdent
          End Select
    End Function
     
    Private Function CharType(ByVal Char As Long) As TokenType
       Select Case Char
            Case 97 To 122, 65 To 90  ' a..z,A..Z alpha
               CharType = tkIdent
            Case 1 To 32: CharType = tkWhite   'blanc
            Case 48 To 57: CharType = tkNumber '0..9
            Case 45: CharType = tkOpMinus '-
            Case 43: CharType = tkOpPlus  '+
            Case 42: CharType = tkOpMul   '*
            Case 47: CharType = tkOpDivF  '/
            Case 94: CharType = tkOpPow   '^
            Case 40: CharType = tkOpenExp  '(
            Case 41: CharType = tkCloseExp ')
            Case 91: CharType = tkOpenBra  '[
            Case 93: CharType = tkCloseBra ']
            Case 0:  CharType = tkNone     '#0
            Case Else
               CharType = tkInvalid
       End Select
    End Function
     
    Private Sub LoadCharSet(ByVal AG As TokenType)
        While CharType(GetChar(fPos + 1)) = AG
           fPos = fPos + 1
        Wend
    End Sub
     
    Private Function NextChar() As Integer
         fPos = fPos + 1
         NextChar = GetChar(fPos)
    End Function
     
    Public Function NextToken() As TokenType
       While CharType(GetChar(fPos)) = tkWhite
         fPos = fPos + 1
       Wend
       fCopyStart = fPos
       CurrToken = CharType(Char)
       Select Case CurrToken
         Case tkIdent
             LoadCharSet tkIdent
         Case tkNumber
         '[0-9]+ (.[0-9]+)? (E|e(-|+)? [0-9]+)?
             LoadCharSet tkNumber
             If Char = DecimalSep Then  ', or .
                NextChar
                LoadCharSet tkNumber
             End If
             If (Char = 69) Or (Char = 101) Then 'E e
                NextChar
                If (GetChar(fPos + 1) = 43) Or (Char = 45) Then ' - +
                    NextChar
                End If
                LoadCharSet tkNumber
             End If
       End Select
       If fPos < fLen Then
          fPos = fPos + 1
       End If
       If CurrToken = tkIdent Then
          CurrToken = IdentType(Token)
       End If
       NextToken = CurrToken
    End Function
     
    Public Property Let Text(ACode As String)
       fCode = ACode
       fPtr = StrPtr(fCode)
       fLen = Len(fCode)
       fPos = 0
       fCopyStart = 0
    End Property
     
    Public Property Get Token() As String
    Dim L As Long
       L = fPos - fCopyStart
       If (L <= fLen) And (L >= 0) Then
        Token = Space(L)
        RtlMoveMemory StrPtr(Token), fPtr + fCopyStart * 2, L * 2
        Token = UCase$(Token)
       End If
    End Property
     
    Public Property Get Position() As String
        Position = fPos
    End Property
    classe CExpression
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    Option Explicit
     
    Private fLexer As New CLexer
     
    Private Property Get CurrentToken() As TokenType
         CurrentToken = fLexer.CurrToken
    End Property
     
    Private Function NextToken() As TokenType
         NextToken = fLexer.NextToken
         SynErr fLexer.CurrToken = tkInvalid
    End Function
     
    Private Function IsUnairyOp(ByVal AOP As TokenType) As Boolean
       Select Case AOP
            Case tkOpNot, tkOpPlus, tkOpMinus
                 IsUnairyOp = True
       End Select
    End Function
     
    Private Function IsFacOperator(ByVal AOP As TokenType) As Boolean
       IsFacOperator = ((AOP > OpFacBegin) And (AOP < OpFacEnd))
    End Function
     
    Private Function IsTermOperator(ByVal AOP As TokenType) As Boolean
       IsTermOperator = ((AOP > OpTermBegin) And (AOP < OpTermEnd))
    End Function
    '<Factor> ::= (+|-)<Expression>| NUMBER | <Simple Call> | CONSTANT |'('<Expression>')' | '['<Expression>']' | NULL
    Private Function GetFactor() As Double
    Dim UOP As TokenType
    Dim Ret As Double
    Dim Ident As String
        If IsUnairyOp(NextToken) Then
           UOP = CurrentToken
           Ret = GetExpression()
           UnairyOperation UOP, Ret
        Else
          Select Case CurrentToken
            Case tkNumber
                  Ret = CDbl(fLexer.Token)
            Case tkIdent
                  Ident = fLexer.Token
                  If Not GetConstValue(Ret, Ident) Then
                     SynErr NextToken <> tkOpenExp
                     Ret = Simple_Call(GetExpression, Ident)
                     SynErr CurrentToken <> tkCloseExp
                  End If
            Case tkOpenExp
                  Ret = GetExpression
                  SynErr CurrentToken <> tkCloseExp
            Case tkOpenBra
                  Ret = GetExpression
                  SynErr CurrentToken <> tkCloseBra
            Case tkNone
                  Ret = 0
            Case Else
                  SynErr
          End Select
          NextToken
        End If
        GetFactor = Ret
    End Function
    '<Exp> ::= <Factor>(^<Factor>)*
    Private Function GetExp() As Double
         GetExp = GetFactor
         While CurrentToken = tkOpPow
               BinaryOperation CurrentToken, GetExp, GetFactor
         Wend
    End Function
    '<Term> ::= <Exp> (DIV | * | / | MOD ... <Exp>)*
    Private Function GetTerm() As Double
         GetTerm = GetExp
         While IsFacOperator(CurrentToken)
               BinaryOperation CurrentToken, GetTerm, GetExp
         Wend
    End Function
    '<Expression>  ::= <Term> (+|- ... <Term>)*
    Private Function GetExpression() As Double
         GetExpression = GetTerm
         While IsTermOperator(CurrentToken)
               BinaryOperation CurrentToken, GetExpression, GetTerm
         Wend
    End Function
    '<Simple Call> ::= (SIN|COS|SQR|EXP|LOG ...)
    Private Function Simple_Call(Value As Double, ByVal AFuncName As String) As Double
        Select Case AFuncName
            Case "SIN": Simple_Call = Sin(Value)
            Case "COS": Simple_Call = Cos(Value)
            Case "SQR": Simple_Call = Sqr(Value)
            Case "EXP": Simple_Call = Exp(Value)
            Case "LOG": Simple_Call = Log(Value)
            Case Else
                    SynErr
         End Select
    End Function
    '<Const>  ::= (PI|...)
    Private Function GetConstValue(Value As Double, ByVal AConstName As String) As Boolean
        GetConstValue = True
        Select Case AConstName
            Case "PI": Value = 3.14159265358979
            Case Else
                 GetConstValue = False
        End Select
    End Function
     
    Private Sub UnairyOperation(ByVal OP As TokenType, Value As Double)
        Select Case OP
            Case tkOpMinus: Value = -Value
            Case tkOpNot: Value = Not Value
            Case tkOpPlus: Value = Value
            Case Else
                    SynErr
         End Select
    End Sub
     
    Private Sub BinaryOperation(ByVal OP As TokenType, Value1 As Double, Value2 As Double)
        Select Case OP
            Case tkOpPlus:  Value1 = Value1 + Value2
            Case tkOpMinus:  Value1 = Value1 - Value2
            Case tkOpMul:  Value1 = Value1 * Value2
            Case tkOpDivF: Value1 = Value1 / Value2
            Case tkOpDiv:  Value1 = Value1 \ Value2
            Case tkOpMod:  Value1 = Value1 Mod Value2
            Case tkOpOr:   Value1 = Value1 Or Value2
            Case tkOpAnd:  Value1 = Value1 And Value2
            Case tkOpXor:  Value1 = Value1 Xor Value2
            Case tkOpPow:  Value1 = Value1 ^ Value2
            Case tkOpShl:  Value1 = Value1 * (2 * Value2)
            Case tkOpShr:  Value1 = Value1 \ (2 * Value2)
            Case Else
                    SynErr
         End Select
    End Sub
     
    Public Function EvalExp(ByVal Text As String) As Double
        fLexer.Text = Text
        EvalExp = GetExpression
        SynErr CurrentToken <> tkNone
    End Function
     
    Private Sub SynErr(Optional ByVal ARaise As Boolean = True)
         If ARaise Then
           Err.Description = "Erreur d'évaluation à la position " & CStr(fLexer.Position)
           Err.Raise 1001
         End If
    End Sub
    Pour tester
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Dim D As New CExpression
       MsgBox D.EvalExp("10+20/2")

Discussions similaires

  1. Code source d'une calculatrice
    Par DarkSmooth dans le forum Débuter
    Réponses: 6
    Dernier message: 25/01/2011, 18h47
  2. VB6 Connection à une base de données dont la source est une adresse url
    Par yangoal25 dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 13/11/2006, 18h47
  3. [VB6] Source D'une erreur
    Par krest dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 16/07/2003, 17h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo