int main(){ // Expected response double testResults1[16] = { 25.9375, 3.6875, -4.625, -5.0, -4.0, -1.75, 3.75, -3.75, 11.0, -9.0, 4.5, 2.0, -3.0, 4.5, -0.5, -3.0 }; // Testing int length = 16; double *results = transform(HAAR_WAVELET, test1, length); int correct = 0; for(int i = 0; i < length; ++i){ if(doubleEquality(results[i], testResults1[i])){ correct = 1; printf("Incorrect for Haar, test1: [%d] = %f, not %f\n", i, results[i], testResults1[i]); } } return correct; }
void seleziona(char* file_da_leggere, char* file_da_scrivere, int colonna_analizzata, double valore_riferimento) { std::ifstream da_leggere, da_selezionare; std::ofstream da_scrivere; bool fallita_lettura_da_leggere = true, fallita_apertura_da_scrivere = true; da_leggere.open(file_da_leggere); fallita_lettura_da_leggere = da_leggere.fail(); std::cout << "File in: " << std::string(file_da_leggere) << std::endl; if ((fallita_lettura_da_leggere)) { std::cout << "Unable to open input file" << std::endl; return; } int contacolonne_da_leggere = 0; char str[LINE_MAX_LENGTH], backup_str[LINE_MAX_LENGTH]; char * pch; da_leggere.getline(str, LINE_MAX_LENGTH); strcpy(backup_str, str); // non serve riportare lo stream all'inizio, anzi cosi' non dobbiamo piu' scartare la prima riga di commento! // da_leggere.clear(); // da_leggere.seekg(0, std::ios::beg); pch = std::strtok(str, "\t"); if (pch != NULL) contacolonne_da_leggere++; while (pch != NULL) { pch = std::strtok(NULL, "\t"); if (pch != NULL) contacolonne_da_leggere++; } printf("%d columns were found in the input file\n", contacolonne_da_leggere); printf("Considering %d-th column as the one used to select data\n", colonna_analizzata); if (contacolonne_da_leggere <= colonna_analizzata) { printf("Not enough columns in the input file\n"); return; } da_scrivere.open(file_da_scrivere); fallita_apertura_da_scrivere = da_scrivere.fail(); std::cout << "File out: " << std::string(file_da_scrivere) << std::endl; if ((fallita_apertura_da_scrivere)) { std::cout << "Unable to open output file" << std::endl; return; } // size_t size_vector = RESERVE_SIZE_VECTOR; // int multiplo = 1; std::vector<double> in_lettura; in_lettura.resize(contacolonne_da_leggere); std::vector< std::vector< double > > righe_salvate; // righe_salvate.reserve(size_vector*multiplo); while (1) { for (std::vector<double>::size_type i = 0; i < in_lettura.size(); i++) da_leggere >> in_lettura[i]; if (da_leggere.eof()) break; if (doubleEquality(in_lettura[colonna_analizzata - 1], valore_riferimento)) righe_salvate.push_back(in_lettura); } std::cout << righe_salvate.size() << " useful lines found" << std::endl; da_scrivere << backup_str << std::endl; for (std::vector<double>::size_type i = 0; i < righe_salvate.size(); i++) { for (std::vector<double>::size_type j = 0; j < in_lettura.size(); j++) da_scrivere << righe_salvate.at(i).at(j) << "\t"; da_scrivere << std::endl; } da_leggere.close(); da_scrivere.close(); }