#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct student
{
char name[20];
char num[10];
int age;
char tel[20];
char addr[20];
};
//写数据结构
//死方法:
//1.抽象单一个体
//2.描述最初状态:初始化--->初始化变量
//3.插入,删除
//4.打印遍历
struct Node
{
struct student data;
struct Node* next;
};
struct Node* createList()
{
struct Node* listHeadNode = (struct Node*)malloc(sizeof(struct Node));
//2.初始化一个变量
listHeadNode->next = NULL;
return listHeadNode;
}
struct Node* createNode(struct student data)
{
//有表头链表:第一个结点不存储数据
//无表头链表:第一个结点存储数据
//1.产生一个结构体变量
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
//2.初始化一个变量
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//插入那个链表,插入的数据是多少 :录入信息
void insertNodeByHead(struct Node* listHeadNode, struct student data)
{
struct Node* newNode = createNode(data);
newNode->next = listHeadNode->next;
listHeadNode->next = newNode;
}
//删除
void deleteNodeByAppoinName(struct Node* listHeadNode,char *name)
{
struct Node* posFrontNode = listHeadNode;
struct Node* posNode = listHeadNode->next;
if (posNode == NULL)
{
printf("无相关内容,无法删除!\n");
return;
}
else
{
while (strcmp(posNode->data.name,name))
{
posFrontNode = posNode;
posNode = posFrontNode->next;
if (posNode == NULL)
{
printf("无相关内容,无法删除!\n");
return;
}
}
posFrontNode->next = posNode->next;
free(posNode);
}
}
struct Node* searchNodeByAppoinNum(struct Node* listHeadNode, char *num)
{
struct Node* pMove = listHeadNode->next;
if (pMove == NULL)
return pMove;
else
{
while (strcmp(pMove->data.num, num))
{
pMove = pMove->next;
if (pMove == NULL)
break;
}
return pMove;
}
}
void printNode(struct Node* curNode)
{
printf("姓名\t学号\t年龄\t电话\t\t住址\n");
printf("%s\t%s\t%d\t%s\t%s\n", curNode->data.name, curNode->data.num, curNode->data.age, curNode->data.tel, curNode->data.addr);
}
//打印:浏览信息
void printList(struct Node* listHeadNode)
{
struct Node* pMove = listHeadNode->next;
printf("姓名\t学号\t年龄\t电话\t\t住址\n");
while (pMove)
{
printf("%s\t%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.num, pMove->data.age, pMove->data.tel, pMove->data.addr);
pMove = pMove->next;
}
printf("\n");
}
void readInfoFromFile(char *fileName, struct Node* listHeadNode)
{
FILE *fp = fopen(fileName, "r");
if (fp == NULL)
{
fp = fopen(fileName, "w");
}
struct student tempData;
while (fscanf(fp, "%s\t%s\t%d\t%s\t%s\n", tempData.name, tempData.num,
&tempData.age, tempData.tel, tempData.addr) != EOF)
{
insertNodeByHead(listHeadNode, tempData);
memset(&tempData, 0, sizeof(tempData));
}
fclose(fp);
}
void saveInfoToFile(char *fileName, struct Node* listHeadNode)
{
FILE *fp = fopen(fileName, "w");
struct Node* pMove = listHeadNode->next;
while (pMove)
{
fprintf(fp,"%s\t%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.num,
pMove->data.age, pMove->data.tel, pMove->data.addr);
pMove = pMove->next;
}
fclose(fp);
}
struct Node* list = NULL;
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("\t\t5.查找信息\n");
printf("---------------------------------------------\n");
printf("请输入(0~5):");
//每一次交互,都没有告诉别人程序的这个地方需要交互
}
void keyDown()
{
int userKey;
struct student tempData;
scanf("%d", &userKey);
switch (userKey)
{
case 0:
printf("\t\t【退出系统】\n");
system("pause");
exit(0);
break;
case 1:
printf("\t\t【插入信息】\n");
printf("请输入姓名,学号,年龄,电话,住址:");
if (searchNodeByAppoinNum(list, tempData.num) == NULL)
{
scanf("%s%s%d%s%s", tempData.name, tempData.num, &tempData.age, tempData.tel, tempData.addr);
insertNodeByHead(list, tempData);
}
else
{
printf("请重新输入学号!");
}
saveInfoToFile("student.txt", list);
break;
case 2:
printf("\t\t【浏览信息】\n");
printList(list);
break;
case 3:
printf("\t\t【删除信息】\n");
printf("请输入要删除的学生姓名:");
scanf("%s", tempData.name);
deleteNodeByAppoinName(list, tempData.name);
saveInfoToFile("student.txt", list);
break;
case 4:
printf("\t\t【修改信息】\n");
printf("请输入要修改的学生的学号:");
scanf("%s", tempData.num);
if (searchNodeByAppoinNum(list, tempData.num) == NULL)
{
printf("未找到相关信息!\n");
}
else
{
struct Node* curNode = searchNodeByAppoinNum(list, tempData.num);
printf("请输入新姓名,学号,年龄,电话,住址:");
scanf("%s%s%d%s%s", curNode->data.name, curNode->data.num,
&curNode->data.age, curNode->data.tel, curNode->data.addr);
saveInfoToFile("student.txt", list);
}
break;
case 5:
printf("\t\t【查找信息】\n");
printf("请输入查找的学号:");
scanf("%s", tempData.num);
if (searchNodeByAppoinNum(list, tempData.num) == NULL)
{
printf("未找到相关信息!\n");
}
else
{
printNode(searchNodeByAppoinNum(list, tempData.num));
}
break;
default:
printf("输入错误!重新输入\n");
break;
}
}
int main()
{
list = createList();
readInfoFromFile("student.txt", list);
while (1)
{
systemMenu();
keyDown();
system("pause");
system("cls");
}
return 0;
}
C语言课程设计
最新推荐文章于 2023-12-30 22:09:08 发布