调试有限状态机拼写检查器代码
我需要有人调试我在下面编写的 C++ 代码行,以便它可以运行。它的目的是使用状态到状态转换来检查单词“and”的拼写。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个可用于测试代码的 C 程序:
用法:
输出:
Here's a C program that you could use to test your code:
Usage:
Output:
几点评论:
定义
FILE
对象是不够的。您需要调用fopen
来实际打开一个文件,然后才能使用fscanf
或类似方法读取该文件。strlen()
返回一个size_t
——无符号类型。它们使用 C 风格的空终止字符串。std::string
是一个 C++ 字符串类。要使用strlen
首先获取 C 样式字符串:例如:
通常,
fstream
对象用于文件处理。不要使用全局对象,
error
函数未定义。Few comments:
Defining the
FILE
object is not enough. You need to callfopen
to actually open a file and only then can you read from it usingfscanf
or some such.strlen()
returns asize_t
-- an unsigned type. They work with C-style null-terminated strings.std::string
is a C++ string class. To usestrlen
get a C-style string first:E.g:
Typically,
fstream
objects are used for file handling.Instead of using global objects, try to pass around the information as arguments.
The
error
function is not defined.您可能需要将 -Wall 添加到编译指令中,以便将来自己查找所有缺陷。
此外,可以在互联网上轻松找到好的参考资料,例如 cplusplus.com。
因为我今天感觉很慷慨:
对于状态机来说,开关与递归相结合是一个更加优雅的解决方案......
you might want to add -Wall to your compilation instruction to find all defects yourself in the future.
Also good reference material can be easily found on the internet, for instance cplusplus.com.
And because I'm feeling generous today:
A switch combined with recursion is a far more elegant solution for your state machine...