Use recursive methods to calculate the lastvt set
// 0505 lastvt. cpp: defines the entry point for the console application <SPAN class = 'wp _ keywordlink'> Program </span>. <Br/> // </P> <p> # include "stdafx. H "<br/> # include <stdio. h> <br/> # include <iostream> <br/> using namespace STD; </P> <p> # define length 5 // length of the generative formula <br/> # define count 7 // Number of generative formulas </P> <p> // generation formula <br/>/* <br/> char product [] [length] = {<br/> {'s ', 'A'}, <br/> {'s ',' ^ '}, <br/> {'s', '(','t ',') '}, <br/> {'T', 't',', ', 's'}, <br/> {'T','s '}, <br/>}; // textbook p122 1st question <br/> */<br/> char product [] [length] ={< br/> {'s ', 'V'}, <br/> {'V ', 'T'}, <br/> {'V', 'V', 'I', 't'}, <br/> {'T ', 'F'}, <br/> {'T', 't', '+', 'F'}, <br/> {'F ',')', 'V', '*'}, <br/> {'F', '('}, <br/> }; // textbook p122 3rd question </P> <p> void outputproduct () // print the formula <br/>{< br/> int I = 0, j = 0; <br/> while (product [I] [0]! = NULL) // the left side of the generative formula is not blank <br/>{< br/> printf ("/n % C->", product [I] [0]); // print non-terminator <br/> j = 1; <br/> while (product [I] [J]! = NULL) <br/>{< br/> printf ("% C", product [I] [J]); // print the terminator <br/> J ++; <br/>}< br/> I ++; // The Next Generation formula <br/>}</P> <p> char lastterm (char * s) // The Last Terminator. If no, return a non-terminator <br/>{< br/> int I = 1; // starts from the second, because the storage is all generate <br/> char last = s [I]; // The Last Terminator, the initial value is the first symbol on the right of the generative formula <br/> while (s [I]! = NULL) <br/>{< br/> If (s [I] <'A' | s [I]> 'Z ') // is the terminator <br/>{< br/> last = s [I]; // remember <br/>}< br/> I ++; <br/>}< br/> return last; // return the first symbol on the right of the generative formula <br/>}</P> <p> void lastvt (char S) // calculate the lastvt set of a single non-Terminator and print it directly <br/>{< br/> for (INT I = 0; I <count; I ++) <br/> {<br/> If (product [I] [0] = s) <br/>{< br/> while (product [I] [0] = s) <br/>{< br/> If (lastterm (product [I]) <'A' | lastterm (product [I])> 'Z') // Terminator <br/>{< br/> printf ("% C ", lastterm (product [I]); <br/>}< br/> else <br/>{< br/> lastvt (lastterm (product [I]); <br/>}< br/> I ++; // next generation formula <br/>}</P> <p> void outputlastvt () // print the firstvt set of all non-terminologies <br/>{< br/> for (INT I = 0; I <count; I ++) <br/>{< br/> printf ("/nlastvt (% C) =", product [I] [0]); <br/> while (product [I] [0] = product [I + 1] [0]) <br/>{< br/> I ++; <br/>}< br/> lastvt (product [I] [0]); <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> outputproduct (); <br/> outputlastvt (); </P> <p> printf ("/N "); <br/> system ("pause"); <br/> return 0; <br/>}
L