本文介绍了调试有限状态机拼写检查码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要某人调试下面我写的c ++代码行,以便它可以运行。要使用状态转换来拼写检查单词和。

I need someone to debug the lines of c++ code I wrote below so it can run. It is intended to spell check the word "and" using state to state transition.

#include<iostream>
#include<string>

using namespace std;

string in_str;
int n;
void spell_check()
{
     int i;
     FILE *in_file;

     while (!EOF(in_file))
     {
           fscanf(in_str);
           n = strlen(in_str);
           start(in_str,n);
     }
}

void start()
{
     char next_char;

     int i = 0;
     if (n == 0)
     {
           cout<<"This is an empty string";
           exit();//do something here to terminate the program
     }
     else{
          next_char = in_str[i];

          if(next_char == 'a')
          {
                       i++;
                       if(i >= n) error();
                       else state_A(i);
          }
          else error();
     }
}

void state_A(int i)
{
     if(in_str[i] == 'n')
     {
                  i++;
                  if(i<n) state_AN(i);
                  else error();
     }
     else error();
}

void state_AN(int i)
{
     if(in_str[i] == 'd')
     {
                  if(i == n-1)
                       cout<<" Your keyword spelling is correct";
                  else
                      cout<<"Wrong keyword spelling";
     }
}

int main()
{
    spell_check();
    return 0;
}


推荐答案

这是一个C程序,你可以用来测试代码:

Here's a C program that you could use to test your code:

// -*- coding: utf-8 -*-
#include <stdio.h>
#include <stdlib.h> // EXIT_*

#define log(msg) do { \
  puts(msg); \
  } while(0)

/** $ graph-easy --input=fsm.dot --as=boxart

         ┌─────────────────────────────────────────┐
         │                                         │
         │               [^a]                      │
         │       ┌───────────────────┐             │
         │       ▼                   │             │
         │     ╔═══════╗   [^a]    ╔════╗          │
         │     ║       ║ ───────┐  ║    ║          │
         │     ║ START ║        │  ║ O  ║          │ a
         └──── ║       ║ ◀──────┘  ║    ║ ─┐       │
               ╚═══════╝           ╚════╝  │       │
                 │                   │     │       │
                 │                   │ a   │       │
                 │                   ▼     │       │
                 │                 ╔════╗  │       │
                 │        ┌─────── ║ A  ║ ◀┼───────┘
                 │        │        ╚════╝  │
                 │        │          │     │
                 │        │          │ n   │
                 │        │          ▼     │
                 │        │        ╔════╗  │
                 │        │        ║ N  ║ ─┼───────┐
                 │        │        ╚════╝  │       │
                 │        │          │     │       │
                 │        │          │ d   │       │
                 │        │          ▼     │       │
╔═════╗  EOS     │        │        ╔════╗  │       │
║ END ║ ◀────────┼────────┼─────── ║ D  ║  │       │
╚═════╝          │        │        ╚════╝  │       │
                 │        │          │     │       │
                 │        │ [^n]?    │ .   │ EOS   │ [^d]?
                 │        │          ▼     ▼       ▼
                 │        │        ╔══════════════════════╗
                 │        └──────▶ ║                      ║
                 │                 ║        ERROR         ║
                 │       EOS       ║                      ║
                 └───────────────▶ ║                      ║
                                   ╚══════════════════════╝
*/
typedef enum State {
  O, START, A, N, D, ERROR
} State;

static int next_token(void) {
  int ch = getchar();
  if (ch == '\n') // consider newline as EOS
    return EOF;
  return ch;
}

static int spell_check(void) {
  State state = O;
  int token = -1;
  while ((token = next_token()) != EOF) {
    switch(state) {
    case START:
      log("I am comming");
      // fall through
    case O:
      state = token == 'a' ? A : START; break;
    case A:
      state = token == 'n' ? N : ERROR; break;
    case N:
      state = token == 'd' ? D : ERROR; break;
    case D:
      state = ERROR; break;
    case ERROR:
      return EXIT_FAILURE;
    default:
      log("can't happen");
      return EXIT_FAILURE;
    }
  }
  if (state == O)
    log("This is an empty string");
  return state == D ? EXIT_SUCCESS : EXIT_FAILURE;
}

int main(void) {
  int rc = spell_check();
  if (rc == EXIT_SUCCESS)
    log(" Your keyword spelling is correct");
  else
    log("Wrong keyword spelling");
  return rc;
}

用法:

$ gcc *.c && (echo and | ./a.out; echo $?)

输出:

 Your keyword spelling is correct
0

这篇关于调试有限状态机拼写检查码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 12:22