标题:录入图书保存后退出第二次登录直接添加图书就崩溃 求大佬帮忙
只看楼主
我们0620
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-11-30
结帖率:0
已结贴  问题点数:20 回复次数:1 
录入图书保存后退出第二次登录直接添加图书就崩溃 求大佬帮忙
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
//定义一个学生的结构体类型
typedef struct book{
    char name[20];
   
    char sbm[30];//书本名
    int id;
    float price;
    int tsbh;//图书编号
    int sjl;//书籍量
   
   struct book *next;
}STU;
typedef struct lent
{
    char lent_sbm[30];
    int lent_tsbh;
    int lent_sjl;
    float lent_price;
    char lent_name[20];
    char lent_dz[20];
    int lent_bh;
    struct lent *next;
 }LENT;
typedef struct dz{
    char mz[30];//名字
    int bh;//编号
    struct dz *next;
}DZ;//读者

int menu();
//变量声明

DZ *Nodelist();
STU *CreatList();
void List(STU *head);
void List1(DZ *head1);
void Delxm(STU *head,char delxm[]);
void Addxm(STU *head,char addxm[]);
void Czxm(STU *head,char czxm[]);
void ADDxs(DZ *head1);
void Fwrite(STU *head);
STU *Fread();
void Fwrite1(DZ *head1);
DZ *Fread1();
void Delxs(DZ *head1,char delxs[]);
int Lend(STU *head,DZ *head1,LENT *head2);
LENT *look();
int main(int argc,char *argv){
    int i,choose,rs,xm,xh,pxx,pxx1;
    char sbm[30];
     char czxm[20];
    char xsmz[20];//学生名字
    STU *tou;
    DZ *tou1;
    LENT *tou2;

    while(1){
        switch(choose=menu()){
        case 1:tou=CreatList();break;
        case 2:{
        
        List(tou);}break;
        case 3:{
            
            puts("请输入删除的书本名:");
            getchar();
            gets(sbm);
            Delxm(tou,sbm);
               }break;
        case 4:tou1=Nodelist();break;
        case 5:List1(tou1);break;//dzxx:读者信息                                                                                       
        case 6:{
            Addxm(tou,sbm);
               }break;   
        case 7:{
               ADDxs(tou1);
               }break;
         case 8:Lend(tou,tou1,tou2);break;
        case 15:tou2=look();break;
        case 9:{
            printf("请输入删除学生的名字:");
            getchar();
            gets(xsmz);
            Delxs(tou1,xsmz);
               }break;

   
        case 12:{cz(tou);
               }break;
   
        case 0:exit(0);
        default :puts("error!");
        }        
    }
    return 0;
}
DZ *Nodelist()         //录入学生信息
{
    DZ *head1,*end1,*r1;
    int num;
    head1=(DZ *)malloc(sizeof(DZ));
    end1=head1;
    end1->next=NULL;
    while(1)
    {
        r1=(DZ *)malloc(sizeof(DZ));
        getchar();
        puts("请输入姓名:");
        gets(r1->mz);
        puts("请输入读者编号:");
        scanf("%d",&r1->bh);
        end1->next=r1;
        end1=r1;
        printf("\n录入完成");
        printf("\n如果继续录入请按 1");
        printf("\n如果退出请按其他数字:");
        scanf("\n%d",&num);
        if(num!=1)
        {
            break;
        }
   
    }

end1->next=NULL;
Fwrite1(head1);
return head1;

}
STU *CreatList(){                             //输入书本信息
    STU *head,*end,*r;
    int i,num;
   
    head=(STU *)malloc(sizeof(STU));
    end=head;
    end->next=NULL;               //初始化
    printf("请输入要录入的书本数量:");
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
        r=(STU *)malloc(sizeof(STU));//存后面的人
            
        B:{getchar();                        
        puts("输入书本名:");
        gets(r->sbm);
        puts("输入作者:");
        gets(r->name);
        puts("请输入价格: ");
        scanf("%f",&r->price);
        getchar();
        puts("输入图书编号:");
        scanf("%d",&r->tsbh);
        puts("请输入书籍量: ");
        scanf("%d",&r->sjl);
        puts("                ");}
        end->next=r;      //开辟下一个
        end=r;
               
    }

    end->next=NULL; //结束空间开辟
    Fwrite(head);
    return head;
        
   
    }
void List1(DZ *head1)     //输出读者信息
{
    DZ *pxx1;
    pxx1=head1->next;
    head1=Fread1();
    while(pxx1!=NULL)
    {
        printf("姓名:%s\n编号:%d\n",pxx1->mz,pxx1->bh);
        printf("            \n");
        pxx1=pxx1->next;
        
    }
    printf("");
    getch();
    return;
}
void List(STU *head){//输出书本信息
    STU *pxx;
   head=Fread();
    pxx=head->next;   
        printf("\t书本名\t作者\t价格\t图书编号\t书籍量\n");
    while(pxx!=NULL){
        printf("\t%s\t%s\t%.2f\t%d\t\t%d\n",pxx->sbm,pxx->name,pxx->price,pxx->tsbh,pxx->sjl);

                   pxx=pxx->next;
        }
        
        getch();
        return head;
}
void Delxs(DZ *head1,char delxs[20])      //删除学生
    {
        DZ *pxx1,*pxxq1;
        pxxq1=head1;
        pxx1=pxxq1->next;
        while(pxx1!=NULL)
        {
            if(strcmp(pxx1->mz,delxs)==0)
            {
                printf("姓名:%s\n编号:%d\n",pxx1->mz,pxx1->bh);
                pxxq1->next=pxx1->next;
                return;
            }
            else
            {
                pxxq1=pxxq1->next;
                pxx1=pxxq1->next;

            }
        }
        Fwrite1(head1);
        getch();
    }
void Delxm(STU *head,char delxm[20]){          //删除图书
    STU *pxx,*pxxq;
    pxxq=head;
    pxx=pxxq->next;
    while(pxx!=NULL){
        if(strcmp(pxx->sbm,delxm)==0){//strcmp(char *str1,char *str2)
            printf("\t书本名\t作者\t价格\t图书编号\t书籍量\n");
            printf("\t%s\t%s\t%.2f\t%d\t\t%d\n",pxx->sbm,pxx->name,pxx->price,pxx->tsbh,pxx->sjl);
            pxxq->next=pxx->next;
            return;
        }
        else{
            pxxq=pxxq->next;
            pxx=pxxq->next;
        }
    }
    Fwrite(head);
    getch();
}
//图书
void Fwrite(STU *head){          //保存  
    int n=0;
    STU *pxx;
    FILE *fp;    //    定义一个文件指针
    fp=fopen("stu.txt","wb");
    if(fp==NULL){
        puts("文件打开失败!");
        return ;    }
    pxx=head->next;
    while(pxx!=NULL){        
        fwrite(pxx,sizeof(STU),1,fp);
        pxx=pxx->next;
        n++;}
    fclose(fp);
    }
STU *Fread(){                        //读取文件
    int n=0;
         STU *head,*end,*r;
         FILE *fp;
         fp=fopen("stu.txt","rb");
            if(fp==NULL){
         puts("文件打开失败");
         return;    }
    head=(STU *)malloc(sizeof(STU));
    end=head;
    while(!feof(fp)){    //若结束,返回非0,否则返回0
        r=(STU *)malloc(sizeof(STU));
        if(fread(r,sizeof(STU),1,fp)==1){
        end->next=r;
        end=r;
        n++;
        }
    }
    end->next=NULL;
    fclose(fp);
   
    return head;
}

//读者
void Fwrite1(DZ *head1){          //保存  
    int n=0;
    DZ *pxx;
    FILE *fp;    //    定义一个文件指针
    fp=fopen("stu.txt","wb");
    if(fp==NULL){
        puts("文件打开失败!");
        return ;    }
    pxx=head1->next;
    while(pxx!=NULL){        
        fwrite(pxx,sizeof(DZ),1,fp);
        pxx=pxx->next;
        n++;}
    fclose(fp);
    }
DZ *Fread1(){                        //读取文件
    int n=0;
         DZ *head1,*end,*r;
         FILE *fp;
         fp=fopen("stu.txt","rb");
            if(fp==NULL){
         puts("文件打开失败");
         return;    }
    head1=(DZ *)malloc(sizeof(DZ));
    end=head1;
    while(!feof(fp)){    //若结束,返回非0,否则返回0
        r=(DZ *)malloc(sizeof(DZ));
        if(fread(r,sizeof(DZ),1,fp)==1){
        end->next=r;
        end=r;
        n++;
        }
    }
    end->next=NULL;
    fclose(fp);
   
    return head1;
}
//读取函数
/*STU *Fread()
{
    FILE *fp;
    STU *head,*p,*q;
    if((fp=fopen("f:\\软件\\图书馆\\图书馆答辩(1)","rb"))==NULL)
    {
        printf("不能读取文件");
        exit(0);
    }
    head=(STU *)malloc(sizeof(STU));
    fread(head,sizeof(STU),1,fp);
    if(head==NULL)
    {
        printf("文件打开错误");
        return NULL;
    }
    head->next=NULL;//1
    p=head;
    q=(STU *)malloc(sizeof(STU));
    fread(q,sizeof(STU),1,fp);
    while(!feof(fp))
    {
        q->next=NULL;
        p->next=q;
        p=q;
        q=(STU *)malloc(sizeof(STU));
        fread(q,sizeof(STU),1,fp);
    }
    printf("读取成功");
    List(head);
    system("cls");
    return head;
}
//保存函数
void Fwrite(STU *head)
{
    FILE *fp;
    STU *p;
    if((fp=fopen("f:\\软件\\图书馆\\图书馆答辩(1)","wb"))==NULL)
    {
        printf("不能建立文件");
        exit(0);
    }
    p=head;
    while(p!=NULL)
    {
        fwrite(p,sizeof(STU),1,fp);
        p=p->next;
    }
    fclose(fp);
    List(head);
    system("cls");
}*/

void ADDxs(DZ *head1)         //添加学生
{
    DZ *pxxq1;
    DZ *pxx1;
    pxxq1=head1;
    pxx1=(DZ *)malloc(sizeof(DZ));
    C:{getchar();
    puts("请输入姓名:");
    gets(pxx1->mz);
    puts("请输入编号:");
    scanf("%d",&pxx1->bh);}
    while(pxxq1->next!=NULL)
    {
        pxxq1=pxxq1->next;
        if((strcmp(pxx1->mz,pxxq1->mz)==0)||(pxxq1->bh==pxx1->bh))
        {
            printf("输入有误!\n请重新输入!\n");
            goto C;
        }

    }
    pxxq1->next=pxx1;
    pxx1->next=NULL;
    printf("添加成功");
    Fwrite1(head1);
    getch();
    return head1;
}
void Addxm(STU *head,char addxm[]){          //增加图书
    STU *pxxq;
    STU *pxx;
    pxxq=head;
    pxx=(STU *)malloc(sizeof(STU));
    A:{getchar();
      puts("输入书本名:");
        gets(pxx->sbm);
        puts("输入作者:");
        gets(pxx->name);
        puts("请输入价格: ");
        scanf("%f",&pxx->price);
        getchar();
        puts("输入图书编号:");
        scanf("%d",&pxx->tsbh);
        puts("请输入书籍量: ");
        scanf("%d",&pxx->sjl);}
    while(pxxq->next!=NULL)
    {
        pxxq=pxxq->next;
        
        if(((strcmp(pxx->sbm,pxxq->sbm)==0)&&(strcmp(pxx->name,pxxq->name)==0))||(pxxq->tsbh==pxx->tsbh))
        {
            printf("输入有误!\n请重新输入!\n");
            goto A;}
    }
    pxxq->next=pxx;
    pxx->next=NULL;
    printf("添加成功\n");
   
    getch();
   return head;
}
   


int cz(STU *head){//查找
    int select;
    char mc[30];//书本名称
    char zz[20];//作者
    STU *pxx;
    int f=0;
    if(!head->next)
    {
        
        printf("\n这本书不存在!\n");
        getchar;
        return NULL;
    }

    printf("\n1.输入书本名查找\n2.输入作者名查找\n");
    printf("请选择 [1,2]:           ");
    scanf("%d",&select);
    getchar();
    switch(select)
    {
        case 1:
        printf("输入书本名:");
        gets(mc);
        pxx=head->next;
        if(strcmp(pxx->sbm,mc)!=0)
        {
            printf("未找到");
            break;
        }
        printf("\t书本名\t作者\t价格\t图书编号\t书籍量\n");
        while(pxx)
        {
            
            if(strcmp(pxx->sbm,mc)==0)
            {
                f=1;
                printf("\t%s\t%s\t%.2f\t%d\t\t%d\n",pxx->sbm,pxx->name,pxx->price,pxx->tsbh,pxx->sjl);
            }
            
            
               
            
            pxx=pxx->next;
        }
        getchar();
        break;
    case 2:
        printf("输入作者名:");
        gets(zz);
        pxx=head->next;
        printf("\t书本名\t作者\t价格\t图书编号\t书籍量\n");
        while(pxx)
        {
            if(strcmp(pxx->name,zz)==0)
            {
                f=1;
                printf("\t%s\t%s\t%.2f\t%d\t%d\n",pxx->sbm,pxx->name,pxx->price,pxx->tsbh,pxx->sjl);
            }
            pxx=pxx->next;
        }
        break;
    }
    return f;
}   




int Lend(STU *head,DZ *head1,LENT *head2)       //图书借阅
{
    LENT *p2,*q2;
    STU *p,*q;
    DZ *p1,*q1;
   
    q2=head2;
    p2=(LENT *)malloc(sizeof(LENT));
    q=head;
    p=q->next;
    while(p!=NULL)
    {
    E:{getchar();
    puts("请输入读者名字:");
    gets(p2->lent_dz);
    }
    if(strcmp(p2->lent_dz,p1->mz)!=0)
    {
        puts("该读者还未录入,请重新输入");
        goto E;
     }
        
    }

               
    return head2;   
               
}



int menu(){                  //菜单
    int choose;
    puts("                                                ");
    puts("┏━━━━━━━━━━━━━━━━━━━━━━┓");
    puts("┃  Welcome to the result management system   ┃");
    puts("┃                  1-录入图书                ┃");
    puts("┃                  2-查看图书                ┃");
    puts("┃                  3-删除图书                ┃");
    puts("┃                  4-录入读者信息            ┃");
    puts("┃                  5-查看读者信息            ┃");
    puts("┃                  6-添加图书                ┃");
    puts("┃                  7-添加学生                ┃");
    puts("┃                  9-删除学生                ┃");
    puts("┃                  8-借阅                    ┃");
    puts("┃                  10-查看读者借阅情况       ┃");
    puts("┃                  0-退出                    ┃");
    puts("┗━━━━━━━━━━━━━━━━━━━━━━┛");
    scanf("%d",&choose);
    return choose;
}

LENT *look()         //录入学生信息
{
    LENT *head2,*end2,*r2;
    int num;
    head2=(LENT *)malloc(sizeof(LENT));
    end2=head2;
    end2->next=NULL;
    while(1)
    {
        r2=(LENT *)malloc(sizeof(LENT));
        getchar();
        puts("请输入姓名:");
        gets(r2->lent_dz);
        puts("请输入读者编号:");
        scanf("%d",&r2->lent_bh);
        getchar();                        
        puts("输入书本名:");
        gets(r2->lent_sbm);
        puts("输入作者:");
        gets(r2->lent_name);
        puts("请输入价格: ");
        scanf("%f",&r2->lent_price);
        getchar();
        puts("输入图书编号:");
        scanf("%d",&r2->lent_tsbh);
        end2->next=r2;
        end2=r2;
        printf("\n录入完成");
        printf("\n如果继续录入请按 1");
        printf("\n如果退出请按其他数字:");
        scanf("\n%d",&num);
        if(num!=1)
        {
            break;
        }
   
    }

end2->next=NULL;
return head2;

}
搜索更多相关主题的帖子: next STU head puts 输入 
2017-12-15 20:49
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:20 
写了那么多才出现问题
2017-12-16 05:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-483410-1-1.html




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

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