注:此文章系一个编程友人在论坛上让修改的题目 在这里修改后引用了。
// l.cpp : 定义控制台应用程序的入口点。
//
//#include "stdafx.h" //如果在vs里面加这个
#include <iostream>
#include <string>
#include <time.h>
#include <iomanip>
#include <fstream>
#include<stdio.h>
#include <time.h>
#include <conio.h>
using namespace std;
template <typename Ch, typename Tr>
basic_ofstream <Ch,Tr> & operator <<(basic_ofstream <Ch,Tr> & fout, const basic_string <Ch,Tr> & str)
{
fout << str.c_str() << static_cast <Ch> (0); //用空字符0作为分隔符
return fout;
}
// 用 > > 从文件中读 string
template <typename Ch, typename Tr>
basic_ifstream <Ch,Tr> & operator >> (basic_ifstream <Ch,Tr> & fin, basic_string <Ch,Tr> & str)
{
getline(fin, str, static_cast <Ch> (0));
return fin;
}
/
//----------构造链表节点---------------
struct Library
{
string m_strID;//学号。
string m_strName;
string m_strAuthor;
string m_strPulishing;
string m_strClass;
//unsigned int Price;
string Price;
string m_strISBN;
//unsigned int m_nYear;//出生年份。
string m_nYear;
struct Library *next;
};
//------- 取别名--------------
typedef struct Library Node;
typedef Node *LinkList;//用来定义链表指针。
typedef Node *LinkNode;//用来定义节点指针。
//----------函数声明-------------
LinkList Init(void);
LinkList CreateList(void);
int GetLength(LinkList head);
int GetTime();
void FunLoad(LinkList head,fstream& iofile);
LinkNode GetNode(LinkList head,int i);
LinkNode LocateNode(LinkList head,string& strID);
void InsertList(LinkList head,Node& node,int i);
void DisplayNode(LinkNode pNode);
void ReleaseList(LinkList head);
void FunDelete(LinkList head);
int FunSearch(LinkList head);
Library *Readfile(struct Library *head);
//int Readfile(LinkList head);//
int Writefile(struct Library *head);
void FunModify(LinkList head);
void Librarysearch(LinkList *head);
int TectherSystem();
int StudentSystem();
///
//===========链表操作函数====================
LinkList Init(void)
{//构造带头节点的空链表,返回带头节点的链表的头指针。
LinkList head=new Node;//在堆中申请的空间,因为要返回这个空链表。
head->next=NULL;
return head;
}
LinkList CreateList(void)
{//用尾插法,构造带头节点的链表,返回带头节点的链表的头指针。
LinkList Init(void);
LinkList head=new Node;//在堆中申请的空间,因为要返回这个空链表.
head->next=NULL;//用尾插法,为了实现与输入一致性。
LinkNode pNode,rear;//定义两个指针,一个是操作指针,一个是尾指针。
rear=head;
char chAgain;
do
{
pNode=new Node;
if(!pNode)
{
cout<<"内存申请失败!退出系统."<<endl;
exit(-1);
}
cout<<"请输入书号:";
cin>>pNode->m_strID;//待测试。
cout<<endl<<"请输入书名:";
cin>>pNode->m_strName;
cout<<endl<<"请输入作者:";
cin>>pNode->m_strAuthor;
cout<<endl<<"请输入出版社:";
cin>>pNode->m_strPulishing;
cout<<endl<<"请输入分类:";
cin>>pNode->m_strClass;
cout<<endl<<"请输入定价:";
cin>>pNode->Price;
cout<<endl<<"请输入图书ISBN:";
cin>>pNode->m_strISBN;
cout<<endl<<"请输入出版年份:";
cin>>pNode->m_nYear;
//链入表尾。
rear->next=pNode;
rear=pNode;//将尾指针指向新表尾。
cout<<"信息录入成功~!是否继续?(Y/N)";
cin>>chAgain;
}while(chAgain=='Y'||chAgain=='y');
rear->next=NULL;//结束单链表。
return head;//返回头指针。
}
返回带头节点的链表的表长。
int GetLength(LinkList head)
{//返回带头节点的链表的表长。
LinkNode pNode=head->next;//从开始节点计算。
int n=0;
while(pNode)
{//遍历链表。
n++;
pNode=pNode->next;
}
return n;
}
LinkNode GetNode(LinkList head,int i)
{//按"序号"查找。此时要增加一个计数器j.为什么不直接用i?因为i可能是"不合法"的值。
LinkNode pNode=head;
int j=0;//要从头节点开始扫描。因为可把头节点看作序号为0。
while(pNode->next&&j<i)
{
pNode=pNode->next;
j++;
}
if(j==i)
return pNode;
else
return NULL;
}
//按书号查询
LinkNode LocateNode(LinkList head,string& strID)
{//按值查询。值=学号。
LinkNode pNode=head->next;//从需要从开始节点开始扫描,因为头节点没数据。
while(pNode&&pNode->m_strID!=strID)
pNode=pNode->next;
return pNode;
}
//
//插入新的记录
void InsertList(LinkList head,Node& node,int i)
{//插入动作。在位序i插入新node的值。
//第一步,找i-1节点,判断i的合法性。
LinkNode pPreNode=GetNode(head,i-1);
cin>>i;
if(!pPreNode)
{
cout<<"i序号不合法,不能插入。"<<endl;
return;
}
//第二步,构造节点,插入。
LinkNode pNode=new Node;
//----数据录入--------
cout<<"请输入书号:";
cin>>pNode->m_strID;//待测试。
cout<<endl<<"请输入书名:";
cin>>pNode->m_strName;
cout<<endl<<"请输入作者:";
cin>>pNode->m_strAuthor;
cout<<endl<<"请输入出版社:";
cin>>pNode->m_strPulishing;
cout<<endl<<"请输入分类:";
cin>>pNode->m_strClass;
cout<<endl<<"请输入定价:";
cin>>pNode->Price;
cout<<endl<<"请输入图书ISBN:";
cin>>pNode->m_strISBN;
cout<<endl<<"请输入出版年份:";
cin>>pNode->m_nYear;
//-----指针链入-------
pNode->next=pPreNode->next;
pNode->next=pNode;
}
//按序号删除
//***********************************************
void DeleteList(LinkList head,int i)
{//按序号来删除。
LinkNode pPreNode=GetNode(head,i-1);
LinkNode pNode=pPreNode->next;//指向被删除节点。
//----i的合法性--------
if(pPreNode==NULL||pNode==NULL)
{
cout<<"i不合法,无法删除。"<<endl;
return ;
}
pPreNode->next=pNode->next;
delete pNode;
cout<<"删除成功!"<<endl;
}
///
//*********************************************/
int GetLocation(LinkList head,string& strID)
{//返回此节点在链表中的序号.返回的是第一个被找到的节点。
int i=1;//节点序号。
LinkNode pNode=head->next;//从开始节点开始扫描。
while(pNode&&pNode->m_strID!=strID)
{
i++;
pNode=pNode->next;
}
if(pNode && pNode->m_strID==strID){
return i;}
else
return -1;//表示没有此节点。
}
int GetLocation1(LinkList head,string& BookName)
{//返回此节点在链表中的序号.返回的是第一个被找到的节点。
int i=1;//节点序号。
LinkNode pNode=head->next;//从开始节点开始扫描。
while(pNode&&pNode->m_strName!=BookName)
{
i++;
pNode=pNode->next;
}
if(pNode && pNode->m_strName==BookName)
return i;
else
return -1;//表示没有此节点。
}
int GetLocation2(LinkList head,string& BookAuthor)
{//返回此节点在链表中的序号.返回的是第一个被找到的节点。
int i=1;//节点序号。
LinkNode pNode=head->next;//从开始节点开始扫描。
while(pNode&&pNode->m_strAuthor!=BookAuthor)
{
i++;
pNode=pNode->next;
}
if(pNode && pNode->m_strPulishing==BookAuthor)
return i;
else
return -1;//表示没有此节点。
}
int GetLocation3(LinkList head,string& BookClass)
{//返回此节点在链表中的序号.返回的是第一个被找到的节点。
int i=1;//节点序号。
LinkNode pNode=head->next;//从开始节点开始扫描。
while(pNode&&pNode->m_strClass!=BookClass)
{
i++;
pNode=pNode->next;
}
if(pNode && pNode->m_strPulishing==BookClass)
return i;
else
return -1;//表示没有此节点。
}
int GetLocation4(LinkList head,string& BookPub)
{//返回此节点在链表中的序号.返回的是第一个被找到的节点。
int i=1;//节点序号。
LinkNode pNode=head->next;//从开始节点开始扫描。
while(pNode&&pNode->m_strPulishing!=BookPub)
{
i++;
pNode=pNode->next;
}
if(pNode && pNode->m_strPulishing==BookPub)
return i;
else
return -1;//表示没有此节点。
}
void DisplayList(LinkList head)
{//在STDIO中显示整个链表的内容。
LinkNode pNode=head->next;//从开始节点开始扫描。
cout<<"********************************************************************"<<endl;
cout<<"书号 书名 作者 出版社 分类 定价 ISBN 出版年份"<<endl;
cout<<"--------------------------------------------------------------------"<<endl;
while(pNode)
{
DisplayNode(pNode);
cout<<endl;
pNode=pNode->next;
}
cout<<"========================================================="<<endl;
}
void DisplayNode(LinkNode pNode)
{
cout<<setw(8)<<left<<pNode->m_strID<<" "
<<setw(8)<<left<<pNode->m_strName<<" "
<<setw(8)<<left<<pNode->m_strAuthor<<" "
<<setw(8)<<left<<pNode->m_strPulishing<<" "
<<setw(8)<<left<<pNode->m_strClass<<" "
<<setw(8)<<left<<pNode->Price<<" "
<<setw(8)<<left<<pNode->m_strISBN<<" "
<<setw(8)<<left<<pNode->m_nYear<<" ";
}
void ReleaseList(LinkList head)
{//释放整个链表,回收new的空间。
LinkNode pNode;//构造一个操作指针pNode。
while(head)
{
pNode=head;
head=head->next;
delete pNode;
}
cout<<"链表释放成功!"<<endl;
}
//================菜单功能函数======================
void FunDelete(LinkList head)
{//按书号删除。
string strID;
cout<<"请输入要删除图书的书号:";
cin>>strID;
int i;
i=GetLocation(head,strID);
if(i==-1)
{
cout<<"没找到此图书的信息,无法删除。"<<endl;
return ;
}
DeleteList(head,i);
}
int FunSearch(LinkList head)
{//查询子系统。
cout<<"-----------------------------------------------------------------------"<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
while(1)
{
cout<<"--1.按书号查询 2.按书名查询 3.按作者查询 4.按分类查询 5.按出版社查询 0.退出子系统 --"<<endl;
cout<<"请选择合理的子菜单项: ";
int subMenu;
cin>>subMenu;
while(cin.fail()||subMenu<0||subMenu>5)
{
cout<<"您选择了不合法的菜单项!"<<endl;
cin.clear();
fflush(stdin);
cin>>subMenu;
}
switch(subMenu)
{
case 0:
cout<<"安全退出子系统."<<endl;
return -1;
case 1:
{
LinkNode pNode;
string strID;
cout<<"请输入要查询图书的书号:";
cin>>strID;
int i;
i=GetLocation(head,strID);
if(i==-1)
{
cout<<"没找到关于此图书的信息。"<<endl;
return -1 ;
}
pNode=GetNode(head,i);
DisplayNode(pNode);
cout<<endl;
break;
}
case 2:
{
LinkNode pNode;
string BookName;
cout<<"请输入要查询图书的书名:";
cin>>BookName;
int i;
i=GetLocation1(head,BookName);
if(i==-1)
{
cout<<"没找到关于此图书的信息。"<<endl;
return -1 ;
}
pNode=GetNode(head,i);
DisplayNode(pNode);
cout<<endl;
break;
}
case 3:
{
LinkNode pNode;
string BookAuthor;
cout<<"请输入要查询图书的作者:";
cin>>BookAuthor;
int i;
i=GetLocation2(head,BookAuthor);
if(i==-1)
{
cout<<"没找到关于此图书的信息。"<<endl;
return -1;
}
pNode=GetNode(head,i);
DisplayNode(pNode);
cout<<endl;
break;
}
case 4:
{
LinkNode pNode;
string BookClass;
cout<<"请输入要查询图书的分类:";
cin>>BookClass;
int i;
i=GetLocation3(head,BookClass);
if(i==-1)
{
cout<<"没找到关于此图书的信息。"<<endl;
return -1;
}
pNode=GetNode(head,i);
DisplayNode(pNode);
cout<<endl;
break;
}
case 5:
{
LinkNode pNode;
string BookPub;
cout<<"请输入要查询图书的出版社:";
cin>>BookPub;
int i;
i=GetLocation4(head,BookPub);
if(i==-1)
{
cout<<"没找到关于此图书的信息。"<<endl;
return -1;
}
pNode=GetNode(head,i);
DisplayNode(pNode);
cout<<endl;
break;
}
}
}
}
void FunModify(LinkList head)
{//进入修改子系统。
string strID;
cout<<"进入修改子系统,请输入你要修改的图书号:";
cin>>strID;
LinkNode pNode=LocateNode(head,strID);
cout<<"原信息为:"<<endl;
DisplayNode(pNode);
cout<<endl;
while(1)
{
cout<<"-----------------------------------可修改项目-----------------------------------"<<endl;
cout<<"-- 1.书号 2.书名 3.作者 4.出版社 5.分类 6.定价 7.ISBN 8.出版年份 0.退出子系统 --"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"请输入子菜单项:";
int subMenu;
cin>>subMenu;
while(cin.fail()||subMenu<0||subMenu>8)
{
cout<<"您选择了不合法的菜单项!"<<endl;
cin.clear();
fflush(stdin);
cin>>subMenu;
}
switch(subMenu)
{
case 0:
cout<<"安全退出子系统."<<endl;
return;
case 1:
{
cout<<"请输入新书号:";
string strID;
cin>>strID;
pNode->m_strID=strID;
break;
}
case 2:
{
cout<<"请输入新书名:";
string strName;
cin>>strName;
pNode->m_strName=strName;
break;
}
case 3:
{
cout<<"请输入新作者:";
string strAuthor;
cin>>strAuthor;
pNode->m_strAuthor=strAuthor;
break;
}
case 4:
{
cout<<"请输入新出版社:";
string strPublishing;
cin>>strPublishing;
pNode->m_strPulishing=strPublishing;
break;
}
case 5:
{
cout<<"请输入新分类:";
string strClass;
cin>>strClass;
pNode->m_strClass=strClass;
break;
}
case 6:
{
cout<<"请输入新定价:";
unsigned int Price;
cin>>Price;
pNode->Price=Price;
break;
}
case 7:
{
cout<<"请输入新ISBN:";
string strISBN;
cin>>strISBN;
pNode->m_strISBN=strISBN;
break;
}
case 8:
{
cout<<"请输入新出版年月:";
unsigned int nYear;
cin>>nYear;
pNode->m_nYear=nYear;
break;
}
}
}
}
int Writefile(LinkList head)
{
//FILE *fp;
//if((fp=fopen("file.txt","w"))==NULL)
// cout<<"文件写入失败!"<<endl;
//struct Library *temp=head;
ofstream file( "file.txt ",ios::app);
LinkNode temp=head->next;
//int ii=0;
while(temp)
{
//文件中的数据以制表符分开,每条记录占一行
cout<<temp->m_strID<<endl;
cout<<temp->m_strISBN;
cout<<temp->Price;
cout<<temp->m_nYear;
/*fprintf(fp,"%s",temp->m_strID);
fprintf(fp,"%s",temp->m_strName);
fprintf(fp,"%s",temp->m_strAuthor);
fprintf(fp,"%s",temp->m_strPulishing);
fprintf(fp,"%s",temp->m_strClass);
fprintf(fp,"%s",temp->Price);
fprintf(fp,"%s",temp->m_strISBN);
fprintf(fp,"%s",temp->m_nYear);*/
/*fprintf(fp,"%s %s %s %s %s %s %s %s/n",
temp->m_strID,
temp->m_strName,
temp->m_strAuthor,
temp->m_strPulishing,
temp->m_strClass,
temp->Price,
temp->m_strISBN,
temp->m_nYear);*/
// ii++;
file<<temp->m_strID;
file<<temp->m_strName;
file<<temp->m_strAuthor;
file<<temp->m_strPulishing;
file<<temp->m_strClass;
file<<temp->Price;
file<<temp->m_strISBN;
file<<temp->m_nYear;
temp=temp->next;
}
//fwrite(temp,sizeof(Library),ii,fp);
//fclose(fp);
file.close();
cout<<"success to write"<<endl;
return 0;
}
Library *Readfile(struct Library *head)//head传入时为NULL
{
// cout<<"FLAG"<<endl;
//FILE *fp;
//if((fp=fopen("file.txt","r"))==NULL)
//cout<<"文件读取失败!"<<endl;
ifstream file("file.txt");
LinkList Init(void);
LinkList head1=new Node;//在堆中申请的空间,因为要返回这个空链表.
head1->next=NULL;//用尾插法,为了实现与输入一致性。
LinkNode q,rear;//定义两个指针,一个是操作指针,一个是尾指针。
rear=head1;
//struct Library *p=NULL,*q1;//创建新链表存储读取的数据
//q=(struct Library*)malloc(sizeof(struct Library));
//for(int c=0;!feof(fp);c++)
cout<<endl<<"下面将显示所有的书籍"<<endl;
for(int c=0;!file.eof();c++)
{
// q=(struct Library*)malloc(sizeof(struct Library));
//fscanf(fp,"%s",q->m_strID);
//fscanf(fp,"%s",q->m_strName);
q=new Node;
file>>q->m_strID;
file>>q->m_strName;
file>>q->m_strAuthor;
file>>q->m_strPulishing;
file>>q->m_strClass;
file>>q->Price;
file>>q->m_strISBN;
file>>q->m_nYear;
// %s/t %s/t %s/t %d/t %s/t %d/n",
//fscanf(fp,"%s",q->m_strAuthor);
// fscanf(fp,"%s",q->m_strPulishing);
// fscanf(fp,"%s",q->m_strClass);
// fscanf(fp,"%s",q->Price);
// fscanf(fp,"%s",q->m_strISBN);
// fscanf(fp,"%s",q->m_nYear);
// &q->m_strName
// &q->m_strAuthor,
// &q->m_strPulishing,
// &q->m_strClass,
// &q->Price,
//&q->m_strISBN,
// &q->m_nYear);//读取文件内容并存入新链表中
//q->next=NULL;
cout<<setw(8)<<left<<q->m_strID<<" "
<<setw(8)<<left<<q->m_strName<<" "
<<setw(8)<<left<<q->m_strAuthor<<" "
<<setw(8)<<left<<q->m_strPulishing<<" "
<<setw(8)<<left<<q->m_strClass<<" "
<<setw(8)<<left<<q->Price<<" "
<<setw(8)<<left<<q->m_strISBN<<" "
<<setw(8)<<left<<q->m_nYear<<" ";
cout<<endl;
//cout<<q->m_strID<<endl;
rear->next=q;
rear=q;
//if(head==NULL)
// head=q;
//if(p==NULL)//
// p=q;//
//else
// p->next=q;
// q=q->next;//
//p=q;
}
rear->next=NULL;
//head=NULL;//
//head->next=p;//
//fclose(fp);
file.close();
head=head1;
cout<<endl<<"读取成功flag"<<endl;
return head;
//return 0;//
}
int GetTime()
{
time_t t;
t=time(NULL);
printf("%s/n", ctime(&t));
return 0;
}
int TectherSystem()
{
LinkList head=0;
while(1)
{
cout<<"**************************************************************************"<<endl;
cout<<" 图书馆信息管理系统 "<<endl;
cout<<endl;
{
cout<<"**1.录入 2.删除 3.查找 4.修改 5.保存 6.显示 7.加载文件 8.帮助 0.退出**"<<endl;
cout<<"**************************************************************************"<<endl;
int menu,i;
Library node;
cout<<endl<<"请选择合适的菜单项:";
cin>>menu;
while(cin.fail()||menu<0||menu>9)
{
cout<<"您选择了不合法的菜单项!"<<endl;
cin.clear();
fflush(stdin);
cin>>menu;
}
switch(menu)
{
case 0:
cout<<"安全退出系统."<<endl;
exit(0);
case 1:
head=CreateList();
break;
case 2:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
FunDelete(head);
break;
}
case 3:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
FunSearch(head);
break;
}
case 4:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
FunModify(head);
break;
}
case 5:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
Writefile(head);
break;
}
case 6:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
DisplayList(head);
break;
}
case 7:
{
Readfile(head);
break;
}
case 8:
{cout<<endl;
cout<<"1.本系统可以实现图书数据的管理"<<endl;
cout<<"2.操作中Y和N区分大小写"<<endl;
break;
}
case 9:
{if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
//InsertList(head,node,i);
break;
}
}
}
}
ReleaseList(head);
return 0;
}
int StudentSystem()
{
LinkList head=0;
while(1)
{
cout<<"**************************************************************************"<<endl;
cout<<" 图书馆信息管理系统 "<<endl;
cout<<endl;
{
cout<<"**1.查找 2.显示 3.帮助 4.加载文件(创建链表) 0.退出**"<<endl;
cout<<"**************************************************************************"<<endl;
int menu;
cout<<endl<<"请选择合适的菜单项:";
cin>>menu;
while(cin.fail()||menu<0||menu>4)
{
cout<<"您选择了不合法的菜单项!"<<endl;
cin.clear();
fflush(stdin);
cin>>menu;
}
switch(menu)
{
case 0:
cout<<"安全退出系统."<<endl;
exit(0);
case 1:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
FunSearch(head);
break;
}
case 2:
{
if(!head)
{
cout<<"链表不存在,请先建立链表."<<endl;
break;
}
DisplayList(head);
break;
}
case 3:
{cout<<endl;
cout<<"1.本系统可以实现图书数据的管理"<<endl;
cout<<"2.操作中Y和N区分大小写"<<endl;
break;
}
case 4:
{
head=Readfile(head);
break;
}
}
}
}
return 0;
}
int main()
{ GetTime();
string P,W;
P="ABC123";
cout<<"欢迎使用图书馆信息系统"<<endl;
cout<<"请选择用户类型:1.管理员 2.学生 0.退出"<<endl;
int menu;
cout<<endl<<"请选择合适的菜单项:";
cin>>menu;
while(cin.fail()||menu<0||menu>3)
{
cout<<"您选择了不合法的菜单项!"<<endl;
cin.clear();
fflush(stdin);
cin>>menu;
}
switch(menu)
{
case 0:
cout<<"安全退出系统."<<endl;
exit(0);
case 1:
{
cout<<"请输入密码:";
cin>>W;
if(P==W)
{TectherSystem();}
if(P!=W)
{cout<<"密码错误,您无权使用管理员系统!"<<endl;break;}
}
case 2:
{
StudentSystem();
}
}
return 0;
}