di thrantir il 16 ott 2007, 16:29 
			
			uno dei vantaggi del codice aperto è la completa trasparenza di ciò che avviene. Per dimostrarlo, per cogliere un'occasione di parlare un po di programmazione, e per fare un po gli sboroni, ecco il codice del programmino che ha estratto gli incontri per la nostra fantacoppa
- Codice: Seleziona tutto
 thrantir@Rivendell:~/workspace/fantacalcio$ cat estrazione.cc
#include <fstream>
#include <list>
#include <iostream>
#include <stdexcept>
#include <ctime>
struct Team {
        Team(const std::string & a_name, int an_id)
                : name(a_name)
                , id(an_id)
        {}
        std::string name;
        int id;
};
typedef std::list<Team> Teams;
typedef std::pair<Team, Team> Match;
typedef std::list<Match> Matches;
Teams the_teams;
Matches the_matches;
void readTeams(const std::string & file_name);
void printTeams();
void extractMatches();
void printMatches();
int main() {
        readTeams("teams.dat");
        //printTeams();
        extractMatches();
        printMatches();
        return EXIT_SUCCESS;
}
void readTeams(const std::string & file_name) {
        std::ifstream file(file_name.c_str());
        std::string line;
        if(!file.good()) {
                throw std::runtime_error("no team file");
        }
        while(!file.eof()) {
                getline(file, line);
                if(line.length() > 0) {
                        int index = line.find('|');
                        if(index != std::string::npos) {
                                std::string my_name = line.substr(index+1, line.length() - index) ;
                                int my_id = atoi(line.substr(0, index).c_str());
                                the_teams.push_back(Team(my_name, my_id));
                        }
                }
        }
}
void printTeams() {
        Teams::const_iterator my_iter = the_teams.begin();
        Teams::const_iterator my_end = the_teams.end();
        for( /* void */;
                my_iter != my_end;
                ++my_iter) {
                std::cout << "team " << my_iter->id << " " << my_iter->name << std::endl;
        }
}
void extractMatches() {
        if((the_teams.size() % 2) != 0 ) {
                perror("squadre dispari!!!");
        }
        srand(time(0));
        while( the_teams.size() > 0) {
        //long long my_rand = rand();
                int my_choose =  (the_teams.size() * (rand() / (RAND_MAX + 1.0)));
                Teams::iterator my_iter = the_teams.begin();
                for (/* void */; my_choose > 0 ; --my_choose) ++my_iter;
                Team first(*my_iter);
                the_teams.erase(my_iter);
                my_choose =  (the_teams.size() * (rand() / (RAND_MAX + 1.0)));
                my_iter = the_teams.begin();
                for (/* void */; my_choose > 0 ; --my_choose) ++my_iter;
                Team second(*my_iter);
                the_teams.erase(my_iter);
                the_matches.push_back(std::make_pair(first, second));
        }
}
void printMatches() {
        Matches::const_iterator my_iter = the_matches.begin();
        Matches::const_iterator my_end = the_matches.end();
        for( /* void */;
                my_iter != my_end;
                ++my_iter) {
                std::cout << "match: " << my_iter->first.name << " - " << my_iter->second.name << std::endl;
        }
}
 
			Fletto i muscoli e sono nel vuoto
Principi di architettura degli eleboratoriX postare immagini-----BEGIN GEEK CODE BLOCK-----
GCS/IT/L/MU d- s: a C++$>+++ UL+>++ P L+++>++++ E--- W++ N++>+++ o+>++ K? w O-- M- VMS? V- PS++ Y+ PGP+ t 5? X+ R++>+++ tv+ b+++>++++ DI+++ D++ G e++ h- r++ y++
------END GEEK CODE BLOCK------
Addio Dani, sono più ricco perchè ti ho conosciuto