1er Semestre 2013 503208: Programación I Ejercicios Input/Output — 11 de Mayo, 2013 Prof. Leo Ferres 1. Autor: Javier González N. Archivos de texto Cuando se usa redireccion (./a.out < archivo.txt, por ejemplo) estamos diciendole a nuestro programa que lea datos desde archivo.txt en vez de hacerlos desde la entrada estandar (stdin, es cuando escribimos en la consola). Lo que veremos a continuacion es como leer esos datos para procesarlos posteriormente. 3\n Pedro\n Juan\n Diego\n 2\n Pedro Juan\n Diego Juan\n EOF 3 Pedro Juan Diego 2 Pedro Juan Diego Juan La imagen de la izquierda es el texto plano tal cual lo vemos nosotros, el de la derecha es como lo ve el lenguaje C, podemos ver que agrega \n al final de cada lı́nea para indicar un salto de lı́nea y al final todo el archivo agrega EOF (End Of File). 2. Funciones utiles 1. int scanf(const char* format, ...): Trata de encontrar el patrón especificado. Retorna el número de elementos que pudo extraer del archivo, si llego al fin de archivo devolverá EOF. 2. char* fgets(char* str, int num, FILE* stream): Lee hasta que encuentra un EOF o un salto de linea. Parametros: str: Cadena de caracteres donde se guarda lo que fgets extrae. num: Tamaño de str stream: Archivo desde donde se lee, en nuestro caso siempre será stdin Hay mas información disponible sobre estas funciones en: www.cplusplus.com/reference/cstdio 1 3. Ejercicios Problema 1: Cada linea contiene 2 enteros, el archivo termina con EOF. Sample Input 1 10 100 200 201 210 900 1000 Sample Output: 1 10 100 200 201 210 900 1000 Solucion: int a, b; while(scanf("%d %d", &a, &b) != EOF) { printf("%d %d\n", a, b); } 2 Problema 2: The input contains on the first line the number of test cases (N). Each test case has on its first line the number (J) of lines in JuryOut, and the number (S) of SubmitOut lines, separated by a single space. Then follow the J lines of JuryOut and the S lines of SubmitOut. Both JuryOut and SubmitOut are no longer than 10 lines. A line is at most 80 characters long. Essentials are non-empty strings that do not cross line boundaries. The first and last characters of an essential are not white space. Essentials will not be nested. Example Input 4 1 2 Just one line? Just one line? 2 2 The first characters of the alphabet are: [abcde] Here they come: a b c d e 1 1 That’s it: [abcde] That’s it: AbCdE 1 1 [2] and [3] make [5] I guess 2 and 3 are less than 50. Solucion: #include <stdio.h> int main() { char line[1000]; int n; scanf("%d", &n); while(n--) { int J, S; scanf("%d %d\n", &J, &S); while(J--) { fgets(line, 1000, stdin); printf("%s", line); } while(S--) { fgets(line, 1000, stdin); printf("%s", line); } printf("\n"); } return 0; } 3 Problema 3: The input data for this problem will contain one candidate ISBN per line of input, perhaps preceded and/or followed by additional spaces. No line will contain more than 80 characters, but the candidate ISBN may contain illegal characters, and more or fewer than the required 10 digits. Valid ISBNs may include hyphens at arbitrary locations. The end of file marks the end of the input data. Sample Input: 0-89237-010-6 0-8306-3637-4 0-06-017758-6 This_is_garbage 1-56884-030-6 0-8230-2571-3 0-345-31386-0 0-671-88858-7 0-8104-5687-7 0-671-74119-5 0-812-52030-0 0-345-24865-1-150 0-452-26740-4 0-13-139072-4 0-1315-2447-X Solucion: #include <stdio.h> int main() { char line[1000]; while(scanf("%s", line) != EOF) { printf("%s\n", line); } return 0; } 4 Problema 4: The input file starts with a line containing the number of cases c to be analysed. Each case starts with a line with two numbers n and m . These indicate the number of star systems and the number of wormholes. The star systems are numbered from 0 (our solar system) through n-1 . For each wormhole a line containing three integer numbers x, y and t is given. These numbers indicate that this wormhole allows someone to travel from the star system numbered x to the star system numbered y, thereby ending up t years in the future. Sample Input 2 3 0 1 2 4 0 1 2 3 3 1 2 1 4 1 2 3 0 1000 15 -42 10 20 30 -60 #include <stdio.h> int main() { int t, i; scanf("%d", &t); while(t--) { int n, m; scanf("%d %d", &n, &m); for(i = 0; i < n; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); printf("Wormhole %d te lleva desde %d a %d\n", i, a, b); } } return 0; } 5 Problema 5: The input file will include a few data sets. Each data set will be a pair of boards as shown in the sample input. All entries will be upper case letters. Two consecutive entries on same board will be separated by one blank. The first row in the first board will be on the same line as the first row of the second board. They will be separated by four spaces, the same will hold for the remaining 3 rows. Board pairs will be separated by a blank line. The file will be terminated by . Sample Input: D F F B W A T U G I B R O K J M Y A K M B E L O S E P Y U T Q R Z U Y H F F A O U T L B W N T G A C G P V O I M G A G B S H N O # #include <stdio.h> #include <string.h> int main() { char line[1000]; while(1) { fgets(line, 1000, stdin); if(strncmp("#", line, 1) == 0) break; if(strncmp(line, "\n", 1) != 0) { char a, b, c, d, e, f, g, h; sscanf(line, "%c %c %c %c %c %c %c %c", &a, &b, &c, &d, &e, &f, &g, &h); printf("%c %c %c %c | %c %c %c %c\n", a, b, c, d, e, f, g, h); } } return 0; } 6