河北师大软件学院第17次作业

来源:互联网 发布:微博个性域名怎么隐藏 编辑:程序博客网 时间:2024/04/30 06:12

声明:
仅供参考,分享请注明出处。
任务一:

/********************************************        任务:结构体,链表-排序        时间:2015年6月28日18:26:03*********************************************/# include <stdio.h># include <malloc.h># include <string.h>/*定义结构体*/typedef struct student{    char name[20];    int score;    struct student *Next;}STU,*List;/*创建接收学生信息*/int i=0;const List stuList(){    List head = NULL;    List temp,temp1 = 0;        char names[20];    while(1)    {        printf("请输入第%d个学生信息(直接回车停止输入学生信息):\n",++i);        printf("\n\t姓名:");        fflush(stdin);        gets(names);                        //接收字符串        if(!strlen(names))            break;        temp = (STU*) malloc(sizeof(STU)); //创建新节点        strcpy(temp -> name , names);        printf("\t成绩:");        scanf("%d",&temp -> score);        temp->Next = 0;        printf("\n");        if(i==1)                        //连接节点            head = temp;        if(temp1)            temp1->Next=temp;        temp1=temp;    }    return head;}/*输出学生成绩单*/void print(List head){    printf("\n\t姓名\t\t成绩\n");    while(NULL!=head)    {        printf("\t%s\t\t%d\n",head->name,head->score);        head = head -> Next;    }    printf("\n");}/*学生成绩排序*/void sort(List head){    List p1;    List p2;    int temp;    char tmpCh[20];    for (p1 = head; p1 != NULL; p1 = p1->Next)    {        for (p2 = p1 -> Next; p2 != NULL; p2 = p2->Next)        {            if (p1 -> score < p2 -> score)            {               /*将结构体的成绩交换*/                temp = p1 -> score;                p1 -> score = p2 -> score;                p2 -> score = temp;                /*将结构体的姓名交换*/                strcpy( tmpCh , p1 -> name );                strcpy( p1 -> name , p2 -> name );                strcpy( p2 -> name , tmpCh);            }        }    }    print(head);}/*释放内存*/void freeList(List pH){  List p = NULL;  printf("FreeList\n");  while(NULL != pH)  {    p = pH;    pH = pH->Next;    free(p);  }}int main (void){    const List head = stuList();    printf("\n===================未排序成绩表单===================\n");    print(head);    printf("\n==================成绩由大到小表单==================\n");    sort(head);    printf("\n");    freeList(head);    return 0;}

任务二:

/******************************************        任务:链表、结构体 查找        时间:2015年6月29日00:31:43*******************************************/# include <stdio.h># include <malloc.h># include <string.h>/*定义结构体*/typedef struct student{    char name[20];    int score;    struct student *Next;}STU,*List;/*创建接收学生信息*/int i=0;const List stuList(){    List head = NULL;    List temp,temp1 = 0;        char names[20];    while(1)    {        printf("请输入第%d个学生信息(直接回车停止输入学生信息):\n",++i);        printf("\n\t姓名:");        fflush(stdin);        gets(names);                        //接收字符串        if(!strlen(names))            break;        temp = (STU*) malloc(sizeof(STU)); //创建新节点        strcpy(temp -> name , names);        printf("\t成绩:");        scanf("%d",&temp -> score);        temp->Next = 0;        printf("\n");        if(i==1)                        //连接节点            head = temp;        if(temp1)            temp1->Next=temp;        temp1=temp;    }    return head;}/*输出学生成绩单*/void print(List head){    printf("\n\t姓名\t\t成绩\n");    while(NULL!=head)    {        printf("\t%s\t\t%d\n",head->name,head->score);        head = head -> Next;    }    printf("\n");}/*学生成绩排序*/void sort(List head){    List p1;    List p2;    int temp;    char tmpCh[20];    for (p1 = head; p1 != NULL; p1 = p1->Next)    {        for (p2 = p1 -> Next; p2 != NULL; p2 = p2->Next)        {            if (p1 -> score < p2 -> score)            {               /*将结构体的成绩交换*/                temp = p1 -> score;                p1 -> score = p2 -> score;                p2 -> score = temp;                /*将结构体的姓名交换*/                strcpy( tmpCh , p1 -> name );                strcpy( p1 -> name , p2 -> name );                strcpy( p2 -> name , tmpCh);            }        }    }    print(head);}/*查找成员*/List find(List head){    List p;    char fname[20];    printf("请输入您要查找的学生姓名:");    fflush(stdin);    gets(fname);    for(p = head ;p != NULL;p = p -> Next)    {        if( 0 == strcmp(p -> name,fname))        {            return p;        }    }    if(p == NULL)        return 0;}/*释放内存*/void freeList(List pH){  List p = NULL;  printf("FreeList\n");  while(NULL != pH)  {    p = pH;    pH = pH->Next;    free(p);  }}int main (void){    const List head = stuList();    List p;    printf("\n===================未排序成绩表单===================\n");    print(head);    printf("\n==================成绩由大到小表单==================\n");    sort(head);    printf("\n====================================================\n");    p = find(head);    if(p)        printf("\n该学生信息的节点位置为:0x%0x\n",p);    else        printf("\n未找到该成员信息退出查找\n\n");    printf("\n");    freeList(head);    return 0;}

任务三:

/*******************************************        任务:结构体,链表查找并修改        时间:2015年6月29日10:47:08********************************************/# include <stdio.h># include <malloc.h># include <string.h>/*定义结构体*/typedef struct student{    char name[20];    int score;    struct student *Next;}STU,*List;/*创建接收学生信息*/int i=0;const List stuList(){    List head = NULL;    List temp,temp1 = 0;        char names[20];    while(1)    {        printf("请输入第%d个学生信息(直接回车停止输入学生信息):\n",++i);        printf("\n\t姓名:");        fflush(stdin);        gets(names);                        //接收字符串        if(!strlen(names))            break;        temp = (STU*) malloc(sizeof(STU)); //创建新节点        strcpy(temp -> name , names);        printf("\t成绩:");        scanf("%d",&temp -> score);        temp->Next = 0;        printf("\n");        if(i==1)                        //连接节点            head = temp;        if(temp1)            temp1->Next=temp;        temp1=temp;    }    return head;}/*输出学生成绩单*/void print(List head){    printf("\n\t姓名\t\t成绩\n");    while(NULL!=head)    {        printf("\t%s\t\t%d\n",head->name,head->score);        head = head -> Next;    }    printf("\n");}/*学生成绩排序*/void sort(List head){    List p1;    List p2;    int temp;    char tmpCh[20];    for (p1 = head; p1 != NULL; p1 = p1->Next)    {        for (p2 = p1 -> Next; p2 != NULL; p2 = p2->Next)        {            if (p1 -> score < p2 -> score)            {               /*将结构体的成绩交换*/                temp = p1 -> score;                p1 -> score = p2 -> score;                p2 -> score = temp;                /*将结构体的姓名交换*/                strcpy( tmpCh , p1 -> name );                strcpy( p1 -> name , p2 -> name );                strcpy( p2 -> name , tmpCh);            }        }    }    print(head);}/*查找成员*/List find(List head){    List p;    char fname[20];    printf("\n请输入您要修改的学生的姓名:");    fflush(stdin);    gets(fname);    for(p = head ;p != NULL;p = p -> Next)    {        if( 0 == strcmp(p -> name,fname))        {            return p;        }    }    if(p == NULL)        return 0;}/*修改成员信息*/int findAndEdit(List head){    List p;    int a;    p = find(head);    if(p)    {        printf("\n请输入修改后的成绩:");        scanf("%d",&a);        p -> score = a;        if(p -> score == a)            printf("\n修改成功\n");        return 1;    }    else    {        printf("\n未找到该成员信息,不能修改\n\n");        return 0;    }    return;}/*释放内存*/void freeList(List pH){  List p = NULL;  printf("FreeList\n");  while(NULL != pH)  {    p = pH;    pH = pH->Next;    free(p);  }}int main (void){    const List head = stuList();    int m;    printf("\n===================未排序成绩表单===================\n");    print(head);    printf("\n==================成绩由大到小表单==================\n");    sort(head);    printf("\n====================================================\n");    m = findAndEdit(head);    printf("\n==================修改后的成绩表单==================\n");    sort(head);    printf("\n====================================================\n\n");    freeList(head);    return 0;}
0 0