-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleer.cpp
More file actions
54 lines (47 loc) · 1.51 KB
/
leer.cpp
File metadata and controls
54 lines (47 loc) · 1.51 KB
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
/*Este programa se encarga de leer un archivo, pedir al usuario un numero y ordenar los numeros de mayor a menor,
mostrar los 10 mayores numeros por pantalla*/
#include <stdio.h>
#include <stdlib.h>
#define CANTIDAD_DE_NUMEROS 11
int main(){
FILE *archivo,
*archivo2;
int numero[CANTIDAD_DE_NUMEROS],
temporal;
//abre el flujo de datos
archivo = fopen ("numero.txt", "r");
//recoge todas las puntuaciones
if(archivo == NULL){
archivo = fopen ("numero.txt", "w+");
for(int imprime=0; imprime<10; imprime++)
fprintf(archivo, "%i \t0\n", imprime+1);
fclose(archivo);
archivo = fopen ("numero.txt", "r");
}
for(int pasada=0; pasada<10; pasada++){
fscanf(archivo, " %*i %i", &numero[pasada]);
printf("%i\n", numero[pasada]);
}
//se cierra el flujo
fclose(archivo);
//pide al usuario que introduzca un numero
printf("Introduzca numero: ");
scanf(" %i", &numero[10]);
//ordena los numeros de mayor a menor, incluido el del usuario
for(int mayor=CANTIDAD_DE_NUMEROS; mayor>=0; mayor--){
for (int menor=mayor-1; menor>=0;
menor--){
if(numero[mayor] > numero[menor]){
temporal = numero[menor];
numero[menor] = numero[mayor];
numero[mayor] = temporal;
}//fin if
}//fin for menor
}//fin for mayor
//imprime por pantalla los 10 mayores numeros
archivo2 = fopen("numero.txt", "w");
for(int menor=1; menor<CANTIDAD_DE_NUMEROS; menor++)
fprintf(archivo2, "%i \t%i\n", menor, numero[menor]);
fclose(archivo2);
return EXIT_SUCCESS;
}