简单实现从控制台记录密码的类

     昨天在逛百度空间时,看到心烦意乱空间的一个c++例子,简单实现从控制台读入密码。无聊就改善之,使之满足

(1)输入密码时,以"*"回显。

(2)支持backspace。

     比较简单...。

 

  1. #include <iostream>
  2. #include <conio.h>//_getch()
  3. #include <string>
  4. #include <cctype>
  5. using namespace std;
  6. class CKeyString
  7. {
  8. private:
  9.     string m_strkey;
  10. public:
  11.     void SetKey();
  12.     bool Range();
  13.     friend istream& operator >> (istream& is,CKeyString * str);
  14.     string ToString() const ;
  15. };
  16. bool CKeyString::Range()
  17. {
  18.     string::size_type n = m_strkey.size();
  19.     return (n < 11 && n > 5);
  20. }
  21. void CKeyString::SetKey()
  22. {
  23.     int n = 0;
  24.     string tempkey;
  25.     while(1)
  26.     {
  27.         if(n)
  28.         cout<<"Input the key again :"<<endl;
  29.         else
  30.         cout<<"Input the key (6b~10b): "<<endl;
  31.         cin>>this;
  32.         n++;
  33.         cout<<endl;
  34.         if(n == 1)
  35.         {
  36.             if(m_strkey.empty())
  37.             {
  38.                 n = 0;
  39.                 cout<<"can't be empty! input again..."<<endl;
  40.             }
  41.             else 
  42.                 if(!Range())
  43.                 {
  44.                     cout<<"the key isn't content..again"<<endl;
  45.                     n = 0;
  46.                 }
  47.                 else 
  48.                     tempkey = m_strkey;
  49.             m_strkey = "";
  50.         }
  51.         else//n == 2
  52.         {
  53.             if(m_strkey == tempkey)
  54.             {
  55.                 cout<<"input succeedly..."<<endl;
  56.                 break;
  57.             }
  58.             cout<<"not the same...again"<<endl; 
  59.             n = 0;
  60.             m_strkey = "";
  61.             tempkey = "";
  62.         }
  63.     }
  64. }
  65. string CKeyString::ToString() const
  66. {
  67.     return m_strkey;
  68. }
  69. istream& operator >> (istream& is,CKeyString *str)
  70. {
  71.     char ch = '/n';
  72.     int size = 0;
  73.     while((ch = _getch()) != 13)
  74.     {
  75.         if(isalnum(ch))
  76.         {//key只可为数字和字母
  77.             str->m_strkey.push_back(ch);
  78.             cout<<"*";
  79.             size++;
  80.         }
  81.         else
  82.         if(ch == '/b')//ch == 8
  83.         {//退格键
  84.             if(str->m_strkey.empty()== false)
  85.             {
  86.                 str->m_strkey.erase(str->m_strkey.erase(str->m_strkey.end()-1));
  87.                 cout<<'/b'<<' '<<'/b';
  88.                 size--;
  89.             }
  90.         }
  91.     }
  92.     return is;
  93. }
  94. //测试
  95. int main()
  96. {
  97.     CKeyString ks;
  98.     ks.SetKey();
  99.     cout<<"the key is: "<<endl;
  100.     cout<<ks.ToString()<<endl;
  101.     return 0;
  102. }

    其中退格实现cout<<'/b'<<' '<<'/b',有点投机取巧。不过不知道如何对控制台的缓冲进行操作,就这样了。

    期待有更好的实现方式....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以为您提供一些思路和代码示例。控制台图书管理系统可以分为以下几个模块: 1. 用户登录:需要输入用户名和密码,验证通过后才能进入系统。 2. 图书管理:可以进行图书的添加、删除、修改和查询操作。 3. 借还管理:可以进行借阅和归还图书的操作,需要记录借阅日期和归还日期。 4. 统计报表:可以生成图书被借阅统计报表。 以下是一个简单的示例代码: ```C #include<stdio.h> #include<string.h> #include<stdlib.h> struct book { int book_id;//图书编号 char book_name[20];//图书名称 char publisher[20];//出版社 float price;//价格 int num;//库存数量 char author[20];//作者 }; int id=0; //图书信息数组 struct book book_list[1000]; //已借图书信息数组 struct book borrow_list[1000]; //借阅信息数组 struct { int borrow_id;//借阅编号 int book_id;//图书编号 char book_name[20];//图书名称 char borrower_name[20];//借阅人名称 char borrow_date[20];//借阅日期 char return_date[20];//归还日期 } borrow_info[1000]; int book_count=0; int borrow_count=0; int find_book_by_id(int book_id) { for(int i=0;i<book_count;i++) { if(book_list[i].book_id==book_id) return i; } return -1; } int find_borrow_by_id(int borrow_id) { for(int i=0;i<borrow_count;i++) { if(borrow_info[i].borrow_id==borrow_id) return i; } return -1; } //添加图书 void add_book() { int book_id; printf("请输入图书编号:"); scanf("%d",&book_id); getchar(); int index=find_book_by_id(book_id); if(index!=-1) { printf("该书已存在!\n"); return; } book_list[book_count].book_id=book_id; printf("请输入图书名称:"); gets(book_list[book_count].book_name); printf("请输入作者:"); gets(book_list[book_count].author); printf("请输入出版社:"); gets(book_list[book_count].publisher); printf("请输入价格:"); scanf("%f",&book_list[book_count].price); printf("请输入库存数量:"); scanf("%d",&book_list[book_count].num); book_count++; printf("添加成功!\n"); } //修改图书 void modify_book() { int book_id; printf("请输入要修改的图书编号:"); scanf("%d",&book_id); getchar(); int index=find_book_by_id(book_id); if(index==-1) { printf("该书不存在!\n"); return; } printf("请输入图书名称:"); gets(book_list[index].book_name); printf("请输入作者:"); gets(book_list[index].author); printf("请输入出版社:"); gets(book_list[index].publisher); printf("请输入价格:"); scanf("%f",&book_list[index].price); printf("请输入库存数量:"); scanf("%d",&book_list[index].num); printf("修改成功!\n"); } //删除图书 void delete_book() { int book_id; printf("请输入要删除的图书编号:"); scanf("%d",&book_id); getchar(); int index=find_book_by_id(book_id); if(index==-1) { printf("该书不存在!\n"); return; } for(int i=index;i<book_count-1;i++) { book_list[i]=book_list[i+1]; } book_count--; printf("删除成功!\n"); } //查询图书 void find_book() { int book_id; printf("请输入要查询的图书编号:"); scanf("%d",&book_id); getchar(); int index=find_book_by_id(book_id); if(index==-1) { printf("该书不存在!\n"); return; } printf("图书编号:%d,图书名称:%s,作者:%s,出版社:%s,价格:%f,库存数量:%d\n",book_list[index].book_id,book_list[index].book_name,book_list[index].author,book_list[index].publisher,book_list[index].price,book_list[index].num); } char* get_time() { time_t t = time(NULL); char* datetime = ctime(&t); datetime[strlen(datetime)-1]='\0'; return datetime; } //借阅图书 void borrow_book() { int book_id,borrow_id; printf("请输入要借阅的图书编号:"); scanf("%d",&book_id); getchar(); int index=find_book_by_id(book_id); if(index==-1) { printf("该书不存在!\n"); return; } if(book_list[index].num==0) { printf("该书无库存!\n"); return; } borrow_list[borrow_count]=book_list[index]; book_list[index].num--; borrow_id=++id; printf("请输入借阅人名称:"); gets(borrow_info[borrow_count].borrower_name); borrow_info[borrow_count].borrow_id=borrow_id; borrow_info[borrow_count].book_id=book_id; strcpy(borrow_info[borrow_count].book_name,book_list[index].book_name); strcpy(borrow_info[borrow_count].borrow_date,get_time()); borrow_count++; printf("借阅成功!\n"); } //归还图书 void return_book() { int borrow_id; printf("请输入要归还的借阅编号:"); scanf("%d",&borrow_id); getchar(); int index=find_borrow_by_id(borrow_id); if(index==-1) { printf("该借阅记录不存在!\n"); return; } for(int i=0;i<book_count;i++) { if(borrow_list[index].book_id==book_list[i].book_id) { book_list[i].num++; break; } } strcpy(borrow_info[index].return_date,get_time()); printf("归还成功!\n"); } //打印图书列表 void print_book_list(struct book list[],int count) { for(int i=0;i<count;i++) { printf("图书编号:%d,图书名称:%s,作者:%s,出版社:%s,价格:%f,库存数量:%d\n",list[i].book_id,list[i].book_name,list[i].author,list[i].publisher,list[i].price,list[i].num); } } //打印借阅列表 void print_borrow_list() { for(int i=0;i<borrow_count;i++) { printf("借阅编号:%d,借阅人:%s,图书编号:%d,图书名称:%s,借阅日期:%s,归还日期:%s\n",borrow_info[i].borrow_id,borrow_info[i].borrower_name,borrow_info[i].book_id,borrow_info[i].book_name,borrow_info[i].borrow_date,borrow_info[i].return_date); } } //生成统计报表 void generate_report() { int borrow_counts[1000]={0};//每本图书借阅次数 int book_ids[1000]={0};//所有图书编号 int book_num=0;//图书数量 for(int i=0;i<borrow_count;i++) { for(int j=0;j<book_num;j++) { if(borrow_info[i].book_id==book_ids[j]) { borrow_counts[j]++; break; } } book_ids[book_num]=borrow_info[i].book_id; borrow_counts[book_num]++; book_num++; } printf("图书编号\t图书名称\t借阅次数\n"); for(int i=0;i<book_num;i++) { int index=find_book_by_id(book_ids[i]); printf("%d\t%s\t%d\n",book_ids[i],book_list[index].book_name,borrow_counts[i]); } } int main() { int choice; do { printf("1.添加图书\n2.修改图书\n3.删除图书\n4.查询图书\n5.借阅图书\n6.归还图书\n7.打印借阅列表\n8.生成统计报表\n9.退出系统\n"); scanf("%d",&choice); getchar(); switch(choice) { case 1: add_book(); break; case 2: modify_book(); break; case 3: delete_book(); break; case 4: find_book(); break; case 5: borrow_book(); break; case 6: return_book(); break; case 7: print_borrow_list(); break; case 8: generate_report(); break; } }while(choice!=9); return 0; } ``` ### 回答2: 控制台图书管理系统是一种以文本界面为基础的图书管理软件,它在控制台窗口中提供各种操作,方便用户管理图书的借阅、归还、查询等功能。 实现控制台图书管理系统可以遵循以下步骤: 1. 设计数据库:创建图书管理系统所需的数据库表,如图书信息表、用户信息表、借阅记录表等。每个表都应具有相应的段来存储相关信息。 2. 连接数据库:使用编程语言中的数据库连接库,连接控制台图书管理系统与数据库。 3. 界面设计:在控制台中创建一个用户界面,用于显示菜单选项和接收用户输入。可以使用循环结构设计主菜单,通过选择数母来执行相应功能。 4. 功能实现:根据用户选择的菜单项,编写相应的代码实现借阅、归还、查询等功能。例如,用户选择借阅功能时,系统会要求用户输入图书编号或名称,并在数据库中进行查询和更新。 5. 错误处理:为了保证系统的稳定性和可靠性,需要添加适当的错误处理机制。例如,当用户输入错误的图书编号时,系统应给出相应的提示信息。 6. 数据库更新:在用户借阅、归还图书时,需要更新数据库中的相关信息。编写代码,将用户输入的信息存储到数据库中。 7. 用户权限管理:根据用户型,设置相应的权限。例如,管理员可以对图书信息进行增加、删除和修改,而普通用户只能进行图书的查阅和借阅。 8. 数据备份和恢复:定期进行数据库备份,以避免数据丢失。同时,为了防止系统故障或意外情况,设计恢复机制,以便在需要时快速恢复数据。 通过以上步骤的实施,可以完成一个基本的控制台图书管理系统。根据实际需求,还可以进一步进行功能扩展和优化。 ### 回答3: 可以使用C语言来实现控制台图书管理系统。 首先,需要定义一个图书的结构体,包括图书的编号、名称、作者等信息。然后,可以定义一个数组,用来存储图书的信息。 在控制台中,可以显示一个菜单,提供对图书的增加、删除、查询等功能。用户输入相应的操作编号后,系统根据用户的输入执行对应的操作。 例如,如果用户选择添加图书的功能,系统会要求用户输入图书的信息,然后将图书添加到数组中。如果用户选择查询图书的功能,系统会要求用户输入图书的编号,然后在数组中查找相应的图书信息并显示。 为了保证数据的完整性和安全性,可以添加一些验证机制。例如,在添加图书时,可以验证图书的编号是否已存在,避免重复添加;在删除图书时,可以将要删除的图书移到数组的末尾,然后将数组长度减一,保证数组中的数据连续。 此外,还可以为图书管理系统添加其他功能,如图书借阅、归还等。可以通过在图书结构体中添加相应的属性来实现这些功能。 综上所述,使用C语言可以实现一个简单控制台图书管理系统,通过菜单和用户输入的方式,操作图书信息并提供相关的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值