Affichage trivial de statistique basic

From Tuxunix
Jump to: navigation, search

Exemple d'execution


$ ./basic_stats < /dev/null
$ cat demo_data
luc             14
stephanie       15.3
marc            11
romain          7.5
lucie           18
jerome          9
thomas          18
$ ./basic_stats < demo_data
Best: lucie (18)
Worst: romain (7.5)
Average: 13.2571
$
   1./*
   2. * basic_stats.cpp for (self)
   3. *
   4. * Made by pterrier
   5. * Login <pterrier@insia.org>
   6. *
   7. * Started on mer 25 oct 2006 09:46:22 pterrier
   8. * Last update jeu 26 oct 2006 19:09:10 pterrier
   9.*/
  10.       
  11.#include <iostream>
  12.#include <string>
  13.      
  14.int main (void){
  15.    std::string name = "" ;
  16.    double note = 0 ;
  17.    std::string bestName = "";
  18.    std::string worstName = "" ;
  19.    double noteMin = 0 ;
  20.    double noteMax = 0 ;
  21.    int nbNotes = 0 ;
  22.    double moyenne = 0 ;
  23.       
  24.    if(std::cin >> name >> note){
  25.        bestName = name ;
  26.        worstName = name ;
  27.        noteMin = note;
  28.        noteMax = note;
  29.        moyenne += note;
  30.        ++nbNotes;
  31.    }
  32.    while(std::cin >> name >> note){
  33.        if(note > noteMax){
  34.        noteMax = note;
  35.        bestName = name;
  36.        }else{
  38.            if( note < noteMin ){
  39.                noteMin = note;
  40.                worstName = name;
  41.            }
  42.        }
  43.        moyenne += note ;
  44.        ++nbNotes ;
  45.    }
  46.    if(0 < nbNotes){
  47.        std::cout << "Best: "<< bestName &lt;&lt; " ("
  48.        << noteMax << ")" << std::endl ;
  49.        std::cout << "Worst: "<< worstName &lt;&lt; " ("
  50.        << noteMin << ")" << std::endl ;
  51.        std::cout << "Average: "<< moyenne / nbNotes
  52.        << std::endl ;
  53.    }
  54.    return 0 ;
  55.}