C 课程设计 图书库存记录管理 源代码.docx
- 文档编号:8596215
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:13
- 大小:16.74KB
C 课程设计 图书库存记录管理 源代码.docx
《C 课程设计 图书库存记录管理 源代码.docx》由会员分享,可在线阅读,更多相关《C 课程设计 图书库存记录管理 源代码.docx(13页珍藏版)》请在冰豆网上搜索。
C课程设计图书库存记录管理源代码
#include
#include
#include
#include
ifstreamin;
ofstreamout;
classBook//Book类
{
charbookNo[5],bookName[40];
intNum;
charbookPress[40];
public:
Book(char*bookNo="",char*bookNum="",intNum=0,char*bookPress="")
{
if(*bookNo)
strcpy(this->bookNo,bookNo);
elsestrcpy(this->bookNo,"");
if(*bookName)
strcpy(this->bookName,bookName);
elsestrcpy(this->bookName,"");
this->Num=Num;
if(*bookPress)
strcpy(this->bookPress,bookPress);
elsestrcpy(this->bookPress,"");
}
voidShow()
{
cout< } voidPrint(ofstream&out) { out< } voidsetInfo(char*nop,char*nap) { strcpy(bookNo,nop); strcpy(bookName,nap); } voidsetNum(intn) {Num=n;} voidsetPress(char*bpp) { strcpy(bookPress,bpp);; } intgetNum() {returnNum;} friendclassbookList; }; classbookList//bookList类 { Book*Books; intcurLen; intmaxLen; public: bookList(intLen=20) { curLen=0; maxLen=Len; if(Len) Books=newBook[Len]; else Books=0; } ~bookList() {delete[maxLen]Books;} voidappendBook(char*nop,char*nap,intn,char*bpp)//增加一条图书记录 { if(curLen==maxLen) { Book*newBooks; newBooks=newBook[maxLen10]; for(inti=0;i newBooks[i]=Books[i]; delete[maxLen]Books; maxLen=10; Books=newBooks; } strcpy(Books[curLen].bookNo,nop); strcpy(Books[curLen].bookName,nap); Books[curLen].Num=n; strcpy(Books[curLen].bookPress,bpp); curLen; } intisIn(char*bkNo)//按书号查找图书 { inti; for(i=0;i if(strcmp(Books[i].bookNo,bkNo)==0)returni; return-1; } Book&getBooks(inti) { returnBooks[i]; } voiddeleteBook(char*bkNo)//删除指定书号的图书记录 { Book*newBooks; newBooks=newBook[maxLen]; inti,n; for(i=0;i if(strcmp(Books[i].bookNo,bkNo)==0)n=i; for(i=0;i =n;i) newBooks[i]=Books[i]; if(i while(i { newBooks[i]=Books[i1]; i; } delete[maxLen]Books; Books=newBooks; curLen--; } voidsortBooks()//将书籍按书号排成升序(此处用选择法排序) { inti,j,p; Bookt; for(i=0;i { p=i; for(j=i1;j if(strcmp(Books[j].bookNo,Books[p].bookNo)<0)p=j; if(p! =i) {t=Books[p];Books[p]=Books[i];Books[i]=t;} } } voidshowAll()//显示全部图书库存记录 { inti; for(i=0;i cout< < } intcurBookLen() {returncurLen;} voidsetCurBookLen(intLen) {curLen=Len;} voidreadFromFile(char*filename)//从参数指定的文件中读入库存记录 { in.open(filename,ios: : nocreate); intn,i=curLen; in>>n; curLen=n; charinfo[40]=""; for(;i ='\0';i) { in>>info; strcpy(Books[i].bookNo,info); in>>info; strcpy(Books[i].bookName,info); in>>n; Books[i].Num=n; in>>info; strcpy(Books[i].bookPress,info); } in.close(); } voidinitBookList()//用bookdata.txt文件初始化线性表 {readFromFile("bookdata.txt");} voidwriteToFile(char*filename)//将线性表中的记录写到参数指定的文件中 { out.open(filename); out< for(inti=0;i out< < out.close(); } }; //原型声明 intmenu_select(); voidappendRecord(bookList&booksObj); voiddelRecord(bookList&booksObj); voidshowAll(bookList&booksObj); voidquery(bookList&booksObj); voidaddBooks(bookList&booksObj); voidsellBooks(bookList&booksObj); voidappendFromTxtFile(bookList&booksObj); voidwriteToTxtFile(bookList&booksObj); voidsortByNo(bookList&booksObj); voiddelAll(bookList&booksObj); charQuit(bookList&booksObj); voidmain() { bookListbooksObj; intsel; booksObj.initBookList(); for(;;) { switch(sel=menu_select()) { case1: appendRecord(booksObj); system("pause");break; case2: delRecord(booksObj); system("pause");break; case3: showAll(booksObj); system("pause");break; case4: query(booksObj); system("pause");break; case5: addBooks(booksObj); system("pause");break; case6: sellBooks(booksObj); system("pause");break; case7: appendFromTxtFile(booksObj); system("pause");break; case8: writeToTxtFile(booksObj); system("pause");break; case9: sortByNo(booksObj); system("pause");break; case10: delAll(booksObj); system("pause");break; case0: if(Quit(booksObj)! ='y') continue; } if(sel==0)break; } } intmenu_select() { char*m[11]={"1.追加图书记录", "2.删除图书记录", "3.显示所有图书记录", "4.查询图书", "5.图书入库", "6.图书销售", "7.批量导入图书记录", "8.导出图书记录", "9.按书号排序", "10.删除全部图书记录", "0.退出"}; inti,choice; do{ system("cls"); cout<<"欢迎进入图书库存记录管理系统! \n\n菜单: \n"; for(i=0;i<11;i) cout< cout<<"\n请输入选择: "; cin>>choice; }while(choice<0||choice>10); return(choice); } voidappendRecord(bookList&booksObj) { cout<<"输入一本书的书号、书名、库存、出版社,换行隔开: "< charbkNo[5]="",bkName[40]="",bkPress[40]=""; intnum; cin>>bkNo; cin>>bkName; cin>>num; cin>>bkPress; booksObj.appendBook(bkNo,bkName,num,bkPress); } voiddelRecord(bookList&booksObj) { cout<<"请输入书号: "; charbkNo[5]=""; cin>>bkNo; intn=booksObj.isIn(bkNo); if(n>=0) { booksObj.getBooks(n).Show(); cout<<"是否删除该书籍? (y/n)"< chars; cin>>s; if(s=='y') { booksObj.deleteBook(bkNo); cout<<"删除成功! "< } elsecout<<"书籍未删除! "< } else cout<<"书籍不存在,删除失败! "< } voidshowAll(bookList&booksObj) {booksObj.showAll();} voidquery(bookList&booksObj) { cout<<"请输入书号: "; charbkNo[5]=""; cin>>bkNo; intn=booksObj.isIn(bkNo); if(n>=0) { cout<<"该书为: "; booksObj.getBooks(n).Show(); } else cout<<"书籍不存在! "< } voidaddBooks(bookList&booksObj)//图书入库 { cout<<"请输入书号和入库(增加)数量: "< charbkNo[5]=""; intNum; cin>>bkNo; cin>>Num; intn=booksObj.isIn(bkNo); if(n>=0) { booksObj.getBooks(n).Show(); cout<<"库存已增加: "< "< booksObj.getBooks(n).setNum(NumbooksObj.getBooks(n).getNum()); booksObj.getBooks(n).Show(); } else//若要增加库存的图书不存在,则转为入库 { cout<<"书籍不存在,转为入库,输入书名与出版信息入库: "< charbkName[40]="",bkPress[40]=""; cin>>bkName; cin>>bkPress; booksObj.appendBook(bkNo,bkName,Num,bkPress); } } voidsellBooks(bookList&booksObj) { cout<<"请输入销售图书的书号和销售数量: "< charbkNo[5]=""; intn; cin>>bkNo; cin>>n; ints=booksObj.isIn(bkNo); if(s>=0) { cout<<"该书为: "; booksObj.getBooks(s).Show(); intcur=booksObj.getBooks(s).getNum(); if(n { booksObj.getBooks(s).setNum(cur-n); cout<<"修改库存成功! 当前图书记录为: \n"; booksObj.getBooks(s).Show(); } else cout<<"库存不足,修改失败! "< } else cout<<"书籍不存在! "< } voidappendFromTxtFile(bookList&booksObj)//从文件批量导入图书记录 { cout<<"请输入文件名(带后缀): "; charfilename[40]=""; cin>>filename; booksObj.readFromFile(filename); } voidwriteToTxtFile(bookList&booksObj) { cout<<"输入导出文件名(带后缀): "; charfilename[40]=""; cin>>filename; booksObj.writeToFile(filename); } voidsortByNo(bookList&booksObj) {booksObj.sortBooks();} voiddelAll(bookList&booksObj) { cout<<"是否删除全部图书? (y/n)"< chars; cin>>s; if(s=='y') { booksObj.setCurBookLen(0); cout<<"书籍已全部删除! "< } elsecout<<"已取消操作! "< } charQuit(bookList&booksObj) { cout<<"是否确定退出? (y/n)"< chars; cin>>s; if(s=='y') { booksObj.writeToFile("bookdata.txt"); return'y'; } elsereturn'n'; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 课程设计 图书库存记录管理 源代码 图书 库存 记录 管理