Finite automaton programming

Source: Internet
Author: User

        /** * Consider a C-language program that prints the first English word of each line by reading the data one line at a standard input stream. * Therefore, it is necessary to confirm the first English word before a blank, if any, you need to read all the blanks to skip printing, read the first English word and then print, and then read the rest of the text is skipped until you read the line-break symbol. * Whenever you read a newline symbol in any situation, start the algorithm again, and in any case, just read the end of the file (end-of-file) symbol and end the program. */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= ' # ';//do not use automaton 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;}  Use the Automaton 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 ':p utchar (' \ n '); state = Before;break;default:putchar (c);} Break;case After:if (c = = ' \ n ') {putchar (' \ n '); state = Before;}}} return 0;} The self-Motive code Jane public int method3 () {int C;int state = Before;while ((c = GetChar ()) = EOF) {if (c = = ' \ n ') {putchar (' \ n '); stat e = before;} Elseswitch (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;} The self-Motive code separates public void step (int state, int c) {if (c = = ' \ n ') {putchar (' \ n '); state = before;} elseswitch (state) {case befor E: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;}    Use the state Transition table 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;                }};p ublic branch transitive_table[][]={/* ' \ n ' Others *//* before */{new Branch (before,false), New Branch (before,true), New branch (inside,true)},/* inside */{new Branch (afte R,FALSE), New Branch (before,true), New branch (inside,true)},/* after */{new branch (after,false), new branch (before,true ), New Branch (After,false)}};p ublic void Step2 (int state,int c) {int idx2 = (c = = ")? 0: (c = = ' \ n ')? 1:2;branch b =trans Itive_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;} Policy mode: Use state transition table instead of state class StateMachine {public int state;public branch the_table[][];p ublic 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_stat E;if (B.should_putchar) Putchar (c);} void Main () {int C; StAtemachine machine = new StateMachine (), while ((c = GetChar ())! = EOF) machine. Feedchar (c); return;};


Finite automaton programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.