기존 코드에 추가해서 코드를 작성하는건데요

ineveri의 이미지

#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함수를 작성하는 겁니다.
배열이 가득 차 있을 경우 삽입이 이루어 지지 않게 해야합니다..
도와주세요ㅜㅜ제발

익명 사용자의 이미지

뭘 어떻게 도와드릴까요? 소스코드 돌려보고 직접 디버깅하길 원하시나요?
재료만 사와서 김치찌개 끓여달라는 거랑 다른게 없습니다.

simminjo의 이미지

누가봐도 학교숙제인데..........
symbol들은 C언어의 축약모델인듯하고...tiny c정도 되나요.....

---------------------------------------------------------------
Opensource에 기여하는 것이 꿈입니다.
내가 만든 코드를 모두가 사용할 때 까지~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.