Why WA 1?
Послано
wwwwww 7 апр 2005 00:16
It is my code.
Please let me some tests or some hints to find bugs.
#include <stdio.h>
#include <string.h>
#define LEN 200013
#define WORDN 35
char KeyWord[WORDN][25] =
{
"and", "array", "begin", "case", "class", "const", "div",
"do", "else", "end", "for", "function", "if", "implementation",
"interface", "mod", "not", "of", "or", "procedure", "program",
"record", "repeat", "shl", "shr", "string", "then", "to",
"type", "unit", "until", "uses", "var", "with", "while"
};
int WLen[WORDN];
char S[LEN];
int Len = 1;
int Equal( char *Word, char *S )
{
while (*Word)
if (*Word != *S && (*Word != *S - 'A' + 'a' || *S < 'A' || 'Z' < *S))
return 0;
else
Word++, S++;
return 1;
}
int IsLetter( int c )
{
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
int main( void )
{
int com_open = 0, com2 = 0, is_str = 0, is_num = 0, no_is = 1;
int c, pos, dot, num, i;
for (i = 0; i < WORDN; i++)
WLen[i] = strlen(KeyWord[i]);
S[0] = ' ';
while ((c = getc(stdin)) != EOF)
S[Len++] = c;
S[Len] = 0;
for (pos = 1; pos <= Len; pos++)
{
if (S[pos - 1] == '/' && S[pos] != '/' && no_is)
putc(S[pos - 1], stdout);
no_is = 0;
if (com2)
{
if (S[pos] == '\n')
com2 = 0, printf("</span>");
putc(S[pos], stdout);
}
else if (com_open > 0)
{
putc(S[pos], stdout);
if (S[pos] == '{')
com_open++;
else if (S[pos] == '}')
if (--com_open == 0)
printf("</span>");
}
else if (is_str)
{
putc(S[pos], stdout);
if (S[pos] == '\'')
is_str = 0, printf("</span>");
}
else if (is_num)
{
if ('0' <= S[pos] && S[pos] <= '9')
putc(S[pos], stdout);
else if (!dot && S[pos] == '.' && '0' <= S[pos + 1] && S[pos + 1] <= '9')
dot = 1, putc(S[pos], stdout);
else
is_num = 0, printf("</span>"), pos--;
}
else if (S[pos] == '{')
{
printf("<span class=comment>");
putc(S[pos], stdout);
com_open++;
}
else if (S[pos] == '/')
if (S[pos - 1] == '/')
{
printf("<span class=comment>");
putc(S[pos], stdout);
putc(S[pos], stdout);
com2 = 1;
}
else
no_is = 1;
else if (S[pos] == '\'')
{
printf("<span class=string>");
putc(S[pos], stdout);
is_str = 1;
}
else if (S[pos] == '#')
{
printf("<span class=string>");
putc(S[pos++], stdout);
while ('0' <= S[pos] && S[pos] <= '9')
putc(S[pos++], stdout);
printf("</span>");
pos--;
}
else if ('0' <= S[pos] && S[pos] <= '9')
{
printf("<span class=number>");
is_num = 1, dot = 0, num = 0, pos--;
}
else
{
for (i = 0; i < WORDN; i++)
if (Equal(KeyWord[i], S + pos) &&
!IsLetter(S[pos + WLen[i]]) &&
(pos == 0 || !IsLetter(S[pos - 1])))
break;
if (i < WORDN)
{
printf("<span class=keyword>%s</span>", KeyWord[i]);
pos += WLen[i] - 1;
}
else if (S[pos] != 0)
putc(S[pos], stdout);
}
}
return 0;
}