m0_67563591 2022-06-18 20:52 采纳率: 100%
浏览 28
已结题

c++文件与链表结合

在做文件与链表的程序中,当添加两次图书后,程序的运行窗口不能正常显示。
希望能够解决。

#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
struct  Book                /*图书结构*/
{
    int  BookNumber;        /*对应书号*/           
    char name[150];     /*书名*/
    char strWroter[150];    /*作者*/   
    int  AllNum;            /*可借阅册数册数*/        
};

struct Node
{
    struct Book data;
    struct Node* next;  
};
struct Node* list=NULL;
struct  Node* createHead();
struct  Node*  createNode(struct Book data);
void insertNode(struct Node* head,struct Book data);
void insertNode(struct Node* head,struct Book data);
void deleteNode(struct Node* head,int posData);
void printList(struct Node* head);
void saveFile(const char* fileName,struct Node* head);
void readFile(const char* fileName,struct Node* head);
int FirstMenu();/*输出主菜单*/
int SecondMenu(int i);
void add_book();//添加图书 
void sort(struct Node* head);//书籍排序,按书号 
int main()
{
    int flag=0;  
    while(flag!=8)/*当flag=3是表示用户选择了退出*/
    {
        system("cls");
        flag=SecondMenu(FirstMenu());
    }
    
    
    return 0;
}

int FirstMenu()
{
    list=createHead();
    readFile("book1.txt",list);
    int i; /*记录用户的选择情况*/
    /*输出主菜单*/
    cout << "*****************************************************" << endl;
    cout << "*                  图书管理系统                     *" << endl;
    cout << "*****************************************************" << endl;
    cout << "*            1    添加图书                          *" << endl;
    cout << "*            2    查看全部图书                      *" << endl;
    cout << "*            3    退出系统                          *" << endl;
    cout << "*****************************************************" << endl;
    cout << "请您选择(输入数字):";
    cin >> i;    
    return i;
}


int SecondMenu(int i)
{
    switch(i)
        {
           case 1:
               {
                      cout << "*                添加图书                          *" << endl;
                   add_book();//添加 
                   break;
               }
           case 2:
               {
                      cout << "*                查看全部图书                      *" << endl; 
                      sort(list);
                      printList(list);
                   break;
               }
           case 3:
               {
                      cout << "*                退出系统                          *" << endl;
                   cout << "*              退出系统成功!                       *" << endl;
                   exit(1);
                   break;
               }
        }

    return i;
 
}
//文件存操作 
void saveFile(const char* fileName,struct Node* head) 
{
    FILE* fp=fopen(fileName,"w");
    struct Node* p=head->next;
    while(p!=NULL)
    {
        fprintf(fp,"%d\t%s\t%s\t%d\n",p->data.BookNumber,p->data.name,p->data.strWroter,p->data.AllNum);
        p=p->next;
    }
    fclose(fp);
} 
//文件读操作
void readFile(const char* fileName,struct Node* head)
{    
    head=head; 
    FILE* fp=fopen(fileName,"r");
    if(fp==NULL)
    {
        fp=fopen(fileName,"w+");
    }
    struct Book q;
    while(fscanf(fp,"%d\t%s\t%s\t%d\n%d",&q.BookNumber,q.name,q.strWroter,&q.AllNum)!=EOF)
    {
        insertNode(list,q);
    }
    fclose(fp);
}  
//创建表头 
struct  Node* createHead()
{
    struct Node* head=(struct Node*)malloc(sizeof(struct Node));
    head->next=NULL;
    return head;
}
//创建节点 
struct  Node*  createNode(struct Book data)
{
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
}
//插入
void insertNode(struct Node* head,struct Book data) 
{
    struct Node* newNode=createNode(data);
    newNode->next=head->next;
    head->next=newNode;
}
//打印
void printList(struct Node* head)
{
    struct Node* p=head->next;
    cout<<"书号"<<"\t"<<"书名"<<"\t"<<"作者"<<"\t"<<"馆藏总册数"<<endl; 
    while(p!=NULL)
    {
        cout<<p->data.BookNumber<<"\t"<<p->data.name<<"\t"<<p->data.strWroter<<"\t"<<p->data.AllNum<<endl;
        p=p->next;
    }
    cout<<endl;
    system("pause");//防止闪屏 
}
//添加 
void add_book()
{
    struct Book tempBook;//产生一个临时变量存储书籍信息
    cout<<"输入书籍的信息:"<<endl;
    cout<<"书籍编号:" ;
    cin>>tempBook.BookNumber;
    cout<<"书名:";
    cin>>tempBook.name;
    cout<<"作者:";
    cin>>tempBook.strWroter;
    cout<<"可借阅册数:";
    cin>>tempBook.AllNum;   
    insertNode(list,tempBook);
    saveFile("book1.txt",list);
    cout<<"添加成功!"<<endl;
    system("pause") ;
}

//书籍排序,按书号
void sort(struct Node* head)
{
    struct Node* p;
    for(p=head->next;p!=NULL;p=p->next)
    {
        struct Node* q;
        for(q=head->next;q->next!=NULL;q=q->next)
        {
            if(q->data.BookNumber > q->next->data.BookNumber)
            {
                struct Book temp=q->data;
                q->data=q->next->data;
                q->next->data=temp;
            }
        }
    }
}

  • 写回答

2条回答 默认 最新

  • 逆游的懒喵喵 2022-06-18 22:16
    关注

    您好,我解决了这个错误:问题原因在readFile函数中,读取时候在换行符后又读取了一个int数据而导致了错误,此段代码在修改此处后成功添加了第三本书,修改后函数代码如下。

    //文件读操作
    void readFile(const char* fileName,struct Node* head)
    {    
        head=head; 
        FILE* fp=fopen(fileName,"r");
        if(fp==NULL)
        {
            fp=fopen(fileName,"w+");
        }
        struct Book q;
        while(fscanf(fp,"%d\t%s\t%s\t%d\n",&q.BookNumber,q.name,q.strWroter,&q.AllNum)!=EOF)
        {
            insertNode(list,q);
        }
        fclose(fp);
    }  
    

    最后希望您能采纳我的回答。

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月18日
  • 已采纳回答 6月18日
  • 创建了问题 6月18日

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来