From 3c6597c8d690b6ac1751d55a4cad057c468b1a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E4=B8=96=E6=B3=A2?= <2783626707@qq.com> Date: Sun, 12 Nov 2023 09:51:04 +0800 Subject: [PATCH] feat:order --- .gitignore | 1 + ModifyInfo.c | 55 ---------------------------------------------------- init.c | 14 ++++++++++--- main.c | 2 +- menuoutput.c | 2 ++ mysyslib.h | 5 +++++ operations.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 72 insertions(+), 62 deletions(-) delete mode 100644 ModifyInfo.c diff --git a/.gitignore b/.gitignore index 6326d60..eb0df62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.*~ *.out +.*.swp diff --git a/ModifyInfo.c b/ModifyInfo.c deleted file mode 100644 index b753545..0000000 --- a/ModifyInfo.c +++ /dev/null @@ -1,55 +0,0 @@ -#include "mysyslib.h" - -typedef struct Student -{ - char* id; - char* clas; - char* name; - double score1; - double score2; - double score3; - double score; -}student; - -void ModifyInfo(Student stu_info[],int len) -{ - Student new_stu_info; - scanf("%s%s%s%lf%lf%lf", new_stu_info.id, new_stu_info.clas, new_stu_info.name, new_stu_info.score1, new_stu_info.score2, new_stu_info.score3); - for (int i = 0; i < len; i++) { - if (strcmp(new_stu_info.id, stu_info[i].id) == 0) { - stu_info[i] = new_stu_info; - } - } - char* max, min; - strcpy(max, stu_info[0].clas); - strcpy(min, stu_info[0].clas); - for (int i = 0; i < len; i++) { - if (stu_info[i].clas > max) { - strcpy(max, stu_info[i].clas); - } - if (stu_info[i].clas < min) { - strcpy(min, stu_info[i].clas); - } - } - int i = 0, flag = 0; - while (strcmp(min, max) != 1) { - while (i < len && strcmp(min, stu_info[i++].clas) == 0) { - if (flag == 1) { - if (strcmp(stu_info[i].id, new_stu_info.id) == 0) { - printf(" %s %s %lf %lf %lf modified\n", stu_info[i].id, stu_info[i].name, stu_info[i].score1, stu_info[i].score2, stu_info[i].score3); - } - else - printf(" %s %s %lf %lf %lf\n", stu_info[i].id, stu_info[i].name, stu_info[i].score1, stu_info[i].score2, stu_info[i].score3); - } - else { - flag = 1; - if (strcmp(stu_info[i].id, new_stu_info.id) == 0) { - printf("%s %s %s %lf %lf %lf modified\n", stu_info[i].clas, stu_info[i].id, stu_info[i].name, stu_info[i].score1, stu_info[i].score2, stu_info[i].score3); - } - else - printf("%s %s %s %lf %lf %lf modified\n", stu_info[i].clas, stu_info[i].id, stu_info[i].name, stu_info[i].score1, stu_info[i].score2, stu_info[i].score3); - } - } - min = min + 1; - } -} diff --git a/init.c b/init.c index 69c7b90..0a2792d 100644 --- a/init.c +++ b/init.c @@ -1,4 +1,4 @@ -w#include "mysyslib.h" +#include "mysyslib.h" ss* init(int Nums){ ss* stu = (ss*)malloc(sizeof(ss)); @@ -10,8 +10,16 @@ w#include "mysyslib.h" for(int i = 0; i < Nums; ++i){ stu->student[i].id = (char*)malloc(sizeof(char)*6); - stu->student[i].clas = (char*)malloc(sizeof(char)*3); - stu->student[i].name = (char*)malloc(sizeof(char)*10); + stu->student[i].clas = (char*)malloc(sizeof(char)*4); + stu->student[i].name = (char*)malloc(sizeof(char)*20); } return stu; } + +Student *newStudent(){ + Student *stu = (Student*)malloc(sizeof(Student)); + stu->id = (char*)malloc(sizeof(char)*6); + stu->clas = (char*)malloc(sizeof(char)*4); + stu->name = (char*)malloc(sizeof(char)*20); + return stu; +} diff --git a/main.c b/main.c index bdc3f3b..e748c2b 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,7 @@ int main(){ int ipt; ss* stu = init(20); while(1){ - if(mainMenu(stus)){ + if(mainMenu(stu)){ break; } } diff --git a/menuoutput.c b/menuoutput.c index fa519a5..2362ea2 100644 --- a/menuoutput.c +++ b/menuoutput.c @@ -21,9 +21,11 @@ int mainMenu(ss *stu) { break; case 3: printf("You are trying to Selete info\n"); + ModifyInfo(stu); //修改学生信息 break; case 4: printf("You are trying to Make things ordered\n"); + Order(stu); break; case 5: printf("You are trying to Output info\n"); diff --git a/mysyslib.h b/mysyslib.h index f1a514d..4c7ec14 100644 --- a/mysyslib.h +++ b/mysyslib.h @@ -18,10 +18,15 @@ typedef struct{ }ss; ss* init(int); +Student* newStudent(); +void stucpy(Student* dest, Student* src); +void stuswp(Student* fi, Student* se); int mainMenu(ss *stu); +void ModifyInfo(ss* stu); void getCom(ss*); void Print(Student*); void allPrint(ss*); void input(ss*); void cancel(ss*); void Select(ss*); +void Order(ss*); diff --git a/operations.c b/operations.c index 32ed5b1..7ad054a 100644 --- a/operations.c +++ b/operations.c @@ -1,5 +1,22 @@ #include "mysyslib.h" +void stucpy(Student* dest, Student* src){ + strcpy(dest->id,src->id); + strcpy(dest->clas,src->clas); + strcpy(dest->name,src->name); + dest->score1 = src->score1; + dest->score2 = src->score2; + dest->score3 = src->score3; + dest->score = src->score; +} + +void stuswp(Student* fi, Student* se){ + Student *tmp = newStudent(); + stucpy(tmp,se); + stucpy(se,fi); + stucpy(fi,tmp); +} + //录入数据 void input(ss*stu){ char tmp[5]; @@ -75,17 +92,17 @@ void cancel(ss*stu){ } //修改学生信息 -void ModifyInfo(Student stu->student[]) +void ModifyInfo(ss *stu) { int len = stu->nums; Student new_stu_info; - scanf("%s%s%s%lf%lf%lf", new_stu_info.id, new_stu_info.clas, new_stu_info.name, new_stu_info.score1, new_stu_info.score2, new_stu_info.score3); + scanf("%s%s%s%lf%lf%lf", &new_stu_info.id, &new_stu_info.clas, &new_stu_info.name, &new_stu_info.score1, &new_stu_info.score2, &new_stu_info.score3); for (int i = 0; i < len; i++) { if (strcmp(new_stu_info.id, stu->student[i].id) == 0) { stu->student[i] = new_stu_info; } } - char* max, min; + char *max = malloc(sizeof(char)*20), *min = malloc(sizeof(char)*20); strcpy(max, stu->student[0].clas); strcpy(min, stu->student[0].clas); for (int i = 0; i < len; i++) { @@ -118,3 +135,35 @@ void ModifyInfo(Student stu->student[]) min = min + 1; } } + +//排序 +void Order(ss *stu){ + for(int i=0;inums;i++){ + char minclas[10]; + int minp = i; + strcpy(minclas,stu->student[i].clas); + for(int j=i+1;jnums;j++){ + if(strcmp(stu->student[j].clas,minclas)<0){ + strcpy(minclas,stu->student[j].clas); + minp = j; + } + } + stuswp(&stu->student[i], &stu->student[minp]); + } + for(int i=0;inums;i++){ + double maxs = stu->student[i].score; + int maxp = i; + for(int j=i+1;jnums;j++){ + if(strcmp(stu->student[j].clas,stu->student[j-1].clas)!=0){ + break; + } + if(maxsstudent[j].score){ + maxs = stu->student[j].score; + maxp = j; + } + } + stuswp(&stu->student[i],&stu->student[maxp]); + } + printf("Successfully ordered!\n"); +} +