main.c文件
/*·1. 界面
2. 数据结构的设计
//需求和数据结构设计糅合在一起
3. 交互 */
#include "Mylist.h"
void menu();/*数据的设计----->学生信息抽象出来,菜单设计*/
struct Node *list;
void keyDown();/*用户的交互:根据所选菜单项,执行问题*/
int main(int argc, char *argv[]) {
list = createList();
readInfoFromFile(list, "1.txt");
while(1){
menu();
keyDown();
system("pause");
system("cls");
}
// 测试链表代码
// struct Node*list = creatList();
// insertNodeByHead(list, 1);
// insertNodeByHead(list, 3);
// insertNodeByHead(list, 2);
// insertNodeByHead(list, 4);
// printList(list);
// printf("删除指定位置:\n");
// deleteAppoinNode(list, 3);
// printList(list);
// printf("链表的查找:\n");
// printf("%d\n", searchInfoByData(list, 2)->data);
system("pause");
return 0;
}
void menu()
{
/*所有操作都同步到文件*/
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");
}
void keyDown()
{
int choice = 0;
struct student data;
struct Node *pMove = NULL;
scanf("%d", &choice);
switch(choice){
case 0:
printf("正常退出\n");
system("pause");
exit(0);
break;
case 1:
printf("----------------【录入信息】----------------\n");
/*插入链表*/
printf("请输入学生姓名,年龄,性别,电话:");
fflush(stdin);/*清空缓冲区*/
scanf("%s%d%s%s",data.name, &data.age, data.sex, data.tel ) ;
insertNodeByHead(list, data);
break;
case 2:
printf("----------------【浏览信息】-------------------\n");
printList(list);/*打印链表*/
break;
case 3:
printf("----------------【修改信息】-------------------\n");
printf("请输入需要修改的学生姓名:");
scanf("%s", data.name);
modification(list, data.name);
break;
case 4:
printf("----------------【删除信息】-------------------\n");
printf("请输入删除的学生姓名:");
scanf("%s", data.name);
deleteAppoinNode(list, data.name);
break;
case 5:
printf("----------------【查找信息】-------------------\n");
printf("请输入要查找学生的姓名:");
scanf("%s", data.name);
pMove = searchInfoByData(list, data.name);
if((pMove) == NULL){
printf("未找到相关信息!\n");
system("pause");
}
else{
printf("姓名\t年龄\t性别\t电话\n");
printf("%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.age, pMove->data.sex, pMove->data.tel );
}
break;
default:
printf("选择错误,重新输入\n");
system("pause");/*防止闪屏*/
break;
}
writeInfoToFile(list, "1.txt");
}
Mylist.h文件
#include <stdio.h>
#include <stdlib.h> /*防止闪屏*/
#include <string.h>
struct student{
char name[50];
int age;
char sex[5];
char tel[20];
};
//结构分结构去写
//某一种数据结构趋势线什么东西的时候,单独去写这一种数据结构
//先把数据写对了再说
struct Node{
/*int data*/
struct student data;
struct Node*next;
};
/*创建表*/
struct Node *createList(){
/*用结构体变量表示表头*/
/*指针--->变量 动态内存申请*/
struct Node *headNode = (struct Node *)malloc(sizeof(struct Node));
/*表头:做差异化处理 数据域data 不做初始化*/
headNode->next = NULL;
return headNode;
}
/*创建节点*/
struct Node* createNode(struct student data){
struct Node*newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
/*插入节点*/
void insertNodeByHead(struct Node*headNode, struct student data){
struct Node*newNode = createNode(data);
/*表头法插入*/
newNode->next = headNode->next;
headNode->next = newNode;
}
/*删除链表*/
void deleteAppoinNode(struct Node*headNode, char*name){
//struct student
struct Node*posNode = headNode->next;
struct Node*posFrontNode = headNode;
if(posNode == NULL){
printf("数据为空,无法删除!\n");
return;
}
/*姓名是字符串,字符串:strcmp */
while(strcmp(posNode->data.name, name)){
posFrontNode = posNode;
posNode = posFrontNode->next;
if(posNode ==NULL){
printf("未找到指定位置无法删除!\n");
return ;
}
}
/*找到了*/
posFrontNode->next = posNode->next;
free(posNode);
}
/*链表的存储:文件读操作*/
void readInfoFromFile(struct Node*headNode, char *fileName){
/* 1. 打开*/
FILE *fp;
struct student data;/*先把文件读到data中去,在读到链表中去*/
fp = fopen(fileName, "r");/*这个方式是不会创造文件的*/
if(fp == NULL){
fp = fopen(fileName, "w+");/*这个方式能创造文件的*/
}
/*2. 读文件*/
/*先把文件读到data中去,在读到链表中去*/
/*将文件当作输入设备*/
while(fscanf(fp, "%s\t%d\t%s\t%s\n", data.name, &data.age, data.sex, data.tel) != EOF){
/*制表符\t*/ /*在读到链表里去*/
insertNodeByHead(headNode, data);
}
/* 3.关闭文件*/
fclose(fp);
}
/*链表的读取:文件写操作*/
void writeInfoToFile(struct Node*headNode, char*fileName){
FILE *fp;
fp = fopen(fileName, "w");/*已清空的方式*/ /*w+是追加的方式*/
if(fp == NULL){
printf("文件打开失败!");
return ;
}
struct Node *pMove = headNode->next;
while(pMove){
fprintf(fp, "%s\t%d\t%s\t%s\n", pMove->data.name, pMove->data.age, pMove->data.sex, pMove->data.tel);
pMove = pMove->next;
}
fclose(fp);
}
/*修改功能*/ /*方法一*/
struct Node *modification (struct Node* headNode, char* name){
struct student data;
deleteAppoinNode(headNode, name);
printf("请重新输入学生姓名,年龄,性别,电话:");
fflush(stdin);/*清空缓冲区*/
scanf("%s%d%s%s",data.name, &data.age, data.sex, data.tel ) ;
insertNodeByHead(headNode, data);
}
/*查找功能*/
struct Node *searchInfoByData(struct Node* headNode, char *name){
struct Node *pMove = headNode->next;
while(strcmp(pMove->data.name, name)){
pMove = pMove->next;
if(pMove == NULL){
return NULL;
}
}
return pMove;
}
/*打印链表*/
void printList(struct Node*headNode){
struct Node*pMove = headNode->next;
/*设计到数据的处理*/
printf("姓名\t\t年龄\t性别\t电话\n");
while(pMove){
printf("%s\t\t%d\t%s\t%s\n", pMove->data.name, pMove->data.age, pMove->data.sex, pMove->data.tel );
pMove = pMove->next;
}
printf("\n");
}