Compare commits

...

4 Commits
main ... main

@ -0,0 +1,292 @@
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#include <string.h>
typedef struct Student
{
char id[20];
char class[20];
char name[20];
double score1;
double score2;
double score3;
double score;
struct Student *prev;
struct Student *next;
}LinkList;
bool sorted=false;
typedef bool (*Linklist_cmp)(LinkList *,LinkList *);
bool bigger(LinkList * a,LinkList * b){
return a->score<b->score;
}
bool smallerclass_biggerscore(LinkList * a,LinkList * b){
if(strcmp(a->class,b->class)>0){
return 1;
}
else if(strcmp(a->class,b->class)==0) {
return a->score<b->score;
}
return 0;
}
LinkList *Linklist_append(LinkList *prev) {
LinkList *node;
node = (LinkList *) malloc(sizeof(LinkList));
printf("Id ");
scanf("%s",node->id);
printf("class ");
scanf("%s",node->class);
printf("name ");
scanf("%s",node->name);
printf("score1 ");
scanf("%lf",&node->score1);
printf("score2 ");
scanf("%lf",&node->score2);
printf("score3 ");
scanf("%lf",&node->score3);
node->score = node->score1 + node->score2 + node->score3;
prev->next=node;
node->next=NULL;
node->prev=prev;
return node;
}
void Linklist_delete(LinkList *cur) {
if(cur->next != NULL){
cur->next->prev=cur->prev;
}
cur->prev->next=cur->next;
free(cur);
}
void Linklist_swap(LinkList *s1, LinkList *s2){
if(s1->next == s2){
if(s2->next != NULL){
s2->next->prev = s1;
}
if(s1->prev != NULL){
s1->prev->next = s2;
}
s1->next = s2->next;
s2->prev = s1->prev;
s1->prev = s2;
s2->next = s1;
}else if(s1->prev == s2){
if(s1->next != NULL){
s1->next->prev = s2;
}
if(s2->prev != NULL){
s2->prev->next = s1;
}
s2->next = s1->next;
s1->prev = s2->prev;
s2->prev = s1;
s1->next = s2;
}else{
if(s1->next != NULL){
s1->next->prev = s2;
}
if(s1->prev != NULL){
s1->prev->next = s2;
}
if(s2->next != NULL){
s2->next->prev = s1;
}
if(s2->prev != NULL){
s2->prev->next = s1;
}
LinkList *s1Next = s1->next;
LinkList *s1prev = s1->prev;
s1->next = s2->next;
s1->prev = s2->prev;
s2->next = s1Next;
s2->prev = s1prev;
}
}
void Linklist_Sort(LinkList *p,Linklist_cmp cmp){
LinkList *head = p;
while(head != NULL){
LinkList *headNext = head->next;
while(headNext != NULL){
if(cmp(head, headNext)){
Linklist_swap(head, headNext);
//注意这里一定要交换下,因为改了链表节点的位置
LinkList *tmp = head;
head = headNext;
headNext = tmp;
}
headNext = headNext->next;
}
head = head->next;
}
}
void find_delete(LinkList * head){
char todelatte[20];
scanf("%s",todelatte);
LinkList * node_ptr;
node_ptr = head;
while (node_ptr->next != NULL) {
node_ptr = node_ptr->next;
if(strcmp(todelatte,node_ptr->id)==0 || strcmp(todelatte,node_ptr->name)==0) {
Linklist_delete(node_ptr);
break;
}
}
Linklist_Sort(head->next,bigger);
node_ptr=head;
while(node_ptr->next != NULL){
node_ptr = node_ptr->next;
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",node_ptr->id,node_ptr->class,node_ptr->name,node_ptr->score1,node_ptr->score2,node_ptr->score3,node_ptr->score);
}
}
void find(LinkList * head){
char todelatte[20];
scanf("%s",todelatte);
LinkList * node_ptr;
node_ptr = head;
bool find_flag=false;
while (node_ptr->next != NULL) {
node_ptr = node_ptr->next;
if(strcmp(todelatte,node_ptr->id)==0 || strcmp(todelatte,node_ptr->class)==0) {
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",node_ptr->id,node_ptr->class,node_ptr->name,node_ptr->score1,node_ptr->score2,node_ptr->score3,node_ptr->score);
find_flag = true;
}
}
if(!find_flag)printf("there is no eligible student\n");
}
void sort_output(LinkList * head){
Linklist_Sort(head,smallerclass_biggerscore);//
LinkList * node_ptr=head;
while(node_ptr->next != NULL){
node_ptr = node_ptr->next;
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",node_ptr->id,node_ptr->class,node_ptr->name,node_ptr->score1,node_ptr->score2,node_ptr->score3,node_ptr->score);
}
}
void output(LinkList * head){
if(sorted)Linklist_Sort(head,smallerclass_biggerscore);
LinkList * node_ptr=head;
while(node_ptr->next != NULL){
node_ptr = node_ptr->next;
printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n",node_ptr->id,node_ptr->class,node_ptr->name,node_ptr->score1,node_ptr->score2,node_ptr->score3,node_ptr->score);
}
}
void print_options(){
printf("1.input\n");
printf("2.delete\n");
printf("3.select\n");
printf("4.order\n");
printf("5.output\n");
printf("6.quit\n");
printf("please input your option\n");
}
int main(void) {
LinkList *head,*cur_node;//定义头节点,普通节点,尾部节点;
head = (LinkList*)malloc(sizeof(LinkList));//分配地址
head->prev=NULL;
/*
LinkList wang={"1003","11","wang",1,2,3,6,head,NULL};
LinkList zhang={"1002","22","zhang",3,2,3,8,&wang,NULL};
LinkList li={"1001","11","li",1,4,3,9,&zhang,NULL};
head->next=&wang;
wang.next=&zhang;
zhang.next= &li;
*/
while(1){
print_options();
rewind(stdin);
int op = getchar();
switch(op){
case '1':{
cur_node = Linklist_append(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
int yn = getchar();
while(yn != 'n'){
cur_node = Linklist_append(cur_node);
printf("\r\ncontinue?\r\n");
rewind(stdin);
yn=getchar();
}
break;
}
case '2':{
find_delete(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
int yn = getchar();
while(yn != 'n'){
find_delete(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
yn=getchar();
}
break;
}
case '3':{
find(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
int yn = getchar();
while(yn != 'n'){
find(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
yn=getchar();
}
break;
}
case '4':{
///make sure to run this code under ucrt64 but not mingw64!
sort_output(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
int yn = getchar();
while(yn != 'n'){
sort_output(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
yn=getchar();
}
sorted = true;
break;
}
case '5':{
output(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
int yn = getchar();
while(yn != 'n'){
output(head);
printf("\r\ncontinue?\r\n");
rewind(stdin);
yn=getchar();
}
break;
}
default:{
return 0;
}
}
}
}
Loading…
Cancel
Save