blob: 569350c721a9e79c39e49c45f3a23f60197bc8fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "fun.h"
using namespace std;
void cargaarreglo(void *&arreglo,
int (*cmpnumero)(const void *, const void *),
int *leenumero(istream &in),
const char *filename)
{
ifstream in(filename);
if (!in) {
cerr << "Error, no se pudo abrir archivo\n";
return;
}
int **numbers;
int *data;
int *buffer[MAXLEN];
int i;
/* load buffer */
for (i = 0; (data = leenumero(in)); i++) {
buffer[i] = data;
}
buffer[i] = nullptr;
/* create "exact" array of pointers to int */
numbers = new int * [i + 1];
for (i = 0; buffer[i]; i++) {
numbers[i] = buffer[i];
}
numbers[i] = nullptr;
/* sort array */
qsort(numbers, i, sizeof(void *), cmpnumero);
// for (i = 0; numbers[i]; i++) {
// cout << *numbers[i] << '\n';
// }
/* modify parameter reference */
arreglo = numbers;
}
/* qsort comparisong function. Descending order */
int cmpnumero(const void *p1, const void *p2)
{
return - (** (int **) p1 - ** (int **) p2);
}
/* allocates memory */
int *leenumero(istream &in)
{
int n;
if (in >> n)
return new int{n};
else
return nullptr;
}
void imprimenumero(ostream &out, void *data)
{
cout << * (int *) data << '\n';
}
|