链式结构设计
300+代码的链式结构用户管理系统,同步文件操作,C语言基础案例-1.jpg (60.6 KB, 下载次数: 0)
2018-10-25 03:17 上传
项目模块设计
300+代码的链式结构用户管理系统,同步文件操作,C语言基础案例-2.jpg (41.49 KB, 下载次数: 0)
2018-10-25 03:17 上传
singleList.h
#include
#include
#include
#include
//用户的信息
struct userInfo{
char name[20];
char password[7];
int pession;//权限
};
typedef struct Node
{
struct userInfo data;
struct Node* next;
}LIST,*LPLIST;
/*
LIST :struct Node;
LPLIST: struct Node*
*/
//<1>.建立链表
LPLIST createList()
{
//1.指针初始化:分派堆内存初始化
LPLIST headNode = (LPLIST)malloc(sizeof(LIST));
//2.为指针表达式结构体变量初始化
headNode->next = NULL;
return headNode;
}
LPLIST createNode(struct userInfo data)
{
LPLIST newNode = (LPLIST)malloc(sizeof(LIST));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//<2>.插入数据功能
void insertNodeByHead(LPLIST list, struct userInfo data)
{
//头插法
LPLIST newNode = createNode(data);
newNode->next = list->next;
list->next = newNode;
}
//<3>.指定位置删除数据
void deleteNodeByAppoin(LPLIST list,char *name)
{
LPLIST frontPosNode = list;
LPLIST posNode = list->next;
if (posNode == NULL)
{
printf("无相关信息,无法删除\n");
system("pause");
return;
}
else
{
while (strcmp(posNode->data.name,name))
{
frontPosNode = posNode;
posNode = frontPosNode->next;
if (posNode == NULL)
{
printf("无相关信息,无法删除\n");
system("pause");
return;
}
}
frontPosNode->next = posNode->next;
free(posNode);
}
}
//<5>.查找
LPLIST searchNodeByAppoin(LPLIST list, char *name)
{
LPLIST posNode = list->next;
if (posNode == NULL)
{
printf("无相关信息,无法删除\n");
}
else
{
while (strcmp(posNode->data.name, name))
{
posNode = posNode->next;
if (posNode == NULL)
{
break;
}
}
}
return posNode;
}
LPLIST ModifyByAppoin(LPLIST list, char *name,char *password)
{
LPLIST posNode = list->next;
if (posNode == NULL)
{
printf("无相关信息,无法删除\n");
}
else
{
while (strcmp(posNode->data.name, name))
{
posNode = posNode->next;
if (posNode == NULL)
{
break;
}
}
strcpy(posNode->data.password, password);
}
return posNode;
}
//<4>.打印数据
void printList(LPLIST list)
{
LPLIST pMove = list->next;
printf("name\t\tpassword\tpession\n");
while (pMove)
{
printf("%s\t\t%s\t\t%d\n", pMove->data.name, pMove->data.password, pMove->data.pession);
pMove = pMove->next;
}
printf("\n");
}
//把链表信息存储到文件
void saveInfoToFile(LPLIST list, char * fileName, char *mode)
{
FILE *fp = fopen(fileName, mode);
LPLIST pMove = list->next;
while (pMove)
{
fprintf(fp, "%s\t\t%s\t\t%d\n", pMove->data.name, pMove->data.password, pMove->data.pession);
pMove = pMove->next;
}
fclose(fp);
}
system.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "singleList.h"
//<1>.界面
void menu()
{
printf("--------------------------------\n");
printf("\t\t0.退出\n");
printf("\t\t1.用户登岸\n");
printf("--------------------------------\n");
}
void systemMenu()
{
printf("----------【管理员操作界面】-------\n");
printf("\t\t0.退出\n");
printf("\t\t1.修改用户密码\n");
printf("\t\t2.删除用户\n");
printf("\t\t3.添加用户\n");
printf("\t\t4.浏览用户\n");
printf("--------------------------------\n");
}
void systemKeyDown()
{
int choice = 0;
scanf("%d", &choice);
//-----------------------------------------------------------------
LPLIST list = createList();
struct userInfo userTemp;
FILE *fp = fopen("userData.txt", "r");
while (fscanf(fp, "%s\t%s\t%d\n", userTemp.name, userTemp.password, &userTemp.pession) != EOF)
{
insertNodeByHead(list, userTemp);
}
fclose(fp);
//-----------------------------------------------------------------
system("cls");
switch (choice)
{
case 0:
printf("正常退出\n");
system("pause");
exit(0);
break;
case 1:
//修改用户密码
printf("----------【修改】----------\n");
printf("用户名:");
setbuf(stdin, NULL);
gets(userTemp.name);
if (searchNodeByAppoin(list, userTemp.name) == NULL)
{
printf("未找到相关信息!\n");
system("pause");
return;
}
else
{
printf("请输入新密码:");
setbuf(stdin, NULL);
gets(userTemp.password);
ModifyByAppoin(list, userTemp.name,userTemp.password);
}
break;
case 2:
//删除用户
printf("----------【添加用户】----------\n");
printf("输入要删除的用户名:");
setbuf(stdin, NULL);
gets(userTemp.name);
deleteNodeByAppoin(list, userTemp.name);
break;
case 3:
//添加用户
printf("----------【添加用户】----------\n");
printf("用户名:");
setbuf(stdin,NULL);
gets(userTemp.name);
printf("密码:");
setbuf(stdin, NULL);
gets(userTemp.password);
userTemp.pession = 2;
insertNodeByHead(list, userTemp);
break;
case 4:
printf("----------【浏览账户信息】----------\n");
printList(list);
break;
default:
printf("输入毛病!");
break;
}
saveInfoToFile(list, "userData.txt", "w");
}
//输入账户信息
void inputUserInfo()
{
char username[20];
char password[7];
system("cls");
printf("----------【用户登岸模块】----------\n");
printf("\t\t用户名:");
setbuf(stdin, NULL);
gets(username);
int ch = 0;
int iCount = 0;
printf("\t\t密 码:");
setbuf(stdin, NULL);
while ((ch = _getch()) != '\r')
{
password[iCount++] = ch;
putchar('*');
if (iCount == 7)
{
printf("密码过长!");
return;
}
}
password[iCount] = '\0';
printf("\n");
struct userInfo userTemp;
LPLIST list = createList();
FILE *fp = fopen("userData.txt", "r");
while (fscanf(fp, "%s\t%s\t%d\n", userTemp.name, userTemp.password, &userTemp.pession) != EOF)
{
insertNodeByHead(list, userTemp);
}
fclose(fp);
LPLIST searchResult = searchNodeByAppoin(list, username);
if (searchResult == NULL)
{
printf("用户名不存在!");
return;
}
else if (!strcmp(password, searchResult->data.password) && searchResult->data.pession == 1)
{
while (1)
{
system("cls");
systemMenu();
systemKeyDown();
system("pause");
}
}
else
{
printf("密码毛病,无法登岸\n");
return;
}
}
//<2>.按键措置
void keyDown()
{
//可视化:鼠标措置
int choice=0;
scanf("%d", &choice);
switch (choice)
{
case 0:
printf("正常退出\n");
system("pause");
exit(0);
break;
case 1:
inputUserInfo();
break;
default:
printf("输入毛病,请重新输入\n");
break;
}
}
int main()
{
//测试链表是否成功
//LPLIST list = createList();
//insertNodeByHead(list, 1);
//insertNodeByHead(list, 2);
//insertNodeByHead(list, 3);
//printList(list);
//deleteNodeByAppoin(list, 2);
//printList(list);
while (1)
{
menu();
keyDown();
system("pause");
system("cls");
}
system("pause");
return 0;
}
更多内容回复查看:
游客,如果您要查看本帖隐藏内容请回复