Questions that students need to write on the computer when they go to Shanghai Tongji Urban Planning and Design Institute
Write a function bool fun (char * t, char * s)
T is the template string, where * represents 1 to n arbitrary characters, and s is the matching string. True is returned for matching, whereas false is returned.
For example:
AB * and ABC match
AB * And ACD do not match
A * BC and adebc match
A * BC and adebce do not match
A * BC and a ** BC match
B * de * C and bfddsdessscc match
Recursion is used. The following code is modified after I search for a reference on the network. The example is given after the test is passed.
# Include "stdafx. H" <br/> int ismatch (const char * s, const char * P) <br/>{< br/> If (! S &&! P) <br/>{< br/> return 1; <br/>}< br/> else if (! S |! P) <br/>{< br/> return 0; <br/>}< br/> while (* s! = '/0' & * P! = '/0') <br/>{< br/> If (* s = * P) & (* s! = '*') <Br/>{< br/> + + S; <br/> + P; <br/> continue; <br/>}< br/> else if (* P = '*') <br/> {<br/>/* escape all '*/<br/> while (* P = '*') <br/>{< br/> + + P; <br/>}< br/> If (* P = '/0 ') <br/>{< br/> return 1; <br/>}< br/> while (* s! = '/0') <br/>{< br/> If (* s = * P & ismatch (S + 1, p + 1 )) <br/>{< br/> return 1; <br/>}< br/> + + S; <br/>}< br/> else <br/> {<br/> return 0; <br/>}< br/> while (* P = '*') <br/>{< br/> + P; <br/>}< br/> If (* P = '/0') & (* s ='/0 ')) <br/>{< br/> return 1; <br/>}< br/> return 0; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> const char * STR = "ABC"; <br/> const char * Pattern = "AB *"; </P> <p> If (ismatch (STR, pattern) <br/>{< br/> fprintf (stdout, "Match OK! /N "); <br/>}< br/> else <br/> {<br/> fprintf (stdout," Match failed! /N "); <br/>}< br/> return 0; <br/>}
Previously I did not think of recursion, but it was not well written. The last example was not tested.
# Include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; <br/> bool fun (char * t, char * s) <br/>{< br/> bool bsingle = false; <br/> while (* t! = '/0') & (* s! = '/0') <br/>{< br/> while (* t = * s) & (* t! = '/0') <br/>{< br/> ++ t; <br/> ++ S; <br/>}< br/> If (* t = '/0') & (* s! = '/0') <br/>{< br/> return false; <br/>}< br/> while (* t = '*') <br/>{< br/> + + T; <br/>}< br/> while (* t! = * S) <br/>{< br/> ++ S; <br/>}< br/> If (* t = '/0') <br/>{< br/> bsingle = true; <br/>}< br/> return bsingle; <br/>}</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> char * t = "AB *"; <br/> char * s = "ABC "; <br/> bool btemp = fun (t, s); <br/> return 0; <br/>}