价值15元的C语言项目-学生管理系统1(Login)

 /***************
收获:
1:return可跳出当前程序,break跳出当前循环
2:读入数据与设定的类型不同,需清空输入缓存区;办法参见default;
3:文件遍历操作:fopen_s,while((!feof(pFile)){fread},fclose;
4:写文件:fwrite;
5:sleep()暂停函数,头文件为Windows.h
6:光标位置函数;
7:字符串不能直接赋值与比较,需strcpy_s,strcmp;
*******************/

#include "login.h"
void Manage(void);

int main(void) {
	if(Login()){
		system("cls");
		Manage();
	}
	return 0;
}
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void GoToXY(short x, short y);
void LoginMenuUI(void);
bool Login(void);

void CountDown(int Ihang, int Ilie, int ITime);
void QuitLoginUI(void);
void VisiterLoginUI(void);
void LoginUI(void);
void LoginFailUI(void);
void LoginSuccessUI(void);

void RegisterUI(void);
void RegisterFailUI(void);
void RegisterSuccessUI(void);
struct User
{
	char name[20];
	char key[20];
};

void Saveuser(struct User* user);

bool IsTrue(struct User* user);
bool IsUserExist(struct User* user);
void OrderErrorUI(void);
#include"login.h"
struct User user = {"游客"};//不能放在h头文件中?初始化最好放在当前文件中
bool Login(void) 
{	
	int Order=-1;
	LoginMenuUI();
	while (1)
	{
		scanf_s("%d", &Order);
		switch (Order) 
		{
		case 1: {
			//显示登录界面
			LoginUI();
			scanf_s("%s", user.name, 20);
			GoToXY(12, 60);
			scanf_s("%s", user.key, 20);
			//判断是否成功登录
			if (IsTrue(&user)) 
			{
				LoginSuccessUI();
				return true;//成功则可跳出当前程序,否则用break跳出当前循环
			}
			else 
			{
				LoginFailUI();
				break;
			}
		}
		case 2: 
		{
			//显示注册界面
			RegisterUI();
			scanf_s("%s", user.name, 20);
			GoToXY(12, 60);
			scanf_s("%s", user.key, 20);
			//判断用户名是否存在
			if (IsUserExist(&user) == false) 
			{
				Saveuser(&user);
				RegisterSuccessUI();
				return true;
			}
			else 
			{
				RegisterFailUI();
				break;
			}
		}
		case 3:
			VisiterLoginUI();
			return true;
		case 4:
			QuitLoginUI();
			return false;
		default:
		{
			int c;
			while ((c = getchar()) != EOF && c != '\n');//不停地使用getchar()获取缓冲中字符,直到获取的c是“\n”或文件结尾符EOF为止 
			system("cls");
			LoginMenuUI();
			OrderErrorUI();//清空窗口并重新打印;解决极端输入破坏窗口问题;
			break;
		}
		}
	}
}
void QuitLoginUI(void){
	GoToXY(11, 0);
	printf("\t\t\t\t\t*     3 秒后将退出系统!!!          *\n");
	printf("\t\t\t\t\t************************************\n");
	CountDown(11, 46, 3);
}
void VisiterLoginUI(void) {
	system("cls");
	LoginMenuUI();
	GoToXY(11, 0);
	printf("\t\t\t\t\t*     3 秒后将以游客身份登陆!!!    *\n");
	printf("\t\t\t\t\t************************************\n");
	CountDown(11, 46, 3);
}
void OrderErrorUI(void) {
	GoToXY(11, 0);
	printf("\t\t\t\t\t*                                  *\n");
	printf("\t\t\t\t\t*                                  *\n");
	printf("\t\t\t\t\t************************************\n");
	printf("\t\t\t\t\t*   输入错误,请重新输入指令!!!    *\n");
	printf("\t\t\t\t\t************************************\n");
	GoToXY(9, 67);
	printf("        ");
	GoToXY(9, 67);
}
bool IsUserExist(struct User* user) {
	//打开文件
	FILE* pFile;
	fopen_s(&pFile, "user.text", "r");
	if (NULL == pFile){
		return false;
	}
	//循环读取文件
	while (!feof(pFile)){
		struct User us = { 0 };
		fread(&us, sizeof(us), 1, pFile);
		if (strcmp(us.name, user->name) == 0){
			return true;
		}
	}
	fclose(pFile);
	return false;
}
bool IsTrue(struct User* user)
{
	if (NULL == user) 
	{
		return false;
	}
	//打开文件
	FILE* pFile;
	fopen_s(&pFile, "user.text", "r");
	if (NULL == pFile) 
	{
		return false;
	}
	//循环读取文件
	while (!feof(pFile))
	{
		struct User us = { 0 };
		fread(&us, sizeof(us), 1, pFile);
		if (strcmp(us.key,user->key)==0 && strcmp(us.name, user->name) == 0) 
		{
			return true;
		}
	}
	fclose(pFile);
	return false;
}
void Saveuser(struct User* user)
{
	//打开文件
	FILE* pFile;
	fopen_s(&pFile, "user.text", "a");
	if (NULL == pFile) 
	{
		return;
	}
	//写文件
	fwrite(user, sizeof(struct User), 1, pFile);
	//关闭文件
	fclose(pFile);
}
void CountDown(int Ihang, int Ilie, int ITime) {
	for (int i = ITime; i >= 0; i--) {
		GoToXY(Ihang, Ilie);
		printf("%d", i);
		Sleep(1000);   //暂停1秒
	}
}
void LoginUI(void) {
	GoToXY(11, 0);
	printf("\t\t\t\t\t*          登录名 :                *\n");
	printf("\t\t\t\t\t*        登录密码 :                *\n");
	printf("\t\t\t\t\t************************************\n");
	GoToXY(11, 60);
}
void LoginFailUI(void) {
	GoToXY(14, 0);
	printf("\t\t\t\t\t*   登录失败,请重新输入指令!!!    *\n");
	printf("\t\t\t\t\t************************************\n");
	GoToXY(9, 67);
	printf(" ");
	GoToXY(9, 67);
}
void LoginSuccessUI(void) {
	GoToXY(14, 0);
	printf("\t\t\t\t\t*    登录成功, 秒后直接登陆!!!   *\n");
	printf("\t\t\t\t\t************************************\n");
	CountDown(14,55,3);
}
void RegisterUI(void) {
	GoToXY(11, 0);
	printf("\t\t\t\t\t*          注册名 :                *\n");
	printf("\t\t\t\t\t*        注册密码 :                *\n");
	printf("\t\t\t\t\t************************************\n");
	GoToXY(11, 60);
}
void RegisterFailUI(void) {
	GoToXY(14, 0);
	printf("\t\t\t\t\t*   注册失败,请重新输入指令!!!    *\n");
	printf("\t\t\t\t\t************************************\n");
	GoToXY(9, 67);
	printf(" ");
	GoToXY(9, 67);
}
void RegisterSuccessUI(void) {
	GoToXY(14, 0);
	printf("\t\t\t\t\t*    注册成功, 秒后直接登陆!!!   *\n");
	printf("\t\t\t\t\t************************************\n");
	CountDown(14, 55, 3);
}
void LoginMenuUI(void){
	GoToXY(2,0);//起始行输出
	printf("\t\t\t\t\t************************************\n");
	printf("\t\t\t\t\t*          欢迎使用本系统          *\n");
	printf("\t\t\t\t\t*   (相关操作请直接输入对应指令)   *\n");
	printf("\t\t\t\t\t*           1: 用户登录            *\n");
	printf("\t\t\t\t\t*           2: 账号注册            *\n");
	printf("\t\t\t\t\t*           3: 游客登录            *\n");
	printf("\t\t\t\t\t*           4: 退出系统            *\n");
	printf("\t\t\t\t\t*     请输入指令(1/2/3/4):         *\n");
	printf("\t\t\t\t\t************************************\n");
	GoToXY(9, 67);//光标跳到执行输入位置
}
//设置光标位置;
void GoToXY(short x, short y)
{
	COORD cd = { y,x };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);//windows自带的光标函数(自带获取控制台的id,
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值