tÉcnicas de programaciÓn lenguaje c
DESCRIPTION
Séptima Clase (Sexta de C). TÉCNICAS DE PROGRAMACIÓN Lenguaje C. FUNCIONES Y ESTRUCTURA DEL PROGRAMA. CAPÍTULO 4 DE KyR. Ejemplo. Imprimir las líneas de la entrada que contengan un patrón dado: while ( haya otra línea ) if ( la línea contiene el patrón ) imprimirla - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/1.jpg)
TÉCNICAS DE PROGRAMACIÓN
Lenguaje CSéptima Clase(Sexta de C)
![Page 2: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/2.jpg)
2Daniel Finol – ICA - LUZ
FUNCIONES Y ESTRUCTURA DEL PROGRAMA
CAPÍTULO 4 DE KyR
![Page 3: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/3.jpg)
3Daniel Finol – ICA - LUZ
Ejemplo
Imprimir las líneas de la entrada que contengan un patrón dado:
while (haya otra línea) if (la línea contiene el patrón) imprimirla
haya otra línea: getline. imprimirla: printf. Falta: la línea contiene el patrón.
![Page 4: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/4.jpg)
4Daniel Finol – ICA - LUZ
Ejemplo#include <stdio.h>#define MAXLINE 1000
int getline(char line[], int max)int strindex(char source[], char searchfor[]);
char pattern[] = "patrón";
main(){ char line[MAXLINE]; int found = 0;
while (getline(line, MAXLINE) > 0) if (strindex(line, pattern) >= 0) { printf("%s", line); found++; } return found;}
![Page 5: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/5.jpg)
5Daniel Finol – ICA - LUZ
getline(char, int)int getline(char s[], int lim){ int c, i;
i = 0; while(--lim > 0 && (c=getchar()) !=EOF && c!='\n') s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i;}
![Page 6: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/6.jpg)
6Daniel Finol – ICA - LUZ
strindexint strindex(char s[], char t[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) {
for(j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++,k++)
;
if (k > 0 && t[k] == '\0')
return i;
}
return -1;
}
![Page 7: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/7.jpg)
7Daniel Finol – ICA - LUZ
Forma de una funcióntipo-de-retorno nombre(declaraciones de argumentos)
{
declaraciones e instrucciones
}
Una función con un tipo de retorno distinto de void debe retornar algún valor Si la ejecución llega al final de la función y la última
instrucción no es un return el valor de la función es "basura".
![Page 8: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/8.jpg)
8Daniel Finol – ICA - LUZ
Funciones
El programa fuente puede dividirse en varios archivos, mientras las funciones no se dividan.
cc main.c getline.c strindex.c
cc main.c getline.o strindex.o
![Page 9: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/9.jpg)
9Daniel Finol – ICA - LUZ
atof
#include <ctype.h> /* atof: convert string s to double */ double atof(char s[]) { double val, power; int i, sign;
for (i = 0; isspace(s[i]); i++) ; sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') i++; for (val = 0.0; isdigit(s[i]); i++) val = 10.0 * val + (s[i] - '0'); if (s[i] == '.') i++; for (power = 1.0; isdigit(s[i]); i++) { val = 10.0 * val + (s[i] - '0'); power *= 10; } return sign * val / power; }
![Page 10: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/10.jpg)
10
Daniel Finol – ICA - LUZ
Calculadora
#include <stdio.h>
#define MAXLINE 100
/* rudimentary calculator */main(){ double sum, atof(char []); char line[MAXLINE]; int getline(char line[], int max);
sum = 0; while (getline(line, MAXLINE) > 0) printf("\t%g\n", sum+= atof(line)); return 0;}
![Page 11: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/11.jpg)
11
Daniel Finol – ICA - LUZ
int atoi(char s[]) { double atof(char s[]);
return (int) atof(s); }
![Page 12: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/12.jpg)
12
Daniel Finol – ICA - LUZ
Variables Externas
Un programa en C consta de un conjunto de objetos externos: variables y funciones. Los argumentos y las variables definidas dentro de las
funciones son "internos".
Variables externas: Definidas fuera de cualquier función. Disponibles para muchas funciones.
Las funciones siempre son externas.
![Page 13: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/13.jpg)
13
Daniel Finol – ICA - LUZ
Calculadora Polaca Inversa
Notación infija: (1 – 2) * (4 + 5)
Notación postfija: 1 2 – 4 5 + *
No necesita paréntesis
![Page 14: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/14.jpg)
14
Daniel Finol – ICA - LUZ
Calculadora Polaca Inversa
while(siguiente operador u operando no es fin de archivo) if (número) introducirlo else if (operador) extraer operandos hacer operación insertar resultado else if (nueva línea) extrae e imprime tope else error
![Page 15: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/15.jpg)
15
Daniel Finol – ICA - LUZ
Calculadora Polaca Inversamain() { int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER:
push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if (op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s);
break;} } return 0;}
![Page 16: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/16.jpg)
16
Daniel Finol – ICA - LUZ
Pila #define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */ double val[MAXVAL]; /* value stack */
void push(double f) { if (sp < MAXVAL) val[sp++] = f; else printf("error: pila llena %g\n", f); }
double pop(void) { if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; } }
![Page 17: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/17.jpg)
17
Daniel Finol – ICA - LUZ
getop#include <ctype.h>
int getch(void);void ungetch(int);
int getop(char s[]) { int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t');
s[1] = '\0'; if (!isdigit(c) && c != '.') return c; i = 0; if (isdigit(c)) while (isdigit(s[++i] = c = getch())); if (c == '.') while (isdigit(s[++i] = c = getch())); s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER; }
![Page 18: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/18.jpg)
18
Daniel Finol – ICA - LUZ
getch y ungetch #define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */ int bufp = 0; /* next free position in buf */
int getch(void) { return (bufp > 0) ? buf[--bufp] : getchar(); }
void ungetch(int c) { if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c; }
![Page 19: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/19.jpg)
19
Daniel Finol – ICA - LUZ
Reglas de alcance
El alcance de un nombre es la parte del programa en la cual puede usarse para referirse al mismo objeto.
El alcance de las variables locales (incluidos argumentos) es la función en la cual se declaran.
El alcance de variables y funciones externas va desde el punto en que se declaran hasta el fin del archivo.
Para usar una variable externa fuera de su alcance hay que declararla nuevamente como extern.
![Page 20: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/20.jpg)
20
Daniel Finol – ICA - LUZ
![Page 21: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/21.jpg)
21
Daniel Finol – ICA - LUZ
Variables estáticas y register Si se define una variable o se declara una
función externa como static sólo podrá ser usada dentro del archivo donde se define.
Una variable static local se mantiene (con su valor) en vez de aparecer y desaparecer cada vez que se llama y se termina la función. Proporcionan un almacenamiento "permanente" y
privado para una función.
Variable register.
Variables de bloques.
![Page 22: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/22.jpg)
22
Daniel Finol – ICA - LUZ
Inicialización de arreglos
char pattern = "ould";
char pattern[] = { 'o', 'u', 'l', 'd', '\0' };
char pattern[10] = { 'o', 'u', 'l', 'd', '\0' }; == "ould" + basura;
char pattern[4] = { 'o', 'u', 'l', 'd', '\0' }; ERROR;
![Page 23: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/23.jpg)
23
Daniel Finol – ICA - LUZ
Recursividad: printd
#include <stdio.h>
/* printd: print n in decimal */ void printd(int n) { if (n < 0) { putchar('-'); n = -n; } if (n / 10) printd(n / 10); putchar(n % 10 + '0'); }
![Page 24: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/24.jpg)
24
Daniel Finol – ICA - LUZ
Recursividad
El código recursivo no ahorra ni espacio (memoria) ni tiempo. Usualmente es más lento.
Es más compacto y fácil de escribir y entender (no siempre: printd).
![Page 25: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/25.jpg)
25
Daniel Finol – ICA - LUZ
Directivas del preprocesador (#)
#include : " " o < > Anidable
#define nombre texto_de_reemplazo Alcance: archivo donde se define.
#define : macros #define max(A, B) ((A) > (B) ? (A) : (B)) x = max(p+q, r+s); x = ((p+q) > (r+s) ? (p+q) : (r+s)); max(i++, j++) /* MALO */
#define square(x) x * x /* ¿¿¿correcto??? */ #undef
![Page 26: TÉCNICAS DE PROGRAMACIÓN Lenguaje C](https://reader035.vdocuments.co/reader035/viewer/2022081501/56814652550346895db36903/html5/thumbnails/26.jpg)
26
Daniel Finol – ICA - LUZ
#if !defined(HDR) #define HDR
/* contents of hdr.h go here */
#endif_____________________________ #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #elif SYSTEM == MSDOS #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR
__________________________________
#ifdef; #ifndef