用c++编写一个简单的登录系统-笔记

原文:
 
修正了下。
---------------------------------------------------
CGUI.ccp:
 
  1 #include<iostream>
  2 #include <cstdio>
  3 #include "cdbase.h"
  4 using namespace std;
  5 class CGUI
  6 {
  7 public:
  8  CGUI() :_running(true) {}
  9  void run();
 10  void showmainmenu()       //主页面
 11  {
 12   cout << "****************" << endl;
 13   cout << "    1.登录      " << endl;
 14   cout << "    2.注册      " << endl;
 15   cout << "    3.退出      " << endl;
 16   cout << "****************" << endl;
 17  }
 18  void dologin()           //登陆页面
 19  {
 20   cout << "请输入登录名:" << endl;
 21   gets_s(_name);
 22   cout << "请输入密码:" << endl;
 23   gets_s(_pwd);
 24   if (_db.CheckUserAuth(_name, _pwd))
 25   {
 26    cout << "登陆成功" << endl;
 27   }
 28   else
 29   {
 30    cout << "用户名或密码错误!" << endl;
 31   }
 32  }
 33  void doregister()            //注册页面 
 34  {
 35   cout << "欢迎注册" << endl;
 36   cout << "请输入注册名称" << endl;
 37   gets_s(_name);
 38   cout << "请输入密码:" << endl;
 39   gets_s(_pwd);
 40   if (_db.query(_name))
 41   {
 42    cout << "该用户已经注册过,请重新填写" << endl;
 43   }
 44   else
 45   {
 46    _db.WriteUserInfo(_name, _pwd);
 47    cout << "用户名" << _name << "注册成功" << endl;
 48   }
 49  }
 50  void doexit()                   //退出页面
 51  {
 52   exit(0);
 53  }
 54 private:
 55  char _name[20];
 56  char _pwd[20];
 57  bool _running;
 58  CDBase _db;
 59 };
 60 typedef void (CGUI::* pfunc)();
 61 typedef struct _tableitem
 62 {
 63  int choice;
 64  pfunc pfunc;
 65 }tableitem;
 66 tableitem gtable[] =
 67 {
 68 {1,&CGUI::dologin},
 69 {2,&CGUI::doregister},
 70 {3,&CGUI::doexit}
 71 };
 72 int gtablelen = sizeof(gtable) / sizeof(gtable[0]);
 73 void CGUI::run()
 74 {
 75  int chioce;
 76  while (_running)
 77  {
 78   showmainmenu();
 79   cout << "请选择:" << endl;
 80   cin >> chioce;
 81   //getchar();
 82   //switch语句或for循环都可以,不过后者更适用于公司编写
 83   /*switch(choice)
 84   {
 85   case 1:
 86    dologin();
 87    break;
 88   case 2:
 89    doregister();
 90    break;
 91   case 3:
 92    doexit();
 93    break;
 94   }*/
 95   for (int i = 0; i<gtablelen; i++)
 96   {
 97    if (chioce == gtable[i].choice)
 98    {
 99     (this->*gtable[i].pfunc)();
100    }
101   }
102  }
103 }
104 int main()
105 {
106  CGUI gui;
107  gui.run();
108  return 0;
109 }
 
CDBase.h:
 
 1 #ifndef CIRCLE_H
 2 #define CIRCLE_H
 3 #include <iostream>
 4 using namespace std;
 5 class CDBase
 6 {
 7 public:
 8  CDBase()
 9  {
10   _phead = new Node();
11  }
12  ~CDBase()
13  {
14   Node* ptr = _phead;
15   while (ptr != NULL)
16   {
17    _phead = _phead->_pnext;
18    delete ptr;
19    ptr = _phead;
20   }
21  }
22  bool CheckUserAuth(char* name, char* pwd) //判断用户名、密码是否匹配
23  {
24   Node* ptr = _phead->getNext();
25   while (ptr != NULL)
26   {
27    if ((strcmp(name, ptr->_name) == 0 && strcmp(pwd, ptr->_pwd) == 0))
28     return true;
29    ptr = ptr->getNext();
30   }
31   return false;
32  }
33  bool query(char* name) //名字是否匹配
34  {
35   Node* ptr = _phead->getNext();
36   while (ptr != NULL)
37   {
38    if (strcmp(name, ptr->_name) == 0)
39     return true;
40    ptr = ptr->getNext();
41   }
42   return false;
43  }
44  void WriteUserInfo(char* name, char* pwd)
45  {
46   Node* pnode = new Node(name, pwd);
47   pnode->sexNext(_phead->getNext());
48   _phead->sexNext(pnode);
49  }
50 private:
51  struct Node
52  {
53   Node() :_pnext(NULL) {}
54   Node(char* n, char* p) :_pnext(NULL)
55   {
56    strcpy_s(_name, n);
57    strcpy_s(_pwd, p);
58   }
59   char _name[20];
60   char _pwd[20];
61   Node* _pnext;
62   void sexNext(Node* p)
63   {
64    _pnext = p;
65   }
66   Node* getNext()
67   {
68    return _pnext;
69   }
70  };
71  Node* _phead;
72 };
73 #endif
365个夜晚,我希望做到两天更一篇博客。加油,小白!
原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/12197000.html