c语言小型管理程序,小型图书管理系统C语言程序

#includestruct book

{

char writer[20];

char title[20];

char publishinghouse[20];

char number[10];

float price;

struct book * next;

};

struct book * Create_Book();

void Insert(struct book * head);

void Delete(struct book * head);

void Print_Book(struct book * head);

void search_book(struct book * head);

void change_Book(struct book * head);

void save(struct book * head);

struct book * Create_Book()

{

struct book * head;

head=(struct book *)malloc(sizeof(struct book));

head->next=NULL;

return head;

}

void save(struct book * head)

{

struct book *p;

FILE *fp;

p=head;

fp=fopen("kucun.txt","w+");

fprintf(fp,"************************************************\n");

fprintf(fp,"书号\t 书名\t 作者\t 出版社\t 价格\t \n");

fprintf(fp," \n");

while(p->next!= NULL)

{

p=p->next;

fprintf(fp,"%s\t %s\t %s\t %s\t %.2f\t \n",p->number,p->title,p->writer,p->publishinghouse,p->price);

}

fprintf(fp,"************************************************\n");

fclose(fp);

printf(" 已将图书数据保存到 kucun.txt 文件\n");

}

//插入//

void Insert(struct book *head)

{

struct book *s, *p,*p1,*swap;

char flag=Y;

swap=(struct book *)malloc(sizeof(struct book));

p1=swap;

p=head;

while(flag==Y||flag==y)

{

s=(struct book *)malloc(sizeof(struct book));

printf("\n 请输入图书书号:");

fflush(stdin);

scanf("%s",s->number);

printf("\n 请输入图书书名:");

fflush(stdin);

scanf("%s",s->title);

printf("\n 请输入图书作者名:");

fflush(stdin);

scanf("%s",s->writer);

printf("\n 请输入图书出版社:");

fflush(stdin);

scanf("%s",s->publishinghouse);

printf("\n 请输入图书价格:");

fflush(stdin);

scanf("%f",&s->price);

printf("\n");

//排序//

p1=p->next;

if(head->next!=NULL)

do

{

if(strcmp((p1->number),(s->number))>0)

{

strcpy(swap->number,p1->number);

strcpy(swap->title,p1->title);

strcpy(swap->writer,p1->writer);

strcpy(swap->publishinghouse,p1->publishinghouse);

swap->price=p1->price;

strcpy(p1->number,s->number);

strcpy(p1->title,s->title);

strcpy(p1->writer,s->writer);

strcpy(p1->publishinghouse,s->publishinghouse);

p1->price=s->price;

strcpy(s->number,swap->number);

strcpy(s->title,swap->title);

strcpy(s->writer,swap->writer);

strcpy(s->publishinghouse,swap->publishinghouse);

s->price=swap->price;

}

p=p1;

p1=p->next;

}while(p1!=NULL);

p->next=s;

s->next=NULL;

printf(" ******* 添加成功!*******");

printf("\n 继续添加?(Y/N):");

fflush(stdin);

scanf("%c",&flag);

printf("\n");

if(flag==N||flag==n)

{break;}

else if(flag==Y||flag==y)

{continue;}

}

save(head);

}

//查找//

void search_book(struct book *head)

{

struct book * p;

char temp[20];

p=head;

if(head==NULL || head->next==NULL)

{

printf(" ******* 图书库为空!*******\n");

}

else

{

printf("请输入您要查找的书名: ");

fflush(stdin);

scanf("%s",temp);

while(p->next!= NULL)

{

p=p->next;

if(strcmp(p->title,temp)==0)

{

printf("\n图书已找到!\n");

printf("\n");

printf("书号: %s\t\n",p->number);

printf("书名: %s\t\n",p->title);

printf("作者名: %s\t\n",p->writer);

printf("出版社: %s\t\n",p->publishinghouse);

printf("价格: %.2f\t\n",p->price);

}

if(p->next==NULL)

{

printf("\n查询完毕!\n");

}

}

}

}

void Print_Book(struct book * head)

{

struct book * p;

if(head==NULL || head->next==NULL)

{

printf("\n ******* 没有图书记录! *******\n\n");

}

p=head;

printf("*************************************\n");

printf(" 书号\t书名\t作者\t出版社\t价格\t\n");

printf("*************************************\n");

while(p->next!= NULL)

{

p=p->next;

printf("%s\t%s\t%s\t%s\t%.2f\t\n",p->number,p->title,p->writer,p->publishinghouse,p->price);

}

printf("*************************************\n");

printf("\n");

}

//更改//

void change_Book(struct book * head)

{

struct book * p;

int panduan=0;

char temp[20];

p=head;

printf("请输入要修改图书的书号:");

scanf("%s",temp);

while(p->next!= NULL)

{p=p->next;

if(strcmp(p->number,temp)==0)

{

printf("\n 请输入图书书名:");

fflush(stdin);

scanf("%s",p->title);

printf("\n 请输入图书作者名:");

fflush(stdin);

scanf("%s",p->writer);

printf("\n 请输入图书出版社:");

fflush(stdin);

scanf("%s",p->publishinghouse);

printf("\n 请输入图书价格:");

fflush(stdin);

scanf("%f",&p->price);

printf("\n");

panduan=1;}}

if(panduan==0)

{

printf(" \n******没有图书记录!******\n\n");}

}

//删除//

void Delete(struct book *head)

{

struct book *s,*p;

char temp[20];

int panduan;

panduan=0;

p=s=head;

printf(" [请输入您要删除的书名]:");

scanf("%s",temp);

p=p->next;

while(p!= NULL)

{

if(strcmp(p->title,temp)==0)

{

panduan=1;

break;

}

p=p->next;

}

if(panduan==1)

{

while(s->next!=p)

{

s=s->next;

}

s->next=p->next;

free(p);

printf("\n ******* 删除成功!*******\n");

save(head);

}

else

{

printf(" 您输入的书目不存在,请确认后输入!\n");

}

}

//主函数//

void main()

{

struct book * head;

int choice;

head=NULL;

while(1)

{

printf(" *************************************************\n");

printf(" * 小型图书管理系统 *\n");

printf(" * *\n");

printf(" * [1]图书信息录入 *\n");

printf(" * *\n");

printf(" * [2]图书信息浏览 *\n");

printf(" * *\n");

printf(" * [3]图书信息查询 *\n");

printf(" * *\n");

printf(" * [4]图书信息修改 *\n");

printf(" * *\n");

printf(" * [5]图书信息删除 *\n");

printf(" * *\n");

printf(" * [6]退出系统 *\n");

printf(" *************************************************\n");

printf(" 请选择:");

fflush(stdin);

scanf("%d",&choice);

switch (choice)

{

case 1:

if(head==NULL)

{

head=Create_Book();

}

Insert(head);

break;

case 2:

Print_Book(head);

break;

case 3:

search_book(head);

break;

case 4:

change_Book(head);

break;

case 5:

Delete(head);

break;

case 6:

printf("\n");

printf(" ******* 感谢使用图书管理系统 ******\n");

break;

default:

printf(" ******* 输入错误,请重新输入!*******");

break;

}

}

}

- 14 -

C语言源代码的图书管理系统 #include<iostream.h> #include<fstream.H> #include<stdlib.h> #include<string.h> struct bookData { int booknumber; char bookname[30]; int store; }; struct Person { char name[20]; char studynumber[10]; int count; bookData Rbook[2]; //bookData Rbook; }; class History { public: History(fstream& a){readerbook=a;} void writehistory(); void readhistory(fstream&); private: fstream readerbook; }; class Liberian; void Find(fstream&); void CreateTxt(fstream&); class Reader { friend Liberian; public: Reader(fstream&); void print(); void handlebook(fstream&); void change(); bool findreader(); void Addreader(); void deletereader(); private: fstream file; Person person; }; class Liberian { public: Liberian(char *,char *); void changeReader(Reader&); void selfprint(); void Append(fstream&); private: char workname[20]; char worknumber[20]; }; int main() { fstream bookRecord(" book.dat",ios::in| ios::out); if(!bookRecord) { cerr<<" Can't open,try again!"<<endl; exit(1); } fstream readerRecord("reader.dat",ios::in|ios::out); if(!readerRecord) { cerr<<" Can't open,try again!"<<endl; exit(1); } int choice; int key; Reader reader(readerRecord);//读者管理系统 Liberian liberian("李天","123456");//图书管理员 History rec(readerRecord);//声明显示历史记录类 while(1) { cout<<" 请输入您的选择:\n\n" <<"1--图书管理员\n" <<"2--读者系统\n" <<"0--结束程序\n"; cin>>choice; switch(choice) { case 1: { cout<<" Hello,图书管理员:\n"; liberian.selfprint();cout<<endl; cout<<"1-- 增加新图书\n" <<"2-- 改变读者的属性\n" <<"3-- 显示历史记录\n" <<"0-- 返回\n"; cin>>key; switch(key) { case 1: liberian.Append(bookRecord); break; case 2: liberian.changeReader(reader); break; case 3: rec.readhistory(readerRecord); case 0: break ; } } break; case 2: { cout<<"学生读者\n"; int iflag=1; //reader.findreader(); cout<<"1-- 借书与还书信息\n" <<"2-- 修改个人信息\n" <<"0-- 返回\n"; cin>>key; switch(key) { case 1: reader.handlebook(bookRecord); break; case 2: reader.change(); break; case 0: break; } } break; default: return 0; } } return 0; } //增加书籍 void Liberian::Append(fstream& storef) { bookData book; int choice; int key; int num; storef.seekp(0,ios::end); int posEnd=storef.tellp(); cout<<"*************书籍入库***************"<<endl; while(1) { cout<<" Please enter your choice:"<<endl; cout<<"1--添加新书\n"<<"2--已有书籍\n"<<"0--返回\n"; cin>>choice; switch(choice) { case 1: cout<<" 书号, 书名, 数量?"<<endl; cin>>book.booknumber>>book.bookname>>book.store; storef.write(reinterpret_cast<char*>(&book),sizeof(bookData)); break; case 2: storef.seekg(0,ios::beg); cout<<"Booknumber?\n"; cin>>key; do{ storef.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&storef.tellg()!=posEnd); if(book.booknumber==key) { cout<<book.booknumber<<"----"<<book.bookname<<"----"<<book.store<<endl; cout<<" 输入已有书增加的数量:"<<endl; cin>>num; if(num>0) book.store+=num; else { cout<<" Invalid input"<<endl; } storef.seekp(-long(sizeof(bookData)),ios::cur);//////////////////// storef.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<"现在书籍: "<<book.bookname<<" 余量为: "<<book.store<<endl; } else cout<<"找不到此书,请重新查阅书号是否正确!"<<endl; break; case 0: return ; } } } //书籍查询 void Find(fstream & f) { bookData book; int key; int choice; f.seekg(0,ios::end); int posEnd=f.tellp(); cout<<"*************书籍查询***************"<<endl; while(1) { cout<<" 请输入您的选择\n" <<" 1-- 检索一本书\n" <<"2-- 显示全部书籍\n" <<"0-- 返回\n"; cin>>choice; switch(choice) { case 1: f.seekg(0,ios::beg); cout<<"输入你想检索书的书号"<<endl; cin>>key; do{ f.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&f.tellg()!=posEnd); if(book.booknumber==key) cout<<book.booknumber<<"---"<<book.bookname<<"---"<<book.store<<endl; else cout<<"找不到此书,请重新确认!"<<endl; break; case 2: f.seekg(0,ios::beg); do{ f.read(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<book.booknumber<<"---"<<book.bookname<<"--"<<book.store<<endl; }while(f.tellg()!=posEnd); break; case 0: return ; } } } Reader::Reader(fstream& c) { file=c; Person person={"0","0",0,{{0,"0",0}, {0,"0",0}}}; } void Reader::print() { cout<<"Name"<<"---"<<person.name<<'\n\n'<<"studynumber---"<<person.studynumber<<'\n'; } //操作书籍?????????????????????? void Reader::handlebook(fstream &filee) { int key; int choice; bookData book; bool iflag;int num=0; filee.seekp(0,ios::end); int posEnd=filee.tellp(); while(!(iflag=findreader())) { num++; if(num>=3) return; } //cout<<person.name; while(1) { cout<<"1-- 借书\n" <<"2-- 还书\n" <<"3-- 查找一本书\n" <<"0-- 返回"<<'\n'; cin>>choice; switch(choice) { case 1: { filee.seekp(0,ios::end); int posEnd=filee.tellp(); if(person.count<=1) { cout<<" 输入你要借阅图书的书号:"<<endl; cin>>key; filee.seekg(0); do{ filee.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&filee.tellp()!=posEnd); //cout<<book.booknumber<<endl; if(book.booknumber==key) { //cout<<" 找到你想借阅的图书,成功借阅!"<<endl; if(book.store>0) { person.Rbook[person.count]=book; person.count+=1; book.store-=1; filee.seekp(-long(sizeof(bookData)),ios::cur); filee.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<" 找到你想借阅的图书,成功借阅!"<<endl; cout<<"书籍 :"<<book.bookname<<"剩余的本数为:"<<book.store<<endl; } else cout<<" 现在此图书已经被借完了,请耐心等待几天!"<<endl; } else cout<<" 找不到你想要的图书"<<endl; } else cout<<"你最多只能借2本\n\n"; } break; case 2: { bookData blankbook={0,"0",0}; //if(person.count>0) //{ int iflag=0; filee.seekg(0); cout<<" 输入你想要还的书的书号:"<<endl; cin>>key; for(int i=0;i<2;i++) { if(key==person.Rbook[i].booknumber) { person.Rbook[i]=blankbook; cout<<"成功归还此书!"; iflag=1; person.count--; do{ filee.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&filee.tellp()!=posEnd); if(book.booknumber==key) { book.store+=1; filee.seekp(-long(sizeof(bookData)),ios::cur); filee.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<"书籍 :"<<book.bookname<<"余本量为: "<<book.store<<endl; } } } if(!iflag) { cout<<" 你没有借阅那本书,请确认!"<<endl; } // } // else // cout<<"你没有借书,请重新确认!\n\n"; } break; case 3: Find(filee); break; case 0: return; } } } void Reader::change() { char newname[20]; char newnumber[10]; int choice; file.seekp(0,ios::cur); int Posend=file.tellp(); bool iflag;int num=0; while(!(iflag=findreader())) { num++; if(num>=3) return; } cout<<" 请输入你的选择!"<<endl; cout<<"1--改变名字!\n" <<"2--修改学号r\n" <<"0--返回\n"; cin>>choice; switch(choice) { case 1: cout<<"输入你的新名字!\n"; cin>>newname; strcpy(person.name,newname); break; case 2: cout<<"输入你的信学号?\n"; cin>>newnumber; strcpy(person.studynumber,newnumber); break; default: break;; } file.seekp(-long(sizeof(Person)),ios::cur); file.write(reinterpret_cast<char*>(&person),sizeof(Person)); cout<<"信息修改成功!"<<endl; } void Liberian::selfprint() { cout<<" 图书管理员 :\n" <<worknumber<<"---"<<workname<<" 为您服务!\n"; } Liberian::Liberian(char *a,char*b) { strcpy(workname,a); strcpy(worknumber,b); } void Liberian::changeReader(Reader& a) { int choice; char newname[20];char newnumber[10]; cout<<"修改读者的数据信息!\n"; cout<<"1-- 增加一个读者\n" <<"2-- 删除一个读者\n" <<"0-- 结束程序\n"; cin>>choice; switch(choice) { case 1: a.Addreader(); break; case 2: a.deletereader(); break; case 0: return; } } void Reader::Addreader() { Person temp;bookData book={0,"0",0}; file.seekp(0,ios::end); int Posend=file.tellp(); cout<<"输入你想要增加的读者的名字"<<endl; cin>>temp.name; cout<<"新读者的学号:"<<endl; cin>>temp.studynumber; temp.Rbook[1]=book; temp.Rbook[0]=book; temp.count=0; file.write(reinterpret_cast<char*>(&temp),sizeof(Person)); cout<<" 成功添加!"<<temp.name<<endl; } void Reader::deletereader() { file.seekp(0,ios::end); int Posend=file.tellp(); Person person; char name[20]; cout<<"输入要删除的读者的名字!"<<endl; cin>>name; file.seekg(0); do{ file.read(reinterpret_cast<char*>(&person),sizeof(Person)); }while(strcmp(name,person.name)&&file.tellp()!=Posend); if(!strcmp(name,person.name)) { bookData blankbook={0,"0",0}; Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}}; file.seekp(-long(sizeof(Person)),ios::cur); file.write(reinterpret_cast<char*>(&guest),sizeof(Person)); cout<<"成功删除!"<<person.name<<endl; } else cout<<"查无此人!"<<endl; } bool Reader::findreader() { file.seekp(0,ios::end); int Posend=file.tellp(); char name[20]; Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}}; cout<<"输入查找人的名字!"; cin>>name; file.seekg(0); do{ file.read(reinterpret_cast<char*>(&person),sizeof(Person)); }while(strcmp(name,person.name)&&file.tellp()!=Posend); if(!strcmp(name,person.name)) { cout<<"查到此人:"<<person.name<<endl; return true; } else { cout<<"找不到此人,请重新确认!"<<endl; return false; } } //借阅历史 void History::readhistory(fstream& a) { readerbook=a; char name[10]="0"; readerbook.seekp(0,ios::end); int Posend=readerbook.tellp(); Person guest; int iflag=1; cout<<" 书籍借阅信息如下:"<<endl; cout<<"姓名 学号 借书量 书籍名"<<endl; readerbook.seekg(0,ios::beg); do{ readerbook.read(reinterpret_cast<char*>(&guest),sizeof(Person)); if(strcmp(guest.Rbook[1].bookname,name) || strcmp(guest.Rbook[1].bookname,name)) { cout<<guest.name<<" "<<guest.studynumber<<" "<<guest.count<<" "; for(int i=0;i<2;i++) if(strcmp(guest.Rbook[i].bookname,name)) cout<<guest.Rbook[i].bookname; iflag=0; } }while(readerbook.tellp()!=Posend); if(iflag) cout<<"没有读者借阅图书!"<<endl; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值