标签:
如今小明想请熟悉C++的你帮他写一个程序,将用他设计的算法描写叙述规则写成的算法。翻译成C++源代码。输入文件扩展名为.ming。输出文件扩展名为.cpp,例如以下例:
#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://www.cnblogs.com/lcchuguo/p/4885067.html