| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7450 人关注过本帖
标题:学生信息管理系统的排序代码怎么写?
只看楼主 加入收藏
jeffrey98
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-2-17
结帖率:0
收藏
已结贴  问题点数:1 回复次数:2 
学生信息管理系统的排序代码怎么写?
/* Note:Your choice is C IDE */
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<malloc.h>  
#define LEN sizeof(struct Student)  
  
struct Student         
{  
    unsigned int num;   
    char age[10];     
    char name[20];      
    char born[20];      
    char sex[10];        
    char Email[20];      
    char phone[20];     
    char addr[100];     
    struct Student *next;   
};  
  
struct Student *head = NULL;  
unsigned int kg = 0;  

//******************对各功能函数的声明***************************  
  
void show();                                                            
struct Student *input();                                               
struct Student *insert(struct Student *head,struct Student *stu);      
struct Student *del(struct Student *head);                             
void alter(struct Student *head);                                       
void search(struct Student *head,unsigned int num);                     
void output(struct Student *head);                                    
//***************************************************************  
  
void show()  
{  
    int n,z = 1,k = 0;  
    char cho;  
    do  
    {  
        system("cls");
                                                                                                         //***********显示提示信息**********     
        printf("\n  *****************************学生信息管理系统*****************************\n");  
        printf("\n");  
        printf("   *                                                                      *\n");   
        printf("   *                         1.新建排序 学生信息                      *\n");           
        printf("   *                         2.插入     学生信息                      *\n");         
        printf("   *                         3.删除     学生信息                      *\n");            
        printf("   *                         4.修改     学生信息                      *\n");         
        printf("   *                         5.查询     学生信息                      *\n");           
        printf("   *                         6.保存     学生信息                      *\n");                  
        printf("   *                         0.退出系统                               *\n");         
        printf("   *                                                                  *\n");   
        printf("   *                                                                  *\n");
        
        printf("   *************************************************************************\n");  
  
        //********************************  
         
        printf("   请输入功能序号: ");            
        while(!scanf("%d",&n))                     
        {  
            printf("   输入序号有误,请重新输入: ");  
            fflush(stdin);  
        }  
        printf("\n");                                               
  
        while((n < 0 || n > 6))                                    
        {  
            printf("   抱歉,没有此功能,请重新输入功能序号: ");      
            fflush(stdin);  
            while(!scanf("%d",&n))                                 
            {  
                printf("   输入序号有误,请重新输入: ");  
                fflush(stdin);  
            }  
            printf("\n");                                       
        }  
  
        switch(n)                           
        {  
            case 0:  
            {  
                printf("   ");  
                if (k == 1)                 
                {  
                    printf("数据未保存,是否退出? Y.是 N.否 请选择: ");                     
                    fflush(stdin);  
                    cho = getchar();  
                    if (cho == 'y' || cho == 'Y')  
                    {  
                        z = 0;  
                        printf("   ");  
                    }  
                    else if (cho == 'n' || cho == 'N')   
                        {}  
                    else  
                    {  
                        printf("   无效输入\n   ");                       
                        system("pause");  
                    }  
  
                }  
                else  
                {  
                    z = 0;  
                }  
                break;  
            }  
            case 1:  
            {  
                head = input();  
                k = 1;  
                system("pause");  
                break;  
            }     
            case 2:  
            {  
                head = insert(head,NULL);  
                k = 1;  
                printf("   成功插入学生信息\n   ");  
                system("pause");  
                break;  
            }  
            case 3:  
            {  
                head = del(head);  
                k = 1;  
                printf("   ");  
                system("pause");  
                break;  
            }  
            case 4:  
            {  
                alter(head);  
                break;  
            }  
            case 5:  
            {  
                if (head == NULL) k = 1;  
                search(head,0);  
                printf("   ");  
                system("pause");  
                break;  
            }  
            case 6:  
            {  
                k = 0;  
                output(head);  
                break;  
            }   

        }  
    }while(z == 1);  
}  
  
  
//***************************************************************  
  
  
  
struct Student *input()  
{  
    struct Student *p1,*p2;  
    unsigned m,n,i;  
    FILE *read;  
    char filename[20];  
    head = NULL;  
  
    //要求用户选择录入学生信息的方式  
    printf("   请选择录入方式:\n");                     
    printf("   1.从当前窗口录入\n");  
    printf("   2.从已有的文件(txt文件)录入\n");  
    printf("   请选择: ");  
    while(!scanf("%u",&m))                      //排除输入错误的情况  
    {  
        printf("   输入序号有误,请重新输入: ");  
        fflush(stdin);  
    }  
  
  
    while(m < 1 || m > 2)                     //排除输入错误的情况  
    {  
        printf("   请重新选择选择录入方式:\n");  
        printf("   1.从当前界面录入\n");  
        printf("   2.从已有文件(txt文件)录入\n");  
        printf("   请选择:   ");  
        fflush(stdin);  
        while(!scanf("%d",&m))  
        {  
            printf("   输入序号有误,请重新输入: ");  
            fflush(stdin);  
        }  
        printf("\n");         
    }  
  
    if (m == 1)                             //当前窗口录入  
    {  
        printf("   请输入学生人数: ");      
        while (!scanf("%u",&n))            
        {  
            printf("   输入有误,请重新输入学生人数: ");  
            fflush(stdin);  
        }  
        if (n == 0)                         //对录入人数为0的情况进行处理  
        {  
            printf("   学生个数为0人,建立失败\n   ");      
            return (head);  
        }  
        head = p2 = p1 = (struct Student *) malloc (LEN);                              
        printf("   学生个数为:%u个,请按照以下顺序输入\n",n);                          
        printf("   学号、年龄、姓名、出生年月、性别、E-mail、联络电话、家庭住址\n");
        for(i = 0; i < n; i++)  
        {  
            printf("   请输入第%u个学生信息!\n",i+1);                                      
            printf("   ");                                                            
            fflush(stdin);  
            while(!scanf("%d %s %s %s %s %s %s %s",&p2->num,p2->age,p2->name,p2->born,p2->sex,p2->Email,p2->phone,p2->addr))  
            {  
                printf("   输入有误,请重新输入该学生信息\n   ");  
                fflush(stdin);  
            }  
            if (n == 1)                                                                 
            {  
                p2->next = NULL;              
            }  
            else                                                                       
            {  
                head = insert(head,p2);  
                p2 = (struct Student *) malloc (LEN);  
            }  
        }  
    }  
    else        //对文件录入的处理  
    {  
        printf("   请输入文件名(如student): ");  
        while(!scanf("%s",filename))                        //接收用户输入的文件名,并对其进行排错  
        {  
            printf("   输入文件名有误,请重新输入: ");  
            fflush(stdin);  
        }  
        strcat(filename,".txt");                           
        while((read = fopen(filename,"r")) == NULL)         
        {  
            printf("   未找到此文件,请重新输入文件名: ");  
            scanf("%s",filename);  
            strcat(filename,".txt");  
        }  
        if (!fscanf(read,"%u",&n))                        
        {  
            printf("   文件数据有错,结束录入\n   ");  
            fclose(read);  
            fflush(stdin);  
            return head;  
        }  
        p2 = p1 = (struct Student *) malloc (LEN);         
        for(i = 0; i < n; i++)                              
        {  
            if(!fscanf(read,"%d %s %s %s %s %s %s %s",&p2->num,p2->age,p2->name,p2->born,p2->sex,p2->Email,p2->phone,p2->addr))  
            //当遇到数据有误,释放内存空间,返回NULL  
            {  
                fclose(read);  
                p2 = head;  
                p1 = p2->next;  
                do                      //释放链表空间  
                {                        
                    free(p2);  
                    p2 = p1;                     
                    if (p1 != NULL)  
                        p1 = p1->next;  
                    else  
                        break;  
                }while(p2 != NULL);  
                printf("   数据有误,结束录入\n   ");  
                return NULL;        //返回NULL  
            }  
              
            if (n == 1)             //对录入人数为1的情况进行处理  
            {  
                p2->next = NULL;  
            }  
            else                    //对录入人数大于1的情况进行录入,并在录入时进行排序,开辟内存空间用于下一个学生信息的录入  
            {  
                head = insert(head,p2);  
                p2 = (struct Student *) malloc (LEN);  
            }  
        }  
        fclose(read);               //关闭文件  
    }     
    printf("   成功录入学生信息,并且学生信息已经按学号从小到大排列\n");  
    printf("   学生信息如下: \n");  
    search(head,1);                 //以表格的形式显示所录入的学生信息  
    printf("   ");  
    return head;                    //返回*head指针的地址  
}  
  
struct Student *insert(struct Student *head,struct Student *stu)        //插入学生信息功能函数定义  
{  
    struct Student *p1,*p2;  
    if (stu == NULL)            //对于传入插入学生信息为空时要求用户输入插入的学生信息  
    {     
        printf("   请按照学号、年龄、姓名、出生年月、性别、E-mail、联络电话、家庭住址的顺序输入\n   ");  //每个学生信息的输入顺序  
        stu = (struct Student *) malloc (LEN);  
        while(!scanf("%d %s %s %s %s %s %s %s",&stu->num,stu->age,stu->name,stu->born,stu->sex,stu->Email,stu->phone,stu->addr))  
        {  
            printf("   输入信息有误,请重新输入\n");  
            fflush(stdin);  
        }  
    }  
    if (head == NULL)           //对于链表为空的情况的处理  
    {  
        head = stu;  
        head->next = NULL;  
    }  
    else                        //在链表中插入学生信息  
    {  
        p1 = p2 = head;  
        while(stu->num > p2->num && p1->next != NULL)       //查找适合位置的前一个学生  
        {  
            p2 = p1;  
            p1 = p1->next;  
        }  
        if (p2 == p1)           //插入的学生正好为链表头的情况  
        {  
            if (stu->num < p2->num)            //插入在表头前  
            {  
                head = stu;  
                stu->next = p2;  
            }  
            else                            //插入在表头后  
            {  
                p2->next = stu;  
                stu->next = NULL;  
            }  
        }  
        else                    //插入的学生在链表中的情况  
        {  
            if (stu->num < p1->num)            //插入到p1指向的学生前  
            {  
                p2->next = stu;  
                stu->next= p1;  
            }  
            else                            //插入到p1指向的学生后  
            {  
                p1->next = stu;  
                stu->next =  NULL;  
            }  
        }  
    }  
    kg = 1;  
    return(head);  
}  
  
struct Student *del(struct Student *head)               //删除学生信息的功能函数定义  
{  
    unsigned int num,k;  
    struct Student *p1,*p2;  
    if (head == NULL)                                   //对空链表删除的情况处理  
    {  
        printf("   没有学生信息,结束删除\n");      
        return(head);  
    }  
    printf("   请输入要删除的学生学号: ");             //求用户输入要删除的学生学号  
    while(!scanf("%u",&num))                            //接收学生序号并进行排错处理  
    {  
        printf("   输入有误,请重新输入学生序号: ");  
        fflush(stdin);  
    }  
    do  
    {  
        p1 = p2 = head;  
        while(num != p1->num && p1->next != NULL)     //查找删除位置的前一个学生  
        {  
            p2 =p1;  
            p1= p1->next;  
        }  
        if (num == p1->num)                              //对满足要求的学生进行删除  
        {  
            if (num == p1->num)  
            {  
                if (p1->next == NULL && p1 == head)      //对只有一个节点的链表进行删除  
                {  
                    free(p1);  
                    head = NULL;  
                }  
                else if (p1->next == NULL)               //删除链表尾节点  
                {  
                    free(p1);  
                    p2->next = NULL;  
                }  
                else if (head == p1)                    //删除表头节点  
                {  
                    head = p1->next;  
                    free(p1);  
                }  
                else                                    //删除表中节点  
                {  
                    p2->next = p1->next;  
                    free(p1);  
                }  
            }  
            printf("   成功删除学生信息\n");            //提示删除成功信息  
        }  
        else                                            //找不到要删除的学生时的提示  
        {  
            printf("   找不到该同学信息\n");  
            fflush(stdin);  
        }  
        if (head == NULL)                               //对空链表删除时的处理  
        {  
            printf("   没有学生信息,结束删除\n");      
            return(head);  
        }  
        else                                            //提示用户删除后是否继续  
        {  
            printf("   1.继续删除学生信息\n");  
            printf("   2.结束删除\n");  
            printf("   请选择: ");  
            while(!scanf("%u",&k))                      //接收用户选择  
            {  
                printf("   输入有误,请重新输入选择的序号: ");  
                fflush(stdin);  
            }  
            if (k == 1)                                 //选择继续  
            {  
                printf("   请输入要删除的学生学号: ");  
                while(!scanf("%u",&num))  
                {  
                    printf("   输入有误,请重新输入学生学号: ");  
                    fflush(stdin);  
                }  
            }  
            else if (k != 2)                            //对于不是选择2的继续循环提示  
            {  
                k = 1;  
            }  
        }  
    }while(k == 1);   
    return(head);       //返回表头地址  
}  
  
void search(struct Student *head,unsigned int num)      //查询函数的定义  
{  
    unsigned int cho,t = 0;  
    struct Student *p1;  
    if (head == NULL)       //对查询空链表时的操作  
    {  
        printf("   没有学生信息,结束查询\n");      
        return;  
    }  
    p1 = head;  
    if (num == 0)           //当传入实参 num = 0 需要接受用户需要的查询方式(个别或者全部),不等于0则可以直接查询个别学生信息  
    {  
        printf("   1.查询个别学生信息\n");  
        printf("   2.查询全部学生信息\n");  
        printf("   请选择: ");  
        while(!scanf("%u",&cho))    //对错误输入数据的处理  
        {  
            printf("   输入有误,请重新输入序号: ");  
            fflush(stdin);  
        }  
        while(cho != 1 && cho != 2)                                                     //处理不能处理的功能序号                                                
        {  
            printf("   抱歉,没有此功能,请重新输入功能序号: ");              //提示不能处理的信息,并要求用户重新输入功能序号  
            while(!scanf("%u",&cho))                                                        //接收用户重新输入的功能序号  
            {  
                printf("   输入序号有误,请重新输入序号: ");  
                fflush(stdin);  
            }  
            printf("\n");                                                       //输出回车  
        }  
    }  
    else  
    {  
        cho = 1;         //设置直接查询个别学生的开关  
    }  
    if (kg == 1)        //kg(全局变量)为全部学生查询的开关  kg = 1 打开开关由下面直接进行全部学生查询 kg = 0 关闭开关  
    {  
        cho = 2;  
    }  
    switch(cho) //查询类别分支  
    {  
        case 1: //查询个别学生  
        {  
            do  
            {  
                p1 = head;  
                if (num == 0 || t == 1)   
                {  
                    printf("   请输入需要查询的学生学号: ");  
                    while(!scanf("%u",&num))  
                    {  
                        printf("   输入有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    t = 1;  //打开开关,该开关为查询完毕后 num不能作为该分支的条件(因为以下的操作会改变num的值)而建立的,并且能够实现当传入num不等于0时,以下询问用户是否继续的分支就不用执行了  
                }  
                else  
                {  
                    cho = 2; //如果传入的实参num不等于0  则该 DO WHILE 循环只做一次  
                }  
                while(p1->num != num)    //查找查询学生学号  
                {  
                    if (p1->next == NULL) break;  
                    p1 = p1->next;  
                }  
                if (p1->num == num)  //查找成功一表格方式输出该学生信息  
                {  
                    //***********************表格头部设计************************  
                    printf("   ┏━━┳━━┳━━━━┳━━━━┳━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━┓\n");  
                    printf("   ┃学号┃年龄┃  姓名  ┃出生年月┃性别┃E-mail             联络电话             家庭住址            ┃\n");  
                    //**********************************************************  
  
                    //****************表格主体设计以及数据输出******************  
                    printf("   ┣━━╋━━╋━━━━╋━━━━╋━━╋━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━┫\n");  
                    printf("   ┃%-4d┃%-4s┃%-8s┃%-8s┃%-4s┃%-12s    ┃%-13s   ┃%-15s               ┃\n",p1->num,p1->age,p1->name,p1->born,p1->sex,p1->Email,p1->phone,p1->addr);
                    //***********************表格头部设计***********************  
                    printf("   ┗━━┻━━┻━━━━┻━━━━┻━━┻━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━┛\n");
                    //*********************************************************  
                }  
                else //查找不成功,提示用户找不到该学生  
                {  
                    printf("   找不到该学生\n");  
                }  
                if (t == 1)  //如果传入的num=0,则此分支有效,询问用户是否继续查询  
                {  
                    printf("   1.继续查询\n");  
                    printf("   2.结束查询\n");  
                    printf("   请选择: ");  
                    while(!scanf("%u",&cho))  //对输入序号查错,利用cho = 2时的情况跳出  DO WHILE 循环  
                    {  
                        printf("   输入序号有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                }  
            }while (cho == 1);  
            break;  
        }  
        case 2: //查询全部信息  
        {  
            //***********************表格头部设计************************  
            printf("   ┏━━┳━━┳━━━━┳━━━━┳━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━┓\n");  
            printf("   ┃学号┃年龄┃  姓名  ┃出生年月┃性别┃E-mail             联络电话             家庭住址            ┃\n");   

            //**********************************************************  
            //****************表格主体设计以及数据输出******************  
            while(p1 != NULL)  
            {   
            printf("   ┣━━╋━━╋━━━━╋━━━━╋━━╋━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━┫\n");  
            printf("   ┃%-4d┃%-4s┃%-8s┃%-8s┃%-4s┃%-12s    ┃%-13s   ┃%-15s               ┃\n",p1->num,p1->age,p1->name,p1->born,p1->sex,p1->Email,p1->phone,p1->addr);
                p1 = p1->next;  
            }  
            //**********************************************************  
            //***********************表格头部设计***********************  
             printf("   ┗━━┻━━┻━━━━┻━━━━┻━━┻━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━┛\n");
            //**********************************************************  
            break;  
        }  
    }  
    if (kg == 1) kg = 0; //关闭查询全部学生的开关,以便下一次使用  
}  
  

  
  
  
  
  
void alter(struct Student *head)  //修改学生信息的功能函数定义  
{  
    struct Student *p1,*p2;  
    unsigned int num,cho,k,l=0;  
    if (head == NULL)   //对空链表的处理  
    {  
        printf("   没有学生信息,结束修改\n   ");  
        system("pause");  
        return;  
    }  
    printf("   请输入需要修改的学生学号: ");  
    while(!scanf("%u",&num))    //接受用户输入的学号并且进行查错  
    {  
        printf("   输入学号有误,请重新输入: ");  
        fflush(stdin);  
    }  
    do  
    {  
        p2 = p1 = head;  
        while(p1->num != num)    //查找学生学号  
        {  
            if (p1->next == NULL)  
                break;  
            p2 = p1;  
            p1 = p1->next;  
        }  
        if (p1->num == num)      //查找成功  
        {  
            printf("   已找到该学生,该学生的信息为: \n");  
            search(head,num);  
            l = 0;  
            do  
            {  
                if (l == 0)     //开关,当第一次循环时 l=0 执行 IF 语句  
                    printf("   选择修改的内容\n");  
                else      
                    printf("   序号输入错误,请重新选择\n");  
                l = 1;  //打开开关,以在以下操作用户输入序号超出范围时控制上面的分支语句,进而可以更好地向用户提示信息  
                printf("   1.学号\n");  
                printf("   2.年龄\n");  
                printf("   3.姓名\n");  
                printf("   4.出生年月\n");  
                printf("   5.性别\n");  
                printf("   6.E-mail\n");  
                printf("   7.联络电话\n");  
                printf("   8.家庭住址\n");  
                printf("   9.全部\n");  
                printf("   请选择序号: ");  
                fflush(stdin);  
                while(!scanf("%u",&cho)) //接受序号并查错  
                {  
                    printf("   输入序号有误,请重新输入: ");  
                    fflush(stdin);  
                }  
            }while(cho > 9 || cho < 1);  
            switch(cho)  //对序号进行多分支处理  
            {  
                case 1:  
                {  
                    printf("   请输入该学生改正的学号信息: ");  
                    while(!scanf("%u",&p1->num))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 2:  
                {  
                    printf("   请输入该学生改正的年龄信息: ");  
                    while(!scanf("%s",p1->age))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 3:  
                {  
                    printf("   请输入该学生改正的姓名信息: ");  
                    while(!scanf("%s",p1->name))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 4:  
                {  
                    printf("   请输入该学生改正的出生年月信息: ");  
                    while(!scanf("%s",p1->born))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 5:  
                {  
                    printf("   请输入该学生改正的性别信息: ");  
                    while(!scanf("%s",p1->sex))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 6:  
                {  
                    printf("   请输入该学生改正的E-mail信息: ");  
                    while(!scanf("%s",p1->Email))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 7:  
                {  
                    printf("   请输入该学生改正的联络电话信息: ");  
                    while(!scanf("%s",p1->phone))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 8:  
                {  
                    printf("   请输入该学生改正的家庭住址信息: ");  
                    while(!scanf("%s",p1->addr))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
                case 9:  
                {  
                    printf("   请输入该学生全部要改正的信息: ");  
                    while(!scanf("%u %s %s %s %s %s %s %s",&p1->num,p1->age,p1->name,p1->born,p1->sex,p1->Email,p1->phone,p1->addr))  
                    {  
                        printf("   输入改正信息有误,请重新输入: ");  
                        fflush(stdin);  
                    }  
                    break;  
                }  
            }  
            if (cho == 1 || cho == 9)   //对修改过学号的学生进行重新排序  
            {  
                if (p1 == head) //当该学生在链表头时,删除链表头节点  
                {  
                    head = head->next;  
                }  
                else if (p1->next == NULL) //当该学生在链表尾时,删除链表尾节点  
                {  
                    p2->next = NULL;  
                }  
                else    //当该学生在链表中时,删除该学生对应的节点  
                {  
                    p2->next = p1->next;  
                }  
                head = insert(head,p1); //利用插入学生信息功能函数,进行该学生的重新排序  
            }  
            printf("   修改成功,该学生改正后的信息为: \n");  
            search(head,p1->num);    //显示以下修改后的学生信息  
  
            //询问用户是否继续  
            printf("   1.继续修改其他学生信息\n");  
            printf("   2.退出修改\n");  
            printf("   请选择: ");  
            while(!scanf("%u",&k))      //接受用户输入的序号,并进行排错  
            {  
                printf("   输入序号有误,请重新输入: ");  
                fflush(stdin);  
            }  
            if (k == 1) //接受下一次修改的学生学号  
            {  
                printf("   请输入需要修改的学生学号: ");  
                while(!scanf("%u",&num))  
                {  
                    printf("   输入修改信息有误,请重新输入: ");  
                    fflush(stdin);  
                }  
            }  
            else if (k != 2) //排错  
            {  
                printf("   输入有误,请重新输入\n");  
            }  
        }  
        else    //查找不到学生时要求用户进行重新输入学生序号  
        {     
            k = 1;  
            printf("   找不到该学生信息,请重新输入需要修改的学生学号: ");  
            fflush(stdin);  
            while(!scanf("%u",&num));  //接受用户重新输入的学号并进行排错  
            {  
                printf("   输入修改信息有误,请重新输入: ");  
                fflush(stdin);  
            }  
        }  
    }while(k == 1);  
    printf("   ");  
    system("pause");  
}  
  
  
void output(struct Student *head)   //保存学生信息到txt文件功能函数  
{  
    FILE *write;  
    char filename[20];  
    struct Student *p1,*p2;  
    unsigned int cho,n;  
    if (head == NULL)   //对空链表进行处理  
    {  
        printf("   没有学生信息,保存失败\n   ");   
        system("pause");  
        return;  
    }  
    p1 = p2 = head;  
    printf("   请输入保存的文件名(如save): ");  
    while(!scanf("%s",filename))    //接受文件名,并进行排错(几乎不会出现错误的现象,慎重而设)  
    {  
        printf("   输入的文件名有误,请重新输入:");  
        fflush(stdin);  
    }  
    strcat(filename,".txt");    //链接后缀名  
    write = fopen(filename,"w");    //以写入方式打开文件  
  
    //询问用户用哪种方式保存  
    printf("   1.以表格形式保存(不可重新录入)\n");  
    printf("   2.以输入形式保存(可重新录入)\n");  
    printf("   请选择: ");  
    while(!scanf("%u",&cho))    //接受用户输入的序号并进行排错  
    {  
        printf("   输入序号有误,请重新输入: ");  
        fflush(stdin);  
    }  
    while(cho < 1 || cho > 2) //对超出范围的序号进行排错  
    {  
        printf("   输入序号有误,请重新输入: ");  
        while(!scanf("%u",&cho))  
        {  
            printf("   输入序号有误,请重新输入: ");    //对输入错误的序号进行排错  
            fflush(stdin);  
        }  
    }  
    if (cho == 1)   //以表格形式保存  
    {  
        //***********************表格头部设计************************  
        fprintf(write,"   ┏━━┳━━┳━━━━┳━━━━┳━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━┓\n");  
        fprintf(write,"   ┃学号┃年龄┃  姓名  ┃出生年月┃性别┃E-mail             联络电话             家庭住址            ┃\n");   
        //**********************************************************  
        //****************表格主体设计以及数据输出******************  
        while(p1 != NULL)  
        {     
        fprintf(write,"   ┣━━╋━━╋━━━━╋━━━━╋━━╋━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━┫\n");  
        fprintf(write,"   ┃%-4d┃%-4s┃%-8s┃%-8s┃%-4s┃%-12s┃%-13s  ┃%-15s               ┃\n",p1->num,p1->age,p1->name,p1->born,p1->sex,p1->Email,p1->phone,p1->addr);  
            p1 = p1->next;  
        }  
        //**********************************************************  
        //***********************表格头部设计***********************  
        fprintf(write,"   ┗━━┻━━┻━━━━┻━━━━┻━━━┻━━━━━━━┻━━━━━━━━━┻━━━━━━━━━━━━━━┛\n");  
        //**********************************************************  
  
    }  
    else    //以输入的格式保存  
    {  
        n = 0;  
        fprintf(write,"                                   \n");  //为输出学生人数留位置  
        while(p1 != NULL)  
        {     
            fprintf(write,"%u %s %s %s %s %s %s %s\n",p1->num,p1->age,p1->name,p1->born,p1->sex,p1->Email,p1->phone,p1->addr);  
            p1 = p1->next;  
            n++;    //每向文件输出一位学生信息n就累加1  
        }  
        fseek(write,0,SEEK_SET);    //移到文件头部  
        fprintf(write,"%u",n);      //写入学生个数  
  
    }  
    fclose(write);  
    printf("   保存成功\n   ");  
    system("pause");  
}  

  
void main()  
{     
    show();     //调用显示功能函数  
}  




麻烦大家会的 帮忙加入按名字,学号,年龄排序的程序

[此贴子已经被作者于2017-2-17 19:47编辑过]

搜索更多相关主题的帖子: 管理系统 include choice Email 信息 
2017-02-17 19:43
yanzy
Rank: 5Rank: 5
等 级:职业侠客
威 望:2
帖 子:104
专家分:372
注 册:2017-2-7
收藏
得分:1 
https://bbs.bccn.net/viewthread.php?tid=474171&page=1#pid2614426
这个帖子我也写了个类似的,其中有个排序函数,算法粗糙,楼主可以参考一下
2017-02-18 00:22
xzlxzlxzl
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖北
等 级:贵宾
威 望:125
帖 子:1091
专家分:5825
注 册:2014-5-3
收藏
得分:1 
我是不是可以借机宣传下我那万能、通用的链表排序函数了?代码及调用方法如下:
程序代码:
struct Student *lsort(struct Student *head,int offset,int vartype,int sortflg)
{
    /*移动节点方式链表排序参数说明:
    表头指针       head
    排序变量偏移量 offset
    排序变量类型   vartype(0:char、1:int、2:float、3:double、4、string)
    排序方式       sortflg(0:增序、1:降序)*/
    struct Student *pi,*pj,*p,*prei,*prej,*ppj;
    unsigned long i,j,k;
    char *psi,*psj;
    for(prei=NULL,pi=head;pi->next;prei=pi,pi=pi->next)
    {
        for(p=prej=ppj=pi,pj=pi->next;pj;prej=pj,pj=pj->next)
        {
            i=(unsigned long)p+offset;
            j=(unsigned long)pj+offset;
            k=0;
            if(vartype==0)k=(int)(*(char*)i>*(char*)j)^sortflg;
            if(vartype==1)k=(int)(*(int*)i>*(int*)j)^sortflg;
            if(vartype==2)k=(int)(*(float*)i>*(float*)j)^sortflg;
            if(vartype==3)k=(int)(*(double*)i>*(double*)j)^sortflg;
            if(vartype==4)
            {
                for(psi=(char*)i,psj=(char*)j;*psi&&*psj&&*psi==*psj;psi++,psj++);
                if(*psi-*psj>0)k=1^sortflg;
            }
            if(k)
            {
                ppj=prej;
                p=pj;
            }
        }
        if(p!=pi)
        {                           //找到最大或最小的元素节点p,需要将p移到pi前面
            ppj->next=p->next;      //首先将p从链表中跳开,让p前节点直接指向p后节点
            p->next=pi;             //p指向pi,已经移到当前节点前面,但此时只在链表旁路,不在整个链表中
            if(prei)prei->next=p;   //如果当前节点前一个节点有指向,则修改为指向p,p即加入链表
            else head=p;            //如果当前节点前一个节点无指向,则p就是链表头节点,p同样加入链表
            pi=p;                   //将当前节点修改为刚刚移动的节点,让循环继续
        }
    }
    return head;
}


按姓名顺序排序调用:
head=lsort(head,(int)(head->name)-(int)head,4,0);   //将0改为1则会按字母表倒序排序

按年龄顺序排序调用:
head=lsort(head,(int)(head->age)-(int)head,4,0);    //真不知道你为什么把年龄也定义为字符串

按学号顺序排序调用:
head=lsort(head,(int)(&head->num)-(int)head,1,0);  //将0改为1可按学号倒序排序
收到的鲜花
  • 炎天2017-02-21 01:05 送鲜花  6朵   附言:好文章
2017-02-18 09:46
快速回复:学生信息管理系统的排序代码怎么写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053034 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved