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

Pascal Discussion :

Opérations sur les fractions


Sujet :

Pascal

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre régulier
    Femme Profil pro
    Inscrit en
    Mai 2012
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations forums :
    Inscription : Mai 2012
    Messages : 8
    Par défaut Opérations sur les fractions
    Salut.
    Aidez-moi svp, j'ai un exercice dont je n'ai pas compris la question !!
    exercice:
    Soit le type fraction défini par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    type fraction=record
                        num:integer
                        den:integer
                        end;
    Écrire des sous programme chargés des opérations de:
    - addition, soustraction, multiplication et division de deux fractions.
    Alors c'est un exercice donné aux mathématiciens ; le problème est que je ne comprends pas le sens de la question, c.à.d pour l’addition, par exemple, est-ce que on met le résultat d’addition des deux fractions dans une variable de type fraction ou dans une variable réelle ? La même chose pour multiplication, etc. Merci d'avance.

  2. #2
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 166
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 166
    Billets dans le blog
    9
    Par défaut
    Bonjour !

    Il me semble que le résultat doit être une fraction. De toute façon, une fois qu'on a une fraction, on peut toujours obtenir le nombre décimal correspondant.

    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
    program fraction_01;
     
    type fraction = record
                      num: integer;
                      den: integer;
                    end;
     
    var
      f: fraction;
      r: real;
     
    begin
      f.num := 3;
      f.den := 2;
      r := f.num / f.den;
     
      WriteLn('f = ', f.num, ' / ', f.den);
      WriteLn('r = ', r);
     
      ReadLn;
    end.

  3. #3
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    à mon avis il faut rester dans des fractions entières 1/2 + 1/4 = 3/4
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  4. #4
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 166
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 166
    Billets dans le blog
    9
    Par défaut
    Citation Envoyé par Paul TOTH Voir le message
    à mon avis il faut rester dans des fractions entières 1/2 + 1/4 = 3/4
    On pourrait même imaginer un affichage sur mesure, quelque chose comme ça :

    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
    program writefrac1;
     
    uses
      SysUtils;
     
    type
      tOp = (p, m, f, d);
     
    { plus, moins, fois, divisé par }
     
    const
      signe: array[p..d] of char = ('+', '-', '*', '/');
     
    procedure WriteFracOp(a, b, c, d, e, f: integer; op: tOp);
     
    { a/b + c/d = e/f }
    { a/b - c/d = e/f }
    { a/b * c/d = e/f }
    { a/b / c/d = e/f }
     
    begin
      WriteLn(' ', IntToStr( a ):3, '     ', IntToStr( c ):3, '     ', IntToStr( e ):3);
      WriteLn('----- ' + signe[op] + ' ----- ' + '=' + ' -----');
      WriteLn(' ', IntToStr( b ):3, '     ', IntToStr( d ):3, '     ', IntToStr( f ):3);
    end;
     
    begin
      WriteFracOp(1,3, 1,4, 7,12, p);
      ReadLn;
    end.

  5. #5
    Membre Expert
    Avatar de Dr.Who
    Inscrit en
    Septembre 2009
    Messages
    980
    Détails du profil
    Informations personnelles :
    Âge : 46

    Informations forums :
    Inscription : Septembre 2009
    Messages : 980
    Par défaut
    Allé, parce que plus on est de fou plus on ris et que le sujet m'a interpelé, parce que ça fait longtemps que j'avais pas toucher aux fractions et que je voulais m'entrainer encore sur la surcharge d'opé :

    j'ai juste laissé la routine pour les divisions de fraction vide (à faire) en mode comment embrouiller quelqu'un avec un code trop complexe pour lui qui ne risque pas de compiler sur autre chose que Delphi

    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
     
    unit UFraction;
     
    interface
     
    {$IF RTLVersion >= 18.0}
      {$DEFINE RECOP}
    {$IFEND}
    type
      Float = {$IFDEF RECOP}extended{$ELSE}double{$ENDIF};
     
      TFraction = record
        Numerateur, Denominateur : integer;
      {$IFDEF RECOP}
        public
          class function Create(aNumerateur, aDenominateur: integer): TFraction; static;
          class function Simplify(A:TFraction): TFraction; static;
     
          { N/D = réel }  class operator Explicit(A: TFraction): Float;   // Float := Fraction
          { N/D = réel }  class operator Implicit(A: TFraction): Float;   // Float = Float(Fraction)
     
          { a = a/1 }     class operator Explicit(A: Integer): TFraction; // Fraction := integer [+-*/div] Fraction
          { a = a/1 }     class operator Implicit(A: Integer): TFraction; // Fraction = TFraction(integer)
     
          { a/b + c/d }   class operator Add(A,B: TFraction): TFraction;
          { a/b - c/d }   class operator Subtract(A,B: TFraction): TFraction;
          { a/b * c/d }   class operator Multiply(A,B: TFraction): TFraction;
          { a/b / c/d }   class operator Divide(A,B: TFraction): TFraction;
          { a/b div c/d } class operator IntDivide(A,B: TFraction): TFraction;
     
          { simplfy by round } class operator Round(A: TFraction): TFraction;
      {$ENDIF}
      end;
     
    {$IFNDEF RECOP}
    function FractionCreate(aNumerateur, aDenominateur: integer): TFraction;
    function FractionSimplify(A: TFraction): TFraction;
    function FractionToFloat(A: TFraction): Float;
    function IntToFraction(A: integer): TFraction;
    function FractionAdd(A,B: TFraction): TFraction;
    function FractionSubtract(A,B: TFraction): TFraction;
    function FractionMultiply(A,B: TFraction): TFraction;
    function FractionDivide(A,B: TFraction): TFraction;
    {$ENDIF}
     
    implementation
     
    { TFraction }
     
    {$IFDEF RECOP}class operator TFraction.{$ELSE}function Fraction{$ENDIF}Add(A, B: TFraction): TFraction;
    begin
      // 1/2 + 3/4  = ((1*4)+(3*2))/4*2 = (4+6)/8 = 10/8 = 1.25
      // 0.5 + 0.75 = 1.25
      result.Numerateur   := (A.Numerateur*B.Denominateur)+(B.Numerateur*A.Denominateur);
      result.Denominateur := (A.Denominateur*B.Denominateur)
    end;
     
    {$IFDEF RECOP}class function TFraction.{$ELSE}function Fraction{$ENDIF}Create(aNumerateur, aDenominateur: integer): TFraction;
    begin
      result.Numerateur   := aNumerateur;
      result.Denominateur := aDenominateur;
    end;
     
    {$IFDEF RECOP}class function TFraction.{$ELSE}function Fraction{$ENDIF}Simplify(A:TFraction): TFraction;
    var M, DC: integer;
     
    begin
      M := A.Denominateur;
      if A.Numerateur < A.Denominateur then
        M := A.Numerateur;
     
      result := A;
     
      DC := M;
     
      while DC > 0 do
      begin
        if ((result.Numerateur mod DC) = 0) and ((result.Denominateur mod DC) = 0) then
        begin
          result.Numerateur   := result.Numerateur   div DC;
          result.Denominateur := result.Denominateur div DC;
          break;
        end;
        dec(DC);
      end;
    end;
     
    {$IFDEF RECOP}class operator TFraction.{$ELSE}function Fraction{$ENDIF}Divide(A, B: TFraction): TFraction;
    begin
      // TODO  héhéhé
    end;
     
    {$IFDEF RECOP}class operator TFraction.explicit{$ELSE}function FractionToFloat{$ENDIF}(A: TFraction): Float;
    begin
      result := A.Numerateur / A.Denominateur;
    end;
    {$IFDEF RECOP}class operator TFraction.implicit(A: TFraction): Float;
    begin
      result := A.Numerateur / A.Denominateur;
    end;
    {$ENDIF}
     
    {$IFDEF RECOP}class operator TFraction.explicit{$ELSE}function IntToFraction{$ENDIF}(A: integer): TFraction;
    begin
      result.Numerateur   := A;
      result.Denominateur := 1;
    end;
    {$IFDEF RECOP}class operator TFraction.implicit(A: integer): TFraction;
    begin
      result.Numerateur   := A;
      result.Denominateur := 1;
    end;
    {$ENDIF}
     
    {$IFDEF RECOP}
    class operator TFraction.IntDivide(A, B: TFraction): TFraction;
    begin
      result := A / B; // call TFraction.Divide
    end;
    {$ENDIF}
     
    {$IFDEF RECOP}class operator TFraction.{$ELSE}function Fraction{$ENDIF}Multiply(A, B: TFraction): TFraction;
    begin
      // 1/2 * 2/4 = (1*2) / (2*4) = 2/8 {1/4} = 0.5 * 0.5 = 0.25 {1/4}
      result.Numerateur   := A.Numerateur   * B.Numerateur;
      result.Denominateur := A.Denominateur * B.Denominateur;
    end;
     
    {$IFDEF RECOP}class operator TFraction.{$ELSE}function Fraction{$ENDIF}Subtract(A, B: TFraction): TFraction;
    begin
      // 1/2 - 3/4  = ((1*4)-(3*2))/4*2 = (4-6)/8 = -2/8 = -0.25
      // 0.5 - 0.75 = -0.25
      result.Numerateur   := abs((A.Numerateur*B.Denominateur)-(B.Numerateur*A.Denominateur));
      result.Denominateur := (A.Denominateur*B.Denominateur)
    end;
     
    {$IFDEF RECOP}class operator TFraction.Round(A: TFraction): TFraction;
    begin
      result := TFraction.Simplify(A);
    end;
    {$ENDIF}
     
    end.
    contient :
    Création d'un TFraction,
    Calcul réel de la fraction,
    Addition, Soustraction, Multiplication de fraction,
    simplification de fraction (4/16=1/4, 20/1000=1/50),
    conversion d'un entier en fraction (I/1).

    calculs réalisable (avec ou sans simplification) :

    R = A/B + C/D
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := AB.Create(A,B) + CD.Create(C,D)
    R = A/B - C/D
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := AB.Create(A,B) - CD.Create(C,D)
    R = A/B * C/D
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := AB.Create(A,B) * CD.Create(C,D)
    R = a + B/C
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := TFraction(a) + BC.Create(B,C)
    R = a - B/C
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := TFraction(a) - BC.Create(B,C)
    R = a * B/C
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := TFraction(a) * BC.Create(B,C)
    R = a * B/C - d * E/F + G/H * i
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    R := round(TFraction(a) * BC.Create(B,C) - TFraction(d) * EF.Create(E,F) + GH.Create(G,H) * TFraction(i))
    Exemple type sur Delphi2009 :

    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
     
    procedure TForm13.Button1Click(Sender: TObject);
    var A, B, R, S: TFraction;
    begin
      A := TFraction.Create(3,9);
      B := TFraction.Create(4,11);
     
      R := A + B;
      S := round(R);
      ListBox1.Items.Add(
        format(
          '%d/%d + %d/%d = %d/%d = %d/%d = {%.3f ° %.3f = %.3f)',
          [ A.Numerateur, A.Denominateur,
            B.Numerateur, B.Denominateur,
            R.Numerateur, R.Denominateur,
            S.Numerateur, S.Denominateur,
            Float(A), Float(B), Float(R)]
        )
      );
     
      R := A - B;
      S := round(R);
      ListBox1.Items.Add(
        format(
          '%d/%d - %d/%d = %d/%d = %d/%d = {%.3f ° %.3f = %.3f)',
          [ A.Numerateur, A.Denominateur,
            B.Numerateur, B.Denominateur,
            R.Numerateur, R.Denominateur,
            S.Numerateur, S.Denominateur,
            Float(A), Float(B), Float(R)]
        )
      );
     
      R := A * B;
      S := round(R);
      ListBox1.Items.Add(
        format(
          '%d/%d * %d/%d = %d/%d = %d/%d = {%.3f ° %.3f = %.3f)',
          [ A.Numerateur, A.Denominateur,
            B.Numerateur, B.Denominateur,
            R.Numerateur, R.Denominateur,
            S.Numerateur, S.Denominateur,
            Float(A), Float(B), Float(R)]
        )
      );
    end;

    Exemple avec RECOP non définit :

    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
     
    procedure TForm13.Button1Click(Sender: TObject);
    var A, B, R, S: TFraction;
    begin
      A := FractionCreate(3,9);
      B := FractionCreate(4,11);
     
      R := FractionAdd(A,B);
      S := FractionSimplify(R);
      ListBox1.Items.Add(
        format(
          '%d/%d + %d/%d = %d/%d = %d/%d = {%.3f ° %.3f = %.3f)',
          [ A.Numerateur, A.Denominateur,
            B.Numerateur, B.Denominateur,
            R.Numerateur, R.Denominateur,
            S.Numerateur, S.Denominateur,
            FractionToFloat(A), FractionToFloat(B), FractionToFloat(R)]
        )
      );
     
      R := FractionSubtract(A,B);
      S := FractionSimplify(R);
      ListBox1.Items.Add(
        format(
          '%d/%d - %d/%d = %d/%d = %d/%d = {%.3f ° %.3f = %.3f)',
          [ A.Numerateur, A.Denominateur,
            B.Numerateur, B.Denominateur,
            R.Numerateur, R.Denominateur,
            S.Numerateur, S.Denominateur,
            FractionToFloat(A), FractionToFloat(B), FractionToFloat(R)]
        )
      );
     
      R := FractionMultiply(A,B);
      S := FractionSimplify(R);
      ListBox1.Items.Add(
        format(
          '%d/%d * %d/%d = %d/%d = %d/%d = {%.3f ° %.3f = %.3f)',
          [ A.Numerateur, A.Denominateur,
            B.Numerateur, B.Denominateur,
            R.Numerateur, R.Denominateur,
            S.Numerateur, S.Denominateur,
            FractionToFloat(A), FractionToFloat(B), FractionToFloat(R)]
        )
      );
    end;
    [ Sources et programmes de Dr.Who | FAQ Delphi | FAQ Pascal | Règlement | Contactez l'équipe ]
    Ma messagerie n'est pas la succursale du forum... merci!

  6. #6
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    jolie gymnastique
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

Discussions similaires

  1. [TPW] Opérations simples sur les fractions
    Par forum dans le forum Codes sources à télécharger
    Réponses: 0
    Dernier message: 16/11/2011, 20h43
  2. Application d'opération sur les fractions : erreurs de compilation
    Par int828 dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 10/10/2009, 14h09
  3. Opérations sur les matrices...
    Par aokiseiichiro dans le forum C
    Réponses: 32
    Dernier message: 28/07/2005, 17h10
  4. opérations sur les bits d'un byte
    Par petitours dans le forum C++Builder
    Réponses: 4
    Dernier message: 10/02/2004, 20h42
  5. opérations sur les dates
    Par coucoucmoi dans le forum Débuter
    Réponses: 2
    Dernier message: 12/08/2003, 11h45

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