编写一个简单登陆系统,拥有登录、注册、退出等简单的功能。
通过编写,根据程序的提示页面进行操作:
根据窗口提示,进行选择操作。
> 具体代码:#include <iostream>
using namespace std;
class CDBase
{
public:
CDBase()
{
_phead = new Node();
}
~CDBase()
{
Node *ptr = _phead;
while(ptr != NULL)
{
_phead = _phead->_pnext;
delete ptr;
ptr = _phead;
}
}
bool CheckUserAuth(char *name, char *pwd) //判断用户名、密码是否匹配
{
Node *ptr = _phead->getNext();
while(ptr != NULL)
{
if((strcmp(name,ptr->_name)==0&&strcmp(pwd,ptr->_pwd)==0))
return true;
ptr = ptr->getNext();
}
return false;
}
bool query(char *name) //名字是否匹配
{
Node *ptr = _phead->getNext();
while(ptr != NULL)
{
if(strcmp(name,ptr->_name)==0)
return true;
ptr = ptr->getNext();
}
return false;
}
void WriteUserInfo(char *name, char *pwd)
{
Node *pnode = new Node(name,pwd);
pnode->sexNext(_phead->getNext());
_phead->sexNext(pnode);
}
private:
struct Node
{
Node():_pnext(NULL){}
Node(char *n,char *p):_pnext(NULL)
{
strcpy(_name,n);
strcpy(_pwd,p);
}
char _name[20];
char _pwd[20];
Node *_pnext;
void sexNext(Node *p)
{
_pnext = p;
}
Node* getNext()
{
return _pnext;
}
};
Node *_phead;
};
class CGUI
{
public:
CGUI():_running(true){}
void Run();
void ShowMainMenu() //主页面{
cout<<"****************"<<endl;
cout<<" 1.登录 "<<endl;
cout<<" 2.注册 "<<endl;
cout<<" 3.退出 "<<endl;
cout<<"****************"<<endl;
}
void DoLogin() //登陆页面
{
cout<<"请输入登录名:";
gets(_name);
cout<<"请输入密码:";
gets(_pwd);
if(_db.CheckUserAuth(_name,_pwd))
{
cout<<"登陆成功"<<endl;
}
else
{
cout<<"用户名或密码错误!"<<endl;
}
}void DoRegister() //注册页面
{
cout<<"欢迎注册"endl;
cout<<"请输入注册名称"<<endl;
gets(_name);
cout<<"请输入密码:"<<endl;
gets(_pwd);
if(_db.query(_name))
{
cout<<"该用户已经注册过,请重新填写"<<endl;
}
else
{
_db.WriteUserInfo(_name,_pwd);
cout<<"用户名"<<_name<<"注册成功"<<endl;
}
}
void DoExit() //退出页面
{
exit(0);
}
private:
char _name[20];
char _pwd[20];
bool _running;
CDBase _db;
};
typedef void (CGUI::*PFUNC)();
typedef struct _TableItem
{
int choice;
PFUNC pfunc;
}TableItem;
TableItem gTable[] =
{
{1,&CGUI::DoLogin},
{2,&CGUI::DoRegister},
{3,&CGUI::DoExit}
};
int gTablelen = sizeof(gTable)/sizeof(gTable[0]);
void CGUI::Run()
{
int chioce;
while(_running)
{
ShowMainMenu();
cout<<"请选择:"endl;
cin>>chioce;
getchar();
//switch语句或for循环都可以,不过后者更适用于公司编写
/*switch(choice)
{
case 1:
Dologin();
break;
case 2:
DoRegister();
break;
case 3:
DoExit();
break;
}*/
for(int i=0;i gTablelen;i++)
{
if(chioce == gTable[i].choice)
{
(this->*gTable[i].pfunc)();
}
}
}
}
int main()
{
CGUI gui;
gui.Run();
return 0;
}
ok,第一次写,不太会,就这样吧!