#include #include #include #include // using std::cerr; // using namespace std; // #include "arguments.hpp" #include "Array.hpp" #include "String.hpp" template void printArray(const ecci::Array& values, const char* separator = ", ") { std::cout << '['; for (size_t index = 0; index < values.getCount(); ++index) { if (index) { std::cout << separator; } std::cout << values[index]; } std::cout << ']'; } int main() { try { ecci::Array values; ecci::Array copy; double value = 0.0; while (std::cin >> value) { values.append(value); } copy = values; printArray(copy); std::cout << std::endl; // std::cout << values << std::endl; ecci::Array texts; texts.append("que"); texts.append("funcione"); texts.append("please"); texts.append("!!!"); printArray(texts); std::cout << std::endl; ecci::Array> matrix(20); for (size_t row = 0; row < 20; ++row) { matrix[row].resize(20); for (size_t col = 0; col < 15; ++col) { matrix[row][col] = row * 100 + col; } } for (size_t row = 0; row < 20; ++row) { for (size_t col = 0; col < 15; ++col) { std::cout << std::setw(4) << std::setfill('0') << matrix[row][col] << " "; } std::cout << std::endl; } } catch (const std::runtime_error& error) { std::cerr << "median: error: " << error.what() << std::endl; } // std::vector> matrix(rows, std::vector(cols)); } // long cerr = 022928282828228l; #if 0 enum error_code read_values(struct array* values, std::istream& input , const arguments_t* const arguments); int compare_double(const void* p1, const void* p2); double calculate_median(const struct array* const values); int process_file(std::istream& input, const char* filename , const arguments_t* const arguments); // main: int main(int argc, char* argv[]) { int error = ERROR_SUCCESS; arguments_t arguments; arguments_init(&arguments); error = analyze_arguments(&arguments, argc, argv); if (error == ERROR_SUCCESS) { if (argc >= 2) { for (int index = 1; index < argc; ++index) { if (*argv[index] != '-') { // printf("%d = [%s]\n", index, argv[index]); std::ios_base::openmode mode = arguments.is_input_binary ? std::ifstream::in | std::ifstream::binary : std::ifstream::in; std::ifstream input(argv[index], mode); if (input) { process_file(input, argv[index], &arguments); input.close(); } else { // int cerr = 0; // ::cerr = cerr; std::cerr << "could not open " << argv[index] << '\n'; } } } } else { process_file(std::cin, "stdin", &arguments); } arguments_uninit(&arguments); } return error; } int process_file(std::istream& input, const char* filename , const arguments_t* const arguments) { enum error_code error = ERROR_SUCCESS; // Read values as an array of real numbers // array_t values; struct array values; if (array_init(&values) == EXIT_SUCCESS) { error = read_values(&values, input, arguments); if (error == ERROR_SUCCESS) { // Sort values qsort(values.elements, values.count, sizeof(double), compare_double); // Calculate median of values const double median = calculate_median(&values); // Print median std::cout << filename << ": " << median << std::endl; } else { std::cerr << "median: error: could not read values\n"; } array_uninit(&values); } else { std::cerr << "median: error: could not allocate memory\n"; error = ERROR_NO_ENOUGH_MEMORY; } return error; } // (data_type*) C-style cast // const_cast(expr) quitar constancia a un puntero // static_cast(expr) // dynamic_cast(expr) polimorfismo // reinterpret_cast(expr) punteros enum error_code read_values(struct array* values, std::istream& input , const arguments_t* const arguments) { double value = 0.0; if (arguments->is_input_binary) { // size_t fread(void* ptr, size_t size, size_t count, FILE * stream); // while (fread(&value, sizeof(value), 1, input) == 1) { while (input.read(reinterpret_cast(&value), sizeof(value))) { if (array_append(values, value) != EXIT_SUCCESS) { return ERROR_CANNOT_APPEND_VALUE; } } } else { // while (fscanf(input, "%lg", &value) == 1) { // std::cout << value << value << std::endl; // std::cin >> value >> value; while (input >> value) { if (array_append(values, value) != EXIT_SUCCESS) { return ERROR_CANNOT_APPEND_VALUE; } } } return ERROR_SUCCESS; } int compare_double(const void* p1, const void* p2) { // const double* value1 = (double*) p1; // const double* value2 = (double*) p2; // return *value1 - *value2; return *(double*)p1 - *(double*)p2; } // median: double calculate_median(const struct array* const values) { // If value count is odd then if ((*values).count % 2 == 1) { // Return value at index value count / 2 return values->elements[values->count / 2]; } else { // Return average of two values at the center of the array return (values->elements[values->count / 2 - 1] + values->elements[values->count / 2]) / 2.0; } } #endif