1、字典的C+实现#include #include using namespace std;#define MaxCharNum 256#define MaxPassLen 64#define MaxFileNameLen 32#define MaxFileNum 16/一些全局变量struct Flags bool Numbers; bool SmallLetters; bool CapitalLetters; bool SpecialCharacters;Flags flags=false,false,false,false; /使用全局标志来简化程序实现bool UserDefined;c
2、har charsMaxCharNum; /全局数组char passMaxPassLen;void Guide(); /提供选择,并设置相应标志void Build(); /生成字典void GetPass(int strlen,int passlen,int now,ofstream& fout); /递归函数,生成定长密码void Advanced(); /提供高级功能的选择菜单void AddPrefix(); /添加前缀void AddSuffix(); /添加后缀void Join(); /合并字典void Filter(); /过滤重复密码void ByeBye(); /退出程序
3、运行void main() coutendl Welcome to 7dic V1.0endl Code by chris7endl Fineshed at 2005-8-21endl E-mail: emailtechnevol/emailendl Blog: endl;Label: char choice; coutendl Please select one of the follows: endl 1. Make a dictionaryendl 2. Advanced toolsendl 3. Exitendlendl choice; switch(choice) case 1: G
4、uide(); Build(); break; case 2: Advanced(); break; case 3: ByeBye(); default: coutBad Input! Try again: ; while(choice3); coutendl Please select one of the follows: endl 1. Back to the top menuendl 2. Terminate meendlendl choice; switch(choice) case 1: goto Label; break; case 2: ByeBye(); default: c
5、outBad Input! Try again: ; while(choice2);void Guide() coutendl Please select one model: endl 1. Guide Modelendl 2. User-defined Modelendl 3. Exitendlendl choice; switch(choice) case 1: coutendl Please select characters needed: endl 1. All Numbers(0 to 9)endl 2. All Small Letters(a to z)endl 3. All
6、Capital Letters(A to Z)endl 4. All Printed Special Charactersendlendl Please enter some of the numbers above: ; getchar(); char numsMaxCharNum; int i; bool err; do gets(nums); err=false; for(i=0;istrlen(nums);i+) if(err=true) break; switch(numsi) case 1: flags.Numbers=true;break; case 2: flags.Small
7、Letters=true;break; case 3: flags.CapitalLetters=true;break; case 4: flags.SpecialCharacters=true;break; case : break; default: coutBad Input! Try again: ; err=true; while(err); break; case 2: UserDefined=true; coutendl Please enter all the characters that you select(less than MaxCharNum): endl; get
8、char(); gets(chars); break; case 3: ByeBye(); default: coutBad Input! Try again: ; while(choice3);void Build() int maxpasslen; coutendl maxpasslen; int len; char tmp; if(UserDefined=true) len=strlen(chars); for(int i=1;ilen;i+) for(int j=0;ji;j+) if(charsi=charsj) charsi=charslen-1; charslen-1=0; le
9、n-; i-; else len=0; if(flags.Numbers=true) for(tmp=0;tmp=9;tmp+) charslen+=tmp; if(flags.SmallLetters=true) for(tmp=a;tmp=z;tmp+) charslen+=tmp; if(flags.CapitalLetters=true) for(tmp=A;tmp=Z;tmp+) charslen+=tmp; if(flags.SpecialCharacters=true) for(tmp= ;tmp0;tmp+) charslen+=tmp; for(tmp=:;tmpA;tmp+
10、) charslen+=tmp; for(tmp=;tmpa;tmp+) charslen+=tmp; for(tmp=;tmp=;tmp+) charslen+=tmp; charslen=0; ofstream fout(7.dic); if(!fout) cerrendlCannot open output file!endl; ByeBye(); int now=0; for(int i=1;i=maxpasslen;i+) GetPass(len,i,now,fout); fout.close(); coutendlThe dictionary needed has been bul
11、it successfully.endl;void GetPass(int strlen,int passlen,int now,ofstream& fout) if(now=passlen) for(int i=0;inow;i+) foutpassi; foutendl; return; for(int j=0;jstrlen;j+) passnow=charsj; GetPass(strlen,passlen,now+1,fout); void Advanced() coutendl Please select one of the follows: endl 1. Add prefix
12、 to each password in the dictionaryendl 2. Add suffix to each password in the dictionaryendl 3. Join dictionaries togetherendl 4. Filter a specified dictionaryendl 5. Exitendlendl choice; switch(choice) case 1: AddPrefix(); break; case 2: AddSuffix(); break; case 3: Join(); break; case 4: Filter();
13、case 5: ByeBye(); default: coutBad Input! Try again: ; while(choice3);void AddPrefix() coutendl Make sure the dictionary to be added prefix is in current directory.endl dicname; ifstream fin(dicname); if(!fin) cerrendlCan not open input file.endl; ByeBye(); coutendlPlease enter the prefix needed: ;
14、char prefixMaxPassLen; getchar(); gets(prefix); ofstream fout(PreAdded.dic); if(!fout) cerrendlCan not open output file.endl; ByeBye(); char bufferMaxPassLen,PreAddedMaxPassLen; strcpy(PreAdded,prefix); while(!fin.eof()&fin.good() fin.getline(buffer,sizeof(buffer); if(strlen(buffer)!=0) strcat(PreAd
15、ded,buffer); foutPreAddedendl; strcpy(PreAdded,prefix); fin.close(); fout.close(); coutendlPrefix has been added to the specified dictionary successfully.endl;void AddSuffix() coutendl Make sure the dictionary to be added suffix is in current directory.endl dicname; ifstream fin(dicname); if(!fin) c
16、errendlCan not open input file.endl; ByeBye(); coutendlPlease enter the suffix needed: ; char suffixMaxPassLen; getchar(); gets(suffix); ofstream fout(SufAdded.dic); if(!fout) cerrendlCan not open output file.endl; ByeBye(); char bufferMaxPassLen; while(!fin.eof()&fin.good() fin.getline(buffer,sizeo
17、f(buffer); if(strlen(buffer)!=0) strcat(buffer,suffix); foutbufferendl; fin.close(); fout.close(); coutendlSuffix has been added to the specified dictionary successfully.endl;void Join() coutendl Make sure the dictionaries to be joined are in current directory.endl Please enter names of dictionaries
18、: ; char cmdlineMaxFileNameLen*MaxFileNum; getchar(); gets(cmdline); char filesMaxFileNameLenMaxFileNum; int len=strlen(cmdline); int name=0; int tmp; for(int i=0;ilen;i+) tmp=0; while(cmdlinei!= &cmdlinei!=t&ilen) filesnametmp+=cmdlinei+; if(cmdlinei= |cmdlinei=t) filesnametmp=0; while(cmdlinei+1=
19、|cmdlinei+1=t) i+; name+; filesnametmp=0; ofstream fout(Joined.dic); char bufferMaxPassLen; for(int j=0;j=name;j+) ifstream fin(filesj); if(!fin) cerrendl Error opening input file: filesj.endl; ByeBye(); while(!fin.eof()&fin.good() fin.getline(buffer,sizeof(buffer); foutbufferendl; fin.close(); fout
20、.close(); coutendlThe specified dictionaries have been joined successfully.endl;void Filter() coutendl Make sure the dictionary to be filtered is in current directory.endl dicname; ifstream fin(dicname); if(!fin) cerrendlCan not open input file.endl; ByeBye(); ofstream fout(Filtered.dic); if(!fout)
21、cerrendlCan not open output file.endl; ByeBye(); char bufferMaxPassLen; char tmpMaxPassLen; bool isin; while(!fin.eof()&fin.good() fin.getline(buffer,sizeof(buffer); ifstream fcmp(Filtered.dic); if(!fcmp) cerrendlCan not open output file.endl; ByeBye(); isin=false; while(!fcmp.eof()&fcmp.good() fcmp
22、.getline(tmp,sizeof(tmp); if(!strcmp(tmp,buffer) isin=true; break; if(!isin) foutbufferendl; fcmp.close(); fin.close(); fout.close(); coutendlThe specified dictionary has been filtered successfully.endl;void ByeBye() coutendl Thanks for using this program.endl If any bug is found, please contact me.endl Press enter to exit.; getchar(); getchar(); exit(0);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1