Avez-vous une question? (216) 97 656 803 hajjriadh@gmail.com
Flutter

Correction exercices les fonctions en dart Série 01

Un cours complet pour maîtriser Correction exercices les fonctions en dart Série 01.

0 chapitres 0 QCM Accès gratuit
  • Exercice 01

    • Énoncé
    • Solution
      • Version While
      • import 'dart:io';
        void main() {
          bool palindrome(String txt) {
            int index = 0;
            while ((index < txt.length ~/ 2) &&
                (txt[index] == txt[txt.length - index - 1])) index++;
            return index == txt.length ~/ 2;
          }
        
          stdout.write("Veuillez saisir un mot: ");
          String mot = stdin.readLineSync()!;
        
          if (palindrome(mot)) {
            print("$mot est un plaindrôme.");
          } else {
            print("$mot n'est pas un plaindrôme.");
          }
        }
        
      • Version for
      • import 'dart:io';
        void main() {
          bool palindrome(String txt) {
            for (int index = 0; index < txt.length ~/ 2; index++) {
              if (txt[index] != txt[txt.length - index - 1]) return false;
            }
            return true;
          }
        
          stdout.write("Veuillez saisir un mot: ");
          String mot = stdin.readLineSync()!;
        
          if (palindrome(mot)) {
            print("$mot est un plaindrôme.");
          } else {
            print("$mot n'est pas un plaindrôme.");
          }
        }
        
  • Exercice 02

    • Énoncé
    • Solution
      import 'dart:io';
      import 'dart:math';
      
      void main() {
        List randoms(int n, int max, {int min = 1}) {
          List list = [];
          for (int i = 0; i < n; i++) {
            var x = Random().nextInt(max - min + 1) + min;
            list.add(x);
          }
          return list;
        }
      
        int n = 1, minim, max;
        do {
          stdout.write("Veuillez saisir une liste de n nombres aléatoires: ");
          n = int.parse(stdin.readLineSync()!);
        } while (n <= 0);
      
        do {
          stdout.write("Veuillez saisir nombre minimum: ");
          minim = int.parse(stdin.readLineSync()!);
        } while (minim <= 0);
        do {
          stdout.write("Veuillez saisir nombre maximum: ");
          max = int.parse(stdin.readLineSync()!);
        } while (max <= 0 || max <= minim);
        print(randoms(n, max, min: minim));
      }
      
  • Exercice 03

    • Énoncé
    • Solution
      import 'dart:io';
      
      void main() {
        String chaine = "";
        print("Veuillez saisir le nombre: ");
        chaine = stdin.readLineSync()!;
        print("--------------------------------------");
        print("Châine de départ");
        print("--------------------------------------");
        print(chaine);
        print("--------------------------------------\n");
        String convertirMaj(String txt) {
          var newTxt = " ";
          if (txt[0] != ' ') newTxt = txt[0].toUpperCase();
          for (int i = 1; i < txt.length; i++) {
            if ((txt[i - 1] == ' ') && (txt[i] != ' ')) {
              newTxt += txt[i].toUpperCase();
            } else {
              newTxt += txt[i];
            }
          }
          return newTxt;
        }
      
        String afficherLong(String ch) {
          var chList = ch.split(' ');
          int max = 0;
          for (var element in chList) {
            if (element.length > chList[max].length) max = chList.indexOf(element);
          }
          return chList[max];
        }
      
        String convertirVoyelle(String txt) {
          String newTxt = "";
          for (int i = 0; i < txt.length; i++) {
            if (['A', 'E', 'I', 'O', 'U'].contains(txt[i].toUpperCase())) {
              newTxt += txt[i].toUpperCase();
            } else {
              newTxt += txt[i];
            }
          }
          return newTxt;
        }
      
        print("--------------------------------------");
        print("Châine avec première lettre de chaque mot en majuscule");
        print("--------------------------------------");
        print(convertirMaj(chaine));
        print("--------------------------------------");
        print("Le mot le plus long dans cette chaîne");
        print("--------------------------------------");
        print(afficherLong(chaine));
        print("--------------------------------------");
        print("Mettre en majuscule toutes les voyelles ");
        print("--------------------------------------");
        print(convertirVoyelle(chaine));
      }
  • Exercice 04

    • Énoncé
    • Solution
      import 'dart:io';
      
      void main() {
        String chaine = "";
        print("Veuillez saisir le nombre: ");
        chaine = stdin.readLineSync()!;
        print("--------------------------------------");
        print("Châine de départ");
        print("--------------------------------------");
        print(chaine);
        print("--------------------------------------\n");
        String convertirMaj(String txt) {
          var newTxt = " ";
          if (txt[0] != ' ') newTxt = txt[0].toUpperCase();
          for (int i = 1; i < txt.length; i++) {
            if ((txt[i - 1] == ' ') && (txt[i] != ' ')) {
              newTxt += txt[i].toUpperCase();
            } else {
              newTxt += txt[i];
            }
          }
          return newTxt;
        }
      
        String afficherLong(String ch) {
          var chList = ch.split(' ');
          int max = 0;
          for (var element in chList) {
            if (element.length > chList[max].length) max = chList.indexOf(element);
          }
          return chList[max];
        }
      
        String convertirVoyelle(String txt) {
          String newTxt = "";
          for (int i = 0; i < txt.length; i++) {
            if (['A', 'E', 'I', 'O', 'U'].contains(txt[i].toUpperCase())) {
              newTxt += txt[i].toUpperCase();
            } else {
              newTxt += txt[i];
            }
          }
          return newTxt;
        }
      
        print("--------------------------------------");
        print("Châine avec première lettre de chaque mot en majuscule");
        print("--------------------------------------");
        print(convertirMaj(chaine));
        print("--------------------------------------");
        print("Le mot le plus long dans cette chaîne");
        print("--------------------------------------");
        print(afficherLong(chaine));
        print("--------------------------------------");
        print("Mettre en majuscule toutes les voyelles ");
        print("--------------------------------------");
        print(convertirVoyelle(chaine));
      }
      
  • Exercice 05

    • Énoncé
    • Solution
      import 'dart:io';
      
      void main() {
        void inverserPhrase(String phrase) {
          /* Divise la phrase en une liste de mots
        Inversez la liste, puis rejoignez les mots */
          String a = phrase.split(" ").reversed.toList().join(" ");
          print(a);
        }
      
        int compterMots(String phrase) {
          final List l = phrase.split(' ');
          return l.length;
        }
      
        int compterVoyelle(String txt) {
          int j = 0;
          for (int i = 0; i < txt.length; i++) {
            if (['A', 'E', 'I', 'O', 'U'].contains(txt[i].toUpperCase())) {
              j += 1;
            }
          }
          return j;
        }
        stdout.write("Veuillez donner une phrase: ");
        String phrase = stdin.readLineSync()!;
        print("--------------------------------------");
        print(phrase);
        print("--------------------------------------");
        inverserPhrase(phrase);
        print("--------------------------------------");
        print("La chaîne cintient: ${compterMots(phrase)} mots");
        print("--------------------------------------");
        print("La chaîne contient: ${compterVoyelle(phrase)} voyelles");
      }
  • Exercice 06

    • Énoncé
    • Solution
      void main() {
        const order = ['margherita', 'pepperoni', 'pineapple'];
        final total = calculateTotal(order);
        print('Total: \$$total');
      }
      
      double calculateTotal(List order) {
        const pizzaPrices = {
          'margherita': 5.5,
          'pepperoni': 7.5,
          'vegetarian': 6.5,
          'pineapple': 5.8,
        };
        var total = 0.0;
        for (var item in order) {
          final price = pizzaPrices[item];
          if (price != null) {
            total += price;
          }
        }
        return total;
      }
      
  • Exercice 07

    • Énoncé
    • Solution
      void main() {
        // Taux de change
        //double tndToEur = 0.3034;
        //double eurToTnd = 3.2987;
      
        // Conversion de TND en EUR
        double tndAmount = 2023;
        double eurAmount = tndToEurConversion(tndAmount);
        print('$tndAmount dinars tunisiens équivalent à $eurAmount euros.');
      
        // Tableau de conversion de TND en EUR
        List tndValues = List.generate(21, (index) => index * 50.0);
        List eurValues = tndValues.map((tnd) => tndToEurConversion(tnd)).toList();
        print('Conversion de TND en EUR pour les montants de 0 à 1000 TND avec un pas de 50 TND :');
        for (int i = 0; i < tndValues.length; i++) {
          print('${tndValues[i]} TND = ${eurValues[i]} EUR');
        }
      
        // Tableau de conversion d'EUR en TND
        List eurValuesReverse = List.generate(21, (index) => (index + 1) * 10.0);
        List tndValuesReverse = eurValuesReverse.map((eur) => eurToTndConversion(eur)).toList();
        print('Conversion d\'EUR en TND pour les montants de 10 à 200 EUR avec un pas de 10 EUR :');
        for (int i = 0; i < eurValuesReverse.length; i++) {
          print('${eurValuesReverse[i]} EUR = ${tndValuesReverse[i]} TND');
        }
      }
      
      double tndToEurConversion(double tndAmount) {
        return tndAmount * 0.3034;
      }
      
      double eurToTndConversion(double eurAmount) {
        return eurAmount * 3.2987;
      }