parent
0537fb67de
commit
b406fd09bc
@ -1,100 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 打印学生信息
|
|
||||||
void print_student(struct Student arr[]) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int n=3;
|
|
||||||
void selection_sort(struct Student arr[], int n);
|
|
||||||
selection_sort(students,3);//排序
|
|
||||||
|
|
||||||
printf("Are you sure(yes/no)?\n");
|
|
||||||
char ans;
|
|
||||||
scanf("%s", &ans);
|
|
||||||
if(ans != 'y') {
|
|
||||||
// 用户不确认删除
|
|
||||||
printf("Sorted Students:\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("Enter student number or name to be deleted: ");
|
|
||||||
char num_or_name[10];
|
|
||||||
scanf("%s", num_or_name);
|
|
||||||
|
|
||||||
// 查找要删除的学生索引
|
|
||||||
int target_idx = -1;
|
|
||||||
for(int i = 0; i < 3; i++) {
|
|
||||||
if(strcmp(num_or_name, students[i].stunumber) == 0 || strcmp(num_or_name, students[i].name) == 0) {
|
|
||||||
target_idx = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(target_idx == -1) {
|
|
||||||
printf("The student does not exist!\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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 移除目标学生
|
|
||||||
memmove(&students[target_idx], &students[target_idx + 1], sizeof(students) * (3 - target_idx - 1));
|
|
||||||
|
|
||||||
// 输出移除后学生信息
|
|
||||||
printf("Students after deletion:\n");
|
|
||||||
for(int i = 0; i < 2; 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in new issue