标签:数据结构
#include <iostream> #include <string> #include <fstream> #define max_size 30 #define MaxNum 9 using 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[])//对模式串求next[]值 { int j=0,k=-1; nextval[0]=-1; while(j<line.length()) { if(k==-1 || line[j]==line[k])//k为-1或字符相等时 { j++; k++; if(line[j]!=line[k]) nextval[j]=k; else nextval[j]=nextval[k]; } else k=nextval[k]; } } int KMP(string &substr,string &line)//kmp算法实现 { 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());//返回匹配模式串的首字符下标 else return -1;//返回不匹配标志 } void Read(FILE *pf) { FILE *pf2 = fopen("test.txt", "w"); if(pf2 == NULL) { printf("文件写入失败!\n"); fclose(pf); exit(0); } char ch; while(!feof(pf)) { ch = fgetc(pf); putchar(ch); fputc(ch, pf2); } fclose(pf2); fclose(pf); cout<<"文件已成功写入!"<<endl; } int main() { ifstream in("test.ming"); string line; int next[max_size]; ofstream output("test.cpp");// while(getline(in,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<<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<<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<<"文件读取错误!"<<endl; } } if(!go) output<<line; if(fenhao&&go) output<<";"; output<<endl; } in.close(); FILE *pf= fopen("test.cpp", "r"); Read(pf); return 0; }
标签:数据结构
原文地址:http://blog.csdn.net/u013629228/article/details/40655427