#张大帅 2023-06-09 21:58 采纳率: 100%
浏览 21
已结题

关于Program received signal SIGSEGV, Segmentation fault

求解答


#include<stdio.h>
#include<malloc.h>
#define N 100
void menu(){
    printf("----------------------------------\n");
    printf("      1.创建  2.入库  3.显示      \n");
    printf("      4.借书  5.还书  0.退出      \n");
    printf("----------------------------------\n");
}
typedef struct{//顺序表; 
    char idA[20];    
    char nameA[20];
    char authorA[15];
    int nowA;
    int addA;
}bookA; 
typedef struct{
    bookA *Adata;
    int lengthA;
}SeqListA;
typedef struct{//顺序表; 
    char idB[10];    
    char nameB[20];
    char authorB[15];
    int nowB;
    int addB;
}bookB; 
typedef struct{
    bookB *Bdata;
    int lengthB;
}SeqListB;
typedef struct{//顺序表; 
    char idC[10];    
    char nameC[20];
    char authorC[15];
    int nowC;
    int addC;
}bookC; 
typedef struct{
    bookC *Cdata;
    int lengthC;
}SeqListC;
typedef struct{//顺序表; 
    char idD[10];    
    char nameD[20];
    char authorD[15];
    int nowD;
    int addD;
}bookD; 
typedef struct{
    bookD *Ddata;
    int lengthD;
}SeqListD;
void InitList(SeqListA *AL,    SeqListB *BL,SeqListC *CL,SeqListD *DL){//顺序表的创建; 
    AL->Adata=(bookA*)malloc(sizeof(bookA)*N);
    AL->lengthA=0;
    BL->Bdata=(bookB*)malloc(sizeof(bookB)*N);
    BL->lengthB=0;
    CL->Cdata=(bookC*)malloc(sizeof(bookC)*N);
    CL->lengthC=0;
    DL->Ddata=(bookD*)malloc(sizeof(bookD)*N);
    DL->lengthD=0;
}
typedef struct{//链表; 
    char number[8];
    int date;
    int Location; 
}reader;
typedef struct Linknode{
    reader *elem;
    struct Linknode *next;
}Linklist;
Linklist* Initlist(){//链表的创建; 
    Linklist *S;
    S=(Linklist*)malloc(sizeof(Linklist));
    S->next=NULL;
    return S;
}
void InsList(Linklist *S,SeqListA *AL,SeqListB*BL,SeqListC*CL,SeqListD*DL,char r[]){
    Linklist *p;
    Linklist *s;
    int j=0;
    p=S;
    while(p->next!=NULL){
        p=p->next;
        j++;
    }
    if(p!=NULL){
        s=(Linklist *)malloc(sizeof(Linklist));
        s->elem->Location=Locate(AL,BL,CL,DL,r);
        scanf("%s",&s->elem->number ) ;
        scanf("%d",&s->elem->date );
        s->next = p->next ;
        p->next =s;
        printf("ok");
    }else
    printf("error!");
} 
void CreateList(SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL){//顺序表的初始化; 
    int i;
    int n;
    char input;
    do{
        printf("please input you need bookstack;input 0 exit\n");
        getchar();
        scanf("%c",&input); 
    switch(input){
        case 'A':
        printf("how many intput bookdata in SeqListA->");
        scanf("%d",&n);
            for(i=0;i<n;i++){
        printf("\n");
        printf("intput No.%d id->",i+1);
        scanf("%s",&AL->Adata[i].idA);
        printf("intput No.%d name->",i+1);
        scanf("%s",&AL->Adata[i].nameA);
        printf("intput No.%d author->",i+1);
        scanf("%s",&AL->Adata[i].authorA);
        printf("intput No.%d now->",i+1);
        scanf("%d",&AL->Adata[i].nowA);
        printf("intput No.%d add->",i+1);
        scanf("%d",&AL->Adata[i].addA);
        AL->lengthA=i+1;
    }
        break;
        case 'B':
        printf("how many intput bookdata in SeqListB->");
        scanf("%d",&n);
            for(i=0;i<n;i++){
        printf("\n");
        printf("intput No.%d id->",i+1);
        scanf("%s",&BL->Bdata[i].idB);
        printf("intput No.%d name->",i+1);
        scanf("%s",&BL->Bdata[i].nameB);
        printf("intput No.%d author->",i+1);
        scanf("%s",&BL->Bdata[i].authorB);
        printf("intput No.%d now->",i+1);
        scanf("%d",&BL->Bdata[i].nowB);
        printf("intput No.%d add->",i+1);
        scanf("%d",&BL->Bdata[i].addB);
        BL->lengthB=i+1;
    }
        case 'C':
        printf("how many intput bookdata in SeqListC->");
        scanf("%d",&n);
            for(i=0;i<n;i++){
        printf("\n");
        printf("intput No.%d id->",i+1);
        scanf("%s",&CL->Cdata[i].idC);
        printf("intput No.%d name->",i+1);
        scanf("%s",&CL->Cdata[i].nameC);
        printf("intput No.%d author->",i+1);
        scanf("%s",&CL->Cdata[i].authorC);
        printf("intput No.%d now->",i+1);
        scanf("%d",&CL->Cdata[i].nowC);
        printf("intput No.%d add->",i+1);
        scanf("%d",&CL->Cdata[i].addC);
        CL->lengthC=i+1;
    }
        break;
        case 'D':
        printf("how many intput bookdata in SeqListD->");
        scanf("%d",&n);
            for(i=0;i<n;i++){
        printf("\n");
        printf("intput No.%d id->",i+1);
        scanf("%s",&DL->Ddata[i].idD);
        printf("intput No.%d name->",i+1);
        scanf("%s",&DL->Ddata[i].nameD);
        printf("intput No.%d author->",i+1);
        scanf("%s",&DL->Ddata[i].authorD);
        printf("intput No.%d now->",i+1);
        scanf("%d",&DL->Ddata[i].nowD);
        printf("intput No.%d add->",i+1);
        scanf("%d",&DL->Ddata[i].addD);
        DL->lengthD=i+1;
    }
        break;
        case 'E':
            i=0;
            break;
        default:
            printf("input error,please input A B C D");
}
}while(i);
}
void DispList(SeqListA*AL,SeqListB*BL,SeqListC*CL,SeqListD*DL){//显示顺序表内容; 
    int i;
    for(i=0;i<AL->lengthA;i++){
        printf("%s/%s/%s/%d/%d/",AL->Adata[i].idA ,AL->Adata[i].nameA,AL->Adata[i].authorA,AL->Adata[i].nowA,AL->Adata[i].addA);
        printf("\n");
    }
    printf("____________________________________________\n");    
    for(i=0;i<BL->lengthB;i++){
        printf("%s/%s/%s/%d/%d/",BL->Bdata[i].idB ,BL->Bdata[i].nameB,BL->Bdata[i].authorB,BL->Bdata[i].nowB,BL->Bdata[i].addB);
        printf("\n");
    }
    printf("____________________________________________\n");        
    for(i=0;i<CL->lengthC;i++){
        printf("%s/%s/%s/%d/%d/",CL->Cdata[i].idC ,CL->Cdata[i].nameC,CL->Cdata[i].authorC,CL->Cdata[i].nowC,CL->Cdata[i].addC);
        printf("\n");
    }
    printf("____________________________________________\n");        
    for(i=0;i<DL->lengthD;i++){
        printf("%s/%s/%s/%d/%d/",DL->Ddata[i].idD ,DL->Ddata[i].nameD,DL->Ddata[i].authorD,DL->Ddata[i].nowD,DL->Ddata[i].addD);
        printf("\n");
    }
    printf("____________________________________________\n");        
}
int Locate(SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL,char r[]){//形式索引查找; 
    int i=0;  
    if( r[0]== 'A' ) {
    while(i<AL->lengthA && !strcmp(AL->Adata[i].idA, r[10]))
    i++;    
    if(i>=AL->lengthA)
        return 0;
    else
        return i;
}
    if( r[0]=='B') {
    while(i<BL->lengthB && !strcmp(BL->Bdata[i].idB, r[10]))
    i++;    
    if(i>=BL->lengthB)
        return 0;
    else
        return i;
}
    if( r[0]=='C') {
    while(i<CL->lengthC && !strcmp(CL->Cdata[i].idC, r[10]))
    i++;    
    if(i>=CL->lengthC)
        return 0;
    else
        return i;
}
    if( r[0]=='D') {
    i++;    
    while(i<DL->lengthD && !strcmp(DL->Ddata[i].idD, r[10]))
    if(i>=DL->lengthD)
        return 0;
    else
        return i;
}
}
void Inbook(SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL,char r[]){
    if(Locate(AL,BL,CL,DL,r)==0){
    if(r[0]='A'){
        if(AL->lengthA>=N){
            printf("this is full");
        }
        else{
        printf("\n");
        printf("intput No.%d id->",AL->lengthA );
        scanf("%s",&AL->Adata[AL->lengthA].idA);
        printf("intput No.%d name->",AL->lengthA );
        scanf("%s",&AL->Adata[AL->lengthA].nameA);
        printf("intput No.%d author->",AL->lengthA );
        scanf("%s",&AL->Adata[AL->lengthA].authorA);
        printf("intput No.%d now->",AL->lengthA );
        scanf("%d",&AL->Adata[AL->lengthA].nowA);
        printf("intput No.%d add->",AL->lengthA );
        scanf("%d",&AL->Adata[AL->lengthA].addA);
        AL->lengthA++;
    }
        }
if(r[0]='B'){
        if(BL->lengthB>=N){
            printf("this is full");
        }
        else{
        printf("\n");
        printf("intput No.%d id->",BL->lengthB );
        scanf("%s",&BL->Bdata[BL->lengthB].idB);
        printf("intput No.%d name->",BL->lengthB );
        scanf("%s",&BL->Bdata[BL->lengthB].nameB);
        printf("intput No.%d author->",BL->lengthB );
        scanf("%s",&BL->Bdata[BL->lengthB].authorB);
        printf("intput No.%d now->",BL->lengthB );
        scanf("%d",&BL->Bdata[BL->lengthB].nowB);
        printf("intput No.%d add->",BL->lengthB );
        scanf("%d",&BL->Bdata[BL->lengthB].addB);
        BL->lengthB++;
    }
        }
        if(r[0]='C'){
        if(CL->lengthC>=N){
            printf("this is full");
        }
        else{
        printf("\n");
        printf("intput No.%d id->",CL->lengthC );
        scanf("%s",&CL->Cdata[CL->lengthC].idC);
        printf("intput No.%d name->",CL->lengthC );
        scanf("%s",&CL->Cdata[CL->lengthC].nameC);
        printf("intput No.%d author->",CL->lengthC );
        scanf("%s",&CL->Cdata[CL->lengthC].authorC);
        printf("intput No.%d now->",CL->lengthC );
        scanf("%d",&CL->Cdata[CL->lengthC].nowC);
        printf("intput No.%d add->",CL->lengthC );
        scanf("%d",&CL->Cdata[CL->lengthC].addC);
        CL->lengthC++;
    }
        }
        if(r[0]='D'){
        if(DL->lengthD>=N){
            printf("this is full");
        }
        else{
        printf("\n");
        printf("intput No.%d id->",DL->lengthD );
        scanf("%s",&DL->Ddata[DL->lengthD].idD);
        printf("intput No.%d name->",DL->lengthD );
        scanf("%s",&DL->Ddata[DL->lengthD].nameD);
        printf("intput No.%d author->",DL->lengthD );
        scanf("%s",&DL->Ddata[DL->lengthD].authorD);
        printf("intput No.%d now->",DL->lengthD );
        scanf("%d",&DL->Ddata[DL->lengthD].nowD);
        printf("intput No.%d add->",DL->lengthD );
        scanf("%d",&DL->Ddata[DL->lengthD].addD);
        DL->lengthD++;
    }
        }
}
    if(Locate(AL,BL,CL,DL,r)!=0){
        int n;
            if(r[0]='A'){
                printf("Existing the book,please input n");
                scanf("%d",&n);
                AL->Adata[Locate(AL,BL,CL,DL,r)].addA=AL->Adata[Locate(AL,BL,CL,DL,r)].addA+n;
                AL->Adata[Locate(AL,BL,CL,DL,r)].nowA=AL->Adata[Locate(AL,BL,CL,DL,r)].nowA+n;
            }
                if(r[0]='B'){
                    printf("Existing the book,please input n");
                    scanf("%d",&n);
                    BL->Bdata[Locate(AL,BL,CL,DL,r)].addB=BL->Bdata[Locate(AL,BL,CL,DL,r)].addB+n;
                    BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB=BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB+n;
                }
                    if(r[0]='C'){
                        printf("Existing the book,please input n");
                        scanf("%d",&n);
                        CL->Cdata[Locate(AL,BL,CL,DL,r)].addC=CL->Cdata[Locate(AL,BL,CL,DL,r)].addC+n;
                        CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC=CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC+n;
                    }
                        if(r[0]='D'){
                            printf("Existing the book,please input n");
                            scanf("%d",&n);
                            DL->Ddata[Locate(AL,BL,CL,DL,r)].addD=DL->Ddata[Locate(AL,BL,CL,DL,r)].addD+n;
                            DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD=DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD+n;
                        }
    }    
    }
void borrowbook(Linklist *S,SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL){
    char r[10];
    Linklist *P; 
    printf("please input you needed book");
    scanf("%s",&r[10]);
    if(r[0]=='A'){
    if(Locate(AL,BL,CL,DL,r)==0)
    printf("the book dose not exist");
    else{
        if(AL->Adata[Locate(AL,BL,CL,DL,r)].nowA>=1){
        AL->Adata[Locate(AL,BL,CL,DL,r)].nowA--;
        InsList(S,AL,BL,CL,DL, r);}
        else
        printf("There are no books");    
    }
}
    if(r[0]=='B'){
    if(Locate(AL,BL,CL,DL,r)==0)
    printf("the book dose not exist");
    else{
        if(BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB>=1){
        BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB--;
        InsList(S,AL,BL,CL,DL, r);}
        else
        printf("There are no books");    
    }
}
    if(r[0]=='C'){
    if(Locate(AL,BL,CL,DL,r)==0)
    printf("the book dose not exist");
    else{
        if(CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC>=1){
        CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC--;
        InsList(S,AL,BL,CL,DL, r);}
        else
        printf("There are no books");    
    }
}
    if(r[0]=='D'){
    if(Locate(AL,BL,CL,DL,r)==0)
    printf("the book dose not exist");
    else{
        if(DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD>=1){
        DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD--;
        InsList(S,AL,BL,CL,DL, r);}
        else
        printf("There are no books");    
    }
}
}
void DelList(Linklist *S){
    int j=0;
    char D[10];
    Linklist *p;
    Linklist *s;
    p=S;
    printf("please input your boooknumber->");
    scanf("%s",D);
    while(p->next!=NULL&&!strcmp(p->elem->number,D)){
        p=p->next ;
        j++;
    } 
    if(p->next!=NULL){
        s=p->next ;
        p->next = s->next ;
        free(s);
        printf("ok");
    }
    else
    printf("error");
}
void main(){
    SeqListA AL;
    SeqListB BL;
    SeqListC CL;
    SeqListD DL;
    Linklist *S;
    S=Initlist();
    int G; 
    do{
        menu(); 
        char r[10];
        printf("please input Menu->");
        scanf("%d",&G);
        switch(G){
        case 1:
                    CreateList(&AL,&BL,&CL,&DL);
                    break;
            case 2:
                    Inbook(&AL,&BL,&CL,&DL,r);
                    break;
                case 3:
                        DispList(&AL,&BL,&CL,&DL);
                        break;
                        case 4:
                            borrowbook(S,&AL,&BL,&CL,&DL);
                            break;
                            case 5:
                                DelList(S);
                                case 0:
                                    printf("you choose Exit");
                                    break;
                                        default:
                                            printf("you choose error(0-5)");
                                    
}    
}while(G);
}
  • 写回答

2条回答 默认 最新

  • Loup&卡普 2023-06-10 19:22
    关注

    SIGSEG 是段错误 一般是下标越界 现在没带电脑

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月27日
  • 已采纳回答 7月19日
  • 创建了问题 6月9日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀