Fichier en c++
Bonjour,
j'ai un fichier qui présente les lignes suivantes :
code note 1 note 2 note 3 Moyenne
101 11 12 15.5 12.8333
102 11 10 8 9.66667
103 14 17 11.5 14.1667
104 10 14 12.5 12.1667
105 14 17 19 16.6667
106 0 15 8 7.66667
107 14 15 16 15
108 10 10 11.5 10.5
109 11 15.5 12 12.8333
110 11 12 12.5 11.8333
111 14 15 18 15.6667
112 10 11 11.5 10.8333
comment a partir du code, je peux avoir la moyenne? merci
j'ai un fichier qui présente les lignes suivantes :
code note 1 note 2 note 3 Moyenne
101 11 12 15.5 12.8333
102 11 10 8 9.66667
103 14 17 11.5 14.1667
104 10 14 12.5 12.1667
105 14 17 19 16.6667
106 0 15 8 7.66667
107 14 15 16 15
108 10 10 11.5 10.5
109 11 15.5 12 12.8333
110 11 12 12.5 11.8333
111 14 15 18 15.6667
112 10 11 11.5 10.8333
comment a partir du code, je peux avoir la moyenne? merci
A voir également:
- Fichier en c++
- Fichier bin - Guide
- Fichier epub - Guide
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier .dat - Guide
2 réponses
sl é ce tu pouré etre un peu + explicite (lecture de donnés, rangement des donnés,obtenir une formule mathématique,...)
++
++
Ben il faut commencer par lire le fichier ligne par ligne (par exemple avec getline) et scanner chaque ligne lue (par exemple avec sscanf). Ensuite tu calcules ce que tu veux...
ce qui donne :
#include <iostream> #include <ostream> #include <fstream> #include <string> #include <map> struct notes_t{ double note1,note2,note3,moyenne,coeff,coeff_moy; notes_t(){} notes_t( const double & note1_, const double & note2_, const double & note3_, const double & moyenne_, const double & coeff_, const double & coeff_moy_ ): note1(note1_),note2(note2_), note3(note3_),moyenne(moyenne_), coeff(coeff_),coeff_moy(coeff_moy_) {} }; inline std::ostream & operator << (std::ostream & out,const notes_t & notes){ out << "[note1 = " << notes.note1 << " note2 = " << notes.note2 << " note2 = " << notes.note2 << " moyenne = " << notes.moyenne << " coeff = " << notes.coeff << " coeff_moy = " << notes.coeff_moy << ']'; return out; } typedef unsigned code_t; typedef std::map<code_t,notes_t> datas_t; int main(){ // Ouverture du fichier const char *filename = "pouet.txt"; std::ifstream f(filename); if(!f){ std::cerr << "can't open [" << filename << ']' << std::endl; return 1; } // Lecture du fichier datas_t datas; { std::string line; for(unsigned no_line=0;std::getline(f,line);++no_line){ notes_t notes; code_t code; if (line.empty() || line[0] == '#') continue; if(sscanf(line.c_str()," %i %lf %lf %lf %lf %lf %lf", &code,¬es.note1,¬es.note2,¬es.note3, ¬es.moyenne,¬es.coeff,¬es.coeff_moy) == 7 ){ std::cout << notes << std::endl; datas[code] = notes; }else{ std::cerr << "Ligne " << no_line << " invalide [" << line << ']' << std::endl; } } } // Parcours de la structure data; { double moy1 = 0, moy2 = 0, moy3 = 0; std::size_t nb_notes = datas.size(); datas_t::const_iterator datas_it (datas.begin()), datas_end(datas.end()); for(;datas_it!=datas_end;++datas_it){ const code_t & code = datas_it->first; const notes_t & notes = datas_it->second; std::cout << "code = " << code << '\t' << notes << std::endl; moy1 += notes.note1; moy2 += notes.note2; moy3 += notes.note3; } moy1 /= nb_notes; moy2 /= nb_notes; moy3 /= nb_notes; std::cout << "moy1 = " << moy1 << " moy2 = " << moy2 << " moy3 = " << moy3 << std::endl; } // Fermeture du fichier f.close(); return 0; }
ce qui donne :
[note1 = 11 note2 = 12 note2 = 12 moyenne = 12.8333 coeff = 2 coeff_moy = 25.6667] [note1 = 11 note2 = 10 note2 = 10 moyenne = 9.66667 coeff = 1 coeff_moy = 9.66667] [note1 = 14 note2 = 17 note2 = 17 moyenne = 14.1667 coeff = 1 coeff_moy = 14.1667] [note1 = 10 note2 = 14 note2 = 14 moyenne = 12.1667 coeff = 3 coeff_moy = 36.5] [note1 = 14 note2 = 17 note2 = 17 moyenne = 16.6667 coeff = 3 coeff_moy = 50] [note1 = 0 note2 = 15 note2 = 15 moyenne = 7.66667 coeff = 2 coeff_moy = 15.3333] [note1 = 14 note2 = 15 note2 = 15 moyenne = 15 coeff = 1 coeff_moy = 15] [note1 = 10 note2 = 10 note2 = 10 moyenne = 10.5 coeff = 1 coeff_moy = 10.5] [note1 = 11 note2 = 15.5 note2 = 15.5 moyenne = 12.8333 coeff = 1 coeff_moy = 12.8333] [note1 = 11 note2 = 12 note2 = 12 moyenne = 11.8333 coeff = 1 coeff_moy = 11.8333] [note1 = 14 note2 = 15 note2 = 15 moyenne = 15.6667 coeff = 1 coeff_moy = 15.6667] [note1 = 10 note2 = 11 note2 = 11 moyenne = 10.8333 coeff = 1 coeff_moy = 10.8333] ... moy1 = 10.8333 moy2 = 13.625 moy3 = 13Bonne chance