字典的C++实现.docx
《字典的C++实现.docx》由会员分享,可在线阅读,更多相关《字典的C++实现.docx(16页珍藏版)》请在冰豆网上搜索。
字典的C++实现
#include
#include
usingnamespacestd;
#defineMaxCharNum256
#defineMaxPassLen64
#defineMaxFileNameLen32
#defineMaxFileNum16
//一些全局变量
structFlags{
boolNumbers;
boolSmallLetters;
boolCapitalLetters;
boolSpecialCharacters;
};
Flagsflags={false,false,false,false};//使用全局标志来简化程序实现
boolUserDefined;
charchars[MaxCharNum];//全局数组
charpass[MaxPassLen];
voidGuide();//提供选择,并设置相应标志
voidBuild();//生成字典
voidGetPass(intstrlen,intpasslen,intnow,ofstream&fout);//递归函数,生成定长密码
voidAdvanced();//提供高级功能的选择菜单
voidAddPrefix();//添加前缀
voidAddSuffix();//添加后缀
voidJoin();//合并字典
voidFilter();//过滤重复密码
voidByeBye();//退出程序运行
voidmain(){
cout<<<""
<<"Welcometo7dicV1.0"<<<""
<<"Codebychris7"<<<""
<<"Fineshedat2005-8-21"<<<""
<<"E-mail:
[email]technevol@[/email]"<<<""
<<"Blog:
"<Label:
charchoice;
cout<<<"Pleaseselectoneofthefollows:
"<<<"1.Makeadictionary"<<<"2.Advancedtools"<<<"3.Exit"<<<"Pleaseenteryourchoice(1to3):
";
do{
cin>>choice;
switch(choice){
case'1':
Guide();
Build();
break;
case'2':
Advanced();
break;
case'3':
ByeBye();
default:
cout<<"BadInput!
Tryagain:
";
}
}while(choice<'1'||choice>'3');
cout<<<"Pleaseselectoneofthefollows:
"<<<"1.Backtothetopmenu"<<<"2.Terminateme"<<<"Pleaseenteryourchoice(1to2):
";
do{
cin>>choice;
switch(choice){
case'1':
gotoLabel;
break;
case'2':
ByeBye();
default:
cout<<"BadInput!
Tryagain:
";
}
}while(choice<'1'||choice>'2');
}
voidGuide(){
cout<<<"Pleaseselectonemodel:
"<<<"1.GuideModel"<<<"2.User-definedModel"<<<"3.Exit"<<<"Pleaseenteryourchoice(1to3):
";
charchoice;
do{
cin>>choice;
switch(choice){
case'1':
cout<<<"Pleaseselectcharactersneeded:
"<<<"1.AllNumbers(0to9)"<<<"2.AllSmallLetters(atoz)"<<<"3.AllCapitalLetters(AtoZ)"<<<"4.AllPrintedSpecialCharacters"<<<"Pleaseentersomeofthenumbersabove:
";
getchar();
charnums[MaxCharNum];
inti;
boolerr;
do{
gets(nums);
err=false;
for(i=0;iif(err==true)break;
switch(nums[i]){
case'1':
flags.Numbers=true;break;
case'2':
flags.SmallLetters=true;break;
case'3':
flags.CapitalLetters=true;break;
case'4':
flags.SpecialCharacters=true;break;
case'':
break;
default:
cout<<"BadInput!
Tryagain:
";
err=true;
}
}
}while(err);
break;
case'2':
UserDefined=true;
cout<<<"Pleaseenterallthecharactersthatyouselect(lessthan"<"
<getchar();
gets(chars);
break;
case'3':
ByeBye();
default:
cout<<"BadInput!
Tryagain:
";
}
}while(choice<'1'||choice>'3');
}
voidBuild(){
intmaxpasslen;
cout<<<"Pleaseenterthemaximumlenthofthepassword:
";
cin>>maxpasslen;
intlen;
chartmp;
if(UserDefined==true){
len=strlen(chars);
for(inti=1;ifor(intj=0;j
if(chars[i]==chars[j]){
chars[i]=chars[len-1];
chars[len-1]='\0';
len--;
i--;
}
}
}
}
else{
len=0;
if(flags.Numbers==true)for(tmp='0';tmp<='9';tmp++)chars[len++]=tmp;
if(flags.SmallLetters==true)for(tmp='a';tmp<='z';tmp++)chars[len++]=tmp;
if(flags.CapitalLetters==true)for(tmp='A';tmp<='Z';tmp++)chars[len++]=tmp;
if(flags.SpecialCharacters==true){
for(tmp='';tmp<'0';tmp++)chars[len++]=tmp;
for(tmp=':
';tmp<'A';tmp++)chars[len++]=tmp;
for(tmp='[';tmp<'a';tmp++)chars[len++]=tmp;
for(tmp='{';tmp<='~';tmp++)chars[len++]=tmp;
}
chars[len]='\0';
}
ofstreamfout("7.dic");
if(!
fout){
cerr<"<ByeBye();
}
intnow=0;
for(inti=1;i<=maxpasslen;i++)
GetPass(len,i,now,fout);
fout.close();
cout<}
voidGetPass(intstrlen,intpasslen,intnow,ofstream&fout){
if(now==passlen){
for(inti=0;ifout<return;
}
for(intj=0;jpass[now]=chars[j];
GetPass(strlen,passlen,now+1,fout);
}
}
voidAdvanced(){
cout<<<"Pleaseselectoneofthefollows:
"<<<"1.Addprefixtoeachpasswordinthedictionary"<<<"2.Addsuffixtoeachpasswordinthedictionary"<<<"3.Joindictionariestogether"<<<"4.Filteraspecifieddictionary"<<<"5.Exit"<<<"Pleaseenteryourchoice(1to5):
";
charchoice;
do{
cin>>choice;
switch(choice){
case'1':
AddPrefix();
break;
case'2':
AddSuffix();
break;
case'3':
Join();
break;
case'4':
Filter();
case'5':
ByeBye();
default:
cout<<"BadInput!
Tryagain:
";
}
}while(choice<'1'||choice>'3');
}
voidAddPrefix(){
cout<<<"Makesurethedictionarytobeaddedprefixisincurrentdirectory."<<<"Pleaseenterthenameofthedictionary:
";
chardicname[MaxFileNameLen];
cin>>dicname;
ifstreamfin(dicname);
if(!
fin){
cerr<ByeBye();
}
cout<";
charprefix[MaxPassLen];
getchar();
gets(prefix);
ofstreamfout("PreAdded.dic");
if(!
fout){
cerr<ByeBye();
}
charbuffer[MaxPassLen],PreAdded[MaxPassLen];
strcpy(PreAdded,prefix);
while(!
fin.eof()&&fin.good()){
fin.getline(buffer,sizeof(buffer));
if(strlen(buffer)!
=0){
strcat(PreAdded,buffer);
fout<strcpy(PreAdded,prefix);
}
}
fin.close();
fout.close();
cout<}
voidAddSuffix(){
cout<<<"Makesurethedictionarytobeaddedsuffixisincurrentdirectory."<<<"Pleaseenterthenameofthedictionary:
";
chardicname[MaxFileNameLen];
cin>>dicname;
ifstreamfin(dicname);
if(!
fin){
cerr<ByeBye();
}
cout<";
charsuffix[MaxPassLen];
getchar();
gets(suffix);
ofstreamfout("SufAdded.dic");
if(!
fout){
cerr<ByeBye();
}
charbuffer[MaxPassLen];
while(!
fin.eof()&&fin.good()){
fin.getline(buffer,sizeof(buffer));
if(strlen(buffer)!
=0){
strcat(buffer,suffix);
fout<}
}
fin.close();
fout.close();
cout<}
voidJoin(){
cout<<<"Makesurethedictionariestobejoinedareincurrentdirectory."<<<"Pleaseenternamesofdictionaries:
";
charcmdline[MaxFileNameLen*MaxFileNum];
getchar();
gets(cmdline);
charfiles[MaxFileNameLen][MaxFileNum];
intlen=strlen(cmdline);
intname=0;
inttmp;
for(inti=0;itmp=0;
while(cmdline[i]!
=''&&cmdline[i]!
='\t'&&iif(cmdline[i]==''||cmdline[i]=='\t'){
files[name][tmp]='\0';
while(cmdline[i+1]==''||cmdline[i+1]=='\t')i++;
name++;
}
}
files[name][tmp]='\0';
ofstreamfout("Joined.dic");
charbuffer[MaxPassLen];
for(intj=0;j<=name;j++){
ifstreamfin(files[j]);
if(!
fin){
cerr<<<"Erroropeninginputfile:
"<ByeBye();
}
while(!
fin.eof()&&fin.good()){
fin.getline(buffer,sizeof(buffer));
fout<}
fin.close();
}
fout.close();
cout<}
voidFilter(){
cout<<<"Makesurethedictionarytobefilteredisincurrentdirectory."<<<"Pleaseenterthenameofthedictionary:
";
chardicname[MaxFileNameLen];
cin>>dicname;
ifstreamfin(dicname);
if(!
fin){
cerr<ByeBye();
}
ofstreamfout("Filtered.dic");
if(!
fout){
cerr<ByeBye();
}
charbuffer[MaxPassLen];
chartmp[MaxPassLen];
boolisin;
while(!
fin.eof()&&fin.good()){
fin.getline(buffer,sizeof(buffer));
ifstreamfcmp("Filtered.dic");
if(!
fcmp){
cerr<ByeBye();
}
isin=false;
while(!
fcmp.eof()&&fcmp.good()){
fcmp.getline(tmp,sizeof(tmp));
if(!
strcmp(tmp,buffer)){
isin=true;
break;
}
}
if(!
isin)fout<fcmp.close();
}
fin.close();
fout.close();
cout<}
voidByeBye(){
cout<<<"Thanksforusingthisprogram."<<<"Ifanybugisfound,pleasecontactme."<<<"Pressentertoexit.";
getchar();
getchar();
exit(0);
}