📄 userentry.cpp
字号:
#include "main.h"
/*****************************************************************************
* 函数名:SetCUserName
* 参数: 无
* 形参: 无
* 返回值: void
* 功能:
* 设置用户名
* 作者: 安财君
* 编写明细:
* 完成时间 Created 作者名 安财君
* 修改时间 Modify 修改者 修改位置或修改明细
*******************************************************************************/
void CUserEntry::SetCUserName()
{
COORD CurPos;
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);//获得输出句柄
CurPos.X = 33;
CurPos.Y = 17;
SetConsoleCursorPosition(hCon, CurPos);//设置光标位置
for (int i=0; ; ++i)
{
char chText = 0;
while ((chText<'A' || chText>'Z') && (chText<'a' || chText >'z') && chText!=0x0008 && chText!=0x000D)
{
chText = getch();
}
if (chText == 0x0008)//判断是否为退格
{
if (i > 0)
{
cout<<"\b \b";
--i;
}
--i;
continue;
}
if (chText == 0x000D)//判断是否为回车
{
if (i == 0)
{
--i;
continue;
}
else
{
m_strCUserName[i] = '\0';
break;
}
}
if (i < 15) //判断是否长度符合
{
cout<<chText;
m_strCUserName[i] = chText;
}
else
{
--i;
}
}
}
/******************************************************************************************
* 函数名:SetCUserPassWord
* 参数: 无
* 形参: 无
* 返回值: void
* 功能:
* 设置用户名
* 作者: 安财君
* 编写明细:
* 完成时间 Created 作者名 安财君
* 修改时间 Modify 修改者 修改位置或修改明细
********************************************************************************************/
void CUserEntry::SetCUserPassWord()
{
COORD CurPos;
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);//获得输出句柄
CurPos.X = 38;
CurPos.Y = 20;
SetConsoleCursorPosition(hCon, CurPos);//设置光标位置
fflush(stdin);
for (int i=0; i<6; ++i)
{
char chText = '\0';
while ((chText<'0' || chText>'9') && chText != 0x0008)
{
chText = getch();
}
if (chText == 0x0008) //判断是否为退格
{
if (i > 0)
{
cout<<"\b \b";
--i;
}
--i;
continue;
}
if (i < 7) //判断是否长度符合
{
cout << "*";
m_strCUserPassWord[i] = chText;
}
else
{
--i;
}
}
m_strCUserPassWord[6] = '\0';
}
/**********************************************************************************************
* 函数名:ReadCUserInfo
* 参数: 无
* 形参: 无
* 返回值: bool
* 功能:
* 判断用户名和密码是否合法,合法侧返回true,
* 不合法侧返回false
* 作者: 安财君
* 编写明细:
* 完成时间 Created 作者名 安财君
* 修改时间 Modify 修改者 修改位置或修改明细
************************************************************************************************/
bool CUserEntry::ReadCUserInfo()
{
ifstream m_CReadFileCUserInfo;
m_CReadFileCUserInfo.open(READUSER,ios::in|ios::binary);
if (m_CReadFileCUserInfo.fail())
{
CShow* pTempCShow = new CShow;
pTempCShow->ShowFalse();
delete pTempCShow;
exit(-1);
}
while (!m_CReadFileCUserInfo.eof())
{
char strBuf[200];
memset(strBuf,'\0',200);
m_CReadFileCUserInfo.getline(strBuf,200);
m_CReadFileCUserInfo.seekg(10,ios::cur);
m_CReadFileCUserInfo.getline(strBuf,200);
int nCntName=strlen(strBuf)-1;
strBuf[nCntName]='\0';
int nCmpName=strcmp(strBuf,m_strCUserName);
m_CReadFileCUserInfo.seekg(14,ios::cur);
memset(strBuf,'\0',200);
m_CReadFileCUserInfo.getline(strBuf,200);
int nCntPw;
if (m_CReadFileCUserInfo.eof())
{
nCntPw=strlen(strBuf);
strBuf[nCntPw]='\0';
int nCmpPw=strcmp(strBuf,m_strCUserPassWord);
if (nCmpPw!=0||nCmpName!=0)
{
continue;
}
else
{
m_CReadFileCUserInfo.close();
return true;
}
}
else
{
nCntPw=strlen(strBuf)-1;
strBuf[nCntPw]='\0';
int nCmpPw=strcmp(strBuf,m_strCUserPassWord);
if (nCmpPw!=0||nCmpName!=0)
{
continue;
}
else
{
m_CReadFileCUserInfo.close();
return true;
}
}
}
m_CReadFileCUserInfo.close();
return false;
}
/**********************************************************************************************
* 函数名:Land
* 参数: 无
* 形参: 无
* 返回值: int
* 功能:
* 判断用户名和密码是老师还是学生,是老师则返回1,
* 是学生则返回0,既不是老师也不是学生就返回-1;
* 作者: 安财君
* 编写明细:
* 完成时间 Created 作者名 安财君
* 修改时间 Modify 修改者 修改位置或修改明细
************************************************************************************************/
int CUserEntry::Land()
{
CShow* pchCShow;
pchCShow = new CShow;
pchCShow->ShowWelcome();
pchCShow->ShowNotice();
for (int i=0; i<3; ++i)
{
pchCShow->ShowEnterMenu();
SetCUserName();
SetCUserPassWord();
if (ReadCUserInfo())
{
char szName[g_nCUserNameSize] = "";
ifstream RUserName(WRITEUSER, ios::in|ios::binary);
RUserName >> szName;
RUserName.close(); //将用户名读出
if (strcmp(m_strCUserName, "Administrator"))//若不是老师登录(即用户为学生)
{
if (!strcmp(szName, m_strCUserName))
{
pchCShow->ShowSecFail();
exit(-1);
}
ofstream WUserName(WRITEUSER, ios::out|ios::binary);
WUserName << m_strCUserName << " ";
WUserName.close(); //将用户名写入
delete pchCShow;
return 0;
}
else
{
delete pchCShow;
return 1;
}
}
if (i<=1)
{
pchCShow->ShowIDWrong();
}
if(i==2)
{
pchCShow->ShowFreeze();
break;
}
system("cls");
}
delete pchCShow;
return -1;
}
/**************************************************
* 函数名: GetPassWord
* 参数: 无
* 形参:
* 返回值: char*
* 功能:
* 提供接口获得用户名信息
* 作者: 安财君
* 编写明细:
* 完成时间 Created 作者名 安财君
* 修改时间 Modify 修改者 修改位置或修改明细
**************************************************/
char *CUserEntry::GetCUserName()
{
return m_strCUserName;
}
/**************************************************
* 函数名: GetPassWord
* 参数: 无
* 形参:
* 返回值: char*
* 功能:
* 提供接口获得密码信息
* 作者: 安财君
* 编写明细:
* 完成时间 Created 作者名 安财君
* 修改时间 Modify 修改者 修改位置或修改明细
**************************************************/
char *CUserEntry::GetCUserPassWord()
{
return m_strCUserPassWord;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -