#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//学生的结构体
typedef struct stu
{
int num; //学号
char name[20]; //姓名
float chinese; //语文
float math; //数学
float english; //英语
float physics; //物理
struct stu* pnext; //结构体的内部指针
}STU;
STU* pStu = NULL; //定义一个指针
FILE *fp;
void Welcome()
{
printf("\n\n\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 学生管理系统 ┃\n");
printf("\t\t\t┣━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("\t\t\t┃ ①添加学生信息 ┃\n");
printf("\t\t\t┃ ②查找学生信息 ┃\n");
printf("\t\t\t┃ ③删除学生信息 ┃\n");
printf("\t\t\t┃ ④修改学生信息 ┃\n");
printf("\t\t\t┃ ⑤退出学生系统 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t请选择:");
}
//添加信息 //函数里面没有任何的返回值 操作了一个全局变量
void AddInformation()
{
system("cls");
int number;
STU* pNew;
STU* pTemp = pStu;
while (pTemp->pnext != NULL)
{
pTemp = pTemp->pnext;
}
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 添 加 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t您需要添加几位学员:");
scanf("%d", &number); // for已知循环次数 while未知循环次数
for (int i = 1; i <= number; i++)
{
system("cls");
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 添 加 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
pNew = (STU*)malloc(sizeof(STU));
pNew->pnext = NULL;
printf("\t\t\t请输入第%d个学员信息:\n", i);
printf("\t\t\t学号:");
scanf("%d", &pNew->num);
printf("\t\t\t姓名:");
scanf("%s", pNew->name);
printf("\t\t\t语文:");
scanf("%f", &pNew->chinese);
printf("\t\t\t数学:");
scanf("%f", &pNew->math);
printf("\t\t\t英语:");
scanf("%f", &pNew->english);
printf("\t\t\t物理:");
scanf("%f", &pNew->physics);
pTemp->pnext = pNew;
pTemp = pTemp->pnext;
}
printf("\n\t\t\t保存成功!按任意键继续...");
_getch();
}
//全部浏览
void SearchAll()
{
STU* pTemp = pStu->pnext;
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
// for while 循环次数
while (pTemp != NULL) //什么时候退出循环
{
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
pTemp = pTemp->pnext;
}
_getch();
}
//按照学号查找
void SearchNum()
{
int keynum;
STU* pTemp = pStu->pnext;
printf("\t\t\t请输入要查找的学号:");
scanf("%d", &keynum);
while (pTemp != NULL)
{
if (keynum == pTemp->num)
{
printf("\t\t\t查找到的信息:\n");
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
_getch();
return;
}
pTemp = pTemp->pnext;
}
printf("\t\t\t没有找到你需要的内容!\n");
_getch();
}
//按照姓名查找
void SearchName()
{
char keyname[20];
STU* pTemp = pStu->pnext;
printf("\t\t\t请输入要查找的姓名:");
scanf("%s", &keyname);
while (pTemp != NULL)
{
if (!strcmp(keyname, pTemp->name))
{
printf("\t\t\t查找到的信息:\n");
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
_getch();
return;
}
pTemp = pTemp->pnext;
}
printf("\t\t\t没有找到你需要的内容!\n");
_getch();
}
//查找信息
void Search()
{
int choice;
do
{
system("cls");
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 查 找 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t 1.全部浏览\n");
printf("\t\t\t 2.按学号查找\n");
printf("\t\t\t 3.按姓名查找\n");
printf("\t\t\t 4.返回主菜单\n\t\t\t ");
scanf("%d", &choice);
switch (choice)
{
case 1:SearchAll(); break;
case 2:SearchNum(); break;
case 3:SearchName(); break;
default:break;
}
} while (choice != 4);
}
//删除信息
void Delete()
{
int keynum; //按照学号来删除
STU* pPre = pStu;
STU* pDel = pStu->pnext;
system("cls");
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 删 除 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t请输入学号:");
scanf("%d", &keynum);
while (pDel != NULL)
{
if (keynum == pDel->num)
{
printf("已查找到%d号学员:\n", keynum);
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pDel->num, pDel->name, pDel->chinese,
pDel->math, pDel->english, pDel->physics);
printf("\t\t\t 是否删除该信息?(Y/N)");
char ch;
fflush(stdin);
ch = getchar();
if (ch == 'Y' || ch == 'y')
{
pPre->pnext = pDel->pnext;
free(pDel);
return;
}
else
{
return;
}
}
pDel = pDel->pnext;
pPre = pPre->pnext;
}
printf("\t\t\t未找到该学员信息!");
_getch();
}
//修改信息
void Modify()
{
}
void ScanfTxt()
{
}
void PrintToTxt()
{
STU* pTemp = pStu->pnext;
if ((fp = fopen("学生信息.txt", "w")) == NULL)
{
printf("\t\t\t打开文件失败!\n");
_getch();
return;
}
while (pTemp != NULL)
{
fprintf(fp, "%d %s %.1f %.1f %.1f %.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
pTemp = pTemp->pnext;
}
fclose(fp);
printf("\t\t\t保存成功!");
_getch();
exit(0);
}
//主函数
int main()
{
int choice;
if ((fp = fopen("学生信息.txt", "r")) == NULL)
{
printf("\t\t\t打开文件失败!");
_getch();
return -1;
}
pStu = (STU*)malloc(sizeof(STU)); //分配一个学员的内存大小
pStu->pnext = NULL;
while (1)
{
system("cls");
Welcome();
scanf("%d", &choice);
switch (choice)
{
case 1:
//添加学生信息
AddInformation();
break;
case 2:
//查找学生信息
//学号 姓名
Search();
break;
case 3:
//删除信息
Delete();
break;
case 4:
//修改信息
Modify();
break;
case 5:
//让所有信息保存到文件中
PrintToTxt();
break;
default:
break;
}
}
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//学生的结构体
typedef struct stu
{
int num; //学号
char name[20]; //姓名
float chinese; //语文
float math; //数学
float english; //英语
float physics; //物理
struct stu* pnext; //结构体的内部指针
}STU;
STU* pStu = NULL; //定义一个指针
FILE *fp;
void Welcome()
{
printf("\n\n\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 学生管理系统 ┃\n");
printf("\t\t\t┣━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("\t\t\t┃ ①添加学生信息 ┃\n");
printf("\t\t\t┃ ②查找学生信息 ┃\n");
printf("\t\t\t┃ ③删除学生信息 ┃\n");
printf("\t\t\t┃ ④修改学生信息 ┃\n");
printf("\t\t\t┃ ⑤退出学生系统 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t请选择:");
}
//添加信息 //函数里面没有任何的返回值 操作了一个全局变量
void AddInformation()
{
system("cls");
int number;
STU* pNew;
STU* pTemp = pStu;
while (pTemp->pnext != NULL)
{
pTemp = pTemp->pnext;
}
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 添 加 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t您需要添加几位学员:");
scanf("%d", &number); // for已知循环次数 while未知循环次数
for (int i = 1; i <= number; i++)
{
system("cls");
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 添 加 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
pNew = (STU*)malloc(sizeof(STU));
pNew->pnext = NULL;
printf("\t\t\t请输入第%d个学员信息:\n", i);
printf("\t\t\t学号:");
scanf("%d", &pNew->num);
printf("\t\t\t姓名:");
scanf("%s", pNew->name);
printf("\t\t\t语文:");
scanf("%f", &pNew->chinese);
printf("\t\t\t数学:");
scanf("%f", &pNew->math);
printf("\t\t\t英语:");
scanf("%f", &pNew->english);
printf("\t\t\t物理:");
scanf("%f", &pNew->physics);
pTemp->pnext = pNew;
pTemp = pTemp->pnext;
}
printf("\n\t\t\t保存成功!按任意键继续...");
_getch();
}
//全部浏览
void SearchAll()
{
STU* pTemp = pStu->pnext;
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
// for while 循环次数
while (pTemp != NULL) //什么时候退出循环
{
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
pTemp = pTemp->pnext;
}
_getch();
}
//按照学号查找
void SearchNum()
{
int keynum;
STU* pTemp = pStu->pnext;
printf("\t\t\t请输入要查找的学号:");
scanf("%d", &keynum);
while (pTemp != NULL)
{
if (keynum == pTemp->num)
{
printf("\t\t\t查找到的信息:\n");
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
_getch();
return;
}
pTemp = pTemp->pnext;
}
printf("\t\t\t没有找到你需要的内容!\n");
_getch();
}
//按照姓名查找
void SearchName()
{
char keyname[20];
STU* pTemp = pStu->pnext;
printf("\t\t\t请输入要查找的姓名:");
scanf("%s", &keyname);
while (pTemp != NULL)
{
if (!strcmp(keyname, pTemp->name))
{
printf("\t\t\t查找到的信息:\n");
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
_getch();
return;
}
pTemp = pTemp->pnext;
}
printf("\t\t\t没有找到你需要的内容!\n");
_getch();
}
//查找信息
void Search()
{
int choice;
do
{
system("cls");
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 查 找 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t 1.全部浏览\n");
printf("\t\t\t 2.按学号查找\n");
printf("\t\t\t 3.按姓名查找\n");
printf("\t\t\t 4.返回主菜单\n\t\t\t ");
scanf("%d", &choice);
switch (choice)
{
case 1:SearchAll(); break;
case 2:SearchNum(); break;
case 3:SearchName(); break;
default:break;
}
} while (choice != 4);
}
//删除信息
void Delete()
{
int keynum; //按照学号来删除
STU* pPre = pStu;
STU* pDel = pStu->pnext;
system("cls");
printf("\n\n");
printf("\t\t\t┏━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("\t\t\t┃ 删 除 信 息 ┃\n");
printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\t\t\t请输入学号:");
scanf("%d", &keynum);
while (pDel != NULL)
{
if (keynum == pDel->num)
{
printf("已查找到%d号学员:\n", keynum);
printf("\t\t\t 学号\t姓名\t语文\t数学\t英语\t物理\t\n");
printf("\t\t\t %d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
pDel->num, pDel->name, pDel->chinese,
pDel->math, pDel->english, pDel->physics);
printf("\t\t\t 是否删除该信息?(Y/N)");
char ch;
fflush(stdin);
ch = getchar();
if (ch == 'Y' || ch == 'y')
{
pPre->pnext = pDel->pnext;
free(pDel);
return;
}
else
{
return;
}
}
pDel = pDel->pnext;
pPre = pPre->pnext;
}
printf("\t\t\t未找到该学员信息!");
_getch();
}
//修改信息
void Modify()
{
}
void ScanfTxt()
{
}
void PrintToTxt()
{
STU* pTemp = pStu->pnext;
if ((fp = fopen("学生信息.txt", "w")) == NULL)
{
printf("\t\t\t打开文件失败!\n");
_getch();
return;
}
while (pTemp != NULL)
{
fprintf(fp, "%d %s %.1f %.1f %.1f %.1f\n",
pTemp->num, pTemp->name, pTemp->chinese,
pTemp->math, pTemp->english, pTemp->physics);
pTemp = pTemp->pnext;
}
fclose(fp);
printf("\t\t\t保存成功!");
_getch();
exit(0);
}
//主函数
int main()
{
int choice;
if ((fp = fopen("学生信息.txt", "r")) == NULL)
{
printf("\t\t\t打开文件失败!");
_getch();
return -1;
}
pStu = (STU*)malloc(sizeof(STU)); //分配一个学员的内存大小
pStu->pnext = NULL;
while (1)
{
system("cls");
Welcome();
scanf("%d", &choice);
switch (choice)
{
case 1:
//添加学生信息
AddInformation();
break;
case 2:
//查找学生信息
//学号 姓名
Search();
break;
case 3:
//删除信息
Delete();
break;
case 4:
//修改信息
Modify();
break;
case 5:
//让所有信息保存到文件中
PrintToTxt();
break;
default:
break;
}
}
return 0;
}