/**
*考虑一个C语言的程式,由标准输入流一行一行的读取资料,打印各一行的第一个英文单字。
*因此一开始需确认第一个英文单字之前是否有空白,若有,需读取所有空白后略过不打印,读取第一个英文单字然后打印,之后读取其他内容略过不打印,直到读到换行符号为止。
*任何情形下只要读到换行符号,就重新开始此算法,任何情形下只要读到档案结束(end-of-file)的符号,就结束程式。
*/
private static final int before=0;
private static final int inside=1;
private static final int after=2;
public char getchar(){
char c=EOF;
try {
c = (char)System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
return c;
}
public void putchar(int c){
System.out.print(c);
}
public static char EOF='#';
//不使用自动机
public int method1() {
int c;
do {
c = getchar();
while (c == ' ')
c = getchar();
while (c != EOF && c != ' ' && c != '\n') {
putchar(c);
c = getchar();
}
putchar('\n');
while (c != EOF && c != '\n')
c = getchar();
} while (c != EOF);
return 0;
}
//使用自动机
public int method2() {
int state;
int c;
state = before;
while ((c = getchar()) != EOF) {
switch (state) {
case before:
if (c == '\n') {
putchar('\n');
} else if (c != ' ') {
putchar(c);
state = inside;
}
break;
case inside:
switch (c) {
case ' ':
state = after;
break;
case '\n':
putchar('\n');
state = before;
break;
default:
putchar(c);
}
break;
case after:
if (c == '\n') {
putchar('\n');
state = before;
}
}
}
return 0;
}
//自动机代码化简
public int method3() {
int c;
int state = before;
while ((c = getchar()) != EOF) {
if (c == '\n') {
putchar('\n');
state = before;
} else
switch (state) {
case before:
if (c != ' ') {
putchar(c);
state = inside;
}
break;
case inside:
if (c == ' ') {
state = after;
} else {
putchar(c);
}
break;
case after:
break;
}
}
return 0;
}
//自动机代码分离
public void step(int state, int c) {
if (c == '\n') {
putchar('\n');
state = before;
} else
switch (state) {
case before:
if (c != ' ') {
putchar(c);
state = inside;
}
break;
case inside:
if (c == ' ') {
state = after;
} else {
putchar(c);
}
break;
case after:
break;
}
}
int main() {
int c;
int state = before;
while ((c = getchar()) != EOF) {
step(state, c);
}
return 0;
}
//采用状态转换表
class branch {
public int new_state;
public boolean should_putchar;
public branch(int new_state,boolean should_putchar){
this.new_state=new_state;
this.should_putchar=should_putchar;
}
};
public branch transitive_table[][]={
/* ' ' '\n' others */
/* before */{new branch(before,false),new branch(before,true),new branch(inside,true)},
/* inside */{new branch(after,false), new branch(before,true),new branch(inside,true)},
/* after */{new branch(after,false), new branch(before,true),new branch(after,false)}
};
public void step2(int state,int c){
int idx2 =(c ==' ') ? 0: (c =='\n') ? 1 : 2;
branch b =transitive_table[state][idx2];
state =b.new_state;
if(b.should_putchar)
putchar(c);
}
public int state;
public int main2(){
int c;
state = before;
while((c =getchar())!= EOF)
step(state, c);
return 0;
}
//策略模式:采用状态转换表而非状态类
class StateMachine {
public int state;
public branch the_table[][];
public StateMachine() {
this.state = before;
}
void FeedChar(int c) {
int idx2 = (c == ' ') ? 0 : (c == '\n') ? 1 : 2;
branch b = the_table[state][idx2];
state = b.new_state;
if (b.should_putchar)
putchar(c);
}
void main() {
int c;
StateMachine machine = new StateMachine();
while ((c = getchar()) != EOF)
machine.FeedChar(c);
return;
}
};原文:http://blog.csdn.net/zhangheliang2010/article/details/46391539