在做文件与链表的程序中,当添加两次图书后,程序的运行窗口不能正常显示。
希望能够解决。
#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;
}
}
}
}