m0_58799099 2021-12-13 18:45 采纳率: 100%
浏览 42
已结题

谁能帮我排列这个C语言

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
struct books_list { char writer[20];            /作者名/ char title[20]; char ISBN[20];                      /书名/ char publishinghouse[20];          /出版单位/ int  price,borrowed;                        /价格/
struct books_list * next; /链表的指针域/ };
struct books_list * Create_Books_Doc();              /新建链表/ void InsertDoc(struct books_list * head);            /插入/ void sort_maopao(struct books_list * head );          // 排序 void DeleteDoc(struct books_list * head , int num);  /删除/ void Print_Book_Doc(struct books_list * head);        /浏览/ void search_book(struct books_list * head);          /查询/ void info_change(struct books_list * head);          /修改/ void save(struct books_list * head);                  /保存数据至文件/ /新建链表头节点/ struct books_list * Create_Books_Doc() { struct books_list * head; head=(struct books_list *)malloc(sizeof(struct books_list)); /分配头节点空间/ head->next=NULL;  /头节点指针域初始化,定为空/ return head; } /保存数据至文件/ void save(struct books_list * head) { struct books_list *p; FILE *fp; p=head; fp=fopen("data.txt","w+"); /以写方式新建并打开 data.txt文件/ fprintf(fp," --------编号-------书  名---------------作    者----------出版社-----------价格    \n"); /指针从头节点开始移动,遍历至尾结点,依次输出图书信息/ while(p->next!= NULL) { p=p->next; fprintf(fp,"%-6.6s %-15.10s %-10.10s %-10.10s %-10.10d\n",p->ISBN,p->title,p->writer,p->publishinghouse,p->price); } fprintf(fp,"\n"); fclose(fp); printf("    已将图书数据保存到 data.txt 文件\n"); } void sort_maopao(struct books_list * head ) { struct books_list *tail,*p,*q,*p1,*t; //p1等于head t等于head q保存p的next节点 p1=(struct books_list *)malloc(sizeof (struct books_list)); for(t=head->next;t!=NULL;t=t->next) for(p=head->next,p1=head;p->next!=NULL;p=p->next,p1=p1->next) { if(strcmp(p->ISBN,p->next->ISBN)>0) {  q=p->next->next; tail=p->next; tail->next=NULL; p->next=q; p1->next=tail; tail->next=p; p=p1; } } save(head);  //保存文件 } /插入/ void InsertDoc(struct books_list *head) { /定义结构体指针变量 s指向开辟的新结点首地址 p为中间变量/ struct books_list *s, *p; char flag='Y'; /定义flag,方便用户选择重复输入/ p=head; /遍历到尾结点,p指向尾结点/ while(p->next!= NULL) { p=p->next; }  /开辟新空间,存入数据,添加进链表/ while(flag=='Y'||flag=='y') { system("cls"); p->borrowed=0; s=(struct books_list *)malloc(sizeof(struct books_list)); printf("\n                    请输入图书编号:"); fflush(stdin); scanf("%s",s->ISBN); printf("\n                    请输入图书书名:"); fflush(stdin); scanf("%s",s->title); printf("\n                    请输入图书作者名:"); fflush(stdin); scanf("%s",s->writer); printf("\n                    请输入图书出版社:"); fflush(stdin); scanf("%s",s->publishinghouse); printf("\n                    请输入图书价格:"); fflush(stdin); scanf("%d",&s->price); printf("\n"); p->next=s;  /将新增加的节点添加进链表/ p=s;  /p指向尾节点,向后移/ s->next=NULL; printf("                  ━━━━ 添加成功!━━━━"); printf("\n                          继续添加?(Y/N):"); fflush(stdin); scanf("%c",&flag); printf("\n"); if(flag=='N'||flag=='n') {break;} else if(flag=='Y'||flag=='y') {continue;} } save(head);  /保存数据至文件/ system("cls"); return; } /查询操作/ void search_book(struct books_list *head) { struct books_list * p; char temp[20]; p=head; if(head==NULL || head->next==NULL) /判断数据库是否为空/ { printf("                      ━━━━ 图书库为空!━━━━\n"); } else { printf("请输入您要查找的书名: "); fflush(stdin); scanf("%s",temp); /指针从头节点开始移动,遍历至尾结点,查找书目信息/ while(p->next!= NULL) { p=p->next; if(strcmp(p->title,temp)==0) { printf("\n图书已找到!\n"); printf("\n"); printf("编号: %s\t\n",p->ISBN); printf("书名: %s\t\n",p->writer); printf("作者名: %s\t\n",p->writer); printf("出版单位: %s\t\n",p->publishinghouse); printf("价格: %.2d\t\n",p->price); } if(p->next==NULL) { printf("\n查询完毕!\n"); } } } return; } /浏览操作/ void Print_Book_Doc(struct books_list * head) { struct books_list * p; if(head==NULL || head->next==NULL)  /判断数据库是否为空/ { printf("\n                      ━━━━  没有图书记录!  ━━━━\n\n"); return; } p=head; printf("\n\n\n\t\t编号    书 名        作 者    出版单位    价格  \n"); /指针从头节点开始移动,遍历至尾结点,依次输出图书信息/ while(p->next!= NULL) { p=p->next; printf("\t\t%-7.6s %-13.10s %-10.10s %-10.10s %d \n",p->ISBN,p->title,p->writer,p->publishinghouse,p->price); } printf("\n"); } /修改操作/ void info_change(struct books_list * head) { struct books_list * p; int panduan=0; /此变量用于判断是否找到书目/ char temp[20]; p=head; printf("请输入要修改的书名:"); scanf("%s",temp); while(p->next!= NULL) { p=p->next; if(strcmp(p->title,temp)==0) { printf("\n                    请输入图书编号:"); fflush(stdin); scanf("%d",p->ISBN); printf("\n                    请输入图书书名:"); fflush(stdin); scanf("%s",p->title); printf("\n                    请输入图书作者名:"); fflush(stdin); scanf("%s",p->writer); printf("\n                    请输入图书出版社:"); fflush(stdin); scanf("%s",p->publishinghouse); printf("\n                    请输入图书价格:"); fflush(stdin); scanf("%d",&p->price); printf("\n"); panduan=1; } } if(panduan==0) { printf("\n                      ━━━━  没有图书记录!  ━━━━\n\n"); } return; } void book_borrow(struct books_list * head) { struct books_list * p; int panduan=0; /此变量用于判断是否找到书目/ char temp[20]; p=head; printf("请输入要借的书名:"); scanf("%s",temp); while(p->next!= NULL) { p=p->next; if((strcmp(p->title,temp)==0)&&(p->borrowed==0)) { p->borrowed=1; panduan=1; printf("图书已经成功借出\n"); } } if(panduan==0) { printf("\n                      ━━━━  没有要借阅的图书!  ━━━━\n\n"); } return; } /删除操作/ void DeleteDoc(struct books_list * head) { struct books_list *s,*p;    /s为中间变量,p为遍历时使用的指针/ char temp[20]; int panduan; /此变量用于判断是否找到了书目/ panduan=0; p=s=head; printf("                    [请输入您要删除的书名]:"); scanf("%s",temp); /遍历到尾结点/ while(p!= NULL) { if(strcmp(p->title,temp)==0) { panduan++; break; } p=p->next; } if(panduan==1) { for(;s->next!=p;)    /找到所需删除卡号结点的上一个结点/ { s=s->next; } s->next=p->next; /将后一节点地址赋值给前一节点的指针域/ free(p); printf("\n                      ━━━━  删除成功!  ━━━━\n"); } else /未找到相应书目/ { printf("                    您输入的书目不存在,请确认后输入!\n"); } return; } int main(void) { struct books_list * head; int choice; head=NULL; do{ printf("                                                              \n");  printf("          -------------------- 图书管理系统-------------------\n\n"); printf("                              [1]图书信息录入                \n\n"); printf("                              [2]图书信息浏览                \n\n"); printf("                              [3]图书信息查询                \n\n"); printf("                              [4]图书信息修改                \n\n"); printf("                              [5]图书信息删除                \n\n"); printf("                              [6]图书排序                    \n\n"); printf("                              [7]图书借阅                    \n\n"); printf("                                [0]退出系统                    \n\n"); printf("                              请选择:"); fflush(stdin); scanf("%d",&choice); switch(choice) { case 1:  if(head==NULL) { head=Create_Books_Doc(); } InsertDoc(head); break; case 2:  Print_Book_Doc(head); break; case 3:  search_book(head); break; case 4 : info_change(head); break;  case 5:  DeleteDoc(head); break; case 6:  sort_maopao(head); break; case 7: book_borrow(head); break;    case 0 :  printf("\n"); printf("          ━━━━━━━━  感谢使用图书管理系统  ━━━━━━━━\n"); break; default :  printf("                      ━━━━ 输入错误,请重新输入!━━━━"); } }while(choice!=0); return 0; }

  • 写回答

1条回答 默认 最新

  • 开发者小峰 2021-12-13 19:42
    关注
    
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<stdlib.h>
    struct books_list {
        char writer[20];//作者名
        char title[20];
        char ISBN[20];//书名
        char publishinghouse[20];//出版单位
        int  price,borrowed;//价格
        struct books_list * next;//链表的指针域
    };
    struct books_list * Create_Books_Doc();//新建链表
    void InsertDoc(struct books_list * head);//插入
    void sort_maopao(struct books_list * head );//排序
    void DeleteDoc(struct books_list * head );//删除
    void Print_Book_Doc(struct books_list * head);//浏览
    void search_book(struct books_list * head);//查询
    void info_change(struct books_list * head);//修改
    void save(struct books_list * head);//保存数据至文件
    /*新建链表头节点*/
    struct books_list * Create_Books_Doc() {
        struct books_list * head;
        head=(struct books_list *)malloc(sizeof(struct books_list));//分配头节点空间
        head->next=NULL;//头节点指针域初始化,定为空
        return head;
    } /*保存数据至文件*/ void save(struct books_list * head) {
        struct books_list *p;
        FILE *fp;
        p=head;
        fp=fopen("data.txt","w+");//以写方式新建并打开 data.txt文件
        fprintf(fp," --------编号-------书  名---------------作    者----------出版社-----------价格    \n");
        /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/
        while(p->next!= NULL) {
            p=p->next;
            fprintf(fp,"%-6.6s %-15.10s %-10.10s %-10.10s %-10.10d\n",p->ISBN,p->title,p->writer,p->publishinghouse,p->price);
        }
        fprintf(fp,"\n");
        fclose(fp);
        printf("    已将图书数据保存到 data.txt 文件\n");
    }
    void sort_maopao(struct books_list * head ) {
        struct books_list *tail,*p,*q,*p1,*t; //p1等于head t等于head q保存p的next节点
        p1=(struct books_list *)malloc(sizeof (struct books_list));
        for(t=head->next; t!=NULL; t=t->next) for(p=head->next,p1=head; p->next!=NULL; p=p->next,p1=p1->next) {
                if(strcmp(p->ISBN,p->next->ISBN)>0) {
                    q=p->next->next;
                    tail=p->next;
                    tail->next=NULL;
                    p->next=q;
                    p1->next=tail;
                    tail->next=p;
                    p=p1;
                }
            }
        save(head);  //保存文件
    }
    /*插入*/
    void InsertDoc(struct books_list *head) {
        /*定义结构体指针变量 s指向开辟的新结点首地址 p为中间变量*/
        struct books_list *s, *p;
        char flag='Y';
        /*定义flag,方便用户选择重复输入*/
        p=head;
        /*遍历到尾结点,p指向尾结点*/
        while(p->next!= NULL) {
            p=p->next;
        }
        /*开辟新空间,存入数据,添加进链表*/
        while(flag=='Y'||flag=='y') {
            system("cls");
            p->borrowed=0;
            s=(struct books_list *)malloc(sizeof(struct books_list));
            printf("\n                    请输入图书编号:");
            fflush(stdin);
            scanf("%s",s->ISBN);
            printf("\n                    请输入图书书名:");
            fflush(stdin);
            scanf("%s",s->title);
            printf("\n                    请输入图书作者名:");
            fflush(stdin);
            scanf("%s",s->writer);
            printf("\n                    请输入图书出版社:");
            fflush(stdin);
            scanf("%s",s->publishinghouse);
            printf("\n                    请输入图书价格:");
            fflush(stdin);
            scanf("%d",&s->price);
            printf("\n");
            p->next=s;
            /*将新增加的节点添加进链表*/
            p=s;
            /*p指向尾节点,向后移*/
            s->next=NULL;
            printf("                  ━━━━ 添加成功!━━━━");
            printf("\n                          继续添加?(Y/N):");
            fflush(stdin);
            scanf("%c",&flag);
            printf("\n");
            if(flag=='N'||flag=='n') {
                break;
            } else if(flag=='Y'||flag=='y') {
                continue;
            }
        }
        save(head);
        /*保存数据至文件*/
        system("cls");
        return;
    }
    /*查询操作*/
    void search_book(struct books_list *head) {
        struct books_list * p;
        char temp[20];
        p=head;
        if(head==NULL || head->next==NULL) { /*判断数据库是否为空*/
            printf("                      ━━━━ 图书库为空!━━━━\n");
        } else {
            printf("请输入您要查找的书名: ");
            fflush(stdin);
            scanf("%s",temp);
            /*指针从头节点开始移动,遍历至尾结点,查找书目信息*/
            while(p->next!= NULL) {
                p=p->next;
                if(strcmp(p->title,temp)==0) {
                    printf("\n图书已找到!\n");
                    printf("\n");
                    printf("编号: %s\t\n",p->ISBN);
                    printf("书名: %s\t\n",p->writer);
                    printf("作者名: %s\t\n",p->writer);
                    printf("出版单位: %s\t\n",p->publishinghouse);
                    printf("价格: %.2d\t\n",p->price);
                }
                if(p->next==NULL) {
                    printf("\n查询完毕!\n");
                }
            }
        }
        return;
    }
    /*浏览操作*/
    void Print_Book_Doc(struct books_list * head) {
        struct books_list * p;
        if(head==NULL || head->next==NULL) { /*判断数据库是否为空*/
            printf("\n                      ━━━━  没有图书记录!  ━━━━\n\n");
            return;
        }
        p=head;
        printf("\n\n\n\t\t编号    书 名        作 者    出版单位    价格  \n");
        /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/
        while(p->next!= NULL) {
            p=p->next;
            printf("\t\t%-7.6s %-13.10s %-10.10s %-10.10s %d \n",p->ISBN,p->title,p->writer,p->publishinghouse,p->price);
        }
        printf("\n");
    }
    /*修改操作*/
    void info_change(struct books_list * head) {
        struct books_list * p;
        int panduan=0;
        /*此变量用于判断是否找到书目*/
        char temp[20];
        p=head;
        printf("请输入要修改的书名:");
        scanf("%s",temp);
        while(p->next!= NULL) {
            p=p->next;
            if(strcmp(p->title,temp)==0) {
                printf("\n                    请输入图书编号:");
                fflush(stdin);
                scanf("%d",p->ISBN);
                printf("\n                    请输入图书书名:");
                fflush(stdin);
                scanf("%s",p->title);
                printf("\n                    请输入图书作者名:");
                fflush(stdin);
                scanf("%s",p->writer);
                printf("\n                    请输入图书出版社:");
                fflush(stdin);
                scanf("%s",p->publishinghouse);
                printf("\n                    请输入图书价格:");
                fflush(stdin);
                scanf("%d",&p->price);
                printf("\n");
                panduan=1;
            }
        }
        if(panduan==0) {
            printf("\n                      ━━━━  没有图书记录!  ━━━━\n\n");
        }
        return;
    }
    void book_borrow(struct books_list * head) {
        struct books_list * p;
        int panduan=0;
        /*此变量用于判断是否找到书目*/
        char temp[20];
        p=head;
        printf("请输入要借的书名:");
        scanf("%s",temp);
        while(p->next!= NULL) {
            p=p->next;
            if((strcmp(p->title,temp)==0)&&(p->borrowed==0)) {
                p->borrowed=1;
                panduan=1;
                printf("图书已经成功借出\n");
            }
        }
        if(panduan==0) {
            printf("\n                      ━━━━  没有要借阅的图书!  ━━━━\n\n");
        }
        return;
    }
    /*删除操作*/
    void DeleteDoc(struct books_list * head) {
        struct books_list *s,*p;
        /*s为中间变量,p为遍历时使用的指针*/
        char temp[20];
        int panduan;
        /*此变量用于判断是否找到了书目*/
        panduan=0;
        p=s=head;
        printf("                    [请输入您要删除的书名]:");
        scanf("%s",temp);
        /*遍历到尾结点*/
        while(p!= NULL) {
            if(strcmp(p->title,temp)==0) {
                panduan++;
                break;
            }
            p=p->next;
        }
        if(panduan==1) {
            for(; s->next!=p;) {  /*找到所需删除卡号结点的上一个结点*/
                s=s->next;
            }
            s->next=p->next;
            /*将后一节点地址赋值给前一节点的指针域*/ free(p);
            printf("\n                      ━━━━  删除成功!  ━━━━\n");
        } else { /*未找到相应书目*/
            printf("                    您输入的书目不存在,请确认后输入!\n");
        }
        return;
    }
    int main(void) {
        struct books_list * head;
        int choice;
        head=NULL;
        do {
            printf("                                                              \n");
            printf("          -------------------- 图书管理系统-------------------\n\n");
            printf("                              [1]图书信息录入                \n\n");
            printf("                              [2]图书信息浏览                \n\n");
            printf("                              [3]图书信息查询                \n\n");
            printf("                              [4]图书信息修改                \n\n");
            printf("                              [5]图书信息删除                \n\n");
            printf("                              [6]图书排序                    \n\n");
            printf("                              [7]图书借阅                    \n\n");
            printf("                              [0]退出系统                    \n\n");
            printf("                              请选择:");
            fflush(stdin);
            scanf("%d",&choice);
            switch(choice) {
                case 1:
                    if(head==NULL) {
                        head=Create_Books_Doc();
                    }
                    InsertDoc(head);
                    break;
                case 2:
                    Print_Book_Doc(head);
                    break;
                case 3:
                    search_book(head);
                    break;
                case 4 :
                    info_change(head);
                    break;
                case 5:
                    DeleteDoc(head);
                    break;
                case 6:
                    sort_maopao(head);
                    break;
                case 7:
                    book_borrow(head);
                    break;
                case 0 :
                    printf("\n");
                    printf("          ━━━━━━━━  感谢使用图书管理系统  ━━━━━━━━\n");
                    break;
                default :
                    printf("                      ━━━━ 输入错误,请重新输入!━━━━");
            }
        } while(choice!=0);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 创建了问题 12月13日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么