学生管理系统.c文件
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#define MAX 100
struct student
{
char name[20];
int num;
double score[3];
double sum;
};
struct pSysytem
{
struct student stu[MAX];
int curSize;
};
struct pSysytem* createsystem()
{
struct pSysytem* psystem = (struct pSysytem*)calloc(sizeof(struct pSysytem), 1);
return psystem;
}
void insertArray(struct pSysytem * pSystem,struct student stuinfo)
{
if (pSystem->curSize == MAX)
{
printf("满了!!!!\n");
return;
}
pSystem->stu[pSystem->curSize] = stuinfo;
pSystem->curSize++;
}
//浏览信息
void printarray(struct pSysytem* pSystem)
{
printf("姓名\t编号\t数学\t英语\t体育\t总分\n");
for (int i = 0; i < pSystem->curSize; i++)
{
printf("%s\t%d\t",pSystem->stu[i].name,pSystem->stu[i].num);
for (int k = 0; k < 3; k++)
{
printf("%.lf\t",pSystem->stu[i].score[k]);
}
printf("%.lf\n", pSystem->stu[i].sum);
}
}
//录入信息
void intputstudent(struct pSysytem* pSystem)
{
struct student temp;
printf("请输入学生的姓名和编号:");
scanf_s("%s%d", temp.name, 20, &temp.num);
printf("请输入三门课成绩");
temp.sum = 0;
for (int i = 0; i < 3; i++)
{
scanf_s("%lf", &temp.score[i]);
temp.sum += temp.score[i];
}
insertArray(pSystem, temp);
}
//查找信息
int searcharray(struct pSysytem* pSystem, const char* name)
{
for (int i = 0; i < pSystem->curSize; i++)
{
if (strcmp(pSystem->stu[i].name,name) == 0)
return i;
}
return -1;
}
//删除信息
void deletarray(struct pSysytem* pSystem, const char* name)
{
int pos = searcharray(pSystem, name);
if (pos == -1)
{
printf("没找到无法删除\n");
}
else
{
for (int i = 0; i < pSystem->curSize; i++)
{
pSystem->stu[i] = pSystem->stu[i + 1];
}
pSystem->curSize--;
printf("删除成功\n");
}
}
//修改信息
void modifyarray(struct pSysytem* pSystem, const char* name)
{
int pos = searcharray(pSystem, name);
if (pos == -1)
{
printf("没找到无法删除\n");
}
else
{
printf("请输入新学生的姓名和编号:");
scanf_s("%s%d", pSystem->stu[pos].name, 20, &pSystem->stu[pos].num);
printf("请输入新的三门课成绩:");
pSystem->stu[pos].sum = 0;
for (int i = 0; i < 3; i++)
{
scanf_s("%lf", &pSystem->stu[pos].score[i]);
pSystem->stu[pos].sum += pSystem->stu[pos].score[i];
}
printf("修改成功\n");
}
}
//排序信息
void bubblesort(struct pSysytem* pSystem)
{
//按照总分排序
for (int i = 0; i < pSystem->curSize; i++)
{
for (int k = 0; k < pSystem->curSize - i - 1; k++)
{
if (pSystem->stu[k].sum < pSystem->stu[k + 1].sum)
{
struct student temp = pSystem->stu[k];
pSystem->stu[k].sum = pSystem->stu[k + 1].sum;
pSystem->stu[k + 1] = temp;
}
}
}
}
//文件写
void saveinfotofile(struct pSysytem* pSystem, const char* filename)
{
FILE* save = fopen(filename,"w");
fprintf(save,"姓名\t编号\t数学\t英语\t体育\t总分\n");
for (int i = 0; i < pSystem->curSize; i++)
{
fprintf(save,"%s\t%d\t", pSystem->stu[i].name, pSystem->stu[i].num);
for (int k = 0; k < 3; k++)
{
fprintf(save,"%.lf\t", pSystem->stu[i].score[k]);
}
fprintf(save,"%.lf\n", pSystem->stu[i].sum);
}
fclose(save);
}
//文件读
void redeinfotofile(struct pSysytem* pSystem, const char* filename)
{
FILE* read = fopen(filename, "r");
if (read == NULL)
{
read = fopen(filename, "w+");
return;
}
struct student temp;
pSystem->curSize = 0;
while (1)
{
if (feof(read))
{
break;
}
int result = fscanf(read, "%s\t%d\t", pSystem->stu[pSystem->curSize].name, &pSystem->stu[pSystem->curSize].num);
for (int k = 0; k < 3; k++)
{
result = fscanf(read, "%lf\t", &pSystem->stu[pSystem->curSize].score[k]);
}
result = fscanf(read, "%lf\n", &pSystem->stu[pSystem->curSize].sum);
pSystem->curSize++;
}
fclose(read);
}
//菜单
void makeMenu()
{
//打印函数+转义字符
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("\t\t6.排序信息\n");
printf("------------------------------------\n");
}
//按键处理
void keyDown(struct pSysytem* pSystem)
{
//按键输入+开关语句
struct student temp;
int pos = 0;
int userkey = 0;
scanf_s("%d", &userkey);
switch (userkey)
{
case 0:
exit(0);
break;
case 1:
intputstudent(pSystem);
saveinfotofile(pSystem, "student.txt");
break;
case 2:
printarray(pSystem);
break;
case 3:
printf("请输入删除学生的姓名:");
scanf_s("%s",temp.name,20);
deletarray(pSystem, temp.name);
saveinfotofile(pSystem, "student.txt");
break;
case 4:
printf("请输入修改学生的姓名:");
scanf_s("%s", temp.name,20);
modifyarray(pSystem, temp.name);
saveinfotofile(pSystem, "student.txt");
break;
case 5:
printf("请输入查找学生的姓名:");
scanf_s("%s", temp.name,20);
if (pos = searcharray(pSystem, temp.name) == -1)
{
printf("未找到相关信息\n");
}
else
{
printf("姓名\t编号\t数学\t英语\t体育\t总分\n");
printf("%s\t%d\t", pSystem->stu[pos].name, pSystem->stu[pos].num);
for (int i = 0; i < pSystem->stu[pos].score[i]; i++)
{
printf("%.lf\t", pSystem->stu[pos].score[i]);
}
}
break;
case 6:
bubblesort(pSystem);
printarray(pSystem);
break;
default:
printf("输入错误\n");
break;
}
}
int main()
{
struct pSysytem* psystem = createsystem();
redeinfotofile(psystem, "student.txt");
while (true) {
makeMenu();
keyDown(psystem);
system("pause");
system("cls");
}
return 0;
}
版权声明:本文为m0_60548612原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。