You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0 KiB

#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;
//һһ<D2BB><D2BB><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD>class<73><73>ֵ
for (i = 0; i < n-1; i++) {
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Сֵ<D0A1><D6B5>λ<EFBFBD><CEBB>
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;
}
}
}
// ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct Student temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// <20><>ӡѧ<D3A1><D1A7><EFBFBD><EFBFBD>Ϣ
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);//<2F><><EFBFBD><EFBFBD>
printf("Are you sure(yes/no)?\n");
char ans;
scanf("%s", &ans);
if(ans != 'y') {
// <20>û<EFBFBD><C3BB><EFBFBD>ȷ<EFBFBD><C8B7>ɾ<EFBFBD><C9BE>
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);
// <20><><EFBFBD><EFBFBD>Ҫɾ<D2AA><C9BE><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
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;
}
// <20>Ƴ<EFBFBD>Ŀ<EFBFBD><C4BF>ѧ<EFBFBD><D1A7>
memmove(&students[target_idx], &students[target_idx + 1], sizeof(students) * (3 - target_idx - 1));
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
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;
}