ADD file via upload

pull/2/head
p8sljnpht 7 days ago
parent 2e241e1a05
commit 8f1e61eb6d

89
4.c

@ -0,0 +1,89 @@
#include <stdio.h>
struct Data
{
int ID;
int Class;
float score1;
float score2;
float score3;
} stu[1001];
int stl = 0;
struct Data* stuflag;
float total(struct Data* a) {
return a->score1 + a->score2 + a->score3;
}
void swap(struct Data* a, struct Data* b) {
struct Data t = *a;
*a = *b;
*b = t;
if (a == stuflag) stuflag = b;
else
if (b == stuflag) stuflag = a;
}
void Sort() {
int i, j;
for(i = 0; i < stl-1; i++) {
for (j = 0; j < stl-i-1; j++) {
if (stu[j].Class < stu[j+1].Class) continue;
if (stu[j].Class > stu[j+1].Class ||
total(&stu[j]) < total(&stu[j+1])) swap(&stu[j], &stu[j+1]);
}
}
}
void input1() {
scanf("%d", &stu[stl].ID);
scanf("%f", &stu[stl].score1);
scanf("%f", &stu[stl].score2);
scanf("%f", &stu[stl].score3);
stl++;
}
void Input() {
printf("Please input info of three students:\n");
input1();input1();input1();
if (total(&stu[stl-3]) < total(&stu[stl-2])) swap(&stu[stl-3], &stu[stl-2]);
if (total(&stu[stl-2]) < total(&stu[stl-1])) swap(&stu[stl-2], &stu[stl-1]);
if (total(&stu[stl-3]) < total(&stu[stl-2])) swap(&stu[stl-3], &stu[stl-2]);
printf("%d,%.1f,%.1f\n", stu[stl-3].ID, total(&stu[stl-3]), total(&stu[stl-3])/3.0f);
printf("%d,%.1f,%.1f\n", stu[stl-2].ID, total(&stu[stl-2]), total(&stu[stl-2])/3.0f);
printf("%d,%.1f,%.1f\n", stu[stl-1].ID, total(&stu[stl-1]), total(&stu[stl-1])/3.0f);
}
void print1(struct Data* a) {
printf("%d %d %2.1f %2.1f %2.1f", a->ID, a->Class,
a->score1, a->score2, a->score3);
if (a == stuflag) printf(" inserted");
printf("\n");
}
void Insert() {
stu[0] = (struct Data){10001, 11, 99.5, 88.5, 89.5};
stu[1] = (struct Data){10002, 12, 77.9, 56.5, 87.5};
stu[2] = (struct Data){10003, 11, 92.5, 89.0, 60.5};
stl = 3;
scanf("%d", &stu[stl].ID);
scanf("%d", &stu[stl].Class);
scanf("%f", &stu[stl].score1);
scanf("%f", &stu[stl].score2);
scanf("%f", &stu[stl].score3);
stl++;
stuflag = stu + 3;
Sort();
print1(stu + 0);
print1(stu + 1);
print1(stu + 2);
print1(stu + 3);
}
int main() {
Insert();
}
Loading…
Cancel
Save