功能
实现添加,排序,修改,保存等功能
库函数
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
头文件
#define functioncreate(major) void major##compare(mana mn){\
int i,j,sum=mn->sum;\
for(i=0;i<sum-1;i++)\
for(j=0;j<sum-i-1;j++)\
if(mn->student[j].grade->major<mn->student[j+1].grade->major)\
swap(&mn->student[j],&mn->student[j+1]);\
}
typedef struct a1 { //学生成绩信息
int Chinese;
int Math;
int English;
int Physics;
int Chemistry;
int Organism;
int All;
}*grade;
typedef struct a2 { //学生信息
char name[10];
int stunum;
grade grade;
int namelong;
}*student;
typedef struct a3{ //全体学生信息
int sum;
student student;
int capacity;
}*mana;
void setpos(short x, short y); //设置位置
void init(); //初始化
void welcome(); //欢迎界面
void start(); //开始
void menu(); //菜单
void addstu(mana mn);//加入
void initmana(mana mn);// 初始化
void exportcapacity(mana mn);//扩容
void modifystu(mana mn);//修改个人
void showsin(student stu,int i);//展示单人
void showall(mana mn);//展示全部
void clean(mana mn);//清空
void swap(student p1, student p2);//交换
void compare(mana mn);//比较
void initstu(student mn);//初始化学生信息
void stunumcompare(mana mn);//学号排序
void save(mana mn);//保存
void read(mana mn);//读取
void cpy(char*a1,char*a2);//复制字符串
函数实现
void setpos(short x, short y) {
HANDLE mn = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { x,y };
SetConsoleCursorPosition(mn, pos);
}
void init() {
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
//影藏光标操作
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态
}
void welcome() {
init();
system("title 学生成绩管理系统");
system("mode con cols=100 lines=30");
setpos(35,10);
printf("欢迎来到学生成绩管理系统");
setpos(40, 17);
system("pause");
}
void menu() {
system("cls");
setpos(30, 5);
printf("请点击相应按键决定您的下一步操作");
setpos(30,10);
printf("1.添加新学生-------2.清空");
setpos(30, 12);
printf("3.排序---------------4.无");
setpos(30, 14);
printf("5.修改学生信息-----6.查看学生成绩");
setpos(30, 16);
printf("------------0.退出-----------");
setpos(38, 20);
}
void initmana(mana mn) {
mn->capacity = 0;
mn->student = NULL;
mn->sum = 0;
}
void exportcapacity(mana mn) {
if (mn->capacity > mn->sum)
return;
int capacity = mn->capacity == 0 ? 1 : 2 * mn->capacity;
mn->capacity = capacity;
mn->student = (student)realloc(mn->student, sizeof(struct a2) * capacity);
}
void initstu(student mn) {
student now = mn;
int sum = 0;
setpos(30, 10);
printf("请输入新学生的姓名(拼音):");
scanf("%s", now->name);
now->namelong = strlen(now->name);
system("cls");
setpos(35, 10);
printf("请输入新学生的学号:");
scanf("%d", &(now->stunum));
system("cls");
setpos(35, 10);
printf("请输入语文成绩:");
scanf("%d", &(now->grade->Chinese));
sum += now->grade->Chinese;
system("cls");
setpos(35, 10);
printf("请输入数学成绩:");
scanf("%d", &(now->grade->Math));
sum += now->grade->Math;
system("cls");
setpos(35, 10);
printf("请输入英语成绩:");
scanf("%d", &(now->grade->English));
sum += now->grade->English;
system("cls");
setpos(35, 10);
printf("请输入物理成绩:");
scanf("%d", &(now->grade->Physics));
sum += now->grade->Physics;
system("cls");
setpos(35, 10);
printf("请输入化学成绩:");
scanf("%d", &(now->grade->Chemistry));
sum += now->grade->Chemistry;
system("cls");
setpos(35, 10);
printf("请输入生物成绩:");
scanf("%d", &(now->grade->Organism));
sum += now->grade->Organism;
now->grade->All = sum;
system("cls");
}
void stunumcompare(mana mn)
{
int i, j, sum = mn->sum;
for (i = 0; i < sum - 1; i++) {
for (j = 0; j < sum - i - 1; j++) {
if (mn->student[j].stunum < mn->student[j + 1].stunum)
swap(&mn->student[j], &mn->student[j + 1]);
}
}
}
void addstu(mana mn) {
system("cls");
exportcapacity(mn);
mn->student[mn->sum].grade = (grade)malloc(sizeof(struct a1));
initstu(&mn->student[mn->sum]);
mn->sum++;
setpos(40, 10);
printf("加入完成");
setpos(40, 12);
system("pause");
system("cls");
}
void modifystu(mana mn) {
system("cls");
setpos(30, 10);
printf("请输入你要修改的学生名字(拼音):");
char s[10];
scanf("%s", s);
int i;
for (i = 0; i < mn->sum; i++) {
if (strcmp(s, mn->student[i].name)==0)
break;
}
system("cls");
if (i == mn->sum) {
setpos(30, 10);
printf("查无此人");
return;
}
student now = &(mn->student[i]);
initstu(now);
}
void showsin(student stu,int i) {//姓名,学号,语数英物化生顺序
setpos(25, 5 + 2 * i);
printf("%s", stu->name);
setpos(30, 5 + 2 * i);
printf("%d", stu->stunum);
setpos(35, 5 + 2 * i);
printf("%d", stu->grade->Chinese);
setpos(40, 5 + 2 * i);
printf("%d", stu->grade->Math);
setpos(45, 5 + 2 * i);
printf("%d", stu->grade->English);
setpos(50, 5 + 2 * i);
printf("%d", stu->grade->Physics);
setpos(55, 5 + 2 * i);
printf("%d", stu->grade->Chemistry);
setpos(60, 5 + 2 * i);
printf("%d", stu->grade->Organism);
setpos(65, 5 + 2 * i);
printf("%d", stu->grade->All);
}
void showall(mana mn) {
int i = 0, sum = mn->sum;
if (sum == 0) {
setpos(45, 13);
printf("暂空");
}
system("cls");
setpos(25,3);
printf("姓名");
setpos(30, 3);
printf("学号");
setpos(35, 3);
printf("语文");
setpos(40, 3);
printf("数学");
setpos(45, 3);
printf("英语");
setpos(50, 3);
printf("物理");
setpos(55, 3);
printf("化学");
setpos(60, 3);
printf("生物");
setpos(65, 3);
printf("总分");
for (i = 0; i < sum; i++)
showsin(&mn->student[i], i);
setpos(50, 25);
system("pause");
system("cls");
}
void clean(mana mn) {
system("cls");
free(mn->student);
mn->student = NULL;
mn->capacity = mn->sum = 0;
setpos(40, 14);
printf("清空成功");
system("pause");
}
void swap(student p1, student p2) {
struct a2 mn = *p1;
*p1 = *p2;
*p2 = mn;
}
functioncreate(Math)
functioncreate(Chinese)
functioncreate(English)
functioncreate(Physics)
functioncreate(Chemistry)
functioncreate(Organism)
functioncreate(All)
void compare(mana mn) {
system("cls");
setpos(37, 10);
printf("请选择您的排序方式");
setpos(35, 12);
printf("1.语文");
setpos(45, 12);
printf("2.数学");
setpos(35, 14);
printf("3.英语");
setpos(45, 14);
printf("4.物理");
setpos(35, 16);
printf("5.化学");
setpos(45, 16);
printf("6.生物");
setpos(35, 18);
printf("7.学号");
setpos(45, 18);
printf("8.总分");
setpos(40, 20);
int a;
scanf("%d", &a);
switch (a) {
case 1:Chinesecompare(mn); break;
case 2:Mathcompare(mn); break;
case 3:Englishcompare(mn); break;
case 4:Physicscompare(mn); break;
case 5:Chemistrycompare(mn); break;
case 6:Organismcompare(mn); break;
case 7:stunumcompare(mn); break;
case 8:Allcompare(mn); break;
}
system("cls");
setpos(40, 14);
printf("排序完成,请自行查看");
setpos(40, 15);
system("pause");
}
void start() {
welcome();
mana mn = (mana)malloc(sizeof(struct a3));
initmana(mn);
read(mn);
int choose;
do {
menu();
scanf("%d", &choose);
switch (choose) {
case 1:addstu(mn); break;
case 2:clean(mn); break;
case 3:compare(mn); break;
case 5:modifystu(mn); break;
case 6:showall(mn); break;
case 0:break;
}
} while (choose);
save(mn);
}
void save(mana mn) {
FILE* file = fopen("save.txt", "wb");
int i;
for (i = 0; i < mn->sum; i++) {//先存名字长度,再存学号,再存名字,最后存成绩
fwrite(&((mn->student + i)->namelong), sizeof(int), 1, file);
fwrite(&((mn->student+i)->stunum), sizeof(int), 1, file);
fwrite((mn->student + i)->name, (mn->student + i)->namelong, 1, file);
fwrite((mn->student+i)->grade, sizeof(struct a1), 1, file);
}
fclose(file);
}
void cpy(char* a1, char* a2,int sum) {
while (sum--) {
*a1 = *a2;
a1++;
a2++;
}
}
void read(mana mn) {
FILE* file = fopen("save.txt", "rb");
int i;
char arr[10];
while (fread(&i, sizeof(int), 1, file)) {
exportcapacity(mn);
mn->student[mn->sum].namelong = i;
mn->student[mn->sum].grade = (grade)malloc(sizeof(struct a1));
fread(&i, sizeof(int), 1, file);
mn->student[mn->sum].stunum = i;
fread(arr, sizeof(char) * (mn->student[mn->sum].namelong), 1, file);
cpy(mn->student[mn->sum].name, arr, mn->student[mn->sum].namelong);
mn->student[mn->sum].name[mn->student->namelong] = '\0';
fread(mn->student[mn->sum].grade, sizeof(struct a1), 1, file);
mn->sum++;
}
fclose(file);
}