OVH Cloud OVH Cloud

K&R p125 126 déclarations complexes

22 réponses
Avatar
Jean Pierre Daviau
Bonjour à tous,

J'ai remplacé getch(0 par getche() dans la fonction gettoken() afin de voir
sur la fenêtre dos ce que j'écrivais.
La fonction gettoken() a été testé avec l'application dcl() de K&R page
122-123-124 et elle fonctionne normalement.

La fonction undcl(), elle ne fonctionne pas très bien en ce qu'il n'y a pas
moyen de la faire cesser avec la ligne:
while(gettoken() != EOF) dans main.

Voici un copie du programme donnée en exemple par KR chap 5.12
"Complicated declaration".

/*
undcl.c
"x is a function returning a pointer to an array of pointers to functions
returning char"

undcl convert word description to declaration
x () * [] * () char char (*(*x())[])()

*/

#include "dcl.h"

int main(){ /* page 126 */

int type;
char temp[MAXTOKEN];

while(gettoken() != EOF){
strcpy(out, token);
while((type = gettoken()) != '\n'){
if(type == PARENS || type == BRACKETS){
strcat(out, token);
}else if(type == '*'){
sprintf(temp, "(*%s)", out);
strcpy(out, temp);
}else if (type == NAME){
sprintf(temp, "%s %s", token, out);
strcpy(out, temp);
}else{
printf("invalid input at %s\n", token);
}

printf("%s\n", out);
}
}

return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXTOKEN 100

enum {NAME, PARENS, BRACKETS};

void dcl(void);
void dirdcl(void);
int gettoken(void);

int tokentype; /* type of last token */
char token[MAXTOKEN]; /* last token string */
char name[MAXTOKEN]; /* identifyer name */
char datatype[MAXTOKEN]; /* data type = char, int, etc. */
char out[1000]; /* output string */

int gettoken(void){ /* return next token fonction commune a dcl() */

int c, getche(void);
void ungetch(int);
char *p = token;

while((c = getche()) == ' ' || c == '\t')
;
if(c == '('){
if((c = getche()) == ')'){
strcpy(token, "()");
return tokentype = PARENS;
}else{
ungetch(c);
return tokentype = '(';
}
}else if(c == '['){
for(*p++ = c; (*p++ = getche()) != ']'; )
;
*p = '\0';
return tokentype = BRACKETS;
}else if (isalpha(c)){
for(*p++ = c; isalnum(c = getche()); )
*p++ = c;
*p = '\0';
ungetch(c);
return tokentype = NAME;
}else
return tokentype = c;
}


--
Jean Pierre Daviau
--
http://jeanpierredaviau.com

2 réponses

1 2 3
Avatar
Emmanuel Delahaye
Jean Pierre Daviau wrote on 15/03/05 :
YES
Merci

N.B.: YES terme ado signifiant great géant merci cool enfin qué que chose
qui fonctionne c'est beau la vie, etc.


En français ça s'écrit 'YAISSE'

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Avatar
Charlie Gordon
"Emmanuel Delahaye" wrote in message
news:
Jean Pierre Daviau wrote on 15/03/05 :
YES
Merci

N.B.: YES terme ado signifiant great géant merci cool enfin qué que chose
qui fonctionne c'est beau la vie, etc.


En français ça s'écrit 'YAISSE'


Ou IESSE, comme dans liesse.

Chqrlie.


1 2 3