问题及代码1:
- /*
- *文件名称:code.cpp
- *问题描述:输入六位密码,屏幕上都出现*,例如你在取款机上输入密码时,就是******.
- *输入描述:任意六位数字。
- *程序输出:******.
- */
- // C++ 输入 密码 回显 * 的小程序段
- #include <iostream>
- #include<conio.h>
- #include<string>
- using namespace std;
- class Password//用一个类实现,其实也可以不用这么麻烦,练习一下
- {
- public:
- Password()//构造函数,这里主要用于初始化密码,使之为空
- {
- psw="";//初始化密码为空"";
- length=0; //初始化密码长度
- }
- void inputPassword()//用于输入并回显*为密码
- {
- char temp_c;
- cout<<"please input password:";
- while(true)
- {
- temp_c=getch(); //输入一个字符
- if(temp_c!=char(13)) //判断该字符是不为回车,如果是则退出while
- {
- switch (temp_c)
- {
- case 8:
- if(length!=0)
- {
- cout<<"/b /b";
- psw=psw.substr(0,length-1);
- length--;
- }
- else ;
- break;
- default:
- cout<<"*"; //可用用你喜欢的任意字符,如改为cout<<"";则无回显
- psw+=temp_c;//连成字符串;
- length++;
- break;
- }
- }
- else break;
- }
- }
- string getPassword()//返回一个密码字符串,其实可以把密码再次加密,存在数据库中。
- {
- return psw;
- }
- private:
- string psw;//用于存密码的字符串;
- int length;//密码长度
- };
- //测试
- int main()
- {
- Password psw1;
- psw1.inputPassword();
- cout<<endl<<"Your password is:"<<psw1.getPassword()<<endl;
- getch();
- return 0;
- }
运行结果:
问题及代码2:
- /*
- *文件名称:code.cpp
- *问题描述:输入六位密码,屏幕上都出现*,例如你在取款机上输入密码时,就是******.
- *输入描述:任意六位数字。
- *程序输出:******.
- */
- #include<iostream>
- #include<conio.h>
- using namespace std;
- int main()
- {
- char password[100];
- int index=0;
- while(1)
- {
- char ch;
- ch=getch();
- if(ch==8) //退格键
- {
- if(index!=0)
- {
- cout<<char(8)<<" "<<char(8);
- index--;
- }
- }
- else if(ch=='\r') //回车键
- {
- password[index]='\0';
- cout<<endl;
- break;
- }
- else
- {
- cout<<"*";
- password[index++]=ch;
- }
- }
- cout<<"password:"<<password<<endl;
- return 0;
- }
运行结果:
转载自单昕昕大神的资源,众人来看看如何破除腾讯上某些骗人的东西。
当然,这玩意我看不懂……