1 / 26

TÉCNICAS DE PROGRAMACIÓN Lenguaje C

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

claire
Télécharger la présentation

TÉCNICAS DE PROGRAMACIÓN Lenguaje C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Séptima Clase (Sexta de C) TÉCNICAS DE PROGRAMACIÓNLenguaje C

  2. FUNCIONES Y ESTRUCTURA DEL PROGRAMA CAPÍTULO 4 DE KyR

  3. 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.

  4. 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; }

  5. 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; }

  6. strindex int 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; }

  7. Forma de una función tipo-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".

  8. 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

  9. 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; }

  10. 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; }

  11. int atoi(char s[]) { double atof(char s[]); return (int) atof(s); }

  12. 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.

  13. Calculadora Polaca Inversa • Notación infija: • (1 – 2) * (4 + 5) • Notación postfija: • 1 2 – 4 5 + * • No necesita paréntesis

  14. 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

  15. Calculadora Polaca Inversa main() { 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;}

  16. 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; } }

  17. 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; }

  18. 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; }

  19. 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.

  20. 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.

  21. 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;

  22. 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'); }

  23. 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).

  24. 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

  25. #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

More Related