Data_Structure/作業/unit4/DS4/sheng_v2.cpp

365 lines
9.5 KiB
C++
Raw Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Student
{
char name[40];
int eng;
int math;
struct Student *prev;
struct Student *next;
} Student;
Student *head = NULL, *tail = NULL, *current;
// <20>s<EFBFBD>W<EFBFBD>`<60>I<EFBFBD><49><EFBFBD><EFBFBD><ECB5B2><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>
void addToList(const char *name, int eng, int math)
{
// <20>t<EFBFBD>m<EFBFBD>s<EFBFBD>`<60>I<EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD>
Student *newStudent = (Student*)malloc(sizeof(Student));
// <20>ƻs<C6BB><73><EFBFBD>ƨ<EFBFBD><C6A8>s<EFBFBD>`<60>I
strncpy(newStudent->name, name, sizeof(newStudent->name) - 1);
newStudent->name[sizeof(newStudent->name) - 1] = '\0';
newStudent->eng = eng;
newStudent->math = math;
newStudent->next = NULL;
// <20>p<EFBFBD>G<EFBFBD>O<EFBFBD>Ĥ@<40>Ӹ`<60>I
if (head == NULL)
{
newStudent->prev = NULL;
head = tail = newStudent;
}
else // <20>[<5B>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
newStudent->prev = tail;
tail->next = newStudent;
tail = newStudent;
}
}
// <20><><EFBFBD>ܩҦ<DCA9><D2A6>ǥ͸<C7A5><CDB8><EFBFBD>
void displayList()
{
//Student *
current = head;
printf("<EFBFBD>ǥ͸<EFBFBD><EFBFBD>ƦC<EFBFBD><EFBFBD>(<28>q<EFBFBD>Y<EFBFBD>}<7D>l)<29>G\n\n");
printf("<EFBFBD>m<EFBFBD>W\t\t\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
printf("---------------------------------------------\n");
for(int i=0; current!=NULL; current=current->next,i++)
{
printf("%d.\t%-16s\t%d\t%d\n\n", i+1, current->name, current->eng, current->math);
}
printf("---------------------------------------------\n");
}
// <20>q<EFBFBD><71><EFBFBD>}<7D>l<EFBFBD>C<EFBFBD>L
void displayList_inverse()
{
Student *current = tail;
printf("<EFBFBD>ǥ͸<EFBFBD><EFBFBD>ƦC<EFBFBD><EFBFBD>(<28>q<EFBFBD><71><EFBFBD>}<7D>l)<29>G\n\n");
printf("<EFBFBD>m<EFBFBD>W\t\t\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
printf("---------------------------------------------\n");
for(int i=0; current!=NULL; current=current->prev,i++)
{
printf("%d.\t%-16s\t%d\t%d\n\n", i+1, current->name, current->eng, current->math);
}
printf("---------------------------------------------\n");
}
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҧ<EFBFBD><D2A6>ʺA<CABA><41><EFBFBD>t<EFBFBD><74><EFBFBD>`<60>I
void cleanup()
{
Student *current = head;
while (current != NULL)
{
Student *temp = current;
current = current->next;
free(temp);
}
head = tail = NULL;
}
// <20><><EFBFBD>s<EFBFBD><EFBFBD>R<EFBFBD><52><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void deletedate(const char *name, int eng, int math)
{
Student *current = head;
while(current != NULL)
{
if(strcmp(current->name, name) == 0)
{
if(current->prev != NULL) current->prev->next = current->next;
else head = current->next;
if(current->next != NULL) current->next->prev = current->prev;
else tail = current->prev;
free(current);
return;
}
current = current->next;
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD>r<EFBFBD><72><EFBFBD>e<EFBFBD><EFBFBD>Ů<EFBFBD>
void trim(char *str)
{
char *end;
while(isspace(*str)) str++;
if(*str == 0) return;
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
end[1] = '\0';
}
//void displayOriginal(Student **array, int length)
//{
// printf("\n<><6E><EFBFBD>l<EFBFBD><6C><EFBFBD>ƦC<C6A6><43><EFBFBD>G\n\n");
// printf("<22>m<EFBFBD>W\t\t\t\t<>^<5E><>\t<>ƾ<EFBFBD>\n");
// printf("---------------------------------------------\n");
// for(int i = 0; i < length; i++) {
// printf("%d.\t%-16s\t%d\t%d\n\n", i+1, array[i]->name, array[i]->eng, array[i]->math);
// }
// printf("---------------------------------------------\n");
//}//<2F>p<EFBFBD>G<EFBFBD>Q<EFBFBD>b<EFBFBD>Ƨǫ<C6A7><C7AB><EFBFBD><EFBFBD>٦<EFBFBD>switch,<2C>N<EFBFBD><4E><EFBFBD>U<EFBFBD><55><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void displayOriginal()
{
//Student *
current = head;
printf("\n<EFBFBD><EFBFBD><EFBFBD>l<EFBFBD><EFBFBD><EFBFBD>ƦC<EFBFBD><EFBFBD><EFBFBD>G\n\n");
printf("<EFBFBD>m<EFBFBD>W\t\t\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
printf("---------------------------------------------\n");
for(int i = 0; current != NULL; i++) {
printf("%d.\t%-16s\t%d\t%d\n\n", i+1, current->name, current->eng, current->math);
current = current->next;
}
printf("---------------------------------------------\n");
}
// <20>ƧǨ<C6A7><C7A8><EFBFBD>
void bubble_name()
{
int length = 0;
Student *current = head;
// <20>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(current != NULL)
{
length++;
current = current->next;
}
// <20>Ыث<D0AB><D8AB>w<EFBFBD>Ʋ<EFBFBD>
Student **array = (Student**)malloc(length * sizeof(Student*));
current = head;
for(int i = 0; i < length; i++)
{
array[i] = current;
current = current->next;
}
// <20><><EFBFBD>ܭ<EFBFBD><DCAD>l<EFBFBD><6C><EFBFBD><EFBFBD>
//displayOriginal(array, length);
// <20><><EFBFBD>W<EFBFBD>r<EFBFBD>Ƨ<EFBFBD>
for(int i = 0; i < length-1; i++)
{
for(int j = 0; j < length-1-i; j++)
{
if(strcmp(array[j]->name, array[j+1]->name) > 0)
{
Student *temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
// <20><><EFBFBD>ܱƧǫ᪺<C7AB><E1AABA><EFBFBD>G
printf("<EFBFBD>ǥ͸<EFBFBD><EFBFBD>ƦC<EFBFBD><EFBFBD>(<28>̦W<CCA6>r<EFBFBD>Ƨ<EFBFBD>)<29>G\n\n");
printf("<EFBFBD>m<EFBFBD>W\t\t\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
printf("---------------------------------------------\n");
for(int i = 0; i < length; i++) {
printf("%d.\t%-16s\t%d\t%d\n\n", i+1, array[i]->name, array[i]->eng, array[i]->math);
}
printf("---------------------------------------------\n");
free(array);
}
// <20><><EFBFBD>^<5E><EFBFBD>Z<EFBFBD>Ƨ<EFBFBD>
void bubble_eng()
{
int length = 0;
Student *current = head;
// <20>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(current != NULL) {
length++;
current = current->next;
}
// <20>Ыث<D0AB><D8AB>w<EFBFBD>Ʋ<EFBFBD>
Student **array = (Student**)malloc(length * sizeof(Student*));
current = head;
for(int i = 0; i < length; i++) {
array[i] = current;
current = current->next;
}
// <20><><EFBFBD>ܭ<EFBFBD><DCAD>l<EFBFBD><6C><EFBFBD><EFBFBD>
//displayOriginal(array, length);
// <20><><EFBFBD>^<5E><EFBFBD>Z<EFBFBD>Ƨ<EFBFBD>
for(int i = 0; i < length-1; i++) {
for(int j = 0; j < length-1-i; j++) {
if(array[j]->eng < array[j+1]->eng) {
Student *temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
// <20><><EFBFBD>ܱƧǫ᪺<C7AB><E1AABA><EFBFBD>G
printf("<EFBFBD>ǥ͸<EFBFBD><EFBFBD>ƦC<EFBFBD><EFBFBD>(<28>̭^<5E><EFBFBD>Z<EFBFBD>Ƨ<EFBFBD>)<29>G\n\n");
printf("<EFBFBD>m<EFBFBD>W\t\t\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
printf("---------------------------------------------\n");
for(int i = 0; i < length; i++) {
printf("%d.\t%-16s\t%d\t%d\n\n", i+1, array[i]->name, array[i]->eng, array[i]->math);
}
printf("---------------------------------------------\n");
free(array);
}
// <20><><EFBFBD>ƾǦ<C6BE><C7A6>Z<EFBFBD>Ƨ<EFBFBD>
void bubble_math()
{
int length = 0;
Student *current = head;
// <20>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(current != NULL) {
length++;
current = current->next;
}
// <20>Ыث<D0AB><D8AB>w<EFBFBD>Ʋ<EFBFBD>
Student **array = (Student**)malloc(length * sizeof(Student*));
current = head;
for(int i = 0; i < length; i++) {
array[i] = current;
current = current->next;
}
// <20><><EFBFBD>ܭ<EFBFBD><DCAD>l<EFBFBD><6C><EFBFBD><EFBFBD>
//displayOriginal(array, length);
// <20><><EFBFBD>ƾǦ<C6BE><C7A6>Z<EFBFBD>Ƨ<EFBFBD>
for(int i = 0; i < length-1; i++) {
for(int j = 0; j < length-1-i; j++) {
if(array[j]->math < array[j+1]->math) {
Student *temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
// <20><><EFBFBD>ܱƧǫ᪺<C7AB><E1AABA><EFBFBD>G
printf("<EFBFBD>ǥ͸<EFBFBD><EFBFBD>ƦC<EFBFBD><EFBFBD>(<28>̼ƾǦ<C6BE><C7A6>Z<EFBFBD>Ƨ<EFBFBD>)<29>G\n\n");
printf("<EFBFBD>m<EFBFBD>W\t\t\t\t<EFBFBD>^<5E><>\t<EFBFBD>ƾ<EFBFBD>\n");
printf("---------------------------------------------\n");
for(int i = 0; i < length; i++) {
printf("%d.\t%-16s\t%d\t%d\n\n", i+1, array[i]->name, array[i]->eng, array[i]->math);
}
printf("---------------------------------------------\n");
free(array);
}
void select()
{
Student *current=head;
int j;
printf("<EFBFBD>п<EFBFBD><EFBFBD>ܬd<EFBFBD>ݲĴX<EFBFBD>W<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n\n");
scanf("%d",&j);
for(int i=1;i<j;i++)
{
current=current->next;
}
printf("\n%d. Name = %s Eng = %d\tMath = %d",j,current->name,current->eng,current->math);
}
int main()
{
FILE *file;
char line[100];
char name[40];
int eng, math;
char op;
file = fopen("YunTechStudents.txt", "r");
while(fgets(line, sizeof(line), file))
{
if(strstr(line, "****") != NULL)
break;
if(sscanf(line, "%[^\t]%d%d", name, &eng, &math) == 3)
{
trim(name);
addToList(name, eng, math);
}
}
displayList();
displayList_inverse();
while(fgets(line, sizeof(line), file))
{
op = line[0];
if(op == '+' || op == '-')
{
char fullname[40];
if(sscanf(line + 1, " %[^0-9]%d%d", fullname, &eng, &math) == 3)
{
trim(fullname);
if(op == '+')
{
addToList(fullname, eng, math);
printf("\n<EFBFBD>s<EFBFBD>W<EFBFBD><EFBFBD><EFBFBD>Ƭ<EFBFBD>:\t%s\t%d\t%d\n\n", fullname, eng, math);
}
else
{
deletedate(fullname, eng, math);
printf("\n<EFBFBD>R<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭ<EFBFBD>:\t%s\t%d\t%d\n\n", fullname, eng, math);
}
displayList();
displayList_inverse();
}
}
}
int choice;
while(1)
{
printf("\n\n<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n\n");
printf("1. <20>H<EFBFBD>W<EFBFBD>r<EFBFBD>ƦC\t2. <20>H<EFBFBD>^<5E><><EFBFBD><EFBFBD><EFBFBD>ƱƦC\t3. <20>H<EFBFBD>ƾǤ<C6BE><C7A4>ƱƦC\t4. <20><><EFBFBD>ܭ쥻<DCAD><ECA5BB><EFBFBD><EFBFBD>\t5. <20><><EFBFBD>}\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
bubble_name();
select();
break;
case 2:
bubble_eng();
select();
break;
case 3:
bubble_math();
select();
break;
case 4:
displayOriginal();
break;
case 5:
printf("\n<EFBFBD>w<EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD>");
cleanup();
exit(0);
break;
}
}
fclose(file);
return 0;
}