head.h
#define _CRT_SECURE_NO_WARNINGS #include#include #include #include #include #define SZIE 1 #define CURR 3 #define INC_SIZE 2 struct Student //设计学生所具有的属性集合 { int id; char name[10]; char sex[3]; int age; float score[CURR]; float sum; float averge; }; struct StudentMan { struct Student *data; //动态申请空间 int capacity; //学生当前容量 int maxsize; //学生最大容量 }; void Menu(); //选择栏 bool Add_Student(struct StudentMan *sp); //添加一个学生信息 void Preserve_Student(struct StudentMan *sp); //保存学生信息 void Load_Student(struct StudentMan *sp); //从文件加载 void Show(struct StudentMan *sp); //展示学生信息 void Find_ID(struct StudentMan* sp,int id); //按照学号查找指定学生信息 void Find_Name(struct StudentMan* sp,char *cp); //按照姓名查找指定学生信息 void Modify_Ess_Infor(struct StudentMan* sp,int id); //修改学生的基本信息 void Modify_Achiev_Infor(struct StudentMan* sp); //修改学生的成绩信息 void Delete_Student(struct StudentMan* sp, int id); //删除学生信息 void Sort_Student_ID(struct StudentMan* sp); //按id排序 void Sort_Student_Sum(struct StudentMan* sp); //按总分排序 void Sort_Student_Name(struct StudentMan* sp); //按姓名排序 void StudentMan(); //调控函数 void Destroy(struct StudentMan* sp); //释放空间
test.c
#include "head.h"
void Menu()
{
printf("**************************n");
printf("****1.ADD 2.DELETE*****n");
printf("****3.FIND 4.MODIFY*****n");
printf("****5.SORT 6.SHOW *****n");
printf("***** 7.EXIT *****n");
printf("**************************n");
}
static void Init_Student(struct StudentMan *sp)
{
assert(sp != NULL);
sp->capacity = 0;
sp->maxsize = SZIE;
sp->data = (struct Strudent*)malloc(sizeof(struct Student)*SZIE);
memset(sp->data, 0, sizeof(struct Student)*sp->maxsize);
}
static bool is_Empty(const struct StudentMan* sp)
{
assert(sp != NULL);
return sp->capacity == 0; //如果个数为0个则返回1
}
static void Inc_Szie(struct StudentMan* sp) //增容函数
{
assert(sp != NULL);
struct Student*p = (struct Student*)realloc(sp->data, sizeof(struct Student)*(sp->maxsize)*INC_SIZE);
if (p == NULL)
{
exit(1);
}
sp->data = p;
sp->maxsize = sp->maxsize*INC_SIZE;
printf("增容成功!,现在最大容量为%dn", sp->maxsize);
}
static void is_Full(const struct StudentMan* sp)
{
assert(sp != NULL);
if (sp->capacity == sp->maxsize)
{
Inc_Szie(sp);
}
}
bool Add_Student(struct StudentMan* sp)
{
assert(sp != NULL);
Load_Student(sp);
is_Full(sp);
int id = 0;
printf("请输入idn");
scanf("%d", &id);
int i = 0;
for (i = 0; i < sp->capacity; i++)
{
while(id == sp->data[i].id)
{
printf("输入重复,请重新输入!n");
scanf("%d", &id);
for (int j = 0; j < i; j++)
{
if (id == sp->data[j].id)
{
printf("输入重复,请重新输入!n");
scanf("%d", &id);;
}
}
}
while (id <10000000|| id>=100000000)
{
printf("输入非法,请重新输入!n");
scanf("%d", &id);
for (int j = 0; j < i; j++)
{
if (id <10000000 || id >= 100000000)
{
printf("输入非法,请重新输入!n");
scanf("%d", &id);;
}
}
}
}
sp->data[i].id = id;
printf("请输入姓名n");
scanf("%s", sp->data[sp->capacity].name);
printf("请输入性别n");
scanf("%s", sp->data[sp->capacity].sex);
printf("请输入年龄n");
scanf("%d", &sp->data[sp->capacity].age);
printf("请输入语文成绩,数学成绩,英语成绩n");
scanf("%f%f%f", &sp->data[sp->capacity].score[0], &sp->data[sp->capacity].score[1], &sp->data[sp->capacity].score[2]);
float a = sp->data[sp->capacity].score[0];
float b = sp->data[sp->capacity].score[1];
float c = sp->data[sp->capacity].score[2];
sp->data[sp->capacity].averge = (a + b + c) / CURR;
sp->data[sp->capacity].sum = a + b + c;
sp->capacity++;
Preserve_Student(sp);
printf("添加成功n");
}
void Show(struct StudentMan *sp)
{
Load_Student(sp);
assert(sp != NULL);
if (is_Empty(sp))
{
printf("没有学生信息!n");
return;
}
printf("---------------------------------------------------------------------------------------------n");
printf("| ID | name | SEX | AGE | CHINESE | MATN | ENGLISH | SUM | AVERAGE |n");
printf("---------------------------------------------------------------------------------------------n");
for (int i = 0; i < sp->capacity; i++)
{
printf("| %8d | %5s | %4s | %2d | %.2f | %5.2f | %5.2f | %5.2f | %5.2f |n",
sp->data[i].id, sp->data[i].name, sp->data[i].sex, sp->data[i].age,
sp->data[i].score[0], sp->data[i].score[1], sp->data[i].score[2],
sp->data[i].sum, sp->data[i].averge
);
printf("---------------------------------------------------------------------------------------------n");
}
}
static void Preserve_Student(const struct StudentMan *sp)
{
assert(sp != NULL);
FILE* fp = fopen("Student.txt", "wb");
if (fp == NULL)
{
return;
}
fwrite(&sp->capacity, sizeof(int), 1, fp);
fwrite(sp->data, sizeof(struct Student), sp->capacity, fp);
fclose(fp);
fp = NULL;
}
void Load_Student(struct StudentMan *sp)
{
assert(sp != NULL);
FILE* fp = fopen("Student.txt", "rb");
if (fp == NULL)
{
return;
}
fread(&sp->capacity, sizeof(int), 1, fp);
fread(sp->data, sizeof(struct Student), sp->capacity, fp);
fclose(fp);
fp = NULL;
}
void Find_ID(struct StudentMan* sp,int id)
{
assert(sp != NULL);
Load_Student(sp);
int i = 0;
for (i = 0; i < sp->capacity; i++)
{
if (sp->data[i].id = id)
{
break;
}
}
if (i < sp->capacity)
{
printf("查询成功!n");
printf("---------------------------------------------------------------------------------------------n");
printf("| ID | name | SEX | AGE | CHINESE | MATN | ENGLISH | SUM | AVERAGE |n");
printf("---------------------------------------------------------------------------------------------n");
printf("| %8d | %5s | %4s | %2d | %.2f | %5.2f | %5.2f | %5.2f | %5.2f |n",
sp->data[i].id, sp->data[i].name, sp->data[i].sex, sp->data[i].age,
sp->data[i].score[0], sp->data[i].score[1], sp->data[i].score[2],
sp->data[i].sum, sp->data[i].averge
);
printf("---------------------------------------------------------------------------------------------n");
}
else
{
printf("查询失败!n");
printf("查无此人!!!n");
}
}
void Find_Name(const struct StudentMan* sp,const char *cp)
{
assert(sp != NULL);
assert(cp != NULL);
Load_Student(sp);
int i = 0;
for (i = 0; i < sp->capacity; i++)
{
if (!strcmp(sp->data[i].name, cp))
{
break;
}
}
if (i < sp->capacity)
{
printf("查询成功!n");
printf("---------------------------------------------------------------------------------------------n");
printf("| ID | name | SEX | AGE | CHINESE | MATN | ENGLISH | SUM | AVERAGE |n");
printf("---------------------------------------------------------------------------------------------n");
printf("| %8d | %5s | %4s | %2d | %.2f | %5.2f | %5.2f | %5.2f | %5.2f |n",
sp->data[i].id, sp->data[i].name, sp->data[i].sex, sp->data[i].age,
sp->data[i].score[0], sp->data[i].score[1], sp->data[i].score[2],
sp->data[i].sum, sp->data[i].averge
);
printf("---------------------------------------------------------------------------------------------n");
}
else
{
printf("查询失败!n");
printf("查无此人!!!n");
}
}
void Modify_Ess_Infor(struct StudentMan* sp,int id)
{
assert(sp != NULL);
Load_Student(sp);
int i = 0;
char cp[10] = { 0 };
for (i = 0; i < sp->capacity; i++)
{
if (sp->data[i].id == id)
{
break;
}
}
if (i < sp->capacity)
{
printf("请输入您修改后的名字:n");
scanf("%s", cp);
fflush(stdin);
strcpy(sp->data[i].name, cp);
printf("请输入您修改后的性别:n");
scanf("%s", cp);
fflush(stdin);
strcpy(sp->data[i].sex, cp);
printf("请输入您修改后的年龄:n");
scanf("%d", &sp->data[i].age);
printf("修改成功!n");
Preserve_Student(sp);
}
else
{
printf("修改失败,查无此人!!!n");
}
}
void Modify_Achiev_Infor(struct StudentMan* sp,int id)
{
assert(sp != NULL);
Load_Student(sp);
int i = 0;
for (i = 0; i < sp->capacity; i++)
{
if (sp->data[i].id == id)
{
break;
}
}
if (i < sp->capacity)
{
printf("请输入您修改后的语文成绩:n");
scanf("%f", &sp->data[i].score[0]);
printf("请输入您修改后的数学成绩:n");
scanf("%f", &sp->data[i].score[1]);
printf("请输入您修改后的英语成绩:n");
scanf("%f", &sp->data[i].score[2]);
float a = sp->data[i].score[0];
float b = sp->data[i].score[1];
float c = sp->data[i].score[2];
sp->data[i].averge = (a + b + c) / CURR;
sp->data[i].sum = a + b + c;
printf("修改成功!n");
Preserve_Student(sp);
}
else
{
printf("修改失败,查无此人!!!n");
}
}
void Delete_Student(struct StudentMan* sp, int id)
{
assert(sp != NULL);
Load_Student(sp);
int i = 0;
for (i = 0; i < sp->capacity; i++)
{
if (sp->data[i].id == id)
{
break;
}
}
if (i < sp->capacity)
{
for (; i < sp->capacity - 1; i++)
{
sp->data[i] = sp->data[i + 1];
}
sp->capacity--;
printf("删除成功!n");
Preserve_Student(sp);
}
else
{
printf("删除失败,查无此人!!!n");
}
}
static struct StudentMan TmpStudent()
{
struct StudentMan tmp = { 0 };
struct Student*p = (struct Student*)malloc(sizeof(struct Student));
if (p == NULL)
{
exit(1);
}
tmp.data = p;
return tmp;
}
void Sort_Student_ID(struct StudentMan* sp)
{
assert(sp != NULL);
Load_Student(sp);
struct StudentMan tmp = TmpStudent();
for (int i = sp->capacity - 1; i > 0;i--)
{
for (int j = 0; j < i; j++)
{
if (sp->data[j].id > sp->data[j + 1].id)
{
tmp.data[0] = sp->data[j];
sp->data[j] = sp->data[j + 1];
sp->data[j + 1] = tmp.data[0];
}
}
}
free(tmp.data);
printf("排序成功!n");
Preserve_Student(sp);
}
void Sort_Student_Sum(struct StudentMan* sp)
{
assert(sp != NULL);
Load_Student(sp);
struct StudentMan tmp = TmpStudent();
for (int i = sp->capacity - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (sp->data[j].sum < sp->data[j + 1].sum)
{
tmp.data[0] = sp->data[j];
sp->data[j] = sp->data[j + 1];
sp->data[j + 1] = tmp.data[0];
}
}
}
free(tmp.data);
printf("排序成功!n");
Preserve_Student(sp);
}
void Sort_Student_Name(struct StudentMan* sp)
{
assert(sp != NULL);
Load_Student(sp);
struct StudentMan tmp = TmpStudent();
for (int i = sp->capacity - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (strcmp(sp->data[j].name,sp->data[i+1].name)> 0)
{
tmp.data[0] = sp->data[j];
sp->data[j] = sp->data[j + 1];
sp->data[j + 1] = tmp.data[0];
}
}
}
free(tmp.data);
printf("排序成功!n");
Preserve_Student(sp);
}
void Destroy(struct StudentMan* sp)
{
free(sp->data);
sp->data = NULL;
}
void StudentMan()
{
struct StudentMan Student_System;
Init_Student(&Student_System);
int choice = 0;
char op = 0;
int id = 0;
char name[10] = { 0 };
do
{
Menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
Add_Student(&Student_System);
break;
case 2:
printf("请输入要删除学生的学号:");
scanf("%d", &id);
Delete_Student(&Student_System, id);
break;
case 3:
printf("按学号查询请输入a\A,n");
printf("输入其他则为按姓名查询...n");
fflush(stdin);
scanf("%c", &op);
if (op == 'a' || op == 'A')
{
printf("请输入学号:n");
scanf("%d", &id);
Find_ID(&Student_System, id);
}
else
{
printf("请输入姓名:n");
scanf("%s", name);
Find_Name(&Student_System, name);
}
break;
case 4:
printf("请输入您要修改的学号:n");
scanf("%d", &id);
fflush(stdin);
printf("如果您要修改学生的基本信息,请输入a\An");
printf("修改成绩信息请输入其他n");
scanf("%c", &op);
if (op == 'a' || op == 'A')
{
Modify_Ess_Infor(&Student_System, id);
}
else
{
Modify_Achiev_Infor(&Student_System, id);
}
break;
case 5:
printf("按成绩排序请输入a\An");
printf("按学号排序请输入b\Bn");
printf("按姓名排序请输入其他n");
fflush(stdin);
scanf("%c", &op);
if (op == 'a' || op == 'A')
{
Sort_Student_Sum(&Student_System);
}
else if (op == 'b' || op == 'B')
{
Sort_Student_ID(&Student_System);
}
else
{
Sort_Student_Name(&Student_System);
}
break;
case 6:
Show(&Student_System);
break;
case 7:
choice = 0;
printf("谢谢使用,退出成功!n");
break;
default:
break;
}
} while (choice != 0);
Destroy(Student_System.data);
}
Achievement_Management.c
#include "head.h"
int main()
{
StudentMan();
system("pause");
return 0;
}



