기존 코드에 추가해서 코드를 작성하는건데요
#include
#include
#include
#define NKEYS (sizeof symtab / sizeof(Symbol))
#define MAXWORD 100
#define MAXWORD 100
#define TABSIZE 1000
typedef struct key {
char *word;
int keyword;
int count;
} Symbol;
Symbol symtab[] = {
{"auto", 1, 0},
{"break", 1, 0},
{"case", 1, 0},
{"char", 1, 0},
{"const", 1, 0},
{"continue", 1, 0},
{"default", 1, 0},
{"do", 1, 0},
{"double", 1, 0},
{"else", 1, 0},
{"enum", 1, 0},
{"extern", 1, 0},
{"float", 1, 0},
{"for", 1, 0},
{"got0", 1, 0},
{"if", 1, 0},
{"int", 1, 0},
{"long", 1, 0},
{"register", 1, 0},
{"return", 1, 0},
{"short", 1, 0},
{"signed", 1, 0},
{"sizeof", 1, 0},
{"static", 1, 0},
{"struct", 1, 0},
{"switch", 1, 0},
{"typedef", 1, 0},
{"union", 1, 0},
{"unsigned", 1, 0},
{"void", 1, 0},
{"volatile", 1, 0},
{"while", 0}
};
int getword(char *, int);
Symbol *binsearch(char *, Symbol *, int);
Symbol *insert(char *, Symbol *, int, int);
main()
{
Symbol *p;
char word[MAXWORD];
int ntab = 32; /* initial table size = # keywords */
while (getword(word, MAXWORD) != EOF)
if (isalpha(word[0])) {
p = binsearch(word, symtab, ntab);
if (p == NULL)
p = insert(word, symtab, ntab++, TABSIZE);
if (p != NULL)
p->count++;
}
printf("Keywords\n");
for (p = symtab; p < symtab + ntab; p++)
if (p->keyword && p->count > 0)
printf("%4d %s\n", p->count, p->word);
printf("\n\nNon-keywords\n");
for (p = symtab; p < symtab + ntab; p++)
if (!p->keyword)
printf("%4d %s\n", p->count, p->word);
}
Symbol *binsearch(char *word, Symbol *tab, int n)
{
int cond;
Symbol *low = tab;
Symbol *high = tab + n;
Symbol *mid;
while (low < high) {
mid = low + (high - low) / 2;
cond = strcmp(word, mid->word);
if (cond < 0)
high = mid;
else if (cond > 0)
low = mid + 1;
else
return mid;
}
return NULL;
}
Symbol *insert(char *word, Symbol *tab, int n, int tabsize) {
if(현재 식별자의 개수> maximum symtab size){
에러메시지 출력
null 리턴
}
인수로 받은 식별자의 올바른 위치를 찾고(+뒤로 당겨주기작업)
strcpy(symtab[위치].keyword=0;
symtab[위치].keyword=0;
symtab[위치].count=0;
return 위치;
}
int getword(char *word, int lim)
{
int c, getch1(void);
void ungetch1(int);
char *w = word;
while (isspace(c = getch1()))
;
if (c != EOF)
*w++ = c;
if (!isalpha(c)) {
*w++ = '\0';
return c;
}
for ( ; --lim > 0; w++)
if (!isalnum(*w = getch1())) {
ungetch1(*w);
break;
}
*w = '\0';
return word[0];
}
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch1 */
int bufp = 0;
/* next free position in buf */
/* get a (possibly pushed back) character */
int getch1(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
/* push character back on input */
void ungetch1(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch1: too many characters\n");
else
buf[bufp++] = c;
}
여기서 non-keyword 까지 출력하게 하는 insert함수를 작성하는 겁니다.
배열이 가득 차 있을 경우 삽입이 이루어 지지 않게 해야합니다..
도와주세요ㅜㅜ제발
뭘 어떻게 도와드릴까요? 소스코드 돌려보고 직접
뭘 어떻게 도와드릴까요? 소스코드 돌려보고 직접 디버깅하길 원하시나요?
재료만 사와서 김치찌개 끓여달라는 거랑 다른게 없습니다.
..
누가봐도 학교숙제인데..........
symbol들은 C언어의 축약모델인듯하고...tiny c정도 되나요.....
---------------------------------------------------------------
Opensource에 기여하는 것이 꿈입니다.
내가 만든 코드를 모두가 사용할 때 까지~
댓글 달기