#include #include #include #include #include #include #include #include using namespace std; // Structure pour stocker les données du tableau struct Table { vector headers; vector> rows; }; // Fonction pour découper une ligne CSV en colonnes vector split(const string& s, char delimiter) { vector tokens; string token; istringstream tokenStream(s); while (getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } // Fonction pour lire le CSV Table readCSV(const string& filename) { Table table; ifstream file(filename); string line; if (getline(file, line)) { table.headers = split(line, ','); } while (getline(file, line)) { if (line.length() == 0) continue; table.rows.push_back(split(line, ',')); } return table; } // Fonction pour calculer les stats et ajouter les lignes void addStatistics(Table& table) { int numCols = table.headers.size(); int numRows = table.rows.size(); vector meanRow(numCols, "-"); vector stdDevRow(numCols, "-"); // On commence à i=1 si la colonne 0 contient des noms/textes // Ici on tente le calcul sur toutes les colonnes for (int j = 0; j < numCols; ++j) { vector values; for (int i = 0; i < numRows; ++i) { try { values.push_back(stod(table.rows[i][j])); } catch (...) { // Ignore les valeurs non numériques } } if (!values.empty()) { double sum = accumulate(values.begin(), values.end(), 0.0); double mean = sum / values.size(); double sq_sum = 0; for (double v : values) sq_sum += (v - mean) * (v - mean); double stdDev = sqrt(sq_sum / values.size()); stringstream ssMean, ssStd; ssMean << fixed << setprecision(2) << mean; ssStd << fixed << setprecision(2) << stdDev; meanRow[j] = ssMean.str(); stdDevRow[j] = ssStd.str(); } } meanRow[0] = "MOYENNE"; stdDevRow[0] = "ECART-TYPE"; table.rows.push_back(meanRow); table.rows.push_back(stdDevRow); } // Export HTML void saveToHTML(const Table& table, const string& filename) { ofstream file(filename); file << ""; for (const auto& h : table.headers) file << ""; file << ""; for (const auto& row : table.rows) { file << ""; for (const auto& cell : row) file << ""; file << ""; } file << "
" << h << "
" << cell << "
"; } // Export LaTeX void saveToLaTeX(const Table& table, const string& filename) { ofstream file(filename); file << "\\begin{tabular}{" << string(table.headers.size(), 'l') << "}\n\\hline\n"; for (size_t i = 0; i < table.headers.size(); ++i) { file << table.headers[i] << (i == table.headers.size() - 1 ? "" : " & "); } file << " \\\\\n\\hline\n"; for (const auto& row : table.rows) { for (size_t i = 0; i < row.size(); ++i) { file << row[i] << (i == row.size() - 1 ? "" : " & "); } file << " \\\\\n"; } file << "\\hline\n\\end{tabular}"; } int main(int argc, char *argv[]) { string inputFile = "donnees.csv"; if (argc > 1) { inputFile = argv[1]; } Table table = readCSV(inputFile); if (table.headers.empty()) { cout << "Erreur de lecture ou fichier vide." << endl; return 1; } addStatistics(table); saveToHTML(table, "resultat.html"); saveToLaTeX(table, "resultat.tex"); cout << "Traitement terminé. Fichiers resultat.html et resultat.tex générés." << endl; return 0; }