Experimental data structure--KMP algorithm test.ming

Source: Internet
Author: User

Translation Program
Xiao Ming beginner C + +, which identified four arithmetic, relational operators, logical operations, ODE operations, input and output, use simple selection and loop structure. But his English is not very good, remember too many reserved words, he used the reserved words of Hanyu Pinyin, Hut C + +, invented a expression of their own ideas of the algorithm descriptive narrative rules.
The rule is very easy: He will start the program head with a phonetic name tag, C + + program "{,}" with Pinyin "Kaishi,jieshu" visual representation. The selection and circulation are only used in a single structure, and the reserved words are represented by corresponding pinyin, except that he strips out the extra parentheses when expressing the selection or loop condition, and the input and output is also in phonetic notation. The data type retains only integer (int) and real (float), and is represented by pinyin, and is followed by his customary variable in front and type.


Now Xiao Ming wants to be familiar with C + + you help him write a program, will use his design algorithm descriptive narrative rules written algorithm. Translated into C + + source code. The input file name extension is. Ming. The output file name extension is. cpp, such as the following example: Xiaoming algorithm (test.ming):
Chengxu1
Kaishi
I,j zhengxing;
K shixing;
i=1;j=0;
Shuru K;
Xunhuan i<10
Kaishi
J=j+i;
i++;
Jieshu
Ruguo j>10
Kaishi
k=j*1.0/i;
Jieshu
Shuchu k,j;
Jieshu translated C + + source code (TEST.CPP):
#include <iostream>
using namespace Std;
int main ()
{
int i,j;
float k;
i=1;j=0;
cin>>k;
while (I&LT;10)
{
J=j+i;
i++;
}
if (j>10)
k=j*1.0/i;
cout<<k<<j;
return 0;
}Reference Code:

#include <iostream> #include <string> #include <fstream> #define Max_size 30#define maxnum 9using namespace std;string ming[maxnum]={"CHENGXU1",//"Kaishi", "Jieshu", "Xunhuan", "Shuru", "Shuchu", "Ruguo", "zhengxing" , "shixing"};string cpp[maxnum]={"#include <iostream>\n#include <string>\nusing namespace Std;\nvoid main () "," {","} "," while ("," cin>> "," cout<< "," if "," int "," float "};void getnextval (String &line,int Nextval[]//to the pattern string next[] Value {int j=0,k=-1;nextval[0]=-1;while (j<line.length ()) {if (K==-1 | | line[j]==line[k])// K is 1 or character typeface {j++;k++;if (line[j]!=line[k]) nextval[j]=k;elsenextval[j]=nextval[k];} else k=nextval[k];}} int KMP (string &substr,string &line)//KMP algorithm implementation {int sl=substr.length (); int ll=line.length (); int Nextval[max_ size],i=0,j=0; Getnextval (Line,nextval); while (i< sl&& j<ll) {if (J==-1 | | substr[i]==line[j]) {i++;j++;} else {j=nextval[j];} Sl=substr.length (); Ll=line.length ();} if (J>=line.length ()) return (I-line.length ());//Returns the matching pattern stringThe first word Poute else return-1;//returns a mismatch flag}void Read (file *pf) {File *PF2 = fopen ("Test.txt", "w"), if (PF2 = = NULL) {printf ("File Write Failed!\n" ); Fclose (PF); Exit (0); } char ch;  while (!feof (PF)) {ch = fgetc (PF); Putchar (ch); FPUTC (ch, PF2); } fclose (PF2); Fclose (PF); cout<< "File has been successfully written!

"<<endl;} int main () {Ifstream in ("Test.ming"), string line;int next[max_size];ofstream output ("Test.cpp"),//while (Getline (in, Line))//pass: file. Each line {bool Fenhao;if (line[line.length () -1]== '; ') Fenhao=true;else fenhao=false;bool go=false;for (int m=0;m<9;m++) {int i;int x=kmp (line,ming[m]); if (x!=-1) {go=true ; if (m==0| | m==1)//chengxu1{for (i=0;i<x;i++)//kongge{output<<line[i];} OUTPUT&LT;&LT;CPP[M];} /*else if (m==1)//kaishi{output<<cpp[1];} */else if (m==3)//xunhuan{for (i=0;i<x;i++)//kongge{output<<line[i];} Output<<cpp[3];for (I=x+7;i<line.length (); i++) {output<<line[i];} output<< ")";} else if (m==2)//jieshu{for (i=0;i<x;i++) {output<<line[i];} OUTPUT&LT;&LT;CPP[2];} else if (m==4)//shuru{for (i=0;i<x;i++)//kongge{output<<line[i];} Output<<cpp[4];for (I=x+5;i<line.length (); i++) {if (line[i]== ', ') output<< ">>"; else if (Line[i] ==';') Break;else output<<line[i];}} else if (m==5)//shuchu{for (i=0;i<x;i++)//kongge{output<<lIne[i];} output<<cpp[5];for (int i=x+6;i<line.length (); i++) {if (line[i]== ', ') output<< "<<"; else if (line [i]== '; ') Break;else output<<line[i];}} else if (m==6)//ruguo{for (i=0;i<x;i++) {output<<line[i];} Output<<cpp[6];for (I=x+5;i<line.length (); i++) {output<<line[i];} output<< ")";} else if (m==7)//zhengxing{for (i=0;i<x-4;i++)//kongge{output<<line[i];} Output<<cpp[7];for (i=0;i<x;i++) {output<<line[i];}} else if (m==8)//shixing{for (i=0;i<x-4;i++)//kongge{output<<line[i];} Output<<cpp[8];for (i=0;i<x;i++) {output<<line[i];}} else cout<< "file read error. "<<endl;}} if (!go) output<<line;if (fenhao&&go) output<< ";"; o Utput<<endl;} In.close (); FILE *pf= fopen ("Test.cpp", "R"); Read (PF); return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Experimental data structure--KMP algorithm test.ming

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.