第十周项目1.4 密码问题

来源:互联网 发布:win10m vpn软件 编辑:程序博客网 时间:2024/05/22 20:49

问题及代码1:

[cpp] view plaincopy
  1. /*  
  2. *文件名称:code.cpp  
  3. *问题描述:输入六位密码,屏幕上都出现*,例如你在取款机上输入密码时,就是******. 
  4. *输入描述:任意六位数字。  
  5. *程序输出:******. 
  6. */   
  7. // C++ 输入 密码 回显 * 的小程序段  
  8. #include <iostream>  
  9. #include<conio.h>  
  10. #include<string>  
  11. using namespace std;  
  12. class Password//用一个类实现,其实也可以不用这么麻烦,练习一下  
  13. {  
  14.     public:  
  15.     Password()//构造函数,这里主要用于初始化密码,使之为空  
  16.     {  
  17.         psw="";//初始化密码为空"";  
  18.         length=0; //初始化密码长度  
  19.     }  
  20.     void inputPassword()//用于输入并回显*为密码  
  21.     {  
  22.         char temp_c;  
  23.         cout<<"please input password:";  
  24.         while(true)  
  25.         {  
  26.             temp_c=getch();  //输入一个字符  
  27.             if(temp_c!=char(13))  //判断该字符是不为回车,如果是则退出while  
  28.             {  
  29.                 switch  (temp_c)  
  30.                 {  
  31.                 case 8:  
  32.                     if(length!=0)  
  33.                     {  
  34.                         cout<<"/b /b";  
  35.                         psw=psw.substr(0,length-1);  
  36.                         length--;  
  37.                     }  
  38.                     else ;  
  39.                     break;  
  40.                 default:  
  41.                     cout<<"*"//可用用你喜欢的任意字符,如改为cout<<"";则无回显  
  42.                     psw+=temp_c;//连成字符串;  
  43.                     length++;  
  44.                     break;  
  45.                 }  
  46.             }  
  47.             else break;  
  48.         }  
  49.     }  
  50.     string getPassword()//返回一个密码字符串,其实可以把密码再次加密,存在数据库中。  
  51.     {  
  52.         return psw;  
  53.     }  
  54.     private:  
  55.     string psw;//用于存密码的字符串;  
  56.     int length;//密码长度  
  57. };  
  58.   
  59.   
  60. //测试  
  61. int main()  
  62. {  
  63.     Password psw1;  
  64.     psw1.inputPassword();  
  65.     cout<<endl<<"Your password is:"<<psw1.getPassword()<<endl;  
  66.     getch();  
  67.     return 0;  
  68. }  


 

运行结果:

 

问题及代码2:

[cpp] view plaincopy
  1. /*  
  2. *文件名称:code.cpp  
  3. *问题描述:输入六位密码,屏幕上都出现*,例如你在取款机上输入密码时,就是******. 
  4. *输入描述:任意六位数字。  
  5. *程序输出:******. 
  6. */   
  7. #include<iostream>  
  8. #include<conio.h>  
  9. using namespace std;  
  10. int main()  
  11. {  
  12. char password[100];  
  13. int index=0;  
  14. while(1)  
  15. {  
  16. char ch;  
  17. ch=getch();  
  18. if(ch==8) //退格键  
  19. {  
  20. if(index!=0)  
  21. {  
  22. cout<<char(8)<<" "<<char(8);  
  23. index--;  
  24. }  
  25. }  
  26. else if(ch=='\r'//回车键  
  27. {  
  28. password[index]='\0';  
  29. cout<<endl;  
  30. break;  
  31. }  
  32. else  
  33. {  
  34. cout<<"*";  
  35. password[index++]=ch;  
  36. }  
  37. }  
  38. cout<<"password:"<<password<<endl;  
  39. return 0;  
  40. }  


 

运行结果:

 

 转载自单昕昕大神的资源,众人来看看如何破除腾讯上某些骗人的东西。

当然,这玩意我看不懂……


0 0
原创粉丝点击