C++ 在单词之间添加空格
我试图在一组重复单词的文件中添加空间,如下所示。 基本上是状态机的家庭作业。我已经在数组中为它创建了表 再次,痛苦,熊,打,能,抓住,引起,我,更多,早晨,星期,去了,谁,你 在一个文本文件中重复1000次 又一次被打得鼻青脸肿的人可能会在一周内给你带来痛苦 我不知道为什么它不把他们分开,有什么问题C++ 在单词之间添加空格,c++,visual-c++,state-machine,C++,Visual C++,State Machine,我试图在一组重复单词的文件中添加空间,如下所示。 基本上是状态机的家庭作业。我已经在数组中为它创建了表 再次,痛苦,熊,打,能,抓住,引起,我,更多,早晨,星期,去了,谁,你 在一个文本文件中重复1000次 又一次被打得鼻青脸肿的人可能会在一周内给你带来痛苦 我不知道为什么它不把他们分开,有什么问题 //A state machine to parse a sequence of known strings. #include <iostream> #include "stdlib
//A state machine to parse a sequence of known strings.
#include <iostream>
#include "stdlib.h"
using namespace std;
int counter;
// State Table for:
// again, agony, bear, beat, can, caught, caused, me, more, morning, week, went, who, you
char NextState[27][31] = {
// 0 1 2 3 4 5
1, -1, 3, -1, -1, -1, -1, -1, 9, -1, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, 16, -1, 0, -1, 0, -1, -1, -1, -1, 25, 26, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 28, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, 0, 6, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 20, -1, 22, -1, -1, -1, 27, -1, -1, -1, -1, -1,
-1, -1, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 18, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 30, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//7, -1, 0, -1, -1, -1, //1
};
//a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
char map[26] = { 0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
char state = 0;
void process(char ch)
{
// print current char
cout << ch;
// move to next state
state = NextState[map[ch - 'a']][state];
// output a space if we are back to initial state
if (!state) {
cout << " ";
counter++;
}
}
int main(void)
{
char ch;
FILE * f;
// open file for text input
if (!(f = fopen("againTHRUyou.txt", "r"))) {
printf("Cannot open file\n");
exit(1);
}
// read and display one byte at a time
while ((ch = fgetc(f)) != EOF)
{
process(ch);
}
cout<<"The count is " << counter <<endl;
fclose(f);
}
//用于解析已知字符串序列的状态机。
#包括
#包括“stdlib.h”
使用名称空间std;
整数计数器;
//以下项目的状态表:
//再次,痛苦,熊,打,能,抓住,引起,我,更多,早晨,星期,去了,谁,你
char NextState[27][31]={
// 0 1 2 3 4 5
1, -1, 3, -1, -1, -1, -1, -1, 9, -1, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, 16, -1, 0, -1, 0, -1, -1, -1, -1, 25, 26, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 28, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, 0, 6, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 20, -1, 22, -1, -1, -1, 27, -1, -1, -1, -1, -1,
-1, -1, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 18, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 30, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//7, -1, 0, -1, -1, -1, //1
};
//a、 b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
字符映射[26]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
字符状态=0;
无效过程(char ch)
{
//打印当前字符
cout因为这看起来是你的大学作业,所以不要指望这里有完整的答案。我会给你一些提示
考虑这一行代码:
state = NextState[map[ch - 'a']][state];
您对代码运行时的假设是什么
- 进入
NextState
的第一个索引的预期范围是多少?实际参数是否始终在预期范围内
- 进入
NextState
的第二个索引的预期范围是多少?实际参数是否始终在预期范围内
回答这两个问题会让你走上正确的道路。
顺便说一句,请熟悉调试器和断言
另一个提示是,您的代码大部分时间运行良好,只是有时会失败。在这种情况下,最好在程序发展过程中显示程序的状态。添加这样的“cout debbug”在上述代码行之前和之后的一行,以查看程序如何更改状态。为此,请将输入文件缩短到大约100个字符。我没有时间仔细检查您的程序。以下是一些观察结果:
- 一个问题是您在状态/事件表中犯了一些错误。例如,
“morning”
中最后一个字母后的状态是-1
,而不是0
:
-1,2,-1,-1,-1,-1,-1,-1,13,-1,-1,-1,-1,-1,-1,-1,0/*-1*/,0,-1,-1,-1,-1,
< > > >强> >混合C+C++样式< /强>。使用<代码> IFSturi代替文件< /> >:
#include <fstream>
//...
ifstream is{ "c:\\temp\\test.txt" }; // opens the file; the file is automatically closed when the object goes out of scope
if ( !is )
return -1; // could not open
char c;
while ( is.get( c ) ) // get characters until the eof is reached or an error is encountered
{
//...
}
这个状态表给我的打击就像一堵墙一样。;-)我从来没有想过如何调试这样的大块头。但是,这应该是可能的。我会这样做:1.画图以便更容易地跟踪状态。2.逐步调试示例输入。我
enum
{
max_state = 50,
max_event = 26
};
int table[ max_state ][ max_event ];
int new_state = 0;
int as_event( char c )
{
return c - 'a';
}
void add( const char* word )
{
const char* p = word;
int state = 0;
while ( *p )
{
int event = as_event( *p++ );
int next_state = table[ state ][ event ];
if ( next_state != 0 ) // path already exists: just use it
{
state = next_state;
continue;
}
else // new path
{
next_state = ++new_state;
table[ state ][ event ] = next_state;
state = next_state;
}
}
}
void init()
{
memset( table, 0, sizeof table );
add( "again" );
add( "agony" );
add( "bear" );
// ...
}