#include<stdio.h>
#include<string.h>
#define N 30
char str[N]={NULL},st[N]={NULL};
int t=0,t1=0;
struct node//定义一个队列
{
char data;
struct node * next;
};
typedef struct node QueueNode;
struct node2//定义一个链队列
{
QueueNode *front;
QueueNode *rear;
};
typedef struct node2 Queue;
void Print(char str[]);
void Print1(char str1[]);
Queue InitQueue()//初始化队列
{
Queue Q;
Q.front=(QueueNode *)malloc(sizeof(QueueNode));
Q.front->next=NULL;
Q.rear=Q.front;
return(Q);
}
Queue InserQ(Queue Q,char x)//x进队列
{
QueueNode *p;
p=(QueueNode *)malloc(sizeof(QueueNode));
p->data=x;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return(Q);
}
Queue DeleteQ(Queue Q)//出队列
{
int i=t++,j;
QueueNode *p;
char y=NULL;
if(Q.front==Q.rear)
{
printf("队列为,无法出队列!");//判断队列是否为空
return(Q);
}
p=Q.front->next;
y=Q.front->next->data;//将队列中的元素赋值给y
Q.front->next=p->next;
if(p==Q.rear)
Q.rear=Q.front;
if((y>='A'&&y<='Z')||(y>='a'&&y<='z'))//利用ASCII判断y是否是属于字母,并存储在数组str中
str[i]=y;
else
{
if(str[0]!=NULL)//如果数组str非空,则输出
Print(str);
t=0;
if(y=='+')
printf("(13,'%c')\n",y);
else if(y=='-')
printf("(14,'%c')\n",y);
else if(y=='*')
printf("(15,'%c')\n",y);
else if(y=='/')
printf("(16,'%c')\n",y);
else if(y==':')
{
if(p->next->data=='=')//判断运算是否是由两个字符组成
{
printf("(18,'%c%c')\n",y,p->next->data);
Q.front->next=p->next->next;
free(p->next);
}
else
printf("(17,'%c')\n",y);
}
else if(y=='<')
{
if(p->next->data=='=')//判断运算是否是由两个字符组成
{
printf("(21,'%c%c')\n",y,p->next->data);
Q.front->next=p->next->next;
free(p->next);
}
else if(p->next->data=='>')//判断运算是否是由两个字符组成
{
printf("(22,'%c%c')\n",y,p->next->data);
Q.front->next=p->next->next;
free(p->next);
}
else
printf("(20,'%c')\n",y);
}
else if(y=='>')
{
if(p->next->data=='=')//判断运算是否是由两个字符组成
{
printf("(24,'%c%c')\n",y,p->next->data);
Q.front->next=p->next->next;
free(p->next);
}
else
printf("(23,'%c')\n",y);
}
else if(y=='=')//判断字符是否是运算符
printf("(25,'%c')\n",y);
else if(y==';')
printf("(26,'%c')\n",y);
else if(y=='(')
printf("(27,'%c')\n",y);
else if(y==')')
printf("(28,'%c')\n",y);
else if(y=='#')
printf("(0,'%c')\n",y);
else if(y>=48&&y<=57)//判断字符是否为数字
{
j=t1++;
if(j==0)
printf("(11,'");
printf("%c",y);
if(p->next->data<48||p->next->data>57)//判断队列中下一个字符是否为数字,如果是非字符数字,则输出数组st
if(st[0]!='\o')
{
printf("')\n");
t1=0;
}
}
else if(y==' ');
else
printf("(非法字符,'%c')\n",y);//其他的当作特殊符号处理
}
free(p);
return Q;
}
int main()
{
char x,y;
Queue p,q;
p=InitQueue();
printf("请输入你想输入的字母、单词、短语、句子、字符等(必须以非数字结尾,否则程序出错):\n");
while(scanf("%c",&x)==1&&x!='\n')//大神的方法,牛
p=InserQ(p,x);
q=p;
while(p.front!=p.rear)
p=DeleteQ(p);
if(str[0]!='\o')//判断数组str是否为空
Print(str);
}
void Print(char str[])//调用函数来判断关键字与标识符
{
int i=0;
if(strcmp(str,"begin")==0)
printf("(1,'%s')\n",str);
else if(strcmp(str,"if")==0)
printf("(2,'%s')\n",str);
else if(strcmp(str,"then")==0)
printf("(3,'%s')\n",str);
else if(strcmp(str,"while")==0)
printf("(4,'%s')\n",str);
else if(strcmp(str,"do")==0)
printf("(5,'%s')\n",str);
else if(strcmp(str,"end")==0)
printf("(6,'%s')\n",str);
else
{
if(str[0]=='\0')
return;
printf("(10,'%s')\n",str);
}
memset(str,0,N);//清空数组str里的所有元素
}