图书管理系统.zip

  • krabs
    了解作者
  • C/C++
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-07-01 16:29
    上传日期
图书管理系统,可实现书目的增减,排序,计数等等功能。
图书管理系统.zip
  • 图书管理系统.cpp
    8.7KB
内容介绍
#include <iostream> #include <cstring> #include <fstream> #include <stdlib.h> using namespace std; class Book { char bookNo[20]; char bookName[20]; int Sum; public: Book(char *No=NULL,char *Name=NULL,int n=0); void Show() { cout<<bookNo<<'\t'<<bookName<<'\t'<<Sum<<endl; } void print(ofstream &out) { out<<bookNo<<'\t'<<bookName<<'\t'<<Sum<<endl; } void setInfo(char *no, char *name, int n) { strcpy(bookNo,no); strcpy(bookName,name); Sum=n; } void upsetNum(int n) { Sum+=n; } void downsetNum(int n) { Sum-=n; } int getNum() { return Sum; } friend class bookList; }; Book::Book(char *No,char *Name,int n) { if(No) strcpy(bookNo, No); else bookNo[0]='\0'; if(Name) strcpy(bookName, Name); else bookName[0]='\0'; Sum=n; } class bookList { Book *Books; int curLen; int maxLen; public: bookList(int Len=0) { maxLen =Len; curLen =0; Books = new Book[Len]; } ~bookList() { delete [] Books; } void appendBook (char *no,char *name,int n) { if(curLen==maxLen) { Book *ptr; ptr=new Book[maxLen+10]; for(int i=0;i<curLen;i++) ptr[i]=Books[i]; delete [] Books; maxLen+=10; Books=ptr; } strcpy(Books[curLen].bookNo,no); strcpy(Books[curLen].bookName,name); Books[curLen].Sum=n; curLen++; } int isIn(char *bkNo) { int t; for(int i=0;i<curLen;i++) { if(strcmp(Books[i].bookNo,bkNo)==0) return (i); else t=-1; } return t; } void deleteBook (char *boNo) { Book *tempbook; int i,p; tempbook=new Book[maxLen]; for(i=0;i<curLen;i++) if(strcmp(Books[i].bookNo,boNo)==0) setCurBookLen(curLen-1); else { tempbook[i]=Books[i]; } delete Books; Books=tempbook; } Book & getBooks(int i) { return Books[i]; } int getcurLen () { return curLen; } void showAll () { int i; for(i=0;i<curLen;i++) { Books[i].Show(); } } void sortBooksByno() { int i,j; Book s; for(i=0;i<curLen-1;i++) for(j=0;j<curLen-i-1;j++) { if(strcmp(Books[j].bookNo,Books[j+1].bookNo)>0) { s=Books[j]; Books[j]=Books[j+1]; Books[j+1]=s; } } } void sortBooksByname() { int i,j; Book s; for(i=0;i<curLen-1;i++) for(j=0;j<curLen-i-1;j++) { if(strcmp(Books[j].bookName,Books[j+1].bookName)>0) { s=Books[j]; Books[j]=Books[j+1]; Books[j+1]=s; } } } void sortBooksBysum() { int i,j; Book s; for(i=0;i<curLen-1;i++) for(j=0;j<curLen-i-1;j++) { if(Books[j].Sum>Books[j+1].Sum) { s=Books[j]; Books[j]=Books[j+1]; Books[j+1]=s; } } } int curBookLen () { return curLen; } void setCurBookLen(int len) { curLen=len; } void readFromFile(char *filename) { ifstream in; in.open(filename); char a[50],b[50]; int c; if(!in) cout<<"无法打开文件!"<<'\n'; while(in>>a) { in>>b; in>>c; if(curLen==maxLen) appendBook(a,b,c); else { Books[curLen].setInfo(a,b,c); curLen++; } } in.close(); } void initBookList() { char s[20]="bookdata.txt"; readFromFile(s); } void writeToFile(char *filename) { int i; ofstream out; out.open(filename); for(i=0;i<curLen;i++) out<<Books[i].bookNo<<'\t'<<Books[i].bookName<<'\t'<<Books[i].Sum<<endl; } void reverse () { Book temp; int i,j; j=curLen-1; for(i=0;i<j;i++,j--) { temp=Books[i]; Books[i]=Books[j]; Books[j]=temp; } } }; char Quit(bookList &booksObj) { char ans; cout<<"确定退出?y/n: "; cin>>ans; if(ans=='y') booksObj.writeToFile("bookdata.txt"); return ans; } int menu_select( ) { char *menu[ ]={ " 1. 增加图书记录", " 2. 删除图书记录", " 3. 显示所有图书记录", " 4. 查询图书", " 5. 图书入库", " 6. 图书销售", " 7. 批量导入图书记录", " 8. 导出图书记录", " 9. 排序", " 10. 删除全部图书记录", " 11. 逆序", " 0. 退出", NULL }; int choice; do { system("cls"); cout<<"\n ==============图书管理系统============== \n"; for(int i=0; menu[i]; i++) cout<<menu[i]<<endl; cout<<" ======================================= \n"; cout<<"请输入选择:"; cin>>choice; } while(choice<0 || choice>11); return(choice); } void appendRecord(bookList &booksObj) { char no[20]; char name[50]; int s; cout<<"请输入需要添加图书信息:"; cin>>no; cin>>name; cin>>s; booksObj.appendBook (no,name,s); } void delRecord (bookList &booksObj) { cout<<"请输入需要删除的书号:"<<endl; char no[20]; cin>>no; int p=booksObj.isIn(no); if(p>-1&&p<booksObj.getcurLen()) { cout<<"是否删除该书籍?y/n"<<endl; char ans; cin>>ans; if(ans=='y') { booksObj.deleteBook (no); cout<<"删除成功!"; } } else cout<<"该书不存在!"<<endl; } void showAll(bookList &booksObj) { booksObj.showAll(); } void sort(bookList &bookObj) { int ans; cout<<"请输入排序方式:"<<endl; cout<<"1.按书号排序"<<endl<<"2.按书名排序"<<endl<<"3.按库存量排序"<<endl; for(;;) { cin>>ans; cout<<"请输入需要排成"<<endl<<"1.升序"<<'\t'<<"2.降序"<<endl; int i; cin>>i; if(ans==1) if(i==1) {bookObj.sortBooksByno();break;} else if(i==2) { bookObj.sortBooksByno(); bookObj.reverse(); break; } else {cout<<"请输入正确的选项!"<<endl;break;} else if(ans==2) if(i==1) {bookObj.sortBooksByname();break;} else if(i==2) { bookObj.sortBooksByname(); bookObj.reverse(); break; } else {cout<<"请输入正确的选项!"<<endl;break;} else if(ans==3) if(i==1) {bookObj.sortBooksBysum();break;} else if(i==2) { bookObj.sortBooksBysum(); bookObj.reverse(); break; } else {cout<<"请输入正确的选项!"<<endl;break;} else {cout<<"请输入正确的选项!";} } } void query(bookList &booksObj) { char no[20]; int i; cout<<"请输入书号:"; cin>>no; i=booksObj.isIn(no); if(i>-1) { cout<<"该书为:"; booksObj.getBooks(i).Show(); } else cout<<"没有该图书!"<<endl; } void addBooks(bookList &booksObj) { int i,num,p; char s[20],tbook[50]; cout<<"请输入入库图书的书号和入库数量:(用空格隔开)"<<endl; cin>>s>>num; i=booksObj.isIn(s); p=booksObj.getBooks(i).getNum(); if(i>-1) { booksObj.getBooks(i).upsetNum(num); p=booksObj.getBooks(i).getNum(); } else { cout<<"该书不存在! 请输入书名:"<<endl; cin>>tbook; booksObj.appendBook (s,tbook,num); } } void sellBooks(bookList &booksObj) { char a[20]; int num,p,i; cout<<"请输入需要销售图书的书号以及销售数量:"<<endl; cin>>a>>num; i=booksObj.isIn(a); p=booksObj.getBooks(i).getNum(); if(i>-1) { if(p<num) cout<<"该书库存量不足!"<<endl; else { booksObj.getBooks(i).downsetNum(num); p=booksObj.getBooks(i).getNum(); } } else cout<<"该书不存在!"<<endl; } void delAll(bookList &booksObj) { char ans; cout<<"确定删除全部数据?y/n"<<endl; cin>>ans; if(ans=='y') booksObj.setCurBookLen(0); } void appendFromTxtFile(bookList &booksObj) { char s[20]; cout<<"请输入文件名:"<<endl; cin>>s; booksObj.readFromFile(s); } void writeToTxtFile(bookList &booksObj) { char s[50]; cout<<"请输入输出文件名:"<<endl; cin>>s; booksObj.writeToFile(s); } void revers(bookList &booksObj) { booksObj.reverse(); } int main() { bookList booksObj; boo
评论
    相关推荐
    • C++ Primer
      C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。 对C++基本概念和技术全面而且权威的阐述,对...
    • c++课件
      c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件
    • C++
      C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++
    • Effective C++
      学习c++的经典书籍!每一个学习c++的人有时间最好看看!你会获得很大收获!
    • c++information
      c++c++c++c++c++c++c++c++c++c++c++c++
    • SourceStyler C++
      用DEV-C++写代码很方便,就是不能格式化有点郁闷 c++格式化的好工具 效率高
    • c++yuyanbiancheng
      这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!
    • C++ primier
      学习C++的经典之作,希望大家学习愉快,快速掌握C++的各种知识。多下哈
    • effective C++
      有关C++编程方面的检验性介绍,对由C转向C++,和有C++编程基础的程序员有帮助,不过是英文版
    • effective c++
      C++经典书籍Effective C++ By Scott Meyers, 已译成中文,chm格式,方便检阅 Uploaded by save (chaisave@sohu.com)