376 lines
8.9 KiB
C++
376 lines
8.9 KiB
C++
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <string.h>
|
|||
|
#include <ctype.h>
|
|||
|
#include <conio.h>
|
|||
|
|
|||
|
#define MAX_NAME_LENGTH 24
|
|||
|
|
|||
|
//<2F>ŧi<C5A7>禡<EFBFBD>쫬
|
|||
|
void init_f();
|
|||
|
void readStudentsFromFile(char* filename);
|
|||
|
void insertStudent(char* name, int english, int math);
|
|||
|
void printStudentList();
|
|||
|
void sort(int mode , int target);
|
|||
|
void modify_list();
|
|||
|
void add_data(char* name, int english, int math);
|
|||
|
void remove_data(char* name);
|
|||
|
void choose_subject();
|
|||
|
|
|||
|
// <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 ,**arr;
|
|||
|
FILE* file;
|
|||
|
int list_length ;
|
|||
|
|
|||
|
void init_f() {
|
|||
|
head = (Student *) malloc(sizeof(Student));
|
|||
|
tail = (Student *) malloc(sizeof(Student));
|
|||
|
|
|||
|
// 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;
|
|||
|
|
|||
|
// head->llink = tail;
|
|||
|
head->rlink = tail;
|
|||
|
// tail->rlink = head;
|
|||
|
tail->llink = 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;
|
|||
|
|
|||
|
// <20><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD>
|
|||
|
newNode->llink = tail->llink;
|
|||
|
newNode->rlink = tail;
|
|||
|
tail->llink->rlink = newNode;
|
|||
|
tail->llink = newNode;
|
|||
|
}
|
|||
|
|
|||
|
//<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 != tail){
|
|||
|
|
|||
|
current->rlink->llink = prev;
|
|||
|
prev->rlink = current->rlink;
|
|||
|
printf("%s <20>w<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);
|
|||
|
exit(0);
|
|||
|
}
|
|||
|
|
|||
|
char line[100];
|
|||
|
int lineCount = 0;
|
|||
|
|
|||
|
// <20><><EFBFBD>L<EFBFBD>e<EFBFBD>|<7C><>
|
|||
|
while (lineCount < 4 && fgets(line, sizeof(line), file)) {
|
|||
|
lineCount++;
|
|||
|
}
|
|||
|
|
|||
|
// Ū<><C5AA><EFBFBD>ǥ<C7A5><CDB8><EFBFBD>
|
|||
|
char name[MAX_NAME_LENGTH];
|
|||
|
int english, math;
|
|||
|
// %[ ]<5D><><EFBFBD>ܭnŪ<6E>J<EFBFBD>@<40>Ӧr<D3A6>Ŷ<EFBFBD><C5B6>X
|
|||
|
// %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Ū
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
// <09><>flocse<73><65><EFBFBD><EFBFBD><EFBFBD>ե<EFBFBD> <20><><EFBFBD>`<60>ϥ<EFBFBD><CFA5><EFBFBD><EFBFBD>bmodify_list<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// fclose(file);
|
|||
|
}
|
|||
|
|
|||
|
// <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, "%*[^ ] %[^\t]\t%d\t%d\n", name, &english, &math);
|
|||
|
printf("\n===============================\n");
|
|||
|
printf("<EFBFBD>W<EFBFBD>[<5B>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n\n");
|
|||
|
add_data(name, english, math);
|
|||
|
printStudentList();
|
|||
|
}else if(line[0]=='-'){
|
|||
|
sscanf(line, "%*[^ ] %[^\t]\t%d\t%d\n", name, &english, &math);
|
|||
|
printf("\n===============================\n");
|
|||
|
printf("<EFBFBD>R<EFBFBD><EFBFBD><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n\n");
|
|||
|
remove_data(name);
|
|||
|
printStudentList();
|
|||
|
}
|
|||
|
}
|
|||
|
fclose(file);
|
|||
|
}
|
|||
|
|
|||
|
// <20>C<EFBFBD>L<EFBFBD>ǥͦC<CDA6><43>
|
|||
|
void printStudentList() {
|
|||
|
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
printf("\n<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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//<2F>̷<EFBFBD>mode<64>i<EFBFBD><69><EFBFBD>Ƨ<EFBFBD>
|
|||
|
void sort(int mode , int target){
|
|||
|
list_length = 0;
|
|||
|
//<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;
|
|||
|
}
|
|||
|
|
|||
|
current = head->rlink;
|
|||
|
while(current!=tail){
|
|||
|
list_length++;
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
|
|||
|
Student **arr = (Student**)malloc(list_length*(sizeof(Student*)));
|
|||
|
current = head->rlink;
|
|||
|
for(int i = 0; i < list_length; i++) {
|
|||
|
arr[i] = current;
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
|
|||
|
for(int i = 0; i < list_length-1; i++) {
|
|||
|
for(int j = 0; j < list_length-1-i; j++) {
|
|||
|
if(mode==0) {
|
|||
|
if(strcmp(arr[j]->name, arr[j+1]->name) > 0){
|
|||
|
Student *temp = arr[j];
|
|||
|
arr[j] = arr[j+1];
|
|||
|
arr[j+1] = temp;
|
|||
|
}
|
|||
|
}else if(mode==1){
|
|||
|
if(arr[j]->english < arr[j+1]->english ){
|
|||
|
Student *temp = arr[j+1];
|
|||
|
arr[j+1] = arr[j];
|
|||
|
arr[j] = temp;
|
|||
|
}
|
|||
|
} else if(mode==2){
|
|||
|
if(arr[j]->math < arr[j+1]->math ){
|
|||
|
Student *temp = arr[j+1];
|
|||
|
arr[j+1] = arr[j];
|
|||
|
arr[j] = temp;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if(target != -1){
|
|||
|
if(target != 0 && target<=list_length){
|
|||
|
printf("<EFBFBD>ƦW<EFBFBD><EFBFBD>%d<><64><EFBFBD>ǥͬ<C7A5>\n\n"
|
|||
|
"<EFBFBD>m<EFBFBD>W\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n%-15s\t%d\t%d", target, arr[target-1]->name, arr[target-1]->english, arr[target-1]->math);
|
|||
|
printf("\n*****************************\n");
|
|||
|
}else if(target == 0 && target >= list_length){
|
|||
|
printf("\n*****************************\n");
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD><EFBFBD>s<EFBFBD>b");
|
|||
|
printf("\n*****************************\n");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD>ܱƧǫ᪺<C7AB><E1AABA><EFBFBD>G
|
|||
|
printf("\n<EFBFBD>ƦW\t<EFBFBD>m<EFBFBD>W\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
|
|||
|
printf("---------------------------------------------\n");
|
|||
|
for(int i = 0; i < list_length; i++) {
|
|||
|
printf("%d\t%-15s\t%d\t%d\n", i+1, arr[i]->name, arr[i]->english, arr[i]->math);
|
|||
|
}
|
|||
|
printf("---------------------------------------------\n");
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD>ܬ<EFBFBD><DCAC>ؤαƦW
|
|||
|
void choose_subject(){
|
|||
|
int subject ,number , i=1;
|
|||
|
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>ܪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n"
|
|||
|
"1 <20>ƾ<EFBFBD> 2 <20>^<5E><> ==> ");
|
|||
|
subject = getche();
|
|||
|
switch(subject){
|
|||
|
case '1':
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>ƦW(<28><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD>Ы<EFBFBD>Enter) ==> ");
|
|||
|
scanf("%d",&number);
|
|||
|
printf("\n<EFBFBD>ƾ<EFBFBD>");
|
|||
|
sort(1,number);
|
|||
|
break;
|
|||
|
case '2':
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>ƦW(<28><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD>Ы<EFBFBD>Enter) ==> ");
|
|||
|
scanf("%d",&number);
|
|||
|
printf("\n<EFBFBD>^<5E><>");
|
|||
|
sort(2,number);
|
|||
|
break;
|
|||
|
default :
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>~");
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ҧ<EFBFBD><D2A6>`<60>I
|
|||
|
void freeAllNodes() {
|
|||
|
current = head->rlink;
|
|||
|
while (current != tail) {
|
|||
|
temp = current;
|
|||
|
current = current->rlink;
|
|||
|
free(temp);
|
|||
|
}
|
|||
|
free(head); // <20><><EFBFBD><EFBFBD> head <20>`<60>I
|
|||
|
free(tail); // <20><><EFBFBD><EFBFBD> tail <20>`<60>I
|
|||
|
free(arr);
|
|||
|
}
|
|||
|
|
|||
|
int main() {
|
|||
|
char mode ,sort_mode , name[15];
|
|||
|
int english , math;
|
|||
|
init_f();
|
|||
|
readStudentsFromFile("YunTechStudents.txt");
|
|||
|
|
|||
|
printStudentList();
|
|||
|
modify_list();
|
|||
|
|
|||
|
while(1){
|
|||
|
printf("\n******* <20><><EFBFBD>ܶi<DCB6><69><EFBFBD>ƧǩάO<CEAC>ƦW<C6A6><57>n<EFBFBD>W<EFBFBD><57><EFBFBD>ǥ<EFBFBD> *******\n"
|
|||
|
"*--------------------------------------*\n"
|
|||
|
"* 1 <20>i<EFBFBD><69><EFBFBD>Ƨ<EFBFBD> 2 <20><><EFBFBD>ܱƦW<C6A6><57>n<EFBFBD>W<EFBFBD><57><EFBFBD>ǥ<EFBFBD> 3 <20><><EFBFBD><EFBFBD> *\n"
|
|||
|
"* 4 <20><><EFBFBD>ܭ<EFBFBD><DCAD>l<EFBFBD><6C><EFBFBD>C 5 <20>M<EFBFBD><4D><EFBFBD>e<EFBFBD><65> 6 <20>W<EFBFBD>[<5B>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *\n"
|
|||
|
"* 7 <20>R<EFBFBD><52><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *\n"
|
|||
|
"\n<EFBFBD>п<EFBFBD><EFBFBD>ܥ\\<EFBFBD><EFBFBD> ==> "); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>\\<5C><> <20>b<EFBFBD><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|<7C>ܶýX
|
|||
|
mode = getche();
|
|||
|
switch(mode){
|
|||
|
case '1':
|
|||
|
printf("\n\n<EFBFBD><EFBFBD><EFBFBD>ܱƧǤ覡\n"
|
|||
|
"*--------------------------------------*\n"
|
|||
|
"1 <20>W<EFBFBD>r 2 <20>^<5E>妨<EFBFBD>Z 3 <20>ƾǦ<C6BE><C7A6>Z \n"
|
|||
|
"\n<EFBFBD>п<EFBFBD><EFBFBD>ܱƧǤ覡 ==> ");
|
|||
|
sort_mode = getche();
|
|||
|
switch(sort_mode){
|
|||
|
case '1':
|
|||
|
printf("\n\n<EFBFBD>H<EFBFBD>W<EFBFBD>r<EFBFBD>i<EFBFBD><EFBFBD><EFBFBD>Ƨ<EFBFBD>\n");
|
|||
|
sort(0 ,-1);
|
|||
|
break;
|
|||
|
case '2':
|
|||
|
printf("\n\n<EFBFBD>H<EFBFBD>^<5E>妨<EFBFBD>Z<EFBFBD>i<EFBFBD><69><EFBFBD>Ƨ<EFBFBD>\n");
|
|||
|
sort(1 ,-1);
|
|||
|
break;
|
|||
|
case '3':
|
|||
|
printf("\n\n<EFBFBD>H<EFBFBD>ƾǦ<EFBFBD><EFBFBD>Z<EFBFBD>i<EFBFBD><EFBFBD><EFBFBD>Ƨ<EFBFBD>\n");
|
|||
|
sort(2 ,-1);
|
|||
|
break;
|
|||
|
default:
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>~\n");
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
case '2':
|
|||
|
choose_subject();
|
|||
|
break;
|
|||
|
case '3':
|
|||
|
printf("\n\n<EFBFBD><EFBFBD><EFBFBD>¨ϥΡABye~\n");
|
|||
|
freeAllNodes();
|
|||
|
return 0;
|
|||
|
case '4':
|
|||
|
printStudentList();
|
|||
|
break;
|
|||
|
case '5':
|
|||
|
system("cls");
|
|||
|
break;
|
|||
|
case '6':
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>W<EFBFBD>r ==> ");
|
|||
|
// scanf("%[^\n]",name);
|
|||
|
|
|||
|
while(getchar() != '\n'); // <20>M<EFBFBD><4D><EFBFBD>w<EFBFBD>İ<EFBFBD>
|
|||
|
fgets(name, sizeof(name), stdin);
|
|||
|
name[strcspn(name, "\n")] = '\0'; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>^<5E>妨<EFBFBD>Z ==> ");
|
|||
|
scanf("%d",&english);
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>ƾǦ<EFBFBD><EFBFBD>Z ==> ");
|
|||
|
scanf("%d",&math);
|
|||
|
add_data(name,english,math);
|
|||
|
|
|||
|
printStudentList();
|
|||
|
break;
|
|||
|
case '7':
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>W<EFBFBD>r ==> ");
|
|||
|
// scanf("%[^\n]",name);
|
|||
|
|
|||
|
while(getchar() != '\n'); // <20>M<EFBFBD><4D><EFBFBD>w<EFBFBD>İ<EFBFBD>
|
|||
|
fgets(name, sizeof(name), stdin);
|
|||
|
name[strcspn(name, "\n")] = '\0'; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
remove_data(name);
|
|||
|
|
|||
|
printStudentList();
|
|||
|
break;
|
|||
|
default:
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>~\n");
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|