本程序若运行失败请关闭重试,若再次失败可以自行先建立一个文件夹 “D:\\000\\studentInfo.txt”
边学链表,结构体,指针边做的程序,加起来学做了48小时,所以肯定存在很多不足,这是第二版,请多包含。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//2020 12 14--8:16完工
#include<conio.h>//指针,结构,链表,文件操作,封装
#include<time.h>
#include<windows.h>
void menu(struct Node* link , struct student asd);//菜单栏
struct student;
struct Node;
struct Node* creatLink();//创建链表(表头)
struct Node* creatNode(struct student data);//创建节点
void insetNodeByHead(struct Node* headNode, struct student data);//增加
void debeateNode(struct Node* headNode, char name[20]);//删除
void searchNode(struct Node* headNode, char name[20]);//查询
void changeNode(struct Node* headNode, char name[20]);//修改
void printLink(struct Node* headNode);//打印链表
void inset(struct Node* link , struct student asd);//具体增加函数
void debeate(struct Node* link , struct student asd);//具体删除函数
void search(struct Node* link , struct student asd);//具体查询函数
void change(struct Node* link , struct student asd);//具体修改函数
void look(struct Node* link , struct student asd);//具体浏览函数
void readInfoFromFile(struct Node* headNode, const char* readFile);//读出文件
void writeInfoFromFile(struct Node* headNode, const char* writeFile);//写入文件
struct student
{
char name[20] = { '0' };
char sex[5] = { '0' };
int age = 0;
char tel[20] = { '0' };
};
void menu(struct Node* link,struct student asd)
{
int cho;
printf("\n\n\t\t============[学生信息管理系统]============\n\n");
printf("\t\t 1.增加学生信息\n\t\t 2.删除学生信息\n");
printf("\t\t 3.修改学生信息\n\t\t 4.搜索学生信息\n");
printf("\t\t 5.查看信息总表\n\t\t 6.退出管理系统\n\n");
printf("\t\t==========================================\n\n");
printf("\t\t============[请您选择服务项目]============\n\n");
printf("\t\t ");
scanf_s("%d", &cho);
switch (cho)
{
case 1:
inset(link, asd);
break;
case 2:
debeate(link, asd);
break;
case 3:
change(link, asd);
break;
case 4:
search(link, asd);
break;
case 5:
look(link, asd);
break;
case 6:
exit(0);
default:
exit(0);
}
}
struct Node
{
struct student data;
struct Node* next;
};
struct Node* creatLink()//创建链表(表头)
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
struct Node* creatNode(struct student data)//创建节点
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = NULL;
newNode->data = data;
return newNode;
}
void insetNodeByHead(struct Node* headNode, struct student data)//增加
{
struct Node* newNode = creatNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void debeateNode(struct Node* headNode,char name[20])//删除
{
struct Node* pMove = headNode->next;
struct Node* pFrontMove = headNode;
if (pMove == NULL)
{
printf("\t\t无\n");
Sleep(5000);
system("cls");
menu(headNode, headNode->data);
}
else
{
while (strcmp(pMove->data.name,name))
{
pFrontMove = pMove;
pMove = pMove->next;
if (pMove == NULL)
{
printf("\t\t无\n");
Sleep(5000);
system("cls");
menu(headNode, headNode->data);
}
}
pFrontMove->next = pMove->next;
free(pMove);
}
}
void changeNode(struct Node* headNode, char name[20])//修改
{
struct Node* pMove = headNode->next;
if (pMove == NULL)
{
printf("\t\t无\n");
Sleep(5000);
system("cls");
menu(headNode, headNode->data);
}
else
{
while (strcmp(pMove->data.name, name))
{
pMove = pMove->next;
if (pMove == NULL)
{
printf("\t\t无\n");
Sleep(5000);
system("cls");
menu(headNode, headNode->data);
}
}
printf("\t\t请输入学生的 姓名 性别 年龄 电话号码(请勿使用中文)\n\t\t");
scanf_s("%s %s %d %s", pMove->data.name, sizeof(pMove->data.name), pMove->data.sex, sizeof(pMove->data.sex), &pMove->data.age, pMove->data.tel, sizeof(pMove->data.tel));
}
}
void searchNode(struct Node* headNode, char name[20])//查询
{
struct Node* pMove = headNode->next;
if (pMove == NULL)
{
printf("\t\t无\n");
Sleep(5000);
system("cls");
menu(headNode, headNode->data);
}
else
{
while (strcmp(pMove->data.name, name))//strcmp是比较大小的函数,二者相同则为0,0表示否定,循环结束。
{
pMove = pMove->next;
if (pMove == NULL)
{
printf("\t\t无\n");
Sleep(5000);
system("cls");
menu(headNode, headNode->data);
}
}
system("cls");
printf("\t姓名\t\t性别\t\t年龄\t\t电话号码\n");
setbuf(stdin, NULL);
printf("\t%s\t\t%s\t\t%d\t\t%s\n", pMove->data.name,pMove->data.sex,pMove->data.age,pMove->data.tel);
setbuf(stdin, NULL);
}
}
void printLink(struct Node* headNode)//打印链表
{
struct Node* pMove = headNode->next;
printf("\t姓名\t\t性别\t\t年龄\t\t电话号码\n");
while (pMove != NULL)
{
if (strcmp(pMove->data.name, "Age") == 0)
{
return;
}
printf("\t%s\t\t%s\t\t%d\t\t%s\n", pMove->data.name, pMove->data.sex, pMove->data.age, pMove->data.tel);
pMove = pMove->next;
}
}
void inset(struct Node* link , struct student asd)
{
system("cls");
while (1)
{
system("cls");
printf("\t\t请输入学生的 姓名 性别 年龄 电话号码(请勿使用中文)\n\t\t");
setbuf(stdin, NULL);
scanf_s("%s %s %d %s", asd.name, sizeof(asd.name), asd.sex, sizeof(asd.sex), &asd.age, asd.tel, sizeof(asd.tel));
insetNodeByHead(link, asd);
printf("\t\t是否继续(Y/N)?\n\t\t");
setbuf(stdin, NULL);
int a = getchar();
if (a == 'n' || a == 'N')
{
system("cls");
printLink(link);
printf("\n\t\t输入s返回主页面\n\t\t");
setbuf(stdin, NULL);
int b = getchar();
if (b == 's' || b == 'S')
{
writeInfoFromFile(link, "D:\\000\\studentInfo.txt");
system("cls");
menu(link, asd);
}
else
{
continue;
}
}
}
}
void debeate(struct Node* link, struct student asd)
{
system("cls");
printLink(link);
printf("\t\t请输入您要删除的学生的姓名\n\t\t");
setbuf(stdin, NULL);
scanf_s("%s", asd.name, sizeof(asd.name));
debeateNode(link, asd.name);
writeInfoFromFile(link, "D:\\000\\studentInfo.txt");
system("cls");
menu(link, asd);
}
void search(struct Node* link, struct student asd)
{
system("cls");
printLink(link);
printf("\t\t请输入您要查找的学生姓名\n\t\t");
setbuf(stdin, NULL);
scanf_s("%s", asd.name, sizeof(asd.name));
searchNode(link, asd.name);
printf("\n\t\t输入任何数返回主页面\n\t\t");
setbuf(stdin, NULL);
int b = getchar();
if (b != 99999999999999999)
{
system("cls");
menu(link, asd);
}
else
{
exit(0);
}
}
void change(struct Node* link, struct student asd)
{
system("cls");
printLink(link);
printf("\t\t请输入您想要修改的学生的姓名\n\t\t");
setbuf(stdin, NULL);
scanf_s("%s", asd.name, sizeof(asd.name));
changeNode(link, asd.name);
writeInfoFromFile(link, "D:\\000\\studentInfo.txt");
system("cls");
menu(link, asd);
}
void look(struct Node* link, struct student asd)
{
system("cls");
printLink(link);
printf("\t\t输入任何数返回主页面\n\t\t");
setbuf(stdin, NULL);
int a = getchar();
if (a != 99999999999999999)
{
system("cls");
menu(link, asd);
}
else
{
exit(0);
}
}
void readInfoFromFile(struct Node* headNode, const char* readFile)
{
FILE* fp = fopen(readFile, "r");
struct student asd;
if (fp == NULL)
{
fopen(readFile, "w+");
}
while (fscanf_s(fp, "%s %s %d %s", asd.name, sizeof(asd.name), asd.sex, sizeof(asd.sex), &asd.age, asd.tel, sizeof(asd.tel)) != EOF)
{
insetNodeByHead(headNode, asd);
}
fclose(fp);
}
void writeInfoFromFile(struct Node* headNode, const char* writeFile)
{
FILE* fp;
struct Node* pMove = headNode->next;
fp = fopen(writeFile, "w");
fprintf(fp, "\tName\t\tSex\t\tAge\t\tTel\t\t\n");
while (pMove != NULL)
{
fprintf(fp, "\t%s\t\t%s\t\t%d\t\t%s\t\t\n", pMove->data.name, pMove->data.sex, pMove->data.age, pMove->data.tel);
pMove = pMove->next;
}
fclose(fp);
}
int main()
{
struct student asd;
struct Node* link = creatLink();
readInfoFromFile(link, "D:\\000\\studentInfo.txt");
menu( link , asd);
system("pause");
return 0;
}