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.4 KiB
191 lines
5.4 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef struct Node {
|
|
char stus[100];
|
|
int classes;
|
|
char names[100];
|
|
double mathGrade;
|
|
double physicsGrade;
|
|
double englishGrade;
|
|
int boolNew;
|
|
struct Node* next;
|
|
} Node;
|
|
|
|
Node* head = NULL;
|
|
|
|
Node* createNode() {
|
|
Node* newNode = (Node*)malloc(sizeof(Node));
|
|
newNode->next = NULL;
|
|
return newNode;
|
|
}
|
|
|
|
void input(Node* node) {
|
|
printf("Id ");
|
|
scanf("%s", node->stus);
|
|
printf("class ");
|
|
scanf("%d", &node->classes);
|
|
printf("name ");
|
|
scanf("%s", node->names);
|
|
printf("score1 ");
|
|
scanf("%lf", &node->mathGrade);
|
|
printf("score2 ");
|
|
scanf("%lf", &node->physicsGrade);
|
|
printf("score3 ");
|
|
scanf("%lf", &node->englishGrade);
|
|
node->boolNew = 0;
|
|
}
|
|
|
|
void inputStudentInformation() {
|
|
char a[10];
|
|
do {
|
|
Node* newNode = createNode();
|
|
input(newNode);
|
|
newNode->next = head;
|
|
head = newNode;
|
|
printf("One student has been input!\n");
|
|
printf("continue?(yes/no)\n");
|
|
scanf("%s", a);
|
|
} while (strcmp(a, "no") != 0);
|
|
}
|
|
|
|
void insertedNew() {
|
|
Node* newNode = createNode();
|
|
input(newNode);
|
|
newNode->boolNew = 1;
|
|
newNode->next = head;
|
|
head = newNode;
|
|
printf("One student has been input!\n");
|
|
}
|
|
|
|
void informationClear(char a[100]) {
|
|
Node** current = &head;
|
|
while (*current) {
|
|
Node* entry = *current;
|
|
if (strcmp(entry->stus, a) == 0 || strcmp(entry->names, a) == 0) {
|
|
*current = entry->next;
|
|
free(entry);
|
|
}
|
|
else {
|
|
current = &entry->next;
|
|
}
|
|
}
|
|
}
|
|
|
|
void sortStudents() {
|
|
Node* sorted = NULL;
|
|
Node* current = head;
|
|
while (current) {
|
|
Node* next = current->next;
|
|
if (!sorted || current->classes < sorted->classes ||
|
|
(current->classes == sorted->classes &&
|
|
current->mathGrade + current->physicsGrade + current->englishGrade >=
|
|
sorted->mathGrade + sorted->physicsGrade + sorted->englishGrade)) {
|
|
current->next = sorted;
|
|
sorted = current;
|
|
}
|
|
else {
|
|
Node* temp = sorted;
|
|
while (temp->next &&
|
|
(temp->next->classes < current->classes ||
|
|
(temp->next->classes == current->classes &&
|
|
temp->next->mathGrade + temp->next->physicsGrade + temp->next->englishGrade >=
|
|
current->mathGrade + current->physicsGrade + current->englishGrade))) {
|
|
temp = temp->next;
|
|
}
|
|
current->next = temp->next;
|
|
temp->next = current;
|
|
}
|
|
current = next;
|
|
}
|
|
head = sorted;
|
|
printf("Students have been sorted!\n");
|
|
}
|
|
|
|
void printStudentOriginalScore() {
|
|
Node* current = head;
|
|
int classs = 0;
|
|
while (current) {
|
|
if (current->classes != classs) {
|
|
printf("%d %s %s %.1f %.1f %.1f %s\n",
|
|
current->classes, current->stus, current->names,
|
|
current->mathGrade, current->physicsGrade, current->englishGrade,
|
|
current->boolNew == 0 ? "" : current->boolNew == 1 ? "inserted" : "modified");
|
|
}
|
|
else {
|
|
printf(" %s %s %.1f %.1f %.1f %s\n",
|
|
current->stus, current->names,
|
|
current->mathGrade, current->physicsGrade, current->englishGrade,
|
|
current->boolNew == 0 ? "" : current->boolNew == 1 ? "inserted" : "modified");
|
|
}
|
|
classs = current->classes;
|
|
current->boolNew = 0;
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
void modifyStudent() {
|
|
char a[10];
|
|
printf("Are you sure(yes/no) ");
|
|
scanf("%s", a);
|
|
if (strcmp(a, "yes") == 0) {
|
|
char stusToModify[10];
|
|
printf("Enter the student number to modify, followed by new data ( 10001 2 Kobe 59 59 59):\n");
|
|
scanf("%s", stusToModify);
|
|
|
|
Node* current = head;
|
|
while (current) {
|
|
if (strcmp(current->stus, stusToModify) == 0) {
|
|
scanf("%d %s %lf %lf %lf",
|
|
¤t->classes, current->names,
|
|
¤t->mathGrade, ¤t->physicsGrade, ¤t->englishGrade);
|
|
current->boolNew = 2;
|
|
printf("Student data modified successfully!\n");
|
|
return;
|
|
}
|
|
current = current->next;
|
|
}
|
|
printf("Student with number %s not found.\n", stusToModify);
|
|
}
|
|
}
|
|
|
|
int manage() {
|
|
printf("1.input\n2.delete\n3.select\n4.order\n5.output\n6.quit\nPlease input your option: ");
|
|
char keywords;
|
|
scanf(" %c", &keywords);
|
|
switch (keywords) {
|
|
case '1':
|
|
inputStudentInformation();
|
|
return 0;
|
|
case '2': {
|
|
char a[100];
|
|
printf("Please enter student's ID or Name: ");
|
|
scanf("%s", a);
|
|
informationClear(a);
|
|
return 0;
|
|
}
|
|
case '3':
|
|
printStudentOriginalScore();
|
|
return 0;
|
|
case '4':
|
|
sortStudents();
|
|
printStudentOriginalScore();
|
|
return 0;
|
|
case '5':
|
|
printStudentOriginalScore();
|
|
return 0;
|
|
case '6':
|
|
return 1;
|
|
default:
|
|
printf("Invalid option. Please try again.\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
while (manage() == 0) {
|
|
}
|
|
return 0;
|
|
}
|