diff --git a/Task6.c b/Task6.c new file mode 100644 index 0000000..40a378c --- /dev/null +++ b/Task6.c @@ -0,0 +1,96 @@ +#include +#include +#include + +struct Student{ + char stunumber[5]; + int class; + char name[10]; + float grade1; + float grade2; + float grade3; + float sum; + }students[3]={{"10001",11,"Zhang",99.5,88.5,89.5,99.5+88.5+99.5}, + {"10002",12,"Yang",77.9,56.5,87.5,77.9+56.5+87.5}, + {"10003",11,"Liang",92.5,99.0,60.5,92.5+99.0+60.5}}; +void selection_sort(struct Student arr[], int n) { + int i, j, min_idx; + + //一一检索每个结构体中class的值 + for (i = 0; i < n-1; i++) { + // 找到最小值的位置 + min_idx = i; + for (j = i+1; j < n; j++) + { + + if (arr[j].class < arr[min_idx].class) + { + min_idx = j; + } + else if(arr[j].class == arr[min_idx].class) + { + if(arr[j].sum == arr[min_idx].sum) + { + min_idx=j; + } + } + } + // 通过交换位置实现排序 + struct Student temp = arr[min_idx]; + arr[min_idx] = arr[i]; + arr[i] = temp; + } +} +int main(){ + int n=3; + void selection_sort(struct Student arr[], int n); + selection_sort(students,3);//排序 + + int idx=-1;//查找待修改学生 + char num_or_name[10]; + printf("请输入您要修改的学生学号或姓名\n"); + scanf("%s",&num_or_name); + for(int i=0;i<3;i++)//检索学生 + { + if(strcmp(num_or_name, students[i].stunumber) == 0 || strcmp(num_or_name, students[i].name) == 0){ + idx=i; + break; + } + } + + if(idx==-1)//学生不存在 + { + printf("未查找到该学生!\n不予修改!\n"); + for(int i=0;i < 3;i++) + { + printf("%s %d %s %.1f %.1f %.1f \n", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3); + } + return 0; + } + printf("已查找到该学生!\n"); + printf("请输入修改后的信息\n"); + struct Student temp;//学生存在且输入修改后的信息 + scanf("%s %d %s %f %f %f %f",&temp.stunumber,&temp.class,&temp.name,&temp.grade1,&temp.grade2,&temp.grade3,&temp.sum); + students[idx]=temp; + + selection_sort(students,3);//再一次排序 + printf("修改后的学生信息如下:\n"); + int class0=0; + for(int i=0;i < 3;i++){ + printf("\n"); + if(students[i].class==class0) + { + printf(" %s %d %s %.1f %.1f %.1f ", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3); + } + if(class0!=students[i].class){ + class0=students[i].class; + printf("%d ",class0); + printf("%s %d %s %.1f %.1f %.1f ", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3); + } + if(i==idx) + { + printf("modifiled"); + } + } +} +