Rock_magician 2021-09-19 14:09 采纳率: 50%
浏览 69

单链表图书管理系统输出错误

前几天跟着网上的教程做了一个图书管理体系(c++),但是我增加了收集的元素后发现输出始终为0.0
下列为源码

/*
 * @Author: your name
 * @Date: 2021-09-18 22:23:13
 * @LastEditTime: 2021-09-19 17:23:46
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \FILE\FILE\C++\book\5.cpp
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct bookInfo    //图书信息
{
    int num;    //编号
    char name[100];  //书名
    char zuozhe[100];   //作者
    char chubanshe[100];    //出版社
    char riqi[100]; //出版日期
    float price;    //价格
    
};

struct tushuguan    //存放书籍信息
{
    struct bookInfo data;
    struct tushuguan* next;  
};
struct tushuguan* list=NULL;
struct tushuguan* createHead() //创建表头
{
    //动态内存申请
    struct tushuguan*headtushuguan=(struct tushuguan*)malloc(sizeof(struct tushuguan));
    headtushuguan->next = NULL;
    return headtushuguan;
};

// 创建节点
// 把用户的数据变为结构体变量
struct tushuguan* creatatushuguan(struct bookInfo data)
{
    struct tushuguan*newtushuguan=(struct tushuguan*)malloc(sizeof(struct tushuguan));
    newtushuguan->data = data;
    newtushuguan->next = NULL;
    return newtushuguan;
};

// 表头插入
void inserttushuguan(struct tushuguan* headtushugaun,struct bookInfo data)
{
    struct tushuguan* newtushuguan = creatatushuguan(data);
    newtushuguan->next = headtushugaun->next;
    headtushugaun->next = newtushuguan;
}

// 表尾插入
// void inserttushuguanByTali(struct tushugaun* headtushugaun, int data)
// {
//     struct tushuguan* pMove = headtushugaun;
//     while (pMove->next !=   NULL)
//     {
//         pMove = pMove->next;
//     }
//     struct tushuguan* newtushuguan = creatatushuguan(data);
//     pMove->next = newtushuguan; 
// }

//指定删除
//poseLeft->next=postushuguan->next;
//free(postushuguan);
void deletetushuguanByName(struct tushuguan* headtushuguan,char *bookName)
{
    struct tushuguan* posLefttushuguan = headtushuguan;
    struct tushuguan* postushuguan = headtushuguan->next;
    while (postushuguan != NULL && strcmp(postushuguan->data.name,bookName))
    {
        posLefttushuguan = postushuguan;
        postushuguan = posLefttushuguan->next;
    }
    //讨论查找结果
    if(postushuguan == NULL)
    {
        return;
    }
    else
    {
        printf("删除成功\n");
        posLefttushuguan->next=postushuguan->next;
        free(postushuguan);
        posLefttushuguan =NULL;
    }
}

struct tushuguan* searchByName(struct tushuguan* headtushuguan,char* bookName)
{
    struct tushuguan* postushuguan = headtushuguan->next;
    while (postushuguan !=NULL && strcmp(postushuguan->data.name,bookName))
    {
        postushuguan = postushuguan->next;
    }
    return postushuguan;
};


void printList(struct tushuguan* headtushuguan)   //演示部分
{
    struct tushuguan* pMove = headtushuguan->next;
    printf("编号\t书名\t作者\t出版社\t出版日期\t价格\n");
    while(pMove!=NULL)
    {
        printf("%d\t%s\t%s\t%s\t%s\t%.1f\n",pMove->data.num,pMove->data.name,pMove->data.zuozhe,pMove->data.chubanshe,pMove->data.riqi,pMove->data.price);
        pMove = pMove->next;
    }
}

void makeMenu()//主界面菜单
{
    printf("\n\n\n\n\n\t\t\t\t图书信息管理系统\t\t\t\t\n"); 
    printf("\t\t*********************************************\n");
    printf("\t\t\t1 ------------------图书信息录入\n");
    printf("\t\t\t2 ------------------图书信息浏览\n"); 
    printf("\t\t\t3 ------------------图书信息查询\n"); 
    printf("\t\t\t4 ------------------图书信息删除\n"); 
    printf("\t\t\t5 ------------------图书信息修改\n"); 
    printf("\t\t\t0 -----------------退出图书管理系统\n"); 
    printf("\t\t*********************************************\n"); 
    printf("\t\t请选择:");  
}
//文件操作
//存入文件
void saveInfoToFile(const char* fileName,struct tushuguan* headtushuguan)
{
    FILE* fp= fopen(fileName,"w");
    struct tushuguan*pMove = headtushuguan->next;
    while (pMove != NULL)
    {
        fprintf(fp,"%d\t%s\t%s\t%s\t%s\t%.1f\n",pMove->data.num,pMove->data.name,pMove->data.zuozhe,pMove->data.chubanshe,pMove->data.riqi,pMove->data.price);
        pMove = pMove->next;
    }
    fclose(fp);
}
//读取文件
void readInfoFromFile(const char* fileName,struct tushuguan* headtushuguan)
{
    FILE*fp = fopen(fileName,"r");  //第一次打开程序没有文件
    if(fp == NULL)
    {   
        //没有文件就新建一个
        fp = fopen(fileName,"w+");
    }
    struct bookInfo tempData;

    while (fscanf(fp, "%d\t%s\t%s\t%s\t%s\t%.1f\n",&tempData.num,tempData.name,tempData.zuozhe,tempData.chubanshe,tempData.riqi,&tempData.price) !=EOF)
    //while (fscanf(fp, "%s\t%f\t%d\n",tempData.name,&tempData.price,&tempData.num) !=EOF)
    {
        inserttushuguan(list,tempData);
    }
    fclose(fp);
}
void keyDown()//交互
{
    int userKey = 0;
    struct bookInfo tempBook;//产生一个临时变量存放书籍信息
    struct tushuguan* result = NULL;
    scanf("%d",&userKey);
    switch (userKey)
    {
    case 0:
        printf("[退出]\n");
        printf("退出成功");
        system("pause");
        exit(0);           //关闭程序
        break;
    case 1:
        printf("[录入]]\n");
        printf("请输入录入的图书信息(编号,书名,作者,出版社,出版日期,价格):");
        scanf("%d%s%s%s%s%.1f",&tempBook.num,tempBook.name,tempBook.zuozhe,tempBook.chubanshe,tempBook.riqi,&tempBook.price);
        inserttushuguan(list,tempBook);
        saveInfoToFile("E:\\FILE\\FILE\\C++\\book\\bookinfo.txt",list);
        break;
    case 2:
        printf("[浏览]\n");
        printList(list);
        break;
    case 3:
        printf("[查询]\n");
        printf("请输入需要查询的书籍名称:");
        scanf("%s",tempBook.name);
        result = searchByName(list,tempBook.name);
        if(result ==NULL)
        {
            printf("未找到相关信息\n");
        }
        else
        {
            printf("编号\t书名\t作者\t出版社\t出版日期\t价格\n");
            printf("%d\t%s\t%s\t%s\t%s\t%0.1f\n",result->data.num,result->data.name,result->data.zuozhe,result->data.chubanshe,result->data.riqi,result->data.price);
        }
        break;
    case 4:
        printf("[删除]\n");
        printf("请输入需要删除的书籍名称:");
        scanf("%s",tempBook.name);
        deletetushuguanByName(list,tempBook.name);
        saveInfoToFile("E:\\FILE\\FILE\\C++\\book\\bookinfo.txt",list);
        break;
    case 5:
        printf("[修改]\n");
        printf("请输入需要修改的书籍名称:");
        scanf("%s",tempBook.name);
        deletetushuguanByName(list,tempBook.name);
        saveInfoToFile("E:\\FILE\\FILE\\C++\\book\\bookinfo.txt",list);
        printf("请输入修改后的图书信息(编号,书名,作者,出版社,出版日期,价格):");
        scanf("%d%s%s%s%s%.1f",&tempBook.num,tempBook.name,tempBook.zuozhe,tempBook.chubanshe,tempBook.riqi,&tempBook.price);
        inserttushuguan(list,tempBook);
        saveInfoToFile("E:\\FILE\\FILE\\C++\\book\\bookinfo.txt",list);
        break;
    default:
        printf("[输入错误]\n");
        break;
    }
///自己封装一个edit
}
int main()
{
    // 表头输出
    // 输出210
    // struct tushuguan* list = createHead();
    // for(int i = 0; i < 3; i++)
    // {
    //     inserttushuguan(list, i);
    // }
    // printList(list);

    // 表尾输入
    // struct tushugaun* list = createtushuguan();
    // inserttushuguanByTali(list,1);
    // inserttushuguanByTali(list,2);
    // printList(list);
    list= createHead();
    readInfoFromFile("E:\\FILE\\FILE\\C++\\book\\bookinfo.txt",list);
    
    while(1)
    {
        makeMenu();
        keyDown();
        system("pause");
        system("cls");
    }
    makeMenu();
    system("pause");
}

下面是出错截图
我输入的是648

img

  • 写回答

2条回答 默认 最新

  • 关注

    图书管理系统,参考一下:

    #include<stdio.h> //预处理
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>//定义了通过控制台进行数据输入和数据输出的函数
    //定义结构体
    struct book
    {
    int id;
    char name[31]; //书名
    char author[27]; //作者
    char publish[31];//出版社
    int store; //库存
    int total; //总数
    int user[10];
    int days[10];
    }books[100];
    //显示整体标题标题函数
    void page_title(char *menu_item)
    {
    printf("\t\t使用图书馆管理系统\n\n-%s-\n\n",menu_item);
    }
    //等待返回函数
    void return_confirm()
    {
    printf("\n请按任意键返回……\n");
    getch();
    }
    book_add()
    {
    int i;
    system("cls"); //清屏
    page_title("注册新书");
    for(i=0;i<12;i++)
    {
    printf("序号:");
    scanf("%d",&books[i].id);
    printf("书名:");
    scanf("%s",&books[i].name);
    printf("作者:");
    scanf("%s",&books[i].author);
    printf("出版社:");
    scanf("%s",&books[i].publish);
    printf("数量:");
    scanf("%d",&books[i].total);
    books[i].store=books[i].total;
    printf("\n");
    
    }
    }
    
    book_show()
    {
    int i,flag=0;
    system("cls");
    for(i=0;i<100;i++)
    {
    if(strlen(books[i].publish)!=0)
    {
    printf("编号:%d\t ",books[i].id);
    printf("书名:%s\t ",books[i].name);
    printf("作者:%s\t ",books[i].author);
    printf("出版社:%s\t ",books[i].publish);
    printf("库存:%d\t",books[i].store);
    printf("总数:%d\n\n",books[i].total);
    flag=1;
    }
    }
    if(flag==0)
    {
    printf("没有找到相关书籍\n");
    }
    getch();
    return i; }
    int book_search()
    {
    int n,i,flag=0;
    char nam[30];
    system("cls");
    printf("请输入书名:");
    scanf("%s",&nam);
    for(i=0;i<100;i++)
    {
    if(strcmp(books[i].name,nam)==0)
    {
    printf("序号:%d\t ",books[i].id);
    printf("书名:%s\t ",books[i].name);
    printf("作者:%s\t ",books[i].author);
    printf("出版社:%s\t ",books[i].publish);
    printf("存数:%d\t",books[i].store);
    printf("总数:%d\n",books[i].total);
    n=i;
    flag=1;
    getch();
    break;
    }
    }
    
    if(flag==0)
    {
    printf("\n没有找到相关书籍\n");
    getch();
    return -1;
    }
    return n;
    }
    
    book_edit()
    {
    int i,id_change;
    system("cls");
    page_title("修改图书信息:");
    printf("输入要修改的图书id:");
    scanf("%d",&id_change);
    for(i=0;i<100;i++)
    {
    if(books[i].id==id_change)
    {
    printf("修改书名:");
    scanf("%s",&books[i].name);
    printf("修改作者:");
    scanf("%s",&books[i].author);
    printf("修改出版社:");
    scanf("%s",&books[i].publish);
    printf("修改数量:");
    scanf("%d",&books[i].total);
    printf("修改图书信息成功\n");
    books[i].store=books[i].total;
    return i;
    }
    }
    
    printf("\n没有找到相关信息\n");
    getch();
    return -1;
    }
    
    int book_del()
    {
    int i,num,j;
    system("cls");
    page_title("删除图书");
    printf("输入所要删除的书的id:");
    scanf("%d",&num);
    for(i=0;i<100;i++)
    {
    if(books[i].id==num)
    {
    for(j=i;j<100;j++)
    {
    books[j].id=books[j+1].id;
    strcpy(books[j].name,books[j+1].name);
    strcpy(books[j].publish,books[j+1].publish);
    printf("该书已经删除了!\n");
    getch();
    return i;
    }
    }
    }
    printf("未找到相关记录\n");
    getch();//暂停程序运行,等待键盘IO输入
    return -1;
    }
    
    //借书
    book_out()
    {
    int n,s,i,d;
    system("cls");
    page_title("借阅图书");
    n=book_search();
    if(n!=-1&&books[n].store>0)
    {
    //赋初值,全为零
    for(i=0;i<10;i++)
    {
    books[n].user[i]=0;
    books[n].days[i]=0;
    }
    printf("借书证序号:");
    scanf("%d",&s);
    printf("输入可借天数:");
    scanf("%d",&d);
    books[n].user[0]=s;
    books[n].days[0]=d;
    books[n].store--;
    
    // 书上这有何意义
    // for(i=0;i<10;i++)
    // {
    // if(books[n].user[i]==0)
    // {
    // books[n].user[i]=0;
    // books[n].days[i]=0;
    // break;
    // }
    // books[n].store--;
    // }
    
    }
    
    else if(n!=-1&&books[n].store==0)
    {
    printf("此书已经完全借完\n\n");
    getch();
    }
    else;
    return_confirm();
    }
    
    //归还图书
    void book_in()
    {
    int n,s,i;
    page_title("归还图书");
    n=book_search();
    printf("%d",n);
    if(n!=-1&&books[n].store<books[n].total)
    {
    printf("借阅图书证列表\n");
    for(i=0;i<10;i++)
    {
    if(books[n].user!=0)
    {
    printf("[%d]--%d天\n",books[n].user[i],books[n].days[i]);
    }
    printf("输入借书证号:");
    scanf("%d",&s);
    for(i=0;i<10;i++)
    {
    if(books[n].user[i]==s)
    {
    books[n].store++;
    }
    }
    }
    }
    
    if(n!=-1&&books[n].store==books[n].total)
    {
    printf("全部归还\n");
    }
    else if(n!=-1&&books[n].store<books[n].total)
    {
    printf("归还成功\n");
    }
    else
    ;
    return_confirm();
    }
    
    main()
    {
    menu:page_title("操作选单");
    printf("用数字选择操作\n\n");
    printf("\t\t1注册新书\t\t2显示图书\n");
    printf("\t\t3查询图书信息\t\t4修改图书信息\n");
    printf("\t\t5删除图书\t\t6借阅图书\n");
    printf("\t\t7归还图书\t\t8退出系统\n");
    printf("请按数字键:\n");
    switch(getchar())
    {
    case'1': book_add();break;
    case'2': book_show();break;
    case'3': book_search();break;
    case'4': book_edit();break;
    case'5': book_del();break;
    case'6': book_out();break;
    case'7': book_in();break;
    case'8': exit(0);
    }
    goto menu;//goto跳转语句
    }
    
    
    评论

报告相同问题?

问题事件

  • 修改了问题 9月19日
  • 创建了问题 9月19日

悬赏问题

  • ¥15 用conda install时失败
  • ¥15 第三方如何控制E8a进行烧录
  • ¥15 关于lua调用DLL的c/c++动态库(相关搜索:数据库)
  • ¥15 openwrt结合智能家居(相关搜索:路由器)
  • ¥15 求一款免费的pdf编辑js,web端用的
  • ¥20 UE5.2插件Remote Control Web Interface安装失败问题
  • ¥15 求分析下图晶体与三极管组成的振荡电路
  • ¥100 多线程+连接池+代理 运行一段时间线程阻塞
  • ¥15 关于#单片机#的问题:求一个使用C语言将重力加速度gx,gy,gz积分获取到速度的代码(相关搜索:c语言)
  • ¥15 matlab导致电脑重启问题