317 lines
6.8 KiB
C
317 lines
6.8 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <string.h>
|
|||
|
#include <ctype.h>
|
|||
|
|
|||
|
#define MAX_NAME_LENGTH 24
|
|||
|
|
|||
|
FILE* file;
|
|||
|
|
|||
|
// <20>w<EFBFBD>q<EFBFBD>ǥ`<60>I<EFBFBD><49><EFBFBD>c
|
|||
|
typedef struct Student {
|
|||
|
char name[MAX_NAME_LENGTH];
|
|||
|
int english;
|
|||
|
int math;
|
|||
|
struct Student* llink;
|
|||
|
struct Student* rlink;
|
|||
|
} Student;
|
|||
|
Student *head, *tail, *current, *prev, *temp;
|
|||
|
|
|||
|
//<2F>]<5D>@<40><> head<61>A<EFBFBD>N<EFBFBD><4E><EFBFBD>k<EFBFBD>쵲<EFBFBD>ҫ<EFBFBD><D2AB>V<EFBFBD><56><EFBFBD><EFBFBD>
|
|||
|
void init_f() {
|
|||
|
head = (Student *) malloc(sizeof(Student)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> head
|
|||
|
head->llink = head; // <20><><EFBFBD>쵲<EFBFBD><ECB5B2><EFBFBD>V<EFBFBD>ۤv
|
|||
|
head->rlink = head; // <20>k<EFBFBD>쵲<EFBFBD><ECB5B2><EFBFBD>V<EFBFBD>ۤv
|
|||
|
tail = head;
|
|||
|
}
|
|||
|
|
|||
|
// <20>b<EFBFBD><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>s<EFBFBD>ǥ`<60>I
|
|||
|
void insertStudent(char* name, int english, int math) {
|
|||
|
|
|||
|
Student* newNode = (Student*)malloc(sizeof(Student));
|
|||
|
strcpy(newNode->name, name);
|
|||
|
|
|||
|
newNode->english = english;
|
|||
|
newNode->math = math;
|
|||
|
|
|||
|
if(head->rlink == tail) {
|
|||
|
// <20>o<EFBFBD>O<EFBFBD>Ĥ@<40>Ӵ<EFBFBD><D3B4>J<EFBFBD><4A><EFBFBD>`<60>I
|
|||
|
head->rlink = newNode;
|
|||
|
tail->llink = newNode;
|
|||
|
newNode->llink = head;
|
|||
|
newNode->rlink = tail;
|
|||
|
} else {
|
|||
|
//<2F><><EFBFBD>O<EFBFBD>Ĥ@<40><> <20><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD>
|
|||
|
newNode->llink = tail->llink;
|
|||
|
newNode->rlink = tail;
|
|||
|
tail->llink->rlink = newNode;
|
|||
|
tail->llink = newNode;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>**<2A>᪺<EFBFBD>W<EFBFBD>[<5B>δ<EFBFBD><CEB4>ָ<EFBFBD><D6B8><EFBFBD>
|
|||
|
void modify_list(){
|
|||
|
char name[MAX_NAME_LENGTH];
|
|||
|
int english, math;
|
|||
|
char line[100];
|
|||
|
|
|||
|
while(fgets(line, sizeof(line), file)!=NULL){
|
|||
|
if(line[0]=='+'){
|
|||
|
sscanf(line+2, "%23[^\t]\t%d\t%d\n", name, &english, &math);
|
|||
|
printf("\n*******************************\n");
|
|||
|
printf("<EFBFBD>W<EFBFBD>[<5B>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n\n");
|
|||
|
printf("%s\n",name);
|
|||
|
add_data(name, english, math);
|
|||
|
printStudentList();
|
|||
|
}else if(line[0]=='-'){
|
|||
|
sscanf(line+2, "%23[^\t]\t%d\t%d\n", name, &english, &math);
|
|||
|
printf("\n*******************************\n");
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>֤@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n\n");
|
|||
|
printf("%s\n",name);
|
|||
|
remove_data(name);
|
|||
|
printStudentList();
|
|||
|
}
|
|||
|
}
|
|||
|
fclose(file);
|
|||
|
}
|
|||
|
|
|||
|
//<2F>W<EFBFBD>[<5B><><EFBFBD><EFBFBD>
|
|||
|
void add_data(char* name, int english, int math){
|
|||
|
Student* newNode = (Student*)malloc(sizeof(Student));
|
|||
|
strcpy(newNode->name, name);
|
|||
|
|
|||
|
newNode->english = english;
|
|||
|
newNode->math = math;
|
|||
|
|
|||
|
newNode->llink = tail->llink;
|
|||
|
newNode->rlink = tail;
|
|||
|
tail->llink->rlink = newNode;
|
|||
|
tail->llink = newNode;
|
|||
|
printf("%s <20>w<EFBFBD>W<EFBFBD>[\n", name);
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>
|
|||
|
void remove_data(char* name){
|
|||
|
|
|||
|
prev = head;
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
while(current!=tail &&strcmp(name,current->name)!=0){
|
|||
|
prev=current;
|
|||
|
current=current->rlink;
|
|||
|
}
|
|||
|
if(current != head){
|
|||
|
|
|||
|
current->rlink->llink = prev;
|
|||
|
prev->rlink = current->rlink;
|
|||
|
printf("%s <20>w<EFBFBD>Q<EFBFBD>R<EFBFBD><52>\n", name);
|
|||
|
free(current);
|
|||
|
}else /* <20>䤣<EFBFBD><E4A4A3><EFBFBD><EFBFBD><EFBFBD>ƫh<C6AB><68><EFBFBD>ܿ<EFBFBD><DCBF>~ */
|
|||
|
printf(" %s <20><><EFBFBD>b<EFBFBD><62><EFBFBD>C<EFBFBD><43>\n", name);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// <20>q<EFBFBD><71><EFBFBD><EFBFBD>Ū<EFBFBD><C5AA><EFBFBD>ǥ<C7A5><CDB8><EFBFBD>
|
|||
|
void readStudentsFromFile(char* filename) {
|
|||
|
file = fopen(filename, "r");
|
|||
|
if (file == NULL) {
|
|||
|
printf("<EFBFBD>L<EFBFBD>k<EFBFBD>}<7D><><EFBFBD>ɮ<EFBFBD> %s\n", filename);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
char line[100];
|
|||
|
int lineCount = 0;
|
|||
|
|
|||
|
// <20><><EFBFBD>L<EFBFBD>e<EFBFBD>|<7C><>
|
|||
|
// <20><><EFBFBD>|<7C><>
|
|||
|
while (lineCount < 4 && fgets(line, sizeof(line), file)) {
|
|||
|
lineCount++;
|
|||
|
}
|
|||
|
|
|||
|
// Ū<><C5AA><EFBFBD>ǥ<C7A5><CDB8><EFBFBD>
|
|||
|
char name[MAX_NAME_LENGTH];
|
|||
|
int english, math;
|
|||
|
// %23[^\t]<5D><><EFBFBD>uŪ<75><C5AA><EFBFBD>̦h23<32>Ӧr<D3A6>šA<C5A1><41><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><4A>\t<><74><EFBFBD><EFBFBD><EFBFBD>v ^<5E><><EFBFBD>D <20><><EFBFBD>O\t<>NŪ
|
|||
|
// <20>Y<EFBFBD><59><EFBFBD>\Ū<><C5AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ|<7C>^<5E><>3 <20>YŪ<59><C5AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>uŪ<75><C5AA>name<6D>ΨSŪ<53><C5AA><EFBFBD><EFBFBD><EFBFBD>ƫh<C6AB>^<5E>ǭȤ<C7AD><C8A4><EFBFBD><EFBFBD><EFBFBD>3
|
|||
|
while(fgets(line, sizeof(line), file)!=NULL){
|
|||
|
if(isalpha(line[0])){
|
|||
|
sscanf(line, "%23[^\t]\t%d\t%d\n", name, &english, &math);
|
|||
|
insertStudent(name, english, math);
|
|||
|
}else if(line[0]=='*'){
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
fclose(file);
|
|||
|
}
|
|||
|
|
|||
|
// <20>C<EFBFBD>L<EFBFBD>ǥͦC<CDA6><43>
|
|||
|
void printStudentList() {
|
|||
|
|
|||
|
Student* current = head->rlink;
|
|||
|
printf("<EFBFBD>q<EFBFBD>Y<EFBFBD>}<7D>l\n");
|
|||
|
printf("\n<EFBFBD>m<EFBFBD>W\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
|
|||
|
printf("--------------------------------\n");
|
|||
|
|
|||
|
while (current != tail) {
|
|||
|
printf("%-15s\t%d\t%d\n", current->name, current->english, current->math);
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
|
|||
|
current = tail->llink;
|
|||
|
|
|||
|
printf("\n<EFBFBD>q<EFBFBD><EFBFBD><EFBFBD>}<7D>l\n");
|
|||
|
printf("\n<EFBFBD>m<EFBFBD>W\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
|
|||
|
printf("--------------------------------\n");
|
|||
|
while (current != head) {
|
|||
|
printf("%-15s\t%d\t%d\n", current->name, current->english, current->math);
|
|||
|
current = current->llink;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void sort_name(){
|
|||
|
Student* temp ;
|
|||
|
Student* current;
|
|||
|
|
|||
|
//<2F>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>ƩΥu<CEA5><75><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (head->rlink == tail || head->rlink->rlink == tail) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
int flag = 1;
|
|||
|
while(flag){
|
|||
|
flag = 0;
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
while(current->rlink != tail){
|
|||
|
temp = current->rlink;
|
|||
|
|
|||
|
if(strcmp(current->name,temp->name) < 0){
|
|||
|
// <09>u<EFBFBD>ϥΨ<CFA5><CEA8>Ӹ`<60>I<EFBFBD>i<EFBFBD><69><EFBFBD>`<60>I<EFBFBD>洫
|
|||
|
// <09>n<EFBFBD>`<60>N<EFBFBD><4E><EFBFBD>Ч<EFBFBD><D0A7>ܫ᪺<DCAB><E1AABA><EFBFBD><EFBFBD>
|
|||
|
// <09>Y<EFBFBD>U<EFBFBD>C<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD>ǥ洫<C7A5><E6B4AB><EFBFBD>Ы<EFBFBD><D0AB>V<EFBFBD><56><EFBFBD><EFBFBD><EFBFBD>}<7D>|<7C>X<EFBFBD><58>
|
|||
|
// <09>]<5D>i<EFBFBD>H<EFBFBD>h<EFBFBD>ŧi<C5A7><69><EFBFBD>Ӹ`<60>I <20><><EFBFBD><EFBFBD>current->llink<6E><6B>temp->rlink
|
|||
|
// <09>o<EFBFBD>˴N<CBB4>i<EFBFBD>H<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB6><EFBFBD>
|
|||
|
temp->llink = current->llink;
|
|||
|
current->rlink = temp->rlink;
|
|||
|
|
|||
|
temp->rlink->llink = current;
|
|||
|
temp->rlink = current;
|
|||
|
|
|||
|
current->llink->rlink = temp;
|
|||
|
current->llink = temp;
|
|||
|
|
|||
|
flag = 1;
|
|||
|
}
|
|||
|
else{
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
print("<EFBFBD>H<EFBFBD>W<EFBFBD>r<EFBFBD>Ƨ<EFBFBD>\n");
|
|||
|
// printStudentList();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void sort_eng(){
|
|||
|
Student* temp ;
|
|||
|
Student* current;
|
|||
|
|
|||
|
//<2F>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>ƩΥu<CEA5><75><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (head->rlink == tail || head->rlink->rlink == tail) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
int flag = 1;
|
|||
|
while(flag){
|
|||
|
flag = 0;
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
while(current->rlink != tail){
|
|||
|
temp = current->rlink;
|
|||
|
|
|||
|
if(current->english < temp->english){
|
|||
|
// <09>u<EFBFBD>ϥΨ<CFA5><CEA8>Ӹ`<60>I<EFBFBD>i<EFBFBD><69><EFBFBD>`<60>I<EFBFBD>洫
|
|||
|
// <09>n<EFBFBD>`<60>N<EFBFBD><4E><EFBFBD>Ч<EFBFBD><D0A7>ܫ᪺<DCAB><E1AABA><EFBFBD><EFBFBD>
|
|||
|
// <09>Y<EFBFBD>U<EFBFBD>C<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD>ǥ洫<C7A5><E6B4AB><EFBFBD>Ы<EFBFBD><D0AB>V<EFBFBD><56><EFBFBD><EFBFBD><EFBFBD>}<7D>|<7C>X<EFBFBD><58>
|
|||
|
// <09>]<5D>i<EFBFBD>H<EFBFBD>h<EFBFBD>ŧi<C5A7><69><EFBFBD>Ӹ`<60>I <20><><EFBFBD><EFBFBD>current->llink<6E><6B>temp->rlink
|
|||
|
// <09>o<EFBFBD>˴N<CBB4>i<EFBFBD>H<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB6><EFBFBD>
|
|||
|
temp->llink = current->llink;
|
|||
|
current->rlink = temp->rlink;
|
|||
|
|
|||
|
temp->rlink->llink = current;
|
|||
|
temp->rlink = current;
|
|||
|
|
|||
|
current->llink->rlink = temp;
|
|||
|
current->llink = temp;
|
|||
|
|
|||
|
flag = 1;
|
|||
|
}
|
|||
|
else{
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
print("<EFBFBD>H<EFBFBD>^<5E>妨<EFBFBD>Z<EFBFBD>Ƨ<EFBFBD>\n");
|
|||
|
// printStudentList();
|
|||
|
}
|
|||
|
|
|||
|
void sort_math(){
|
|||
|
Student* temp ;
|
|||
|
Student* current;
|
|||
|
|
|||
|
//<2F>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>ƩΥu<CEA5><75><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (head->rlink == tail || head->rlink->rlink == tail) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
int flag = 1;
|
|||
|
while(flag){
|
|||
|
flag = 0;
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
while(current->rlink != tail){
|
|||
|
temp = current->rlink;
|
|||
|
|
|||
|
if(current->math < temp->math){
|
|||
|
// <09>u<EFBFBD>ϥΨ<CFA5><CEA8>Ӹ`<60>I<EFBFBD>i<EFBFBD><69><EFBFBD>`<60>I<EFBFBD>洫
|
|||
|
// <09>n<EFBFBD>`<60>N<EFBFBD><4E><EFBFBD>Ч<EFBFBD><D0A7>ܫ᪺<DCAB><E1AABA><EFBFBD><EFBFBD>
|
|||
|
// <09>Y<EFBFBD>U<EFBFBD>C<EFBFBD>{<7B><><EFBFBD><EFBFBD><EFBFBD>ǥ洫<C7A5><E6B4AB><EFBFBD>Ы<EFBFBD><D0AB>V<EFBFBD><56><EFBFBD><EFBFBD><EFBFBD>}<7D>|<7C>X<EFBFBD><58>
|
|||
|
// <09>]<5D>i<EFBFBD>H<EFBFBD>h<EFBFBD>ŧi<C5A7><69><EFBFBD>Ӹ`<60>I <20><><EFBFBD><EFBFBD>current->llink<6E><6B>temp->rlink
|
|||
|
// <09>o<EFBFBD>˴N<CBB4>i<EFBFBD>H<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB6><EFBFBD>
|
|||
|
temp->llink = current->llink;
|
|||
|
current->rlink = temp->rlink;
|
|||
|
|
|||
|
temp->rlink->llink = current;
|
|||
|
temp->rlink = current;
|
|||
|
|
|||
|
current->llink->rlink = temp;
|
|||
|
current->llink = temp;
|
|||
|
|
|||
|
flag = 1;
|
|||
|
}
|
|||
|
else{
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
print("<EFBFBD>H<EFBFBD>ƾǦ<EFBFBD><EFBFBD>Z<EFBFBD>Ƨ<EFBFBD>\n");
|
|||
|
// printStudentList();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
int main() {
|
|||
|
init_f();
|
|||
|
readStudentsFromFile("YunTechStudents.txt");
|
|||
|
// printStudentList();
|
|||
|
// modify_list();
|
|||
|
|
|||
|
sort_name();
|
|||
|
|
|||
|
sort_math();
|
|||
|
|
|||
|
sort_eng();
|
|||
|
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|