/**
* 2012年6月25日 中午
*
* 山東科技大學信息學院 版權所有
*
* 聯系郵箱:www.2cto.com
*
* Copyright @ 2012, sdust, All Rights Reserved
*
* @author 王昌帥
*
*/
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define MAX_LENGTH 10
#define NUM_RESERVED_WORD 32
#define NUM_OPERATOR 5
#define NUM_SEPARATOR 16
//函數聲明
void input(char*);
int judge(char*);
void parse2Array(char*);
int isChar(char c);
int isOperator(char c);
int isSeparator(char c);
int isNum(char c);
//輸出信息
void output();
//判斷是否是基本保留字(參數:字符串)
int isReservedWord_str(char* str);
//判斷是否是操作符(參數:字符串)
int isOperator_str(char* str);
//判斷是否是操作符(參數:字符串)
int isSeparator_str(char* str);
//判斷是否是數字串(參數:字符串)
int isNum_str(char* str);
//判斷是否是標識符(參數:字符串)
int isIndentifier_str(char* str);
//定義標識符
char sign_reserved_word[][MAX_LENGTH] = {"auto","double","int","struct","break","else","long","switch","case","enum","register","typedef","char","extern","return","union","const","float","short","unsigned","continue","for","signed","void","default","goto","sizeof","volatile","do","if","while","static"};
char sign_operator[][MAX_LENGTH] = {"+","-","*","/","%"};
char sign_separator[][MAX_LENGTH] = {"\n"," ","\"","\t",",",";",":","'","<",">","(",")","{","}","[","]"};
//單詞總數(包括運算符、分隔符等)
int wordNum = 0;
//解析好的單詞
char array[200][20];
//主函數
int main()
{
char str[500];
while(1)
{
input(str);
parse2Array(str);
output();
}
}
//用於輸入的函數
void input(char* str)
{
int i = 0,j = 0;
char c;
str[0] = '\0';
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("請輸入要解析的語句(esc退出):\n\n");
for(i=0;(c=getch())!='@';i++)
{
if(c==27)
exit(0);
if(c==8)
{
i-=2;
if(i<-1)
i=-1;
system("cls");
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("請輸入要解析的語句(esc退出):\n\n");
for(j=0;j<=i;j++)
{
printf("%c",str[j]);
}
continue;
}
else if(c==13)
{
str[i] = '\n';
printf("\n");
}
else
{
str[i] = c;
printf("%c",c);
}
str[i+1] = '\0';
}
}
//將輸入的字符串解析成一個一個的單詞
void parse2Array(char* str)
{
int count;
int i,temp;
wordNum = 0;
count = strlen(str);
for(i=0;i<count;i++)
{
temp = i;
if(isOperator(str[i]))
{//如果是運算符
array[wordNum][0]=str[i];
array[wordNum][1]='\0';
wordNum++;
}
else if(isSeparator(str[i]))
{//如果是分隔符
array[wordNum][0]=str[i];
array[wordNum][1]='\0';
wordNum++;
}
else if(isChar(str[i]))
{//如果是字符
for(;str[i]!='\0'&&(!isOperator(str[i]))&&(!isSeparator(str[i]))&&(isChar(str[i])||isNum(str[i])||str[i]=='_');i++)
{
array[wordNum][i-temp]=str[i];
}
array[wordNum][i-temp]='\0';
wordNum++;
i--;
}
else if(isNum(str[i]))
{//如果是數字
for(;str[i]!='\0'&&(!isOperator(str[i]))&&(!isSeparator(str[i]))&&(isNum(str[i]));i++)
{
array[wordNum][i-temp]=str[i];
}
array[wordNum][i-temp]='\0';
wordNum++;
i--;
}
else
{
array[wordNum][0]=str[i];
array[wordNum][1]='\0';
wordNum++;
}
}
}
//輸出信息
void output()
{
int i ;
printf("\n");
for(i = 0;i<wordNum;i++)
{
int flag = judge(array[i]);
switch(flag)
{
case 1:
printf(" 1、基本保留字:\t%s\n",array[i]);
break;
case 2:
printf(" 2、標識符 :\t%s\n",array[i]);
break;
case 3:
printf(" 3、常數 :\t%s\n",array[i]);
break;
case 4:
printf(" 4、運算符 :\t%s\n",array[i]);
break;
case 5:
if(strcmp(array[i],"\n")==0)
{
printf(" 5、分隔符 :\t\\n\n",array[i]);
}
else if(strcmp(array[i]," ")==0)
{
printf(" 5、分隔符 :\t空格\n",array[i]);
}
else
{
printf(" 5、分隔符 :\t%s\n",array[i]);
}
break;
case -1:
printf("-1、不能識別 :\t%s\n",array[i]);
break;
default:
printf("-1、不能識別 :\t%s\n",array[i]);
break;
}
}
}
//判斷是哪類字符(1、基本保留字 2、標識符 3、常數 4、運算符 5、分隔符 -1、不能識別)
int judge(char * word)
{
if(isSeparator_str(word))
{
return 5;
}
else if(isReservedWord_str(word))
{
return 1;
}
else if(isOperator_str(word))
{
return 4;
}
else if(isNum_str(word))
{
return 3;
}
else if(isIndentifier_str(word))
{
return 2;
}
else
{
return -1;
}
}
//判斷是否是字符
int isChar(char c)
{
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
{
return 1;
}
else
{
return 0;
}
}
//判斷是否是數字(單個字符)
int isNum(char c)
{
if(c>='0'&&c<='9')
{
return 1;
}
else
{
return 0;
}
}
//判斷是否是操作符(單個字符)
int isOperator(char c)
{
int i ;
char sign[] = {'+','-','*','/','%'};
for(i = 0;i<NUM_OPERATOR;i++)
{
if(c==sign[i])
{
return 1;
}
}
return 0;
}
//判斷是否是分隔符(單個字符)
int isSeparator(char c)
{
int i ;
char sign[] = {'\n',' ','\"','\t',',',';',':','\'','<','>','(',')','{','}','[',']'};
for(i = 0;i<NUM_SEPARATOR;i++)
{
if(c==sign[i])
{
return 1;
}
}
return 0;
}
//判斷是否是基本保留字(參數:字符串)
int isReservedWord_str(char* str)
{
int i ;
for(i = 0;i<NUM_RESERVED_WORD;i++)
{
if(strcmp(str,sign_reserved_word[i])==0)
{
return 1;
}
}
return 0;
}
//判斷是否是操作符(參數:字符串)
int isOperator_str(char* str)
{
int i ;
for(i = 0;i<NUM_OPERATOR;i++)
{
if(strcmp(str,sign_operator[i])==0)
{
return 1;
}
}
return 0;
}
//判斷是否是分隔符(參數:字符串)
int isSeparator_str(char* str)
{
int i ;
for(i = 0;i<NUM_SEPARATOR;i++)
{
if(strcmp(str,sign_separator[i])==0)
{
return 1;
}
}
return 0;
}
//判斷是否是數字串(參數:字符串)
int isNum_str(char* str)
{
int i , count;
count = strlen(str);
for(i = 0;i<count;i++)
{
if(str[i]<'0'||str[i]>'9')
{
return 0;
}
}
return 1;
}
//判斷是否是標識符(參數:字符串)
int isIndentifier_str(char* str)
{
int i , count;
count = strlen(str);
for(i = 0;i<count;i++)
{
if(!isChar(str[i])&&!isNum(str[i])&&(str[i]!='_'))
{
return 0;
}
}
return 1;
}
作者:wangchangshuai0010