C:启动旨在识别字符串模式的程序时遇到问题

标签 c grammar context-free-grammar

我正在上第一堂编程课,但我们的一个项目遇到了问题。该程序旨在获取字符串的输入,查看它们是否与模式匹配,并识别模式是否被破坏;在这种情况下,它的目的是识别用户输入的是“hahaha!”、“hohohoho!”还是“ha”和“ho”两个字符的混合(始终以“!”结尾)。

我的麻烦是,我已经开始尝试使用 switch case 来编写此代码,但不知道这是否是该项目编程的最有效方法,或者是否可以这样做。

这是到目前为止我的代码,请帮助我。

#include <stdio.h>
#include <string.h>

#define string_len 100

int main ()
{     
    char string[string_len];
    int state = 0;

    while(1)
    {
        for(int i = 0; i < string_len; i++)
        {
            printf("Hello, I can tell if you are laughing or not, you can exit by typing 'bye': \n");
            scanf("%s", string);

            for(state = 0; state < 5;)
            {
                switch(state)
                {        
                    case 0: 
                        if(strcmp(string, "bye") == 0)
                            printf("Bye now!\n");
                        return 0;
                        break;

                    case 1: 
                        if(string[i] == 'h')
                            state++;
                        else(printf("you are not laughing\n"));
                            break;

                    case 2: 
                        if(string[i] == 'a')
                            state--;
                        else(state++);
                            break;

                    case 3: 
                        if(string[i] == 'o')
                            state = state-2;
                        else(printf("you are not laughing\n"));
                            break;

                    case 4: 
                        if(string[i] == '!')
                            printf("You are laughing!\n");
                        else(printf("You are not laughing\n"));

                }
            }
        }

        return 0;
    }
}

我认为我可能与开关中程序的状态部分混淆了。我试图让它从:

状态0:检查它是否说再见,如果是“现在再见”

状态1:是h吗?如果是,请检查是否有 a 或 o,如果没有,请检查“你没有笑”

状态2:是a吗?如果是这样,请检查是否有“h”或“!” -这是我特别困惑的地方,如果不是的话是o吗?

状态 3:是 o 吗?如果是这样,请检查是否有“h”或“!”,如果没有,请检查“你没有笑”

状态 4:它是“!”吗?如果是,“你在笑”,如果不是,“你没有笑”

我希望我已经足够好地格式化了这个问题,如果我可以以任何方式使这个问题更具可读性,以及您对我有任何疑问,请告诉我。

谢谢大家的帮助。

最佳答案

既然您询问是否有其他方法比 switch() 语句更好,我决定为您编写一个方法的示例。这对我有用。应该干净地编译并运行良好。

#include <stdio.h>
#include <string.h>
#define MAXLEN 100
char *readline();

int main(void) {
    char string[MAXLEN];
    for(;;) {
        memset(string, '\0', MAXLEN);
        printf("Hello, I can tell if you are laughing or not, you can exit by typing 'bye': \n");
        readline(string, MAXLEN, stdin);
        int i = 0;
        int aborted = 0;
        char buf[3] = { 0 };
        while (i < strlen(string) - 1) {
           buf[i % 2] = string[i];
           if (i % 2 == 1) {
              if (strncmp(buf, "ha", 2) != 0 && strncmp(buf, "ho", 2) != 0) {
                  printf("\nYou are NOT laughing [1]\n\n");
                  aborted = 1;
                  break;
              }
           }
           i++;
        }
        if (!aborted) {
            if (string[i] != '!') {
                printf("\nYou are NOT laughing [2]\n\n");
                continue;
            }
            printf("\nYou ARE laughing!\n\n");
        }
    }
}

char *readline (char *buf, size_t length, FILE *f) {
  char *p;
  if ((p = fgets (buf, length, f)) != NULL) {
    size_t last = strlen (buf) - 1;
    if (buf[last] == '\n') {
      buf[last] = '\0';
    } else {
      fscanf (f, "%*[^\n]");
      (void) fgetc (f);
    }
  }
  return p;
} 

关于C:启动旨在识别字符串模式的程序时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463759/

相关文章:

c - Getcontext() 适用于具有结构的数组元素

c - malloc 在没有空闲的情况下失败后成功

normalization - 上下文无关语法转换

antlr4 - 如何在我的语法中执行操作优先级 (+ * -/)?

java - 代表CFG制作

objective-c - 内存在指针上增长?

c - 进程终止,状态为 -1073741819 在 C 中

java - 在简单的整数列表语法中使用 AntLR4 中的访问者

perl - Parse::RecDescent 语法没有按预期工作

c# - 具有讽刺意味的是:如何禁止两个标记之间有空格?