300+代碼的鏈式結構用戶管理系統,同步文件操作,C語言基礎案例

鏈式結構設計

300+代碼的鏈式結構用戶管理系統,同步文件操作,C語言基礎案例

項目模塊設計

300+代碼的鏈式結構用戶管理系統,同步文件操作,C語言基礎案例

singleList.h

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

//用戶的信息

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;

}


分享到:


相關文章: