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

Cobol Discussion :

Virgule avec calcul et entrées


Sujet :

Cobol

  1. #1
    Membre expert
    Avatar de Metalman
    Homme Profil pro
    Enseignant-Chercheur
    Inscrit en
    Juin 2005
    Messages
    1 049
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Enseignant-Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 1 049
    Points : 3 532
    Points
    3 532
    Par défaut Virgule avec calcul et entrées
    Bonjour,

    M'ennuyant un peu et ayant ouvert un LDD... j'ai décidé de calculer mes intérêts en COBOL !... (bon oui j'ai d'abord fait la calculatrice Windows avant...)
    Et petit à petit, je me suis dit que je pouvais faire un truc assez générique si je m'y penche beaucoup.

    Pour le moment je fais très simple : 1 fichier qui contient le contenu du compte, le pourcentage d'intérêts, le nom de la personne, et la description de la ligne (type de compte)

    Voici donc le format d'un fichier en entrée :
    0012000 001.25 TEST LDD
    0000042 000000 LOLILOL Divers.
    Bon, avec 12000 à 1.25%, on doit obtenir à la fin 150 (facile à tester donc).

    Dans ma déclaration COBOL j'ai donc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    	FD  ACCT-DATA.
    	01  ACCT-RECORD.
    	    05	CURRENT-OWNED-IN	PICTURE 9(7).
    		05				PICTURE X(1).
    	    05	PERCENTAGE-IN		PICTURE 999V99.
    	    05				PICTURE X(1).
    	    05	NAME-IN				PICTURE X(20).
    		05				PICTURE X(1).
    		05	DESCRIPTION-IN		PICTURE X(20).
    Et en sortie ça affichera :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    	FD  TOTAL-DATA.
    	01  PRINT-REC.
    	    05	NAME-OUT			PICTURE X(20).
    		05				PICTURE X(10).
    	    05	TOTAL-OUT		PICTURE Z(7).9(2).
    [je viens de tilter que je n'ai pas mis de virgule dans le contenu du compte... on verra plus tard]

    Mon code est très TRES similaire à celui que j'avais fais il y a quelques années et le voici :

    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
    	IDENTIFICATION DIVISION.
    	PROGRAM-ID. INTERETS.
    
    
    	ENVIRONMENT DIVISION.
    	CONFIGURATION SECTION.
    	SOURCE-COMPUTER. PC-I686.
    	OBJECT-COMPUTER. PC-I686.
    
    	INPUT-OUTPUT SECTION.
    	FILE-CONTROL.
    		SELECT ACCT-DATA	ASSIGN TO DISK "ACCT.DAT"
    		       ORGANIZATION IS LINE SEQUENTIAL.
    		SELECT TOTAL-DATA	ASSIGN TO DISK "TOTAL.DAT"
    		       ORGANIZATION IS LINE SEQUENTIAL.
    		SELECT SORTED-DATA      ASSIGN TO DISK "SRT-TMP.DAT".
     *    *		SELECT SORTED-DATA     	ASSIGN TO SYSWORK.
    
    	DATA DIVISION.
    	FILE SECTION.
    	FD  ACCT-DATA.
    	01  ACCT-RECORD.
    	    05	CURRENT-OWNED-IN	PICTURE 9(7).
    		05				PICTURE X(1).
    	    05	PERCENTAGE-IN		PICTURE 999V99.
    	    05				PICTURE X(1).
    	    05	NAME-IN				PICTURE X(20).
    		05				PICTURE X(1).
    		05	DESCRIPTION-IN		PICTURE X(20).
    	SD  SORTED-DATA.
    	01  SORTED-RECORD.
    	    05	CURRENT-OWNED-ST	PICTURE 9(7).
    		05				PICTURE X(1).
    	    05	PERCENTAGE-ST		PICTURE 999V99.
    	    05				PICTURE X(1).
    	    05	NAME-ST				PICTURE X(20).
    		05				PICTURE X(1).
    		05	DESCRIPTION-ST		PICTURE X(20).
    	FD  TOTAL-DATA.
    	01  PRINT-REC.
    	    05	NAME-OUT			PICTURE X(20).
    		05				PICTURE X(10).
    	    05	TOTAL-OUT		PICTURE Z(7).9(2).
    
    	WORKING-STORAGE SECTION.
    	01  ARE-THERE-MORE-RECORDS	PICTURE XXX VALUE 'YES'.
    	77  CURRENT-NAME			PICTURE X(20).
    	77  CURRENT-TOTAL			PICTURE 9(7)V99.
    	77  CURRENT-SUM				PICTURE 9(7)V99.
    	77  CURRENT-ENTRIES			PICTURE 999.
    	01  IS-FIRST-ENTRY			PICTURE XXX VALUE 'YES'.
    	    88 NOT-FIRST-ENTRY					VALUE 'NO '.
    
    
    	PROCEDURE DIVISION.
    	100-MAIN-MODULE.
    	    SORT SORTED-DATA ON ASCENDING KEY NAME-ST OF SORTED-RECORD
    	    	 USING ACCT-DATA
    		 OUTPUT PROCEDURE 200-MAIN-LOOP
    	    STOP RUN.
    
    	200-MAIN-LOOP.
    	    MOVE 'YES' TO IS-FIRST-ENTRY
    	    OPEN OUTPUT TOTAL-DATA
    	    PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO'
          	      RETURN SORTED-DATA
    		     AT END
    			  MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
    			  PERFORM 400-WRITE-SUM-TO-FILE
    			  PERFORM 500-WRITE-LOG
    		     NOT AT END
    		      PERFORM 300-COUNT-ROUTINE
    		END-RETURN
    	    END-PERFORM
    	    CLOSE TOTAL-DATA.
    
    	300-COUNT-ROUTINE.
    	 DISPLAY "Current money : " CURRENT-OWNED-ST
         DISPLAY "Percentage : " PERCENTAGE-ST
    	 DISPLAY "Name temp : " NAME-ST
    	    IF	IS-FIRST-ENTRY = 'YES'
     *    *         Premiere iteration, on initialize tout
    	    	MOVE NAME-ST TO CURRENT-NAME
    			MOVE 0 TO CURRENT-TOTAL
    			MOVE 1 TO CURRENT-ENTRIES
    			MOVE 'NO ' TO IS-FIRST-ENTRY
    	    ELSE
    		IF  NAME-ST = CURRENT-NAME
     *    *             2e iteration ou plus dans un meme bloc de nom
     *    *		    On ajoute la depense associee
    			MULTIPLY CURRENT-OWNED-ST BY PERCENTAGE-ST GIVING CURRENT-SUM
    		    ADD CURRENT-SUM TO CURRENT-TOTAL
    		    ADD 1 TO CURRENT-ENTRIES
    		ELSE
     *    *             On change de nom, donc on ecrit
    		    PERFORM 400-WRITE-SUM-TO-FILE
     *    *		    On reinitialize avec le nouveau nom
    			MOVE NAME-ST TO CURRENT-NAME
    		    MOVE 0 TO CURRENT-TOTAL
    		    MOVE 1 TO CURRENT-ENTRIES
    		END-IF
    	    END-IF.
    
    	400-WRITE-SUM-TO-FILE.
    	    MOVE SPACES TO PRINT-REC
    	    MOVE CURRENT-NAME TO NAME-OUT
    	    MOVE CURRENT-TOTAL TO TOTAL-OUT
    	    WRITE PRINT-REC.
    
    	500-WRITE-LOG.
    	    MOVE "-------------------------------------" TO PRINT-REC
    	    WRITE PRINT-REC.
    
    	END PROGRAM INTERETS.
    Lors des DISPLAY de debug, je peux voir ceci :
    Current money : 0012000
    Percentage : 001..2
    Name temp : TEST
    Current money : 0000042
    Percentage : 000.00
    Name temp : LOLILOL
    Pourquoi y a-t-il un 001..2 au lieu d'un 001.25 ?
    Le nom est décalé d'une espace du coup... (car le 5 passe en X(1) )

    Merci de m'éclairer !
    Je pense que ça vient de ma déclaration 999V99, mais je n'ai pas encore bien compris comment gérer les V , et .
    --
    Metalman !

    Attendez 5 mins après mes posts... les EDIT vont vite avec moi...
    Les flags de la vie : gcc -W -Wall -Werror -ansi -pedantic mes_sources.c
    gcc -Wall -Wextra -Werror -std=c99 -pedantic mes_sources.c
    (ANSI retire quelques fonctions comme strdup...)
    L'outil de la vie : valgrind --show-reachable=yes --leak-check=full ./mon_programme
    Et s'assurer que la logique est bonne "aussi" !

    Ma page Developpez.net

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Ingénieur d'Etude Mainframe/AS400
    Inscrit en
    Novembre 2012
    Messages
    1 767
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'Etude Mainframe/AS400
    Secteur : Finance

    Informations forums :
    Inscription : Novembre 2012
    Messages : 1 767
    Points : 10 764
    Points
    10 764
    Par défaut
    Pour ce qui est des formats numériques avec virgule :

    Le "V" te permet de définir une variable de type numérique avec virgule (tu pourras faire des calculs avec).
    Le "," par contre c'est de l'édition et du coup la variable "groupe" devient de l'alphanumérique.
    Donc après calcul tu fais move de ta variable en V, vers ta variable ',' qui est éditable/visualisable.

  3. #3
    Membre expert
    Avatar de Metalman
    Homme Profil pro
    Enseignant-Chercheur
    Inscrit en
    Juin 2005
    Messages
    1 049
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Enseignant-Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 1 049
    Points : 3 532
    Points
    3 532
    Par défaut
    C'était ça !
    Pour résumer :

    Quand on a en entrée :
    0012000 001.25 TEST LDD
    0000100 002.00 TEST LOL
    0000042 001.00 LOLILOL Divers.
    La colonne 001.25 doit être récupérée via un :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    05	PERCENTAGE-IN		PICTURE 999,99.
    Puis transformée par un (PERCENTAGE-IN est trié et devient PERCENTAGE-ST "automatiquement") :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    77	CURRENT-PERCENTAGE		PICTURE 999V99.
    MOVE PERCENTAGE-ST TO CURRENT-PERCENTAGE
    Mon programme n'avait pas le comportement attendu (normal pour le début...)

    --------

    Et si j'entre :
    0012000 001.25 TEST LDD
    0000100 002.00 TEST LOL
    0000042 001000 LOLILOL Divers.
    Le 001000 devient un 1000, donc 000.00 !
    A faire attention !

    --------

    Ce qui donne finalement comme programme :
    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
    	IDENTIFICATION DIVISION.
    	PROGRAM-ID. INTERETS.
    
    
    	ENVIRONMENT DIVISION.
    	CONFIGURATION SECTION.
    	SOURCE-COMPUTER. PC-I686.
    	OBJECT-COMPUTER. PC-I686.
    
    	INPUT-OUTPUT SECTION.
    	FILE-CONTROL.
    		SELECT ACCT-DATA	ASSIGN TO DISK "ACCT.DAT"
    		       ORGANIZATION IS LINE SEQUENTIAL.
    		SELECT TOTAL-DATA	ASSIGN TO DISK "TOTAL.DAT"
    		       ORGANIZATION IS LINE SEQUENTIAL.
    		SELECT SORTED-DATA      ASSIGN TO DISK "SRT-TMP.DAT".
     *    *		SELECT SORTED-DATA     	ASSIGN TO SYSWORK.
    
    	DATA DIVISION.
    	FILE SECTION.
    	FD  ACCT-DATA.
    	01  ACCT-RECORD.
    	    05	CURRENT-OWNED-IN	PICTURE 9(7).
    		05				PICTURE X(1).
    	    05	PERCENTAGE-IN		PICTURE 999,99.
    	    05				PICTURE X(1).
    	    05	NAME-IN				PICTURE X(20).
    		05				PICTURE X(1).
    		05	DESCRIPTION-IN		PICTURE X(20).
    	SD  SORTED-DATA.
    	01  SORTED-RECORD.
    	    05	CURRENT-OWNED-ST	PICTURE 9(7).
    		05				PICTURE X(1).
    	    05	PERCENTAGE-ST		PICTURE 999,99.
    	    05				PICTURE X(1).
    	    05	NAME-ST				PICTURE X(20).
    		05				PICTURE X(1).
    		05	DESCRIPTION-ST		PICTURE X(20).
    	FD  TOTAL-DATA.
    	01  PRINT-REC.
    	    05	NAME-OUT			PICTURE X(20).
    		05				PICTURE X(10).
    	    05	TOTAL-OUT		PICTURE Z(7).9(2).
    
    	WORKING-STORAGE SECTION.
    	01  ARE-THERE-MORE-RECORDS	PICTURE XXX VALUE 'YES'.
    	77  CURRENT-NAME			PICTURE X(20).
    	77	CURRENT-PERCENTAGE		PICTURE 999V9999.
    	77  CURRENT-TOTAL			PICTURE 9(7)V99.
    	77  CURRENT-SUM				PICTURE 9(7)V99.
    	77  CURRENT-ENTRIES			PICTURE 999.
    	01  IS-FIRST-ENTRY			PICTURE XXX VALUE 'YES'.
    	    88 NOT-FIRST-ENTRY					VALUE 'NO '.
    
    
    	PROCEDURE DIVISION.
    	100-MAIN-MODULE.
    	    SORT SORTED-DATA ON ASCENDING KEY NAME-ST OF SORTED-RECORD
    	    	 USING ACCT-DATA
    		 OUTPUT PROCEDURE 200-MAIN-LOOP
    	    STOP RUN.
    
    	200-MAIN-LOOP.
    	    MOVE 'YES' TO IS-FIRST-ENTRY
    	    OPEN OUTPUT TOTAL-DATA
    	    PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO'
          	      RETURN SORTED-DATA
    		     AT END
    			  MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
    			  PERFORM 400-WRITE-SUM-TO-FILE
    			  PERFORM 500-WRITE-LOG
    		     NOT AT END
     *    *		  DISPLAY "Name temp : " NAME-ST
     *    *		  DISPLAY "Current money : " CURRENT-OWNED-ST
     *    *		  DISPLAY "Percentage ST : " PERCENTAGE-ST
    		      PERFORM 300-COUNT-ROUTINE
     *    *		  DISPLAY "Percentage NM : " CURRENT-PERCENTAGE
    		END-RETURN
    	    END-PERFORM
    	    CLOSE TOTAL-DATA.
    
    	300-COUNT-ROUTINE.
     *    *      Les 999,99 doivent etre convertis en 999V99 pour calculs
     *    *      Calcul du pourcentage exact
    		MOVE PERCENTAGE-ST TO CURRENT-PERCENTAGE
    		DIVIDE CURRENT-PERCENTAGE BY 100 GIVING CURRENT-PERCENTAGE
    	    IF	IS-FIRST-ENTRY = 'YES'
     *    *         Premiere iteration, on initialize tout
    	    	MOVE NAME-ST TO CURRENT-NAME
    			MOVE 0 TO CURRENT-TOTAL
    			MULTIPLY CURRENT-OWNED-ST BY CURRENT-PERCENTAGE GIVING CURRENT-SUM
    			ADD CURRENT-OWNED-ST CURRENT-SUM TO CURRENT-TOTAL
    			MOVE 1 TO CURRENT-ENTRIES
    			MOVE 'NO ' TO IS-FIRST-ENTRY
    		ELSE
    		IF  NAME-ST = CURRENT-NAME
     *    *             2e iteration ou plus dans un meme bloc de nom
     *    *		    On ajoute la depense associee
    			MULTIPLY CURRENT-OWNED-ST BY CURRENT-PERCENTAGE GIVING CURRENT-SUM
    			ADD CURRENT-OWNED-ST CURRENT-SUM TO CURRENT-TOTAL
    		    ADD 1 TO CURRENT-ENTRIES
    		ELSE
     *    *             On change de nom, donc on ecrit
    		    PERFORM 400-WRITE-SUM-TO-FILE
     *    *		    On reinitialize avec le nouveau nom
    			MOVE NAME-ST TO CURRENT-NAME
    			MOVE 0 TO CURRENT-TOTAL
    		   	MULTIPLY CURRENT-OWNED-ST BY CURRENT-PERCENTAGE GIVING CURRENT-SUM
    			ADD CURRENT-OWNED-ST CURRENT-SUM TO CURRENT-TOTAL
    		    MOVE 1 TO CURRENT-ENTRIES
    		END-IF
    	    END-IF.
    
    	400-WRITE-SUM-TO-FILE.
    	    MOVE SPACES TO PRINT-REC
    	    MOVE CURRENT-NAME TO NAME-OUT
    	    MOVE CURRENT-TOTAL TO TOTAL-OUT
    	    WRITE PRINT-REC.
    
    	500-WRITE-LOG.
    	    MOVE "-------------------------------------" TO PRINT-REC
    	    WRITE PRINT-REC.
    
    	END PROGRAM INTERETS.
    EDIT2 : attention... ce code ne calcule absolument PAS le pourcentage... il faut surtout n'importe quoi comme multiplication !
    EDIT3 : C'est bon le code fonctionne !
    --
    Metalman !

    Attendez 5 mins après mes posts... les EDIT vont vite avec moi...
    Les flags de la vie : gcc -W -Wall -Werror -ansi -pedantic mes_sources.c
    gcc -Wall -Wextra -Werror -std=c99 -pedantic mes_sources.c
    (ANSI retire quelques fonctions comme strdup...)
    L'outil de la vie : valgrind --show-reachable=yes --leak-check=full ./mon_programme
    Et s'assurer que la logique est bonne "aussi" !

    Ma page Developpez.net

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Calcul de jours ouvrés avec jours fériés entre deux dates
    Par grimgrim dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 13/03/2015, 21h12
  2. calcul délais entre deux dates avec plages horaires
    Par leila eco dans le forum SAP Crystal Reports
    Réponses: 3
    Dernier message: 03/01/2007, 16h51
  3. [RegEx] Preg_replace avec une requête entre les remplacements
    Par sox83 dans le forum Langage
    Réponses: 6
    Dernier message: 16/12/2005, 18h12
  4. Problème avec listes liées entre elles et bouton "précé
    Par Oluha dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 02/08/2005, 15h10
  5. Vue avec calcule de date
    Par jf-nigou dans le forum Langage SQL
    Réponses: 4
    Dernier message: 01/06/2005, 14h48

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