I have WA#6 :(
Posted by
Roma 15 May 2014 00:06
Please, help me. My code:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
const int MaxSourceSize = 100000;
const int MaxBuffSize = 1000;
const int nKeyWord = 36;
const char *comment_start = "<span class=comment>";
const char *keyword_start = "<span class=keyword>";
const char *string_start = "<span class=string>";
const char *number_start = "<span class=number>";
const char *end_teg = "</span>";
const char KeyWords[nKeyWord][sizeof( "implementation" )] = {
"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 main()
{
register int i, j, g;
char source[MaxSourceSize];
cin.get ( source, MaxSourceSize, 0 );
cin.ignore();
for ( i = 0; source[i]; i++ )
{
if ( isalpha ( source[i] ) || source[i] == '_' )
{
char buff[MaxBuffSize];
bool itKeyWord = false;
for ( g = i, j = 0; isalnum ( source[g] ) || source[g] == '_'; g++, j++ )
buff[j] = tolower ( source[g] );
buff[j] = 0;
for ( j = 0; j < nKeyWord; j++ )
if ( !strcmp ( buff, KeyWords[j] ) )
{
itKeyWord = true;
break;
}
if ( itKeyWord ) cout << keyword_start;
for ( i; isalnum ( source[i] ) || source[i] == '_'; i++ )
cout << source[i];
i--;
if ( itKeyWord ) cout << end_teg;
}
else
{
if ( isdigit ( source[i] ) )
{
cout << number_start;
for ( i; isdigit ( source[i] ) || ( source[i] == '.' && source[i+1] != '.' ); i++ )
cout << source[i];
i--;
cout << end_teg;
}
else
{
switch ( source[i] )
{
case '{':
{
int in = 0;
cout << comment_start;
for ( ; ; i++ )
{
if ( source[i] == '{' ) in++;
else if ( source[i] == '}' )
{
in--;
if ( !in ) {cout << source[i];break;}
}
cout << source[i];
}
cout << end_teg;
//i--;
};break;
case '\\':
{
if ( source[i+1] == '\\' )
{
cout << comment_start;
for ( i; source[i] != '\n'; i++ )
cout << source[i];
cout << end_teg;
i--;
}
else
{
cout << '\\';
}
};break;
case '\'':
{
cout << string_start;
cout << source[i++];
for ( i; source[i] != '\''; i++ )
cout << source[i];
cout << source[i];
cout << end_teg;
};break;
case '#':
{
cout << string_start;
i++;
cout << "#";
for ( i; isdigit ( source[i] ) || ( source[i] == '.' && isdigit ( source[i+1] ) ); i++ )
cout << source[i];
cout << end_teg;
i--;
};break;
default:
{
cout << source[i];
};break;
}
}
}
}
return NULL;
}