1.追加图书记录2.删除图书记录3.显示所有图书记录4.查询图书5.图书入库6.图书销售7.批量导入图书记录8.导出图书记录9.按书号排序10.删除全部图书记录0.退出
1 //C++ 同学交流群373239689 2 //我的网站 rvfu.me 3 //个人公众号 rvfu98 4 #include5 #include 6 #include 7 #include 8 #include 9 using namespace std; 10 11 class Book 12 { 13 char bookNo[20], bookName[20]; 14 int Num; 15 public: 16 Book(char *nop =NULL, char *nap =NULL, int n =0) 17 { 18 if(nop)strcpy(bookNo, nop); 19 if(nap)strcpy(bookName, nap); 20 Num = n; 21 } 22 void Show() 23 { 24 cout << left << setw(20) << bookNo << left << setw(20) << bookName << left << setw(20) << Num << endl; 25 } 26 void Print(ofstream &out) 27 { 28 out << left << setw(20) << bookNo << left << setw(20) << bookName << left << setw(20) << Num << endl; 29 } 30 void setInfo(char *nop, char *nap, int n) 31 { 32 strcpy(bookNo, nop); 33 strcpy(bookName, nap); 34 Num = n; 35 } 36 void setNum(int n) 37 { 38 Num = n; 39 } 40 int getNum() 41 { 42 return Num; 43 } 44 friend class bookList; 45 }; 46 class bookList 47 { 48 Book *Books; 49 int curLen, maxLen; 50 public: 51 bookList(int len =20) 52 { 53 maxLen = len; 54 curLen = 0; 55 Books = new Book[maxLen]; 56 } 57 ~bookList() 58 { 59 delete []Books; 60 } 61 void appendBook(char *nop, char *nap, int n) 62 { 63 if(curLen == maxLen) 64 { 65 Book *newBooks = new Book[maxLen+10]; 66 for(int i=0; i > len;135 for (int i=0; i > bkNo >> bkName >> Num;138 appendBook(bkNo, bkName, Num);139 }140 infile.close();141 cout << "从文件插入了" << len << "条记录" << endl;142 }143 void writeToFile(char *filename)144 {145 ofstream outfile(filename);146 outfile << curLen << endl;147 for (int i=0; i > bkNo >> bkName >> Num;160 booksObj.appendBook(bkNo, bkName, Num);161 }162 163 void delRecord(bookList &booksObj)164 {165 char bkNo[20];166 cout << " 请输入您要删除的书号: ";167 cin >> bkNo;168 if (booksObj.isIn(bkNo) >-1) booksObj.deleteBook(bkNo);169 else cout << "您要删除的书不存在" << endl;170 }171 172 void showAll(bookList &booksObj)173 {174 booksObj.showAll();175 }176 177 void query(bookList &booksObj)178 {179 char bkNo[20];180 int i;181 182 cout << "请输入书号: ";183 cin >> bkNo;184 if ((i=booksObj.isIn(bkNo))>-1)185 {186 cout << "该书为";187 booksObj.getBooks(booksObj.isIn(bkNo)).Show();188 }189 else190 cout << "没有该图书!" << endl;191 }192 193 void addBooks(bookList &booksObj)194 {195 int i,Num, number;196 char bkNo[20];197 198 cout << "请输入您要入库的书的书号和数量: ";199 cin >> bkNo >> number;200 if((i=booksObj.isIn(bkNo)) > -1)201 {202 Num = booksObj.getBooks(i).getNum();203 booksObj.getBooks(i).setNum(Num+number);204 }205 else206 {207 cout << "该书不存在,请增加一条记录" << endl;208 appendRecord(booksObj);209 }210 }211 212 void sellBooks(bookList &booksObj)213 {214 int i, Num, sell;215 char bkNo[20];216 217 cout << "请输入您要销售书的书号、数量: " << endl;218 cin >> bkNo >> sell;219 220 if((i=booksObj.isIn(bkNo)) > -1)221 {222 Num = booksObj.getBooks(i).getNum();223 if(Num > filename;239 booksObj.readFromFile(filename);240 }241 242 void writeToTxtFile(bookList booksObj)243 {244 char filename[20];245 246 cout << "请输入文件名: ";247 cin >> filename;248 booksObj.writeToFile(filename);249 }250 251 void sortByNo(bookList &booksObj)252 {253 booksObj.sortBooks();254 }255 256 void deleteAll(bookList &booksObj)257 {258 booksObj.setCurBookLen(0);259 }260 261 char Quit(bookList &booksObj)262 {263 char flag;264 265 cout << "Are you sure to quit? input y or n :";266 cin >> flag;267 if(flag == 'y') booksObj.writeToFile("bookdata.txt");268 return flag;269 }270 271 int menu_select()272 {273 char *menu[11] = {274 "1. 追加图书记录",275 "2. 删除图书记录",276 "3. 查询所有图书记录",277 "4. 查询图书",278 "5. 图书入库",279 "6. 图书销售",280 "7. 批量导入图书",281 "8. 导出图书记录",282 "9. 按书号排序",283 "10. 删除全部图书记录",284 "0. 退出"285 };286 int choice;287 do{288 system("cls");289 cout << " 图书库存管理系统" << endl << endl;290 cout << " =================================" << endl;291 for(int i=0; i<11; i++)292 cout << " " << menu[i] << endl;293 cout << " =================================" << endl;294 cout << "请输入选择: ";295 cin >> choice;296 }while(choice<0 || choice>10);297 return choice;298 }299 300 int main()301 {302 bookList booksObj;303 int sel;304 booksObj.initBookList();305 306 for (;;)307 {308 switch(sel=menu_select())309 {310 case 1:appendRecord(booksObj);system("pause");break;311 case 2:delRecord(booksObj);system("pause");break;312 case 3:showAll(booksObj);system("pause");break;313 case 4:query(booksObj);system("pause");break;314 case 5:addBooks(booksObj);system("pause");break;315 case 6:sellBooks(booksObj);system("pause");break;316 case 7:appendFromTxtFile(booksObj);system("pause");break;317 case 8:writeToTxtFile(booksObj);system("pause");break;318 case 9:sortByNo(booksObj);system("pause");break;319 case 10:deleteAll(booksObj);system("pause");break;320 case 0:321 if(Quit(booksObj) != 'y') continue;322 }323 if(sel == 0) break;324 }325 return 0;326 }