• C++程序设计(第2版)课后习题答案第13章


    P317,13.3

    View Code
     1 #include <iostream>
     2 #include <stdlib.h>
     3 using namespace std;
     4 
     5 static char daytable[2][13]={
     6     {0,31,28,31,30,31,30,31,31,30,31,30,31},
     7     {0,31,29,31,30,31,30,31,31,30,31,30,31}
     8 };
     9 class Date
    10 {    
    11     //重载为友元
    12     //friend Date & operator+(Date &date,int d);
    13     friend ostream & operator<<(ostream &output,Date &d);
    14 public:
    15     Date();
    16     Date(int y,int m,int d);
    17     ~Date();
    18     //重载为成员函数
    19     Date & operator+(int d);
    20     static int isLeap(int y);
    21     
    22 protected:
    23     int year,month,day;
    24 };
    25 
    26 Date::Date()
    27 {
    28     year=2013;
    29     month=3;
    30     day=25;
    31 }
    32 
    33 Date::Date(int y,int m,int d)
    34 {
    35     year=y;
    36     month=m;
    37     day=d;
    38 }
    39 
    40 int Date::isLeap(int y)
    41 {
    42     return (y%4==0 && y%100 || y%400==0);
    43 }
    44 
    45 Date::~Date()
    46 {}
    47 
    48 Date & Date::operator+(int d)
    49 {
    50     while(day+d>daytable[isLeap(year)][month])
    51     {
    52         d-=daytable[isLeap(year)][month]-day;
    53         if(month==12)
    54         {
    55             year++;
    56             month=1;
    57         }
    58         else
    59             month++;
    60         day=0;
    61     }
    62     day+=d;
    63     
    64     return *this;
    65 }
    66 
    67 
    68 ostream & operator<<(ostream &output,Date &d)
    69 {
    70     output<<d.year<<"-"<<d.month<<"-"<<d.day<<endl;
    71     return output;
    72 }
    73 
    74 /*
    75 Date & operator+(Date &date,int d)
    76 {
    77 while(date.day+d>daytable[Date::isLeap(date.year)][date.month])
    78 {
    79 d-=daytable[Date::isLeap(date.year)][date.month]-date.day;
    80 if(date.month==12)
    81 {
    82 date.year++;
    83 date.month=1;
    84 }
    85 else
    86 date.month++;
    87 date.day=0;
    88 }
    89 date.day+=d;
    90 return date;
    91 }*/
    92 
    93 void main()
    94 {
    95     Date d(2013,3,25);
    96     d=d+365;
    97     cout<<d;
    98     //system("pause");
    99 }

    P317,13.7

    View Code
     1 #include <iostream>
     2 #include <iomanip>
     3 //#include <stdlib.h>
     4 using namespace std;
     5 
     6 class Time
     7 {
     8     friend ostream & operator<<(ostream &output,Time &time);
     9     friend istream & operator>>(istream &input,Time &time);
    10 public:
    11     Time(int h=0,int m=0,int s=0);
    12 private:
    13     int hour,minute,second;
    14 };
    15 
    16 Time::Time(int h,int m,int s)
    17 {
    18     hour=(h>=0 && h<=23)?h:0;
    19     minute=(m>=0 && m<=59)?m:0;
    20     second=(s>=0 && s<=59)?s:0;
    21 }
    22 
    23 ostream & operator<<(ostream &output,Time &time)
    24 {
    25     output<<setw(2)<<setfill('0')<<time.hour<<":"
    26         <<setw(2)<<setfill('0')<<time.minute<<":"
    27         <<setw(2)<<setfill('0')<<time.second<<endl;
    28     //output<<time.hour<<":"<<time.minute<<":"<<time.second;
    29     return output;
    30 }
    31 
    32 istream & operator>>(istream &input,Time &time)
    33 {
    34     char buffer[81];
    35     input.getline(buffer,3,'-');
    36     time.hour=atoi(buffer);
    37     input.getline(buffer,3,'-');
    38     time.minute=atoi(buffer);
    39     input.getline(buffer,3,'\n');
    40     time.second=atoi(buffer);
    41     return input;
    42 }
    43 
    44 void main(void)
    45 {
    46     Time time(12,36,25);
    47     cout<<"hh-mm-ss:"<<endl;
    48     cin>>time;
    49     cout<<time;
    50     //system("pause");
    51 }
  • 相关阅读:
    redis 实例
    redis 常用命令
    redis sets类型及操作
    简单说说PHP优化那些事
    C# IEnumerable与IQueryable ,IEnumerable与IList ,LINQ理解Var和IEnumerable
    全文搜索引擎 elasticsearch.net
    .net 异步
    并行开发 8.用VS性能向导解剖你的程序
    并行开发 7.简要分析任务与线程池
    并行开发 6.异步编程模型
  • 原文地址:https://www.cnblogs.com/shajianheng/p/2980874.html
Copyright © 2020-2023  润新知