首页 > 代码库 > C++程序设计原理与实践 第九章部分答案
C++程序设计原理与实践 第九章部分答案
1 #include "../../st.h" 2 class Name_pairs{ 3 public: 4 void read_names(); 5 void read_age(); 6 //void print(); 7 void sort1(); 8 vector<string> return_name() const; //习题3 9 vector<double> return_age() const; //习题3 10 private: 11 vector<string>name; 12 vector<double>age; 13 }; 14 15 16 vector<string> Name_pairs::return_name() const 17 { 18 return name; 19 } 20 21 vector<double> Name_pairs::return_age() const 22 { 23 return age; 24 } 25 26 void Name_pairs::read_names() 27 { 28 string s; 29 while(1) 30 { 31 cin>>s; 32 if(s=="over") 33 break; 34 name.push_back(s); 35 36 } 37 } 38 39 void Name_pairs::read_age() 40 { 41 double d; 42 for(int i=0;i<name.size();i++) 43 { 44 cout<<"input "<<name[i]<<"‘s age:"; 45 cin>>d; 46 age.push_back(d); 47 } 48 } 49 50 /*void Name_pairs::print() 51 { 52 for(int i=0;i<name.size();i++) 53 cout<<name[i]<<" "<<age[i]<<endl; 54 }*/ 55 56 void Name_pairs::sort1() 57 { 58 vector<string>name1; 59 vector<double>age1; 60 name1=name; 61 age1=age; 62 sort(name.begin(),name.end()); 63 for(int j=0;j<name.size();j++) 64 { 65 for(int k=0;k<name.size();k++) 66 { 67 if(name[j]==name1[k]) 68 age1[j]=age[k]; 69 } 70 } 71 age=age1; 72 73 74 } 75 76 ostream& operator<<(ostream& os,const Name_pairs& np) //习题3 77 { 78 for(int i=0;i<np.return_name().size();i++) 79 os<<np.return_name()[i]<<" "<<np.return_age()[i]<<endl; 80 return os; 81 } 82 83 bool operator==(const Name_pairs&a,const Name_pairs&b) //习题3 84 { 85 return a.return_name()==b.return_name() 86 &&a.return_age()==b.return_age(); 87 } 88 89 bool operator!=(const Name_pairs&a,const Name_pairs&b) //习题3 90 { 91 return !(a==b); 92 } 93 94 int main() 95 { 96 Name_pairs np; 97 np.read_names(); 98 np.read_age(); 99 np.sort1(); 100 cout<<np; 101 Name_pairs np1; 102 np1=np; 103 if(np1==np) 104 cout<<"yes"<<endl; 105 keep_window_open(); 106 }
1 #include "../../st.h" 2 struct ISBN 3 { 4 int n1,n2,n3; 5 char c; 6 7 }; 8 9 istream& operator>>(istream& is,struct ISBN&i) 10 { 11 char c1,c2,c3; 12 is>>i.n1>>c1>>i.n2>>c2>>i.n3>>c3>>i.c; 13 if(!is) 14 return is; 15 if(c1!=‘-‘||c2!=‘-‘||c3!=‘-‘) 16 { 17 is.clear(ios_base::failbit); 18 return is; 19 } 20 return is; 21 } 22 23 bool operator==(const struct ISBN&a,const struct ISBN&b) 24 { 25 return a.n1==b.n1&&a.n2==b.n2&&a.n3==b.n3&&a.c==b.c; 26 } 27 28 ostream& operator<<(ostream& os,struct ISBN&i) 29 { 30 return os<<i.n1<<"-"<<i.n2<<"-"<<i.n3<<"-"<<i.c; 31 } 32 33 struct DATA 34 { 35 int year; 36 int month; 37 }; 38 39 istream& operator>>(istream& is,struct DATA& dd) 40 { 41 char ch; 42 is>>dd.year>>ch>>dd.month; 43 if(!is) 44 return is; 45 46 while(ch!=‘-‘) 47 { 48 //is.clear(ios_base::failbit); 49 cout<<"输入日期错误,重新输入(格式year_month):"; 50 is>>dd.year>>ch>>dd.month; 51 if(!is) 52 return is; 53 } 54 while(dd.month<1||dd.month>12) 55 { 56 cout<<"输入月份错误,重新输入:"; 57 is>>dd.month; 58 if(!is) 59 return is; 60 } 61 return is; 62 } 63 64 class BOOK{ 65 public: 66 enum Genre 67 { 68 ficton=1,nonfiction,periodical,biography,children 69 }; 70 BOOK(); 71 struct ISBN re_isbn() {return isbn;}; 72 string re_name(){return name;}; 73 string re_author(){return author;}; 74 struct DATA re_data(){return data;}; 75 char re_is_borrow(){return is_borrow;}; 76 void change_borrow(); 77 void read_book_one(); 78 Genre re_genre(){return genre;}; 79 private: 80 struct ISBN isbn; 81 string name; 82 string author; 83 struct DATA data; 84 char is_borrow; 85 Genre genre; 86 }; 87 88 BOOK::BOOK() 89 { 90 isbn.n1=isbn.n2=isbn.n3=0; 91 isbn.c=0; 92 name=""; 93 author=""; 94 data.year=2001; 95 data.month=1; 96 is_borrow=‘f‘; 97 genre=Genre(1); 98 } 99 100 void BOOK::change_borrow()101 {102 if(is_borrow==‘t‘)103 is_borrow=‘n‘;104 else105 is_borrow=‘t‘;106 }107 108 void BOOK::read_book_one()109 {110 int i;111 cout<<"录入书名:";112 cin>>name;113 cout<<"录入作者:";114 cin>>author;115 cout<<"录入出版日期(year-month):";116 cin>>data;117 cout<<"录入ISBN号(x-x-x-x):";118 cin>>isbn;119 cout<<"录入书籍类型(ficton=1,nonfiction=2,periodical=3,biography=4,children=5):";120 cin>>i;121 genre=Genre(i);122 cout<<"是否已出借(t/f):";123 cin>>is_borrow;124 }125 126 127 void read_book(vector<BOOK>&book)128 {129 char ch;130 cout<<"是否开始录入书本(y/n):";131 cin>>ch;132 if(ch==‘n‘)133 return;134 BOOK b;135 int ii=1;136 while(1)137 {138 cout<<"录入书本"<<ii++<<":"<<endl;139 b.read_book_one();140 book.push_back(b);141 cout<<endl;142 cout<<"是否继续录入书本(y/n):";143 cin>>ch;144 if(ch==‘n‘)145 break;146 cout<<endl;147 }148 149 }150 151 void borrow(string s,vector<BOOK>&book)152 {153 int i;154 for(i=0;i<book.size();i++)155 {156 if(s==book[i].re_name())157 {158 if(book[i].re_is_borrow()==‘t‘)159 cout<<"sorry,the book is borrowed."<<endl;160 else161 {162 cout<<"here is the book."<<endl;163 book[i].change_borrow();164 }165 break;166 }167 }168 if(i==book.size())169 cout<<"sorry,no you want book"<<endl;170 171 }172 173 void return_book(string s,vector<BOOK>&book)174 {175 int i;176 for(i=0;i<book.size();i++)177 {178 if(s==book[i].re_name()){179 cout<<"thank your cooperation."<<endl;180 book[i].change_borrow();181 break;182 }183 }184 if(i==book.size())185 cout<<"sorry,the book not belong to us"<<endl;186 }187 188 bool operator==(BOOK&a,BOOK&b)189 {190 return a.re_isbn()==b.re_isbn();191 }192 193 bool operator!=(BOOK&a,BOOK&b)194 {195 return !(a==b);196 }197 198 ostream& operator<<(ostream& os,BOOK&b)199 {200 os<<endl<<"《"<<b.re_name()<<"》"<<endl<<"anthor: "<<b.re_author()<<endl<<"ISBN号: "<<b.re_isbn()<<endl<<"书籍类型: ";201 switch(b.re_genre()){202 case 1:203 os<<"fiction"<<endl;204 break;205 case 2:206 os<<"nonfiction"<<endl;207 break;208 case 3:209 os<<"periodical"<<endl;210 break;211 case 4: 212 os<<"biography"<<endl;213 break;214 case 5:215 os<<"children"<<endl;216 break;217 }218 return os;219 }220 221 int main()222 {223 vector<BOOK>book;224 read_book(book);225 /*if(book.size()!=0){226 string ss;227 cout<<"borrow book:";228 cin>>ss;229 borrow(ss,book);230 cout<<"return book:";231 cin>>ss;232 return_book(ss,book);233 }*/234 for(int i=0;i<book.size();i++)235 cout<<book[i]<<endl;236 keep_window_open();237 238 239 240 }
1 //////////////////////////////////////////////////////////////////////////////// 2 //book.h 3 4 #include "../../st.h" 5 struct ISBN 6 { 7 int n1,n2,n3; 8 char c; 9 10 }; 11 12 istream& operator>>(istream& is,struct ISBN&i) 13 { 14 char c1,c2,c3; 15 //is.clear(); 16 // is.sync(); 17 is>>i.n1>>c1>>i.n2>>c2>>i.n3>>c3>>i.c; 18 if(!is) 19 return is; 20 if(c1!=‘-‘||c2!=‘-‘||c3!=‘-‘) 21 { 22 is.clear(ios_base::failbit); 23 return is; 24 } 25 return is; 26 } 27 28 bool operator==(const struct ISBN&a,const struct ISBN&b) 29 { 30 return a.n1==b.n1&&a.n2==b.n2&&a.n3==b.n3&&a.c==b.c; 31 } 32 33 ostream& operator<<(ostream& os,struct ISBN&i) 34 { 35 return os<<i.n1<<"-"<<i.n2<<"-"<<i.n3<<"-"<<i.c; 36 } 37 38 struct DATE 39 { 40 int year; 41 int month; 42 }; 43 44 istream& operator>>(istream& is,struct DATE& dd) 45 { 46 char ch; 47 //is.clear(); 48 //is.sync(); 49 is>>dd.year>>ch>>dd.month; 50 if(!is) 51 return is; 52 53 while(ch!=‘-‘) 54 { 55 //is.clear(ios_base::failbit); 56 cout<<"输入日期错误,重新输入(格式year_month):"; 57 is>>dd.year>>ch>>dd.month; 58 if(!is) 59 return is; 60 } 61 while(dd.month<1||dd.month>12) 62 { 63 cout<<"输入月份错误,重新输入:"; 64 is>>dd.month; 65 if(!is) 66 return is; 67 } 68 return is; 69 } 70 71 72 73 class BOOK{ 74 public: 75 enum Genre 76 { 77 ficton=1,nonfiction,periodical,biography,children 78 }; 79 BOOK(); 80 BOOK(struct ISBN); 81 struct ISBN re_isbn() {return isbn;}; 82 string re_name(){return name;}; 83 string re_author(){return author;}; 84 struct DATE re_date(){return date;}; 85 char re_is_borrow(){return is_borrow;}; 86 void change_borrow(); 87 void read_book_one(); 88 Genre re_genre(){return genre;}; 89 90 private: 91 struct ISBN isbn; 92 string name; 93 string author; 94 struct DATE date; 95 char is_borrow; 96 Genre genre; 97 }; 98 99 BOOK::BOOK()100 {101 isbn.n1=isbn.n2=isbn.n3=0;102 isbn.c=0;103 name="";104 author="";105 date.year=2001;106 date.month=1;107 is_borrow=‘f‘;108 genre=Genre(1);109 }110 111 BOOK::BOOK(struct ISBN is)112 {113 isbn.n1=is.n1;114 isbn.n2=is.n2;115 isbn.n3=is.n3;116 isbn.c=is.c;117 name="";118 author="";119 date.year=2001;120 date.month=1;121 is_borrow=‘f‘;122 genre=Genre(1);123 }124 125 void BOOK::change_borrow()126 {127 if(is_borrow==‘t‘)128 is_borrow=‘n‘;129 else130 is_borrow=‘t‘;131 }132 133 void BOOK::read_book_one()134 {135 int i;136 cout<<"录入书名:";137 cin.clear();138 cin.sync();139 cin>>name;140 cout<<"录入作者:";141 cin.clear();142 cin.sync();143 cin>>author;144 cout<<"录入出版日期(year-month):";145 cin.clear();146 cin.sync();147 cin>>date;148 cout<<"录入ISBN号(x-x-x-x):";149 cin.clear();150 cin.sync();151 cin>>isbn;152 cout<<"录入书籍类型(ficton=1,nonfiction=2,periodical=3,biography=4,children=5):";153 cin.clear();154 cin.sync();155 cin>>i;156 genre=Genre(i);157 cout<<"是否已出借(t/f):";158 cin.clear();159 cin.sync();160 cin>>is_borrow;161 }162 163 164 void read_book(vector<BOOK>&book)165 {166 char ch;167 cout<<"是否开始录入书本(y/n):";168 cin>>ch;169 if(ch==‘n‘)170 return;171 BOOK b;172 int ii=1;173 while(1)174 {175 cout<<"录入书本"<<ii++<<":"<<endl;176 b.read_book_one();177 book.push_back(b);178 cout<<endl;179 cout<<"是否继续录入书本(y/n):";180 cin>>ch;181 if(ch==‘n‘)182 break;183 cout<<endl;184 }185 186 }187 188 void borrow(string s,vector<BOOK>&book)189 {190 int i;191 for(i=0;i<book.size();i++)192 {193 if(s==book[i].re_name())194 {195 if(book[i].re_is_borrow()==‘t‘)196 cout<<"sorry,the book is borrowed."<<endl;197 else198 {199 cout<<"here is the book."<<endl;200 book[i].change_borrow();201 }202 break;203 }204 }205 if(i==book.size())206 cout<<"sorry,no you want book"<<endl;207 208 }209 210 void return_book(string s,vector<BOOK>&book)211 {212 int i;213 for(i=0;i<book.size();i++)214 {215 if(s==book[i].re_name()){216 cout<<"thank your cooperation."<<endl;217 book[i].change_borrow();218 break;219 }220 }221 if(i==book.size())222 cout<<"sorry,the book not belong to us"<<endl;223 }224 225 bool operator==(BOOK&a,BOOK&b)226 {227 return a.re_isbn()==b.re_isbn();228 }229 230 bool operator!=(BOOK&a,BOOK&b)231 {232 return !(a==b);233 }234 235 ostream& operator<<(ostream& os,BOOK&b)236 {237 os<<endl<<"《"<<b.re_name()<<"》"<<endl<<"anthor: "<<b.re_author()<<endl<<"ISBN号: "<<b.re_isbn()<<endl<<"书籍类型: ";238 switch(b.re_genre()){239 case 1:240 os<<"fiction"<<endl;241 break;242 case 2:243 os<<"nonfiction"<<endl;244 break;245 case 3:246 os<<"periodical"<<endl;247 break;248 case 4: 249 os<<"biography"<<endl;250 break;251 case 5:252 os<<"children"<<endl;253 break;254 }255 return os;256 }257 258 259 260 //data.h261 262 #include "../../st.h"263 264 class Date{265 public:266 enum Month{267 Jan=1,Feb,Mar,Apr,May,Jun,Aug,Sep,Oct,Nov,Dec268 };269 class Invalid{};270 271 Date(int y,Month m,int d);272 Date();273 Date(int y,int m,int d);274 275 int day() {return d;};276 Month month() {return m;};277 int year() {return y;};278 279 //void add_day(int n);280 //void add_month(int n);281 //void add_year(int n);282 private:283 int y;284 Month m;285 int d;286 };287 288 289 290 291 292 293 Date& default_date()294 {295 static Date dd(2001,Date::Jan,1); 296 return dd;297 }298 299 Date::Date()300 :y(default_date().year()),m(default_date().month()),d(default_date().day())301 {}302 bool leapyear(int y)303 {304 if((y%4==0&&y%100!=0)||y%400==0) //习题10305 return true;306 else307 return false;308 }309 bool is_date(int y,Date::Month m,int d)310 {311 if(d<=0)312 return false;313 int days_in_month=31;314 switch(m)315 {316 case Date::Feb:317 days_in_month=(leapyear(y))?29:28;318 break;319 case Date::Apr: case Date::Jun:case Date::Sep:case Date::Nov:320 days_in_month=30;321 break;322 }323 if(days_in_month<d)324 return false;325 return true;326 }327 328 329 330 ostream& operator<<(ostream& os,Date&d)331 {332 return os<<‘(‘<<d.year()<<‘,‘<<d.month()<<‘,‘<<d.day()<<‘)‘;333 }334 335 istream& operator>>(istream& is,Date& dd)336 {337 int y,m,d;338 //char ch1,ch2,ch3,ch4;339 is>>y>>m>>d;340 if(!is)341 return is;342 /*if(ch1!=‘(‘||ch2!=‘,‘||ch3!=‘,‘||ch4!=‘)‘)343 {344 is.clear(ios_base::failbit);345 return is;346 }*/347 return is;348 }349 350 351 Date::Date(int yy,Month mm,int dd)352 :y(yy),m(mm),d(dd)353 {354 if(!is_date(yy,mm,dd))355 throw Invalid();356 }357 358 Date::Date(int yy,int mm,int dd)359 :y(yy),m((Month)mm),d(dd)360 {361 if(!is_date(yy,(Month)mm,dd))362 throw Invalid();363 }364 365 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////366 367 //patron.h368 369 #include "../../st.h" 370 class Patron{371 public:372 string re_name(){return name;}373 string re_number(){return number;}374 double re_expense(){return expense;}375 void set_expense(double d);376 void read_patron_one();377 Patron(string);378 Patron();379 private:380 string name;381 string number;382 double expense;383 };384 385 Patron::Patron()386 {387 name="";388 number="";389 expense=0;390 }391 392 Patron::Patron(string s)393 {394 name="";395 number=s;396 expense=0;397 }398 399 void Patron::set_expense(double d)400 {401 expense=d;402 }403 404 void Patron::read_patron_one()405 {406 cout<<"录入读者姓名:";407 cin>>name;408 cout<<"录入读者图书证号:";409 cin>>number;410 cout<<"录入读者借阅费:";411 cin>>expense;412 while(expense<0)413 {414 cout<<"录入借阅费错误,应>=0,重新录入:";415 cin>>expense;416 }417 }418 419 bool judge_expense(Patron& p)420 {421 if(p.re_expense()>0)422 return false;423 if(p.re_expense()==0)424 return true;425 }426 427 void read_patron(vector<Patron>&patron)428 {429 char ch;430 cout<<"是否开始录入读者数据(y/n):";431 cin>>ch;432 if(ch==‘n‘)433 return;434 Patron p;435 int ii=1;436 while(1)437 {438 cout<<"录入读者"<<ii++<<":"<<endl;439 p.read_patron_one();440 patron.push_back(p);441 cout<<endl;442 cout<<"是否继续录入读者数据(y/n):";443 cin>>ch;444 if(ch==‘n‘)445 break;446 cout<<endl;447 }448 449 }450 451 bool operator==(Patron&a,Patron&b)452 {453 return a.re_number()==b.re_number();454 }455 456 bool operator!=(Patron&a,Patron&b)457 {458 return !(a==b);459 }460 461 ostream& operator<<(ostream& os,Patron&p)462 {463 os<<p.re_name()<<" 图书证号:"<<p.re_number()<<endl;464 if(judge_expense(p))465 os<<"借阅费:"<<p.re_expense()<<endl;466 return os;467 }468 469 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////470 471 //transaction.h472 473 #include "book.h"474 #include "patron.h"475 #include "data.h"476 477 struct Transaction{478 BOOK book;479 Patron patron;480 Date date;481 };482 483 ostream& operator<<(ostream& os,struct Transaction&ts)484 {485 os<<ts.book<<ts.patron<<ts.date<<endl;486 return os;487 }488 489 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////490 491 //library.h492 493 #include "transaction.h"494 495 class Library{496 public:497 vector<BOOK> re_book(){return book;};498 vector<Patron> re_patron(){return patron;};499 vector<struct Transaction> re_transaction(){return transaction;};500 vector<struct Transaction> re_transaction_pf(){return transaction_pf;};501 502 void borrow_book_people(Patron&p,BOOK&b);503 void print_qf();504 void _read_book();505 void _read_patron();506 private:507 vector<BOOK> book;508 vector<Patron> patron;509 vector<struct Transaction> transaction;510 vector<struct Transaction> transaction_pf;511 };512 513 void Library::borrow_book_people(Patron&p,BOOK&b)514 {515 int i;516 517 for(i=0;i<book.size();i++)518 {519 if(b==book[i])520 {521 if(b.re_is_borrow()==true)522 cout<<"sorry,the book is already borrowed"<<endl;523 else524 b=book[i];525 break;526 }527 }528 if(i==book.size())529 {530 cout<<"没有这本书籍"<<endl;531 return;532 }533 534 for(i=0;i<patron.size();i++)535 if(p==patron[i])536 {537 p=patron[i];538 break;539 }540 if(i==patron.size())541 {542 cout<<"没有这个读者"<<endl;543 return;544 }545 546 547 548 int a1,b1,c1;549 struct Transaction ts;550 ts.book=b;551 ts.patron=p;552 cout<<"输入今天日期(y m d):"; //若想直接cin>>data,应该需要用友元函数553 cin.clear();554 cin.sync();555 cin>>a1>>b1>>c1;556 ts.date=Date(a1,b1,c1);557 558 if(!judge_expense(p))559 {560 cout<<"该读者已欠费"<<endl;561 transaction_pf.push_back(ts);562 }563 else564 {565 transaction.push_back(ts);566 }567 }568 569 void Library::print_qf()570 {571 for(int i=0;i<transaction_pf.size();i++)572 cout<<transaction_pf[i]<<endl;573 }574 575 void Library::_read_book()576 {577 char ch;578 cout<<"是否开始录入书本(y/n):";579 cin>>ch;580 if(ch==‘n‘)581 return;582 BOOK b;583 int ii=1;584 while(1)585 {586 cin.clear();587 cin.sync();588 cout<<"录入书本"<<ii++<<":"<<endl;589 b.read_book_one();590 book.push_back(b);591 cout<<endl;592 cin.clear();593 cin.sync();594 cout<<"是否继续录入书本(y/n):";595 cin>>ch;596 if(ch==‘n‘)597 break;598 cout<<endl;599 }600 }601 602 void Library::_read_patron()603 {604 char ch;605 cout<<"是否开始录入读者数据(y/n):";606 cin>>ch;607 cin.clear();608 cin.sync();609 if(ch==‘n‘)610 return;611 Patron p;612 int ii=1;613 while(1)614 {615 cout<<"录入读者"<<ii++<<":"<<endl;616 p.read_patron_one();617 patron.push_back(p);618 cout<<endl;619 cin.clear();620 cin.sync();621 cout<<"是否继续录入读者数据(y/n):";622 cin>>ch;623 if(ch==‘n‘)624 break;625 cout<<endl;626 }627 }628 629 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////630 631 //main.cpp632 633 #include "library.h"634 #include "../../st.h"635 636 int main()637 {638 Library l;639 static string s;640 static struct ISBN ib;641 l._read_book();642 l._read_patron();643 char ch;644 cout<<"是否进行借书程序(y/n):";645 cin>>ch;646 647 while(ch!=‘n‘)648 {649 cout<<"输入书籍信息:"<<endl;650 cin>>ib;651 static BOOK b(ib);652 cout<<"输入借书人信息:"<<endl;653 cin>>s;654 static Patron p(s);655 656 l.borrow_book_people(p,b);657 658 cout<<"是否继续借书程序(y/n):";659 cin>>ch;660 }661 662 l.print_qf();663 keep_window_open();664 }
1 #include "../../st.h" 2 class Patron{ 3 public: 4 string re_name(){return name;} 5 string re_number(){return number;} 6 double re_expense(){return expense;} 7 void set_expense(double d); 8 9 private:10 string name;11 string number;12 double expense;13 };14 15 void Patron::set_expense(double d)16 {17 expense=d;18 }19 20 bool judge_expense(Patron p)21 {22 if(p.re_expense()>0)23 return false;24 if(p.re_expense()==0)25 return true;26 }
1 #include "../../st.h" 2 enum Week{ 3 mon=1,tues,wed,thu,fri,sat,sun 4 }; 5 enum Month{ 6 Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec 7 }; 8 9 class Date{ 10 public: 11 12 13 class Invalid{}; 14 15 Date(int y,Month m,int d); 16 Date(); 17 Date(int y,int m,int d); 18 19 int day() {return d;} 20 Month month() {return m;} 21 int year() {return y;} 22 Week week() {return w;} 23 Week get_week(); 24 long sun_day(){return l;} 25 friend istream& operator>>(istream& is,Date& dd); 26 void add_day(int n); 27 void add_month(int n); 28 void add_year(int n); 29 void get_date(); 30 long get_sum_day(); 31 int day_sum(); 32 private: 33 int y; 34 Month m; 35 int d; 36 Week w; 37 long l; 38 }; 39 40 Date& default_date() 41 { 42 static Date dd(1970,Jan,1); 43 return dd; 44 } 45 46 Date::Date() 47 :y(default_date().year()),m(default_date().month()),d(default_date().day()) 48 { 49 50 } 51 52 bool leapyear(int y) //习题10 53 { 54 if((y%4==0&&y%100!=0)||y%400==0) 55 return true; 56 else 57 return false; 58 } 59 60 bool is_date(int y,Month m,int d) 61 { 62 if(d<=0) 63 return false; 64 int days_in_month=31; 65 switch(m) 66 { 67 case Feb: 68 days_in_month=(leapyear(y))?29:28; 69 break; 70 case Apr: case Jun:case Sep:case Nov: 71 days_in_month=30; 72 break; 73 } 74 if(days_in_month<d) 75 return false; 76 return true; 77 } 78 79 void Date::add_year(int n) 80 { 81 y+=n; 82 } 83 84 void Date::add_month(int n) 85 { 86 int i=m; 87 i+=n; 88 while(i>12) 89 { 90 i-=12; 91 add_year(1); 92 } 93 m=(Month)i; 94 } 95 96 void Date::add_day(int n) 97 { 98 int days_in_month; 99 d+=n;100 while(1)101 {102 switch(m)103 {104 case Feb:105 days_in_month=(leapyear(y))?29:28;106 break;107 case Apr: case Jun:case Sep:case Nov:108 days_in_month=30;109 break;110 }111 112 if(d>days_in_month)113 {114 d=d-days_in_month;115 add_month(1);116 }117 else{118 w=get_week();119 l+=n;120 break;121 }122 }123 124 }125 126 ostream& operator<<(ostream& os,Date&d)127 {128 return os<<‘(‘<<d.year()<<‘,‘<<d.month()<<‘,‘<<d.day()<<‘)‘<<"--"<<d.sun_day()<<endl;129 }130 131 Week Date::get_week()132 {133 int y1,m1,c1,d1;134 if(m==1||m==2)135 {136 m1=12+m;137 y1=(y-1)%100;138 c1=(y-1)/100;139 d1=d;140 }141 else142 {143 m1=m;144 y1=y%100;145 c1=y/100;146 d1=d;147 }148 int x=y1+y1/4+c1/4-2*c1+(26*(m1+1)/10)+d1-1;149 x%=7;150 return (Week)x;151 }152 153 void Date::get_date()154 {155 int y1,m1,d1;156 cin>>y1>>m1>>d1;157 while(y1<1970||m1<1||m1>12||d1<1||d1>31)158 {159 if(y1<1970)160 cout<<"输入年份错误,重新输入:";161 if(m1<1||m1>12)162 cout<<"输入月份错误,重新输入:";163 if(d1<1||d1>31)164 cout<<"输入号数错误,重新输入:";165 cin>>y1>>m1>>d1;166 }167 168 //可加上清除输入流的函数169 170 y=y1;171 m=Month(m1);172 d=d1;173 w=get_week();174 l=get_sum_day();175 }176 177 long Date::get_sum_day()178 {179 long l1=0;;180 int i;181 for(i=1970;i<y;i++)182 {183 static Date d1(i,(Month)12,31);184 l1+=d1.day_sum();185 }186 l1+=day_sum();187 l=l1;188 return l1-1;189 }190 191 istream& operator>>(istream& is,Month&m)192 {193 is>>m;194 return is;195 }196 197 istream& operator>>(istream& is,Date& dd)198 {199 int y,m,d;200 //char ch1,ch2,ch3,ch4;201 is>>y>>m>>d;202 if(!is)203 return is;204 /*if(ch1!=‘(‘||ch2!=‘,‘||ch3!=‘,‘||ch4!=‘)‘)205 {206 is.clear(ios_base::failbit);207 return is;208 }*/209 return is;210 }211 212 213 Date::Date(int yy,Month mm,int dd)214 :y(yy),m(mm),d(dd)215 {216 w=get_week();217 l=get_sum_day();218 if(!is_date(yy,mm,dd))219 throw Invalid();220 }221 222 Date::Date(int yy,int mm,int dd)223 :y(yy),m((Month)mm),d(dd)224 {225 w=get_week();226 l=get_sum_day();227 if(!is_date(yy,(Month)mm,dd))228 throw Invalid();229 }230 231 Date next_weekday(Date&d)232 {233 Date d1=d;234 if(d1.week()==sun||d1.week()==mon||d1.week()==tues||d1.week()==wed||d1.week()==thu)235 d1.add_day(1);236 else if(d1.week()==fri)237 d1.add_day(3);238 else239 d1.add_day(2);240 return d1;241 242 }243 244 int Date::day_sum()245 {246 int i=0;247 switch(m)248 {249 case Dec:250 i+=30;251 case Nov:252 i+=31;253 case Oct:254 i+=30;255 case Sep:256 i+=31;257 case Aug:258 i+=31;259 case Jul:260 i+=30;261 case Jun:262 i+=31;263 case May:264 i+=30;265 case Apr:266 i+=31;267 case Mar:268 i+=28;269 case Feb:270 i+=31;271 }272 if(leapyear(y)&&m>2)273 i++;274 i+=d;275 return i;276 }277 278 int week_of_year(Date&d)279 {280 int i=1;281 int j=0;282 Date d1(d.year(),1,1);283 if(d1.week()==sun)284 j+=7;285 else286 j+=7-d1.week();287 while(j<d.day_sum())288 {289 j+=7;290 i++;291 }292 return i;293 }294 295 int main()296 {297 Date d;298 d.get_date();299 cout<<d<<endl;300 keep_window_open();301 }
1 #include "../../st.h" 2 3 class Rational{ 4 public: 5 int re_fenzi()const{return fenzi;} 6 int re_fenmu()const{return fenmu;} 7 Rational():fenzi(0),fenmu(1){} 8 Rational(int i):fenzi(i),fenmu(1){} 9 Rational(int i1,int i2):fenzi(i1),fenmu(i2){} 10 void to_easy(); 11 void operator=(Rational&a); 12 friend istream& operator>>(istream&is,Rational&rt); 13 private: 14 int fenzi; 15 int fenmu; 16 }; 17 18 19 void Rational::to_easy() 20 { 21 int i=(fenzi>fenmu)?fenmu:fenzi; 22 while(i>0) 23 { 24 if(fenzi%i==0&&fenmu%i==0) 25 { 26 fenzi/=i; 27 fenmu/=i; 28 } 29 i--; 30 } 31 } 32 33 Rational operator+(const Rational&a,const Rational&b) 34 { 35 int i=a.re_fenzi()*b.re_fenmu()+a.re_fenmu()*b.re_fenzi(); 36 int j=a.re_fenmu()*b.re_fenmu(); 37 Rational rt(i,j); 38 rt.to_easy(); 39 return rt; 40 } 41 42 Rational operator-(const Rational&a,const Rational&b) 43 { 44 int i=a.re_fenzi()*b.re_fenmu()-a.re_fenmu()*b.re_fenzi(); 45 int j=a.re_fenmu()*b.re_fenmu(); 46 Rational rt(i,j); 47 rt.to_easy(); 48 return rt; 49 } 50 51 Rational operator*(const Rational&a,const Rational&b) 52 { 53 int i=a.re_fenzi()*b.re_fenzi(); 54 int j=a.re_fenmu()*b.re_fenmu(); 55 Rational rt(i,j); 56 rt.to_easy(); 57 return rt; 58 } 59 60 Rational operator/(const Rational&a,const Rational&b) 61 { 62 int i=a.re_fenzi()*b.re_fenmu(); 63 int j=a.re_fenmu()*b.re_fenzi(); 64 Rational rt(i,j); 65 rt.to_easy(); 66 return rt; 67 } 68 69 void Rational::operator=(Rational&b) 70 { 71 b.to_easy(); 72 fenzi=b.fenzi; 73 fenmu=b.fenmu; 74 } 75 76 bool operator==(Rational a,Rational b) 77 { 78 a.to_easy(); 79 b.to_easy(); 80 return a.re_fenmu()==b.re_fenmu()&&a.re_fenzi()==b.re_fenzi(); 81 } 82 83 bool operator!=(Rational a,Rational b) 84 { 85 return !(a==b); 86 } 87 88 ostream& operator<<(ostream&os,Rational&rt) 89 { 90 return os<<rt.re_fenzi()<<"/"<<rt.re_fenmu(); 91 } 92 93 istream& operator>>(istream&is,Rational&rt) 94 { 95 char ch; 96 is>>rt.fenzi>>ch>>rt.fenmu; 97 return is; 98 } 99 100 double rational_to_double(Rational&rt)101 {102 double d;103 if(rt.re_fenmu()!=0)104 d=(double)rt.re_fenzi()/(double)rt.re_fenmu();105 else106 error("分母为0");107 return d;108 }109 110 int main()111 try{112 Rational r1,r2;113 cin>>r1>>r2;114 cout<<r1<<endl<<r2<<endl;115 Rational r3=r1+r2;116 cout<<r3<<endl;117 cout<<r1-r2<<endl;118 cout<<r1*r2<<endl;119 cout<<r1/r2<<endl;120 r2=r1;121 cout<<r1<<endl<<r2<<endl;122 if(r1==r2)123 cout<<"yes"<<endl;124 if(r1!=r2)125 cout<<"no"<<endl;126 cout<<rational_to_double(r1)<<endl;127 keep_window_open();128 }129 catch(exception&e)130 {131 cerr<<e.what()<<endl;132 keep_window_open("~~");133 return 1;134 }
1 #include "../../st.h" 2 3 class Money{ 4 public: 5 Money():yuan(0),fen(0),s("USD"){sum=0;} 6 Money(int i):yuan(i),fen(0),s("USD"){sum=i*100;d=(double)sum/100;} 7 Money(int i1,int i2):yuan(i1),fen(i2),s("USD"){to_real();sum=yuan*100+fen;d=(double)sum/100;} 8 Money(string ss,double dd):s(ss),d(dd){yuan=int(d);fen=int(d*100)%100;} 9 int re_yuan(){return yuan;} 10 int re_fen(){return fen;} 11 double re_double(){return d;} 12 string re_str(){return s;} 13 void to_real(); 14 void to_double(); 15 friend istream& operator>>(istream& is,Money&m); 16 private: 17 int yuan; 18 int fen; 19 long sum; 20 double d; 21 string s; 22 23 }; 24 25 26 27 28 void Money::to_real() //假设输入美分都是三位数 29 { 30 int i; 31 32 if(fen/100!=0) 33 { 34 if((fen%10)>=5) 35 { 36 fen/=10; 37 fen++; 38 } 39 else 40 fen/=10; 41 } 42 sum=yuan*100+fen; 43 } 44 45 istream& operator>>(istream& is,Money&m) 46 { 47 //char c; 48 //is>>c>>m.yuan>>c>>m.fen; 49 is>>m.s>>m.d; 50 m.yuan=int(m.d); 51 m.fen=int(m.d*100)%100; 52 return is; 53 } 54 55 void Money::to_double() 56 { 57 d=double(sum)/100; 58 } 59 60 61 62 ostream& operator<<(ostream& os,Money&m) 63 { 64 m.to_real(); 65 m.to_double(); 66 return os<<m.re_str()<<" "<<m.re_double();; 67 } 68 69 vector<string>name; 70 vector<double>rate; 71 72 void fn() //录入各国家与美元的汇率 73 { 74 string s="USD"; 75 double r=1; 76 name.push_back(s); 77 rate.push_back(r); 78 cout<<"输入美国与其他国家的汇率:"<<endl<<"输入国家:"; 79 while(s!="over") 80 { 81 cin>>s; 82 name.push_back(s); 83 cout<<"输入汇率:"; 84 cin>>r; 85 rate.push_back(r); 86 cout<<"继续输入国家:"; 87 cin>>s; 88 } 89 } 90 91 Money operator+(Money&a,Money&b) 92 { 93 int i,j; 94 for(i=0;i<name.size();i++) 95 if(a.re_str()==name[i]) 96 break; 97 for(j=0;j<name.size();j++) 98 if(b.re_str()==name[j]) 99 break;100 if(i==name.size()||j==name.size())101 error("没有这个汇率登记记录");102 Money mm("USD",a.re_double()*rate[i]+b.re_double()*rate[j]);103 return mm;104 }105 106 int main()107 try{108 vector<string>name;109 vector<double>rate;110 Money m1,m2;111 cin>>m1>>m2;112 cout<<m1<<endl<<m2<<endl;113 fn();114 cout<<m1+m2<<endl;115 keep_window_open();116 }117 catch(exception&e)118 {119 cerr<<e.what()<<endl;120 keep_window_open("~~");121 return 1;122 }
C++程序设计原理与实践 第九章部分答案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。