1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
| #include<bits/stdc++.h.> using namespace std; class Time { int year,month,day; public: Time(){loadtime();} Time(int y,int m,int d):year(y),month(m),day(d){}; void loadtime(); friend istream & operator>>(istream &in,Time & a); friend stringstream & operator>>(stringstream &in,Time & a); friend ostream & operator<<(ostream &out,Time & a); bool operator <(const Time &d)const{ return year!=d.year?year<d.year:month!=d.month?month<d.month:day<d.day; } int judge() const; int judge(int year) const ; Time operator +(int a) ; friend int operator -(Time a,Time b); }; Time Time::operator +(int a) { int m[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; day+=a; int flag=1; while(day>m[month]){ if(month==2){ if(day>m[month]+judge()){ day-=m[month]+judge(); month++; } } else { day=day-m[month]; month++; } if(month>12){ month-=12; year++; } } } int operator -(Time a,Time b) { int monthdays[2][12] = { { 31,28,31,30,31,30,31,31,30,31,30,31 },{ 31,29,31,30,31,30,31,31,30,31,30,31 } }; int yeardays[2] = { 365,366 }; int sumdays=0; if (a.year == b.year&& a.month == b.month){ sumdays = b.day - a.day; } else if (a.year == b.year){ sumdays += monthdays[a.judge(a.year)][a.month-1] - a.day; for (int i = a.month; i < b.month-1; i++) sumdays += monthdays[a.judge(a.year)][i]; sumdays += b.day; } else{ sumdays += monthdays[a.judge(a.year)][a.month-1] - a.day; for (int i = a.month; i < 12; i++) sumdays += monthdays[a.judge(a.year)][i]; for (int i = a.year + 1; i < b.year; i++) sumdays += yeardays[a.judge(i)]; for (int i = 0; i < b.month - 1; i++) sumdays += monthdays[a.judge(b.year)][i]; sumdays += b.day; } return sumdays; } int Time::judge() const{ if(year % 4 == 0 && year %100 != 0 ||year % 400 == 0) return 1; else return 0; } int Time::judge(int year) const{ if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)return 1; else return 0; } void Time::loadtime(){ time_t rawtime; struct tm *ptminfo; time(&rawtime); ptminfo = localtime(&rawtime); year=ptminfo->tm_year + 1900; month=ptminfo->tm_mon + 1; day=ptminfo->tm_mday; } istream & operator>>(istream &in,Time & a){ in>>a.year>>a.month>>a.day; return in; } ostream & operator<<(ostream &out,Time & a) { out<<a.year<<" "<<a.month<<" "<<a.day<<" "; return out; } stringstream & operator>>(stringstream &in,Time & a){ in>>a.year>>a.month>>a.day; return in; }
class Book { string name; string press; string pubtime; string author; string suoshu; string id; string locate; string jsr; int fk; bool xj; Time jy; public: Book():name(""),press(""),author(""),id(""),suoshu(""),locate(""),jsr("-1"),fk(0){} string getname ()const{return name;} string getpress ()const{return press;} string getauthor ( )const{return author;} string getpubtime ( )const{return pubtime;} string getid ( )const{return id;} string getsuoshu ( )const{return suoshu;} int getfk(){jsfk();return fk;} void xxj(){ xj=!xj; } bool getxj(){ return xj;} void js(string idcard){jsr=idcard;} Time& getreback(); void jsfk(); void print(); friend istream & operator>>(istream &in,Book & a); friend ostream & operator<<(ostream &out,Book & a); }; void Book::jsfk() { int brr; if(xj) brr=120; else brr=60; Time tem; fk=tem-jy-brr; if(fk<0) fk=0; } void Book::print() { cout<<name<<" "<<press<<" "<<author<<" "<<id<<" "<<suoshu<<" "<<locate<<" "; if(jsr!="-1") cout<<"已借出 归还时间:"<<getreback()<<endl; else cout<<endl; } Time& Book::getreback(){ static Time tem(jy); tem+60*(1+getxj()); return tem; } istream & operator>>(istream &in,Book & a){ in>>a.name>>a.press>>a.pubtime>>a.author>>a.suoshu>>a.id>>a.jsr>>a.locate>>a.jy>>a.xj; return in; } ostream & operator<<(ostream &out,Book & a){ out<<a.name<<" "<<a.press<<" "<<a.pubtime<<" "<<a.author<<" "<<a.suoshu<<" "<<a.id<<" "<<a.jsr<<" "<<a.locate<<" "<<a.jy<<" "<<a.xj; return out; }
class Student { string name; string id; string key; string mail; vector<string> jy;
int yj; public: Student() :name(""),id(""),key(""),mail(""){jy.clear();} vector<string> getjy() const{return jy;} string getname(){return name;} string getid(){return id;} string getkey(){return key;} string getmail(){return mail;} int getyj()const {return yj;} void setkey(string tem) ; void jieyue(string obid); void reback(string obid);
friend istream & operator>>(istream &in,Student & a); friend ostream & operator<<(ostream &out,Student & a); } ; istream & operator>>(istream &in,Student & a) { in>>a.name>>a.id>>a.key>>a.mail>>a.yj; string tem; for(int i=0;i<a.yj;i++) { in>>tem; a.jy.push_back(tem); } return in; } ostream & operator<<(ostream &out,Student & a) { out<<a.name<<" "<<a.id<<" "<<a.key<<" "<<a.mail<<" "<<a.yj<<" "; for(int i=0;i<a.yj;i++) cout<<a.jy[i]<<" "; return out; } void Student::jieyue(string obid) { if(yj>=10) cout<<"借阅数量已达上限"<<endl; else jy.push_back(obid); yj=jy.size(); } void Student::reback(string obid) { auto po=find(jy.begin(),jy.end(),obid); if(po==jy.end()) cout<<"还书失败,请联系管理员"<<endl; else jy.erase(po); yj=jy.size(); } void Student::setkey(string tem) { while(tem!=key) { cout<<"密码错误,请重试"<<endl; cin>>tem; } cin>>tem; key=tem; }
class Operate { Time time; string oper; string peo; string boo; int fk; public: Operate( ):oper(""),peo(""),boo(""),fk(-1){} Operate(string o,string p,string b,int c):oper(o),peo(p),boo(b),fk(c){time.loadtime();} string getname(){return peo;} string getbook(){return boo;} string getoper(){return oper;} int getfk(){return fk;} void print() ; friend istream & operator>>(istream &in,Operate & a); friend ostream & operator<<(ostream &out,Operate & a); } ; void Operate::print() { cout<<time<<" "<<peo<<" "<<oper<<" "<<boo<<" "; if(fk!=-1) cout<<"缴纳罚款:"<<fk<<endl; } istream & operator>>(istream &in,Operate & a) { in>>a.time>>a.peo>>a.oper>>a.boo>>a.fk; return in; } ostream & operator<<(ostream &out,Operate & a) { out<<a.time<<" "<<a.peo<<" "<<a.oper<<" "<<a.boo<<" "<<a.fk<<" "; return out; }
class Library { string account; string password; vector<Book> book; vector<Student> reader; vector<Operate> record; multimap<string,int> pubtimque; multimap<string,int> suoshuque; multimap<string,int> pressque; map<string,int>idtorea; map<string ,int> idtoob; multimap<string,int> ridtorec; multimap<string,int> bidtorec; public: Library(string ac,string pass): account(ac),password(pass){load();login();cout<<book.size();} void login(); void querysm(string name) ; void queryss(string suoshu) ; void queryath(string ath); void querypres(string pre); void querytim(string pub); void quepers(); void quebrro(); void queop(); int cxkj(); void jieshu(string id) ; void huanshu(string id); void xujie(string id); void load(); void save(); ~Library(){save();} };
void Library::login() { while(1){ auto po=idtorea.find(account); if(po!=idtorea.end()&&reader[po->second].getkey()==password) break; cout<<"账号密码不匹配\n请重新输入\n"; cin>>account>>password; } cout<<"登陆成功\n"; } void Library:: querysm(string name) { for(auto po=book.begin();po!=book.end();po++) { auto m=po->getname().find(name); if(m!=-1) po->print(); } } void Library:: queryss(string suoshu) { auto beg=suoshuque.lower_bound(suoshu); auto en=suoshuque.upper_bound(suoshu); for(auto po=beg;po!=en;po++)book[po->second].print(); } void Library:: queryath(string ath){ for(auto po=book.begin();po!=book.end();po++) { auto m=po->getauthor().find(ath); if(m!=-1) po->print(); } } void Library:: querypres(string pre) { auto beg=pressque.lower_bound(pre); auto en=pressque.upper_bound(pre); for(auto po=beg;po!=en;po++) book[po->second].print(); } void Library:: querytim(string pub) { auto beg=pubtimque.lower_bound(pub); auto en=pubtimque.upper_bound(pub); for(auto po=beg;po!=en;po++)book[po->second].print(); } void Library:: quepers(){ cout<<reader[idtoob[account]].getname()<<" "; cout<<reader[idtoob[account]].getid()<<" "; cout<<reader[idtoob[account]].getmail()<<" "; cout<<reader[idtoob[account]].getyj()<<" "; cout<<cxkj()<<endl; } void Library:: quebrro() { for(auto po=reader[idtoob[account]].getjy().begin(); po!=reader[idtoob[account]].getjy().end();po++) book[idtoob[ *po]].print(); } void Library:: queop() { auto beg=ridtorec.lower_bound(account); auto en=ridtorec.upper_bound(account); for(auto po=beg;po!=en;po++)record[po->second].print(); } int Library:: cxkj() { int ans=0; for(auto po=reader[idtoob[account]].getjy().begin(); po!=reader[idtoob[account]].getjy().end();po++) ans+=book[idtoob[ *po]].getfk(); if(ans>0) return -1; else return 10-reader[idtoob[account]].getjy().size(); } void Library:: jieshu(string id) { if(cxkj()==-1){ cout<<"归还逾期,请先归还\n"; return ;} else if(cxkj()==0) { cout<<"达到借书上限\n"; return ;} reader[idtorea[account]].jieyue(id); book[idtoob[id]].js(account); Operate tem("借书",account,id,-1); record.push_back(tem); ridtorec.insert(make_pair(account,record.size()-1)); bidtorec.insert(make_pair(id,record.size()-1)); } void Library:: huanshu(string id) { reader[idtorea[account]].reback(id); book[idtoob[id]].js("-1"); int money=book[idtoob[id]].getfk(); Operate www("还书",account,id,money); record.push_back(www); ridtorec.insert(make_pair(account,record.size()-1)); bidtorec.insert(make_pair(id,record.size()-1)); } void Library:: xujie(string id) { if(cxkj()==-1){ cout<<"归还逾期,请先归还\n"; return ;} else if(cxkj()==0) { cout<<"达到借书上限\n"; return ;} book[idtoob[id]].xxj(); Operate tem("续借",account,id,-1); record.push_back(tem); ridtorec.insert(make_pair(account,record.size()-1)); bidtorec.insert(make_pair(id,record.size()-1)); } void Library::save() { ofstream out1("d:\\book.txt",ios::out); for(auto po=book.begin(); po!=book.end(); po++) out1<<*po<<endl; out1.close(); ofstream out2("d:\\reader.txt",ios::out); for(auto po=reader.begin(); po!=reader.end(); po++) out2<<*po<<endl; out2.close(); ofstream out3("d:\\record.txt",ios::out); for(auto po=record.begin(); po!=record.end(); po++) out3<<*po<<endl; out3.close(); } void Library:: load() { ifstream in1("d:\\book.txt",ios::in); if(in1) { Book tem1; book.clear(); while(in1>>tem1) { book.push_back(tem1); idtorea.insert(make_pair(tem1.getid(),book.size()-1)); pubtimque.insert(make_pair(tem1.getpubtime(),book.size()-1)); suoshuque.insert(make_pair(tem1.getsuoshu(),book.size()-1)); pressque.insert(make_pair(tem1.getpress(),book.size()-1)); } in1.close(); } ifstream in2("d:\\reader.txt",ios::in); if(in2) { reader.clear(); Student tem2; while(in2>>tem2) { reader.push_back(tem2); idtorea.insert(make_pair(tem2.getid(),reader.size()-1)); } in2.close(); } ifstream in3("d:\\record.txt",ios::in); if(in3) { record.clear(); Operate tem3; while( in3>>tem3) { record.push_back(tem3); ridtorec.insert(make_pair(tem3.getname(),record.size()-1)); bidtorec.insert(make_pair(tem3.getbook(),record.size()-1)); } in3.close(); } } int main() { string account,key,name,suoshu,ath,pre,pub,id; cin>>account>>key; Library demo(account,key); cin>>name; demo. querysm( name) ; cin>>suoshu; demo. queryss( suoshu) ; cin>>ath; demo. queryath( ath); cin>>pre; demo. querypres( pre); cin>>pub; demo. querytim( pub); demo. quepers(); demo. quebrro(); demo. queop(); demo. cxkj(); cin>>id; demo. jieshu( id) ; cin>>id; demo. huanshu( id); cin>>id; demo. xujie( id); return 0; }
|