#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
struct JYY
{
char name[20];
int num;
int age;
char tel[12];
char sex[3];
};
struct Node
{
struct JYY data;
struct Node* next;
};
struct Node* list = NULL;
struct Node* createHead()
{
struct Node*headNode=(struct Node*)malloc(sizeof(struct Node));
assert(headNode);
headNode->next = NULL;
return headNode;
}//创建节点
struct Node* createNode(struct JYY data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
assert(newNode);
newNode->data = data;
newNode->next = NULL;
return newNode;
}//插入
void insertByHead(struct Node* headNode, struct JYY data)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
newNode->next = newNode;
}//删除
void deletByName(struct Node* headNode,const char*name)
{
struct Node* preNode = headNode;
struct Node* posNode = headNode->next;
while (posNode != NULL && strcmp(posNode->data.name,name))
{
preNode = posNode;
posNode = preNode->next;
}
if (posNode != NULL)
{
preNode->next = posNode->next;
free(posNode);
printf("删除成功");
}
}
struct Node* searchByNum(struct Node* headNote, int posData)
{
struct Node* pMove = headNote->next;
while (pMove != NULL && pMove->data.num != posData)
{
pMove = pMove->next;
}
return pMove;
}
//遍历
void printlist(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("姓名\t编号\t年龄\t电话号码\t性别\n");
while (pMove != NULL) {
printf("%s\t%d\t%d\t%s\t%s\n",
pMove->data.name,
pMove->data.num,
pMove->data.age,
pMove->data.tel,
pMove->data.sex);
pMove = pMove->next;
}
printf("\n");
}
void readFile(struct Node* headNode, const char* fileURL)
{
File* fp = fopen(fileURL, "r");
if (fp == NULL)
{
fp = fopen(fileURL, "w+");
fclose(fp);
return;
}
struct JYY data;
while (fscanf(fp, " % s\t % d\t % d\t % s\t % s\t % s\n",
data.name,
data.num,
data.age,
data.tel,
data.sex) != EOF)
{
insertByHead(list,data);
}
fclose(fp);
}
void saveFile(struct Node* headNode, const char* fileURL)
{
File* fp = fopen(fileURL, "w");
struct Node* pMove = headNode->next;
while (pMove!=NULL)
{
fprintf(fp, "%s\t%d\t%d\t%s\t%s\n", pMove->data.name,
pMove->data.num,
pMove->data.age,
pMove->data.tel,
pMove->data.sex);
pMove = pMove->next;
}
}
//用户交互
void makemenu()
{
printf("==========JYY管理系统==========\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("请输入你的选择\n");
}
void keyDown()
{
int userKey = 0;
scanf_s("%d", &userKey);
switch (userKey)
{
case'0':
printf("退出成功!\n");
system("pause");
exit(0);
break;
case'1':
printf("请输入学生信息(name,num,age,tel,sex):");
scanf_s("%s%d%d%s%s%s",
data.name,20,
&data.num,
&data.age,
data.tel, 12,
data.sex,3);
insertByHead(list, data);
printf("录入成功!...\n");
saveFile(list, "JYY.txt");
break;
case'2':printlist(list);
break;
case'3':
printf("请输入要删除的学生姓名:");
scanf_s("%s", data.name, 20);
deletByName(list, data.name);
break;
case'4':
printf("请输入要查找的学生的编号:");
scanf_s("%d", &data.num);
result = searchByNum(list, data.num);
if (result != NULL)
{
printf("姓名\t编号\t年龄\t电话号码\t性别\n");
printf("%s\t%d\t%d\t%s\t%s\n",
result->data.name,
result->data.num,
result->data.age,
result->data.tel,
result->data.sex);
}
else {
printf("未找到相关信息!\n");
}
break;
case'5':
printf("请输入要修改的学生的编号:");
scanf_s("%d", data.num);
result = searchByNum(list, data.num);
if (result != NULL)
{
printf("请输入学生新的信息(name,num,age,tel,sex):");
scanf_s("%s%d%d%s%s",
result->data.name,20
&result->data.num,
&result->data.age,
result->data.tel,12,
result->data.sex,3);
printf("修改成功!\n");
saveFile(list, "JYY管理系统.txt");
}
else
{
printf("未找到相关信息!\n");
}
break;
default:
printf("输入错误,请重新输入");
break;
}
}
int main()
{
list = createHead();
readFile(list, "JYY管理系统.txt");
while(1)
{
makeMenu();
keyDown();
system("pause");
system("cls");
}
return 0;
}