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.

191 lines
5.5 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
char id[20];
char mclass[20];
char name[20];
float score1, score2, score3;
struct Student* next;
} Student;
Student* head = NULL; // Í·½ÚµãÖ¸Õë
void showMenu() {
printf("%30s1. input\n", " ");
printf("%30s2. delete\n", " ");
printf("%30s3. select\n", " ");
printf("%30s4. order\n", " ");
printf("%30s5. output\n", " ");
printf("%30s6. quit\n", " ");
printf("%30splease input your option\n", " ");
}
void insertStudent() {
Student* newStudent = (Student*)malloc(sizeof(Student));
printf("ID: ");
scanf("%s", newStudent->id);
printf("class: ");
scanf("%s", newStudent->mclass);
printf("name: ");
scanf("%s", newStudent->name);
printf("score1: ");
scanf("%f", &newStudent->score1);
printf("score2: ");
scanf("%f", &newStudent->score2);
printf("score3: ");
scanf("%f", &newStudent->score3);
newStudent->next = head;
head = newStudent;
}
void printStudent() {
Student* current = head;
while (current != NULL) {
printf("%s %s %s %.1f %.1f %.1f\n", current->id, current->mclass, current->name, current->score1, current->score2, current->score3);
current = current->next;
}
}
void deleteStudent() {
char idOrName[20];
scanf("%s", idOrName);
Student* current = head;
Student* prev = NULL;
while (current != NULL) {
if (strcmp(current->id, idOrName) == 0 || strcmp(current->name, idOrName) == 0) {
if (prev == NULL) {
head = current->next;
} else {
prev->next = current->next;
}
free(current);
break;
}
prev = current;
current = current->next;
}
printStudent();
}
void selectStudent() {
char idOrName[20];
scanf("%s", idOrName);
Student* current = head;
int found = 0;
while (current != NULL) {
if (strcmp(current->id, idOrName) == 0 || strcmp(current->name, idOrName) == 0) {
printf("%s %s %s %.1f %.1f %.1f\n", current->id, current->mclass, current->name, current->score1, current->score2, current->score3);
found = 1;
}
current = current->next;
}
if (found == 0) {
printf("There is no eligible student\n");
}
}
void orderStudent()
{
Student *ptr1, *ptr2;
Student tempStudent;
if (head == NULL) {
return;
}
for (ptr1 = head; ptr1->next != NULL; ptr1 = ptr1->next) {
for (ptr2 = ptr1->next; ptr2 != NULL; ptr2 = ptr2->next) {
if ((ptr1->mclass)>(ptr2->mclass)||((ptr1->mclass)==(ptr2->mclass)&&(ptr1->score1 + ptr1->score2 + ptr1->score3) < (ptr2->score1 + ptr2->score2 + ptr2->score3)))
{
// ½»»»Á½¸öѧÉú½ÚµãµÄÊý¾Ý
strcpy(tempStudent.id, ptr1->id);
strcpy(tempStudent.mclass, ptr1->mclass);
strcpy(tempStudent.name, ptr1->name);
tempStudent.score1 = ptr1->score1;
tempStudent.score2 = ptr1->score2;
tempStudent.score3 = ptr1->score3;
strcpy(ptr1->id, ptr2->id);
strcpy(ptr1->mclass, ptr2->mclass);
strcpy(ptr1->name, ptr2->name);
ptr1->score1 = ptr2->score1;
ptr1->score2 = ptr2->score2;
ptr1->score3 = ptr2->score3;
strcpy(ptr2->id, tempStudent.id);
strcpy(ptr2->mclass, tempStudent.mclass);
strcpy(ptr2->name, tempStudent.name);
ptr2->score1 = tempStudent.score1;
ptr2->score2 = tempStudent.score2;
ptr2->score3 = tempStudent.score3;
}
}
}
printStudent();
}
int main() {
int choice;
char a[4];
while (1) {
showMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
insertStudent();
while (1) {
printf("Continue?\n");
scanf("%s", &a);
if (strcmp(a, "yes") == 0) {
insertStudent();
continue;
} else {
break;
}
}
break;
case 2:
deleteStudent();
while (1) {
printf("Continue?\n");
scanf("%s", &a);
if (strcmp(a, "yes") == 0) {
deleteStudent();
continue;
} else {
break;
}
}
break;
case 3:
selectStudent();
while (1) {
printf("Continue?\n");
scanf("%s", &a);
if (strcmp(a, "yes") == 0) {
selectStudent();
continue;
} else {
break;
}
}
break;
case 4:
orderStudent();
break;
case 5:
printStudent();
break;
case 6:
return 0;
}
}
}