zoukankan      html  css  js  c++  java
  • Date

    准备用C++重写上次的QA的大作业日历。

    最近在看C++看见不少Date相关的题目和内容,记在这里。

    1.C++程序设计语言 5.9.13 定义一个Date以保存日期的轨迹。提供一些函数,从输入读Date,向输出写Date,以及用一个日期去初始化Date.

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 struct Date
     6 {
     7     int  day_:5; //位域仅用5个位 代表这个整数成员
     8     int month_:4;
     9     int year_:15;
    10 };
    11 
    12 
    13 
    14 std::istream& operator>>(std::istream &input,Date &d)
    15 {
    16     int const bufsize=6;
    17     char buffer[bufsize];
    18     input.getline(buffer,bufsize,'/');
    19     d.month_=atoi(buffer);
    20     input.getline(buffer,bufsize,'/');
    21     d.day_=atoi(buffer);
    22     input>>d.year_;
    23     return input;
    24 }
    25 
    26 
    27 std::ostream& operator<<(std::ostream &output,Date &d)
    28 {
    29     output<<d.month_<<"/"<<d.day_<<"/"<<d.year_;
    30     return output;
    31 }
    32 
    33 Date& init(Date& d,unsigned day,unsigned month,int year)
    34 {
    35     d.day_=day;
    36     d.month_=month;
    37     d.year_=year;
    38     return d;
    39 }

    遇到的问题:1> getline (来自百度百科)

    istream& getline ( istream &is , string &str , char delim );
    istream& getline ( istream& , string& );
    is 进行读入操作的输入流
    str 存储读入的内容
    delim 终结符
    返回值:与参数is是一样的
    将输入流is中读到的字符存入str中,直到遇到终结符delim才结束。对于第一个函数delim是可以由用户自己定义的终结符;对于第二个函数delim默认为 ' '(换行符)。
    函数在输入流is中遇到文件结束符(EOF)或者在读入字符的过程中遇到错误都会结束。
    在遇到终结符delim后,delim会被丢弃,不存入str中。在下次读入操作时,将在delim的下个字符开始读入。
    这个getline是个全局函数,而不是iostream的成员函数getline。
    getline操作返回的是is(输入流),在使用while(getline(cin,line))的时候若想退出循环应使用EOF或ctrl+Z。
    etline也可以作为成员函数使用
    cin.getline(char* cha,int num,char f);
    向cha中输入num个字符,输入过程中达到num个数或者提前遇到f字符,输入结束。

    2>atoi

    int atoi (const char * str);
    Convert string to integer

    3>struct 4>

    1 struct Date
    2  {
    3      int  day_:5; //位域仅用5个位 代表这个整数成员
    4      int month_:4;
    5      int year_:15;
    6  };
    1 input>>d.year_; 
     通不过 
    5>关于流


    2> 7.10.19 写一个函数,实现为5.9[13]定义的Date加上一天,一个月,一年的功能,写一个函数对于所给的Date给出对应的星期几。写一个函数给出参数Date之后第一个星期一对应的Date。

      1 struct Date{
      2     unsigned day_:5;
      3     unsigned month_:4;
      4     int year_:15;
      5 };
      6 
      7 
      8 bool is_leap_year(int y)
      9 {
     10     return !(y%4)&&((y%100)||!(y%400)); //四年一润,百年不闰,四百年再闰  ((y%4==0)&&(y%100!=0))||(y%400==0)
     11 }
     12 
     13 struct Month{
     14     enum{Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
     15     static int n_days(int m,int y)    //n_days 显示每年每月最多多少天?
     16     {
     17         return m!=Feb?n_days_[m-1]:(is_leap_year(y)?29:28); 
     18     }
     19 private:
     20     static int const n_days_[12];
     21 
     22 };
     23 
     24 int const  Month::n_days_[12]={31,28,31,30,31,30,31,31,30,31,30,31};  
     25 
     26 
     27 Date next_year(Date const &d)
     28 {
     29     Date n;
     30     n.day_=d.day_;
     31     n.month_=d.month_;
     32     n.year_=d.year_+1;
     33     //2月29日,但不是闰年!  这个月是29天!
     34     if (n.day_>Month::n_days(n.month_,n.year_))  
     35     {
     36         n.month_=3;
     37         n.day_=1;
     38     }
     39     return n;
     40 }
     41 /*
     42 Date next_month(Date const &d)
     43 {
     44    Date n;
     45    n.day_=d.day_;
     46    n.month_=d.month_+1;
     47    n.year_=d.year_;
     48    if(n.month_>12)  //12月
     49    {
     50        n.month_=1;
     51        n.year_+=1;
     52    }
     53    return n;
     54 }
     55 */
     56 Date next_month(Date const &d)
     57 {
     58     Date n;
     59     n.day_=d.day_;
     60     n.month_=d.month_%12+1;
     61     n.year_=d.year_;
     62     if(n.month_==1)   //如果是1月 说明年数+1,年数加1 next——year(n);
     63         n=next_year(n);
     64     if(n.day_>Month::n_days(n.month_,n.year_))// 这个月的天数超过下一个月的天数!将使用下一个月的天数!!!
     65         n.day_=Month::n_days(n.month_,n.year_);
     66     return n;
     67 }
     68 
     69 
     70 /*
     71 Date next_day(Date const &d)
     72 {
     73     Date n;
     74     n.day_=d.day_+1;
     75     n.month_=d.month_;
     76     n.year_=n.year_;
     77     if(n.day_>Month::n_days(n.month_,n.year_))  //超过每月最多天数
     78     {
     79         if(n.month_==12)
     80         {
     81             n.year_+=1;
     82             n.month_=1;
     83             n.day_=1;
     84         }
     85         else
     86         {
     87           n.month_+=1;
     88           n.day_=1;
     89               }
     90 }
     91 */
     92 
     93 Date next_day(Date const &d)
     94 {
     95     Date n;
     96     n.day_=(d.day_)%Month::n_days(d.month_,d.year_)+1;
     97     n.month_=d.month_;
     98     n.year_=d.year_;
     99     if(n.day_==1)
    100         return next_month(n);
    101     else return n ;
    102 }
    103 //每一年的一月一日的星期几都向前移一天,而闰年要移两天。如果处于非闰年Y中,必须推移[Y-1]+((Y-1)/4-(Y-1)/100+(Y-1)/400)]%7个星期中的天数。处闰年的3月或者再后面的月需要再推一天。在非闰年与该年的1月1日比月M的第一天需要推移MS【M-1】个星期的天数MS【】
    104 enum DayOfWeek{Sunday=0,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};//1年1月1日从星期一开始。
    105 int MS[]={0,3,3,6,1,4,5,2,5,0,3,5};
    106 DayOfWeek day_of_week(Date const &dt)
    107 {
    108     int const y=dt.year_,m=dt.month_,d=dt.day_;
    109     int const j1=(1+y-1+(y-1)/4-(y-1)/100+(y-1)/400)%7;
    110     if(m<=2||is_leap_year(y))
    111         return DayOfWeek((j1+MS[m-1]+d-1)%7);
    112     else
    113         return DayOfWeek((j1+MS[m-2]+d)%7);
    114 }
    115 
    116 Date next_monday(Date const &d)
    117 {
    118     DayOfWeek dday=day_of_week(d);
    119     int const n_days=(dday==Sunday)?1:7-dday;
    120     Date Monday(d);
    121     for (int k=0;k!=n_days;++k)
    122     {
    123         Monday=next_day(Monday);
    124     }
    125     return Monday;
    126 
    127 }

     暂时不清楚的问题:函数如果调入调出 输入输出的问题还是不很清楚!
  • 相关阅读:
    国产html编辑器
    豆瓣90mhz电台还行
    购物搜索
    linode vps 优化集锦
    这个系统,我看行,
    代理网站
    合理设置域名TTL值给网站加速
    dns切换到dnspod
    elixir usage
    JSSH介绍
  • 原文地址:https://www.cnblogs.com/huhaibo/p/3378139.html
Copyright © 2011-2022 走看看