首页 > 解决方案 > 用于计算文件中“int”运算符的有限状态机 - 不起作用

问题描述

我有一个制作有限状态机的项目。程序必须打开文件并分析它的“Int”和“float”语句,并将结果保存到一个新文件中。下面是有限状态机的代码:

typedef enum sta_word{None, In_int, End_int} s_INT;
s_INT ComputeInt(int c, char* INT, s_INT status);



s_INT ComputeInt(int c, char* INT, s_INT status)
{
    static int ind = 0;
    char ch = c;
    if (status == End_int)
    {
        status = None;
        ind = 0;
    }
    switch (status)
    {
    case None:
        if (ch!='int')
        {
            status = In_int;
            INT[ind++] = c;
        }break;

    case In_int:
        if (ch=='int')
        {
            status = End_int;
            INT[ind] = 0;
        }
        else
        {
            INT[ind++] = c;
        }
        break;
    }
    return status;
} 

也就是计数函数:(FILE* fp 是打开文件进行读取)

int CountINT(FILE* fp)
{
    FILE* file;
    file = fopen("count.txt", "w");
    if (file == NULL)
    {
        perror("ERROR: ");
    }
    int br_INT = 0;
    int c;
    s_INT status = None;
    char INT[256];
    rewind(fp);
    do
    {
        c = fgetc(fp);
        status = ComputeInt(c, INT, status);
        if (status == End_int)
        {
            br_INT++;
        }
    } while (c != EOF);
    fprintf(file, "\n\ncompute %d INT Operators into file", br_INT);
    return 0;
}

但是当我打开文件进行写入时,什么都没有。

标签: c

解决方案


推荐阅读