有限自动机的构造与识别

本文介绍了一个使用队列数据结构实现的简单正则表达式解析程序。通过定义队列和链队列的数据结构,实现了队列的基本操作如初始化、入队和出队。并在主函数中读取用户输入的正则表达式进行处理,通过队列出队操作完成解析过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#define N 30
char str[N]={NULL};
int st[N];
int t=0,k=0,a,t1=0;
struct node//定义一个队列
{
    char data;
    struct node * next;
};
typedef struct node QueueNode;
struct node2//定义一个链队列
{
    QueueNode *front;
    QueueNode *rear;
};
typedef struct node2 Queue;
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;
    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;
    str[i]=y;//存储单个字符
    st[k]=i;//存储位置
    if(y==' ');
    if(y=='*'||y=='|')
    {
        //if(y=='*')
            //str[i]=y;
        if(str[0]!=NULL)//如果数组str非空,则输出
            Print(0,str[0]);
        t=0;
        //continue;
    }
   /*if((y>='A'&&y<='Z')||(y>='a'&&y<='z'))//利用ASCII判断y是否是属于字母,并存储在数组str中
   {
        str[i]=y;
   }*/
   k++;
    free(p);
    return Q;
}
void Print(int i,char y);//调用函数来判断并输出
main()
{
    char x,y;
    Queue p,q;
    p=InitQueue();
    printf("请输入正规式:");
    while(scanf("%c",&x)==1&&x!='\n')
        p=InserQ(p,x);
    q=p;
    while(p.front!=p.rear)
        p=DeleteQ(p);
  
}
void Print(int i,char y)
{
    a=st[i+1]+1;
    if(y=='|'||y=='*')
    {
        k--;
        return;
    }
    if(str[i]!='|')
        a=t1++;
    if(str[i+1]=='*')
    {
        printf("f(%d,%c)=%d\n",i,'~',k);
        printf("f(%d,%c)=%d\n",k,y,k);
        printf("f(%d,%c)=%d\n",k,'~',a);
    }
    else
        printf("f(%d,%c)=%d\n",i,y,st[i+1]);
    if(i<t)
        Print(i+1,str[i+1]);
    memset(str,0,N);//清空数组str里的所有元素
}

 

转载于:https://www.cnblogs.com/severusandsusa/p/6150454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值