#2
菜鸟,求帮忙2015-07-12 14:51
|
#include<iostream>
#include<stdio.h>
#include<fstream>
using namespace std;
enum statetype{INNUM, INID, INASSIGN, STRING, OPERATION, INCOMMENT, START, DONE};
char Token[100];
bool IsCout;
//int count = 0;
bool IsOPERATION;
bool IsINCOMMENT;
StateType state;
char type[10];
char keyword[14][20]={"main","cin","cout","int","char","bool","float","return","if","switch","while","do","case","break"};
char operation[9][3] = { "+", "-", "*", "/","%", "++", "--", "+=", "-=" };
char incomment[8][3] = { "<<", ">>", "<=", ">=", "==", "!=", "&&", "||" };
void c_init()
{
state = Strat;
// count = -1;
IsCout = true;
IsOPERATION = false;
IsINCOMMENT = false;
memset(type, 0, sizeof(type));
memset(Token, 0, sizeof(Token));
}
void c_out()
{ if( IsCout )
{
int i;
for( i=0; i < count; i++ )
{
printf("%c", Token[i]);
}
printf(" ");
for( i=0; i < 10; i++ )
{
printf("%c", type[i]);
}
printf("\n");
}
c_init();
}
bool IsINASSIGN(char c[])
{
for(int i=0; i < 14; i++)
{
if( !strcmp(keyword[i], c) ) return true;
}
return false;
}
bool IsINCOMMENT(char c[])
{
for(int i=0; i < 8; i++)
{
if( !strcmp(incomment[i], c) ) return true;
}
return false;
}
struct node
{
bool flag;
char c;
}
void c_calc()
{
freopen("test.cpp", "r", stdin);
node temp;
temp.flag=false;//初始化为无存放临时值
c_init();
while( 1 )
{
//count++;
if( temp.flag )
{ Token[count]=temp.c;
temp.flag = false; }
else
{
//if( scanf("%c", &Token[count]) == EOF )
//{ break; }
}
if( state == Start )
{
if( isdigit(Token[count]) )
{ state = INNUM; }//数字
else
{
if( isalpha(Token[count]) )
{ state = INID; }//标识符
else
{
if( !strcmp(Token, "\'") || !strcmp(Token, "\"") )
{ state = STRING; }
else
{
if( (Token[count] == ' ') || (Token[count] == '\n') )
{ state = DONE; IsCout = false; }
else
{
if( !strcmp(Token, "+") || !strcmp(Token, "-") || !strcmp(Token, "*") || !strcmp(Token, "%") || !strcmp(Token, "/") )
{ state = OPERATION; }
else state = INCOMMENT; //特殊符号
}
}
}
}
}
switch( state )
{
case INNUM:
{
if( !isdigit(Token[count]) )
{
temp.flag = true; temp.c = Token[count];
if( (Token[count] == ' ') || (Token[count] == '\n') ){ temp.flag = false; }
strcpy(type, "数");
state = DONE; break;
}
break;
}
case INID:
{
if( !isdigit(Token[count]) && !isalpha(Token[count]) && (Token[count] != '.') )
{
temp.flag = true; temp.c = Token[count];
if( (Token[count] == ' ') || (Token[count] == '\n') ){ temp.flag = false; }
Token[count] = '\0';
if( IsINASSIGN(Token) ) strcpy(type, "关键字");
else strcpy(type, "标识符");
state = DONE; break;
}
break;
}
case STRING:
{
if( Token[0] == '\'' )
{
if( count > 0 && Token[0] == Token[count] )
{
count++;
strcpy(type, "字符");
state = DONE; break;
}
break;
}
if( Token[0] == '\"' )
{
if( count > 0 && Token[0] == Token[count] )
{
count++;
strcpy(type, "串");
state = DONE; break;
}
break;
}
break;
}
case OPERATION:
{
if( count > 0 )
{
if( isdigit(Token[count]) || isalpha(Token[count]) )
{ temp.flag = true; temp.c = Token[count]; }
else count++;
strcpy(type, "运算符号");
state = DONE; break;
}
break;
}
case INCOMMENT:
{
if( IsINCOMMENT )
{
if( !IsINCOMMENT(Token) )
{ temp.flag = true; temp.c = Token[count]; count--;}
}
if( !strcmp(Token, "<") || !strcmp(Token, ">") || !strcmp(Token, "!") || !strcmp(Token, "=") || !strcmp(Token, "+") || !strcmp(Token, "-") || !strcmp(Token, "|") || !strcmp(Token, "&") )
{ IsINCOMMENT = true; break; }
else
{
strcpy(type, "特殊符号");
//count++;
state = DONE; break;
}
break;
}
case DONE: break;
}
if( state == DONE ){ c_out(); }
}
}
void main()
{
c_calc();
}