ADD file via upload

pull/2/head
p8sljnpht 1 week ago
parent f5dd47b6ca
commit d8aebb756b

245
8.c

@ -0,0 +1,245 @@
#include <stdio.h>
#include <string.h>
struct Data
{
int ID;
int Class;
float score1;
float score2;
float score3;
char name[101];
} stu[1001];
int stl = 0;
struct Data* stuflag = 0;
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(int i)
{
printf("%d,%d,%s,%.1f,%.1f,%.1f,%.1f\n", stu[i].ID, stu[i].Class, stu[i].name,
stu[i].score1, stu[i].score2, stu[i].score3, total(&stu[i]));
}
void Search() {
int t; char com[101];
int i;
scanf("%d %s", &t, com);
switch(t) {
case 1: {
int from, to;
sscanf(com, "%d-%d", &from, &to);
for (i=0; i<stl; i++) {
if (stu[i].Class >= from && stu[i].Class <= to)
Print1(i);
}
}break;
case 2: {
int from, to;
sscanf(com, "%d-%d", &from, &to);
for (i=0; i<stl; i++) {
if (stu[i].ID >= from && stu[i].ID <= to)
Print1(i);
}
}break;
case 3: {
for (i=0; i<stl; i++) {
int flag = 1, j;
for (j=0; com[j] != '*'; j++) {
if (stu[i].name[j] != com[j]) {
flag = 0; break;
}
}
if(flag)
Print1(i);
}
}break;
case 4: {
float gate;
sscanf(com, "%f", &gate);
for (i=0; i<stl; i++) {
if (total(stu + i) >= gate - 0.01f)
Print1(i);
}
}break;
case 5: {
int Class, from, to;
sscanf(com, "%d.%d-%d", &Class, &from, &to);
for (i=0; i<stl; i++) {
if (stu[i].ID >= from && stu[i].ID <= to)
if (stu[i].Class == Class)
Print1(i);
}
}
}
}
void Output() {
int i;
for(i=0; i<stl; i++) {
printf("%d,%d,%s,%.1f,%.1f,%.1f,%.1f\n", stu[i].ID, stu[i].Class, stu[i].name,
stu[i].score1, stu[i].score2, stu[i].score3, total(&stu[i]));
}
}
void Typein()
{
printf("Id ");
scanf("%d", &stu[stl].ID);
printf("class ");
scanf("%d", &stu[stl].Class);
printf("name ");
scanf("%s", stu[stl].name);
printf("score1 ");
scanf("%f", &stu[stl].score1);
printf("score2 ");
scanf("%f", &stu[stl].score2);
printf("score3 ");
scanf("%f", &stu[stl].score3);
stl++;
}
void Delete() {
int ID = -1; char name[101];
scanf("%s", name);
getchar();
int NumFlag = 1, i;
for(i = 0; name[i]!='\0'; i++)
if (name[i] < '0' || name[i] > '9') {
NumFlag = 0; break;
}
if (NumFlag) {
sscanf(name, "%d", &ID);
name[0] = '\0';
}
for(i=0; i<stl; i++) {
if(stu[i].ID == ID || (name[0]!='\0' && strcmp(stu[i].name, name) == 0)) {
struct Data* r = stu + i;
for(; r+1 < stu+stl; r++) {
*r = *(r+1);
}
stl--;
break;
}
}
Output();
}
void Select_3() {
char com[101];
scanf("%s", com);
int NumFlag = 1, i;
for(i = 0; com[i]!='\0'; i++)
if (com[i] < '0' || com[i] > '9') {
NumFlag = 0; break;
}
if (NumFlag) {
int num;
sscanf(com, "%d", &num);
if (strlen(com) > 2)
for (i=0; i<stl; i++) {
if (stu[i].Class == num)
Print1(i);
}
else
for (i=0; i<stl; i++)
if (stu[i].ID == num)
Print1(i);
}
else {
for (i=0; i<stl; i++)
if (strcmp(stu[i].name, com) == 0)
Print1(i);
}
}
int main() {
char com[11];
while(1) {
printf("1.input\n");
printf("2.delete\n");
printf("3.select\n");
printf("4.order\n");
printf("5.output\n");
printf("6.quit\n");
printf("please input your option\n");
int t; scanf("%d", &t);
switch(t) {
case 1:
do{
Typein();
printf("continue?\n");
scanf("%s", com);
}while(strcmp(com, "yes") == 0);
Sort();
break;
case 2:
do {
Delete();
printf("continue?\n");
scanf("%s", com);
}while(strcmp(com, "yes") == 0);
break;
case 3:
do {
Select_3();
printf("continue?\n");
scanf("%s", com);
}while(strcmp(com, "yes") == 0);
break;
case 4:
Sort();
Output();
break;
case 5:
Output();
break;
case 6:
return 0;
break;
}
}
}
Loading…
Cancel
Save