初级会员 
  
	- 积分
 - 59
 
        - 金钱
 - 59 
 
       - 注册时间
 - 2017-7-23
 
      - 在线时间
 - 20 小时
 
 
 
 | 
 
1金钱 
 
#include <stdio.h> 
#include <malloc.h> 
#include <conio.h> 
#include <stdlib.h> 
void print(struct student *head); 
struct student *insert(struct student *stu_2,struct student *head); 
void KeepMessage(struct student *head); 
//链表单元定义,链表相关变量 
unsigned int                 Prin_n; 
struct student  
{ 
    int id; 
    float score; 
        char name[20]; 
        char sex[20]; 
    struct student *next; 
} *head,*pthis; 
 
//输入数据创建链表 
void input() 
{ 
    struct student *tmp; 
        FILE *fp; 
    printf("\n\n请输入学生的信息以学号为0结束!!:\n"); 
        if(!(fp=fopen("student_list","wb")))//打开文本 
        { 
                printf("cannot\n"); 
                return; 
        } 
        do 
        { 
                 
        printf("ID\t成绩\t姓名\t性别\t以Tab或者回车输入\n"); 
 
                if ((tmp=(struct student *)malloc(sizeof(struct student)))==NULL)//申请内存 
                { 
            printf("\n错误!不能申请所需的内存!\n"); 
            exit(0); 
        } 
        scanf("%d\t%f\t%s\t%s",&tmp->id,&tmp->score,tmp->name,tmp->sex);//输入信息 
                fwrite(tmp,sizeof(struct student),1,fp); 
        tmp->next=NULL; 
 
                if (tmp->id!=0)  
                {         
                         
            if (head==NULL)  
                        { 
                head=tmp; 
                pthis=head; 
            }  
                        else  
                        { 
                pthis->next=tmp; 
                pthis=pthis->next; 
            } 
        } 
        else 
        { 
                tmp->next=NULL; 
        } 
         
         
    } while (tmp->id!=0); 
          
        fclose(fp);        //关闭文件 
        free(tmp);//释放内存地址 
         
} 
 
void  KeepMessage(struct student *head)//保存数据函数存入二进制文件 
{ 
        FILE *fp; 
        struct student *bianl; 
        bianl=head; 
        if(!(fp=fopen("student_list","wb"))) 
                { 
                        printf("cannot\n"); 
                        return; 
                }              
        do{ 
                fwrite(bianl,sizeof(struct student),1,fp); 
                bianl=bianl->next; 
                }while(bianl!=NULL); 
 
        fclose(fp); 
//        return temp;         
 
} 
 
//搜索链表找到第一个符合条件的项目输出 
void search(int id) 
 {         
        FILE *fp; 
    printf("\n\n查询结果\n"); 
    printf("ID\t成绩\n"); 
    printf("-------------------------------\n"); 
         
        if(!(fp=fopen("student_list","r")))//防止无文件 
        { 
                printf("不能读写\n"); 
                return ; 
        } 
    if (head==NULL)  
        { 
        printf("\n错误!没有数据!\n"); 
        return; 
    } 
    pthis=head; 
    while (pthis!=NULL) 
        { 
        if (pthis->id==id)  
                { 
            printf("%d\t%.2f %s %s\n",pthis->id,pthis->score,pthis->name,pthis->sex); 
            return; 
        } 
                else 
                { 
            pthis=pthis->next; 
        } 
    } 
    printf("\n没有找到!\n"); 
} 
 
//列表输出链表中的所有项排序 
void list()  
{ 
        struct student *view_sort_chinese(struct student *h); 
        FILE *fp; 
        void print(struct student *head); 
                if(!(fp=fopen("student_list","r"))) 
        { 
                printf("不能读写\n"); 
                return ; 
        } 
    printf("\n\n数据列表\n"); 
    printf("ID\t成绩\t姓名\t性别\t\n"); 
    printf("-------------------------------\n"); 
        printf("按学号排序如下\n"); 
        printf("-------------------------------\n"); 
 
         
    if (head==NULL)  
        { 
        printf("错误,没有数据!\n"); 
        return; 
    } 
        print (view_sort_chinese(head)); 
 
 
} 
struct student *view_sort_chinese(struct student *h)  //冒泡排序       
{ 
 
                //在调用函数里面判断head=NULL,这里不判断。 
                struct student *endpt,*u,*v,*p;      
                u = (struct student *)malloc(sizeof(head));    
                u->next=h;    
                h=u;   
 
                for(endpt=NULL;endpt!=h;endpt=p)  
                { 
                        for(p=u=h;u->next->next!=endpt;u=u->next)   
                        { 
                                if(u->next->id > u->next->next->id) 
                                {                             
                                        v=u->next->next;    
                                        u->next->next=v->next;    
                                        v->next=u->next;    
                                        u->next=v;    
                                        p=u->next->next;    
                                }  
                        } 
                } 
                u=h;    
                h=h->next; 
                head=h; 
                return head;  
} 
 
void print(struct student *head)//打印文本 
        { 
                                struct student *p; 
                                p=head; 
                                if(head) 
                                { 
                                                do 
                                        { 
                                                printf("%d\t%.2f %s %s\n",p->id,p->score,p->name,p->sex); 
                                                p=p->next; 
                                        }while(p!=NULL);//只打印大于0的数;&&(p->id>0) 
                                } 
                                else 
                                { 
                                        printf("打印错误,文件为空\n"); 
                                } 
 
        } 
 
 
 
//追加数据 
struct student *insert(struct student *hea,struct student *stu)// 
{ 
        struct student *p0,*p1; 
         
        p1 = hea; 
        p0 = stu; 
        if(NULL  == hea) 
        { 
                head=p0; 
                p0->next=NULL; 
                return head; 
        } 
        else 
        { 
                p0->next=hea; 
                head=p0; 
         
        } 
        KeepMessage(head); 
        return head; 
} 
 
//删除数据 
struct student *del(int num) 
{ 
        struct student *p1,*p2; 
        if(NULL==head) 
        { 
                printf("\nthis list is null!\n"); 
                return head; 
        } 
 
        p1=head; 
        while(p1->id != num && p1->next != NULL) 
        { 
                p2=p1; 
                p1=p1->next; 
        } 
        if(num==p1->id) 
        { 
                if(p1==head) 
                { 
                        head=p1->next; 
                } 
                else 
                { 
                        p2->next=p1->next; 
                } 
                printf("\n删除成员:%d 成功!\n",num); 
        } 
        else  
        { 
                printf("%d没有找到!\n",num); 
        } 
         
        KeepMessage(head); 
        return head; 
         
} 
 
struct student *load1() 
{ 
        FILE *fp; 
 
        struct student *temp;         
        head=NULL; 
        if(!(fp=fopen("student_list","r"))) 
        { 
                printf("不能读写,或无文件\n"); 
                return 0; 
        } 
 
        do 
        { 
                if ((temp=(struct student *)malloc(sizeof(struct student)))==NULL) 
                { 
                        printf("\n错误!不能申请所需的内存!\n"); 
                        exit(0); 
                } 
                fread(temp,sizeof(struct student),1,fp); 
                temp->next=NULL; 
                if(!(!feof(fp)&&(temp->id>0))) 
                { 
                        break; 
                } 
                        if (head==NULL)  
                                { 
                                        head=temp; 
                                        pthis=head; 
                                }  
                                else  
                                { 
                                        pthis->next=temp; 
                                        pthis=pthis->next; 
                                } 
        }while(!feof(fp)&&(temp->id>0)); 
 
        fclose(fp); 
        return head; 
         
} 
struct student *load() 
{ 
        FILE *fp; 
        struct student *temp;         
        head=NULL; 
        if(!(fp=fopen("student_list","r"))) 
        { 
                printf("不能读写\n"); 
                return 0; 
        }         
        do 
        { 
                if ((temp=(struct student *)malloc(sizeof(struct student)))==NULL) 
                { 
                        printf("\n错误!不能申请所需的内存!\n"); 
                        exit(0); 
                } 
                fread(temp,sizeof(struct student),1,fp); 
                temp->next=NULL; 
 
                        if (temp->id!=0)  
                        {         
                                 
                                if (head==NULL)  
                                { 
                                        head=temp; 
                                        pthis=head; 
                                }  
                                else  
                                { 
                                        pthis->next=temp; 
                                        pthis=pthis->next; 
                                } 
                        } 
 
        } 
        while (temp->id!=0); 
         
        fclose(fp); 
        return head; 
} 
 
 
void Dere_list() 
{ 
                struct student *stu_2; 
    char command=0; 
    int id=0; 
        int n; 
        int i; 
        do { 
        printf("\n\n\t 菜单\n"); 
        printf("-------------------------------\n"); 
        printf("\ta,输入数据\n"); 
        printf("\tc,排列顺序\n"); 
        printf("\td,插入数据\n"); 
        printf("\te,查询数据\n"); 
        printf("\tf,删除记录\n"); 
                printf("\th,载入文本数据\n"); 
        printf("\tg,退出系统\n"); 
        printf("-------------------------------\n"); 
        printf("\t请选择:");         
        command=getch(); 
                printf("\n"); 
//命令处理 
        switch (command)  
                { 
        case 'a': 
            if (head==NULL)  
                        { 
                input(); 
                break; 
            }  
                        else  
                        { 
                printf("\n\n数据已经存在!\n"); 
                break; 
            } 
        case 'c': 
            list(); 
            break; 
        case 'd': 
                if ((stu_2=(struct student *)malloc(sizeof(struct student)))==NULL)//申请stu_2内存,可以连续输入,以占用上次。 
                { 
                        printf("\n错误!不能申请所需的内存!\n"); 
                        exit(0); 
                } 
            printf("\nID\t成绩\t姓名\t性别\t以Tab或者回车输入\n"); 
                        scanf("%d\t%f\t%s\t%s",&stu_2->id,&stu_2->score,stu_2->name,stu_2->sex); 
                        if(stu_2->id<0) 
                                { 
                                        printf("输入有误!\n"); 
                                } 
                                print(insert(head,stu_2)); 
                        //        free(stu_2); 
                                 
            break; 
        case 'e': 
                        scanf("%d",&i); 
                        search(i); 
                        //print(head); 
 
            break; 
        case 'f': 
                        printf("请输入学号ID\n"); 
            scanf("%d",&n); 
                        print(del(n)); 
                 
            break; 
                case 'h': 
                        load1(); 
                        print(head); 
        } 
    } while (command!='g'); 
 
        printf("按任意键退出\n"); 
} 
 
//程序主函数 
void main()  
{ 
 
//主循环 
        Dere_list(); 
         
 
} 
 
 
 
 |   
 
 
 
 
 
 |