标签:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct node
{
char list[81];
int tempi;
}sentance;
int printch(char sentance[]);
void produce(char sentance[],int &tempi);
int main()
{
sentance one,two,three;
char whole[800];
char word=NULL;
int i=1;
one.tempi=two.tempi=three.tempi=0;
printf("Please input the code and end with the ‘#‘:");
gets(whole);
word=whole[0];
while(word!=‘#‘)
{
while(ispunct(word)&&(word!=‘#‘))
{
two.list[two.tempi]=word;
two.tempi++;
word=whole[i];
i++;
if(!(ispunct(word)))
{
produce(two.list,two.tempi);
two.tempi=0;
}
}
while(isalpha(word)&&(word!=‘#‘))
{
one.list[one.tempi]=word;
one.tempi++;
word=whole[i];
i++;
if(!(isalpha(word)))
{
produce(one.list,one.tempi);
one.tempi=0;
}
}
while(isdigit(word))
{
three.list[three.tempi]=word;
three.tempi++;
word=whole[i];
i++;
if(!(isdigit(word)))
{
three.list[three.tempi]=‘\0‘;
printf("<11,\"%s\">\n",three.list);
three.tempi=0;
}
}
if(word==‘ ‘)
{
word=whole[i];
i++;
}
if(word==‘\0‘)
break;
}
return 0;
}
int printch(char sentance[],int &tempi)
{
char clist[][6]={"begin","if","then","while","do","end","","","","","","","+","-","*","/",":",":=","","<","<=","<>",">",">=","=",";","(",")","#"};
int i;
for(i=0;i<=28;i++)
{
if(strcmp(clist[i],sentance)==0)
{
printf("<%d,\"%s\">\n",i+1,sentance);
return 0;
}
}
printf("<10,\"%s\">\n",sentance);
return 0;
}
void compose(char sentance[],int &tempi)
{
sentance[tempi]=‘\0‘;
if(strlen(sentance)!=0)
{
printch(sentance,tempi);
}
}
标签:
原文地址:http://www.cnblogs.com/peivxuan/p/4835372.html