为啥老是报错“引发了未经处理的异常:读取访问权限冲突。 L 是 nullptr。”具体要怎么修改呢,恳请大佬帮忙!

新手上路,请多包涵

image.png

image.png

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

constexpr auto MAXSIZE = 1000;;

//顺序结点结构体
typedef struct {
    int ISBN;
    char Name[20];
    int Prize;
}BookNode;

//顺序表结构体
typedef struct {
    BookNode* ListPointer;
    int Length;
}Book;

//顺序表初始化函数
Book* initList(Book* L)
{
    L = (Book*)malloc(sizeof(Book));
    L->ListPointer = (BookNode*)malloc(MAXSIZE * sizeof(BookNode));
    //判断分配内存是否成功
    if (L->ListPointer == NULL)
        return NULL;
    else
        L->Length = 0;
    return L;
}

//向顺序表录入数据,num为要录入的数据数量
int inputData(Book* L, int num)
{
    printf("#   #   #   #   #   #   #\n");
    printf("  向顺序表录入图书信息\n");
    printf("#   #   #   #   #   #   #\n");
    for (int i = 0; i < num; i++) {
        if (L->Length > MAXSIZE)
        {
            printf("存储区已满\n");
            return -1;
        }
        printf("请录入第%d本书的ISBN号:", L->Length + 1);
        scanf("%d", &L->ListPointer[L->Length].ISBN);
        printf("请录入第%d本书的书名:", L->Length + 1);
        scanf("%d", &L->ListPointer[L->Length].Name);
        printf("请录入第%d本书的定价:", L->Length + 1);
        scanf("%d", &L->ListPointer[L->Length].Prize);
        L->Length++;
    }
    return 1;
}

//打印顺序表所有数据函数
void dispList(Book* L)
{
    printf("#   #   #   #   #   #   #\n");
    printf("打印图书信息管理系统所有数据\n");
    printf("#   #   #   #   #   #   #\n");
    for (int i = 0; i < L->Length; i++)
    {
        printf("第%d本图书的ISBN:", i + 1, L->ListPointer[i].ISBN);
        printf("第%d本图书的书名:", i + 1, L->ListPointer[i].Name);
        printf("第%d本图书的定价:", i + 1, L->ListPointer[i].Prize);
    }
}

//查找函数
int LocateElem(Book* L, int ISBN)
{
    printf("#   #   #   #   #   #   #\n");
    printf("查找ISBN为%d的图书信息\n", &ISBN);
    printf("#   #   #   #   #   #   #\n");
    for (int i = 0; i < L->Length; i++)
    {
        if (L->ListPointer[i].ISBN = ISBN)
        {
            printf("ISBN为%d的图书名称为:", ISBN, &L->ListPointer[i].Name);
            printf("ISBN为%d的图书定价为:", ISBN, &L->ListPointer[i].Prize);
            return i + 1;
        }
    }
    printf("没找到ISBN为%d的图书\n", &ISBN);
    return -1;
}

//插入函数
int ListInsert(Book* L, int p, BookNode* SN)
{
    printf("#   #   #   #   #   #   #\n");
    printf("在第%d个图书后插入数据\n", p);
    printf("#   #   #   #   #   #   #\n");
    if (p<0 || p>L->Length)
    {
        printf("插入位置非法\n");
        return -1;
    }
    for (int i = L->Length; i > p; i--) {
        L->ListPointer[i] = L->ListPointer[i - 1];
    }
    L->ListPointer[p].ISBN = SN->ISBN;
    strcpy(L->ListPointer[p].Name, SN->Name);
    L->ListPointer[p].Prize = SN->Prize;
    L->Length++;
    printf("插入成功\n");
    return 1;
}

//删除函数
int ListDelete(Book* L, int p)
{
    printf("#   #   #   #   #   #   #\n");
    printf("    删除第%d个图书数据\n", p);
    printf("#   #   #   #   #   #   #\n");
    if (p<0 || p>L->Length)
    {
        printf("删除位置非法\n");
        return -1;
    }
    for (int i = p; i <= L->Length; i++)
    {
        L->ListPointer[i - 1] = L->ListPointer[i];
    }
    L->Length--;
    printf("删除成功\n");
    return 1;
}

//修改函数
int ChangeElem(Book* L, int ISBN, BookNode* SN)
{
    printf("#   #   #   #   #   #   #\n");
    printf("修改ISBN为%d的图书信息\n", &ISBN);
    printf("#   #   #   #   #   #   #\n");
    for (int i = 0; i < L->Length; i++)
    {
        if (L->ListPointer[i].ISBN = ISBN)
        {
            L->ListPointer[i].ISBN = SN->ISBN;
            strcpy(L->ListPointer[i].Name, SN->Name);
            L->ListPointer[i].Prize = SN->Prize;
            return i + 1;
        }
    }
    printf("没找到ISBN为%d的图书,无法修改\n", &ISBN);
    return -1;
}

int main(int agrc, int agrv[])
{
    //定义图书管理系统顺序表指针
    Book* Bookptr = NULL;

    //初始化图书管理系统顺序表
    Book* initList(Bookptr);

    //向图书管理系统录入三组数据
    inputData(Bookptr, 3);

    //打印图书管理系统所有数据
    dispList(Bookptr);

    //查找系统中ISBN为1002的图书,并输出其相关信息
    LocateElem(Bookptr, 1002);

    //在系统第二本图书之后插入新图书的信息
    BookNode* BookNode2 = (BookNode*)malloc(sizeof(BookNode));
    BookNode2 = NULL;
    BookNode2->ISBN = 1005;
    strcpy(BookNode2->Name, "上下五千年");
    BookNode2->Prize = 65;
    ListInsert(Bookptr, 2, BookNode2);

    //删除第四本图书的信息
    ListDelete(Bookptr, 4);

    //修改ISBN为1001的图书的信息
    BookNode* BookNode3 = (BookNode*)malloc(sizeof(BookNode));
    BookNode3->ISBN = 1006;
    strcpy(BookNode3->Name, "百年孤独");
    BookNode3->Prize = 70;
    ChangeElem(Bookptr, 1001, BookNode3);

    //打印图书管理系统所有数据
    dispList(Bookptr);

    //计算该图书管理系统中共记录了多少本书的信息
    printf("该图书管理系统中共记录了%d本书的信息", Bookptr->Length);
}
阅读 4.1k
1 个回答

image.png

这是个啥……


P.S. 题外话,别学 VC 的糟粕,按 C99/C++11 标准来,main 函数改一下。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进