C++ implements problems such as string segmentation, string conversion to integer type, day of the year, day of the week for a certain date, etc.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
int IsLeapyear(int year)//判断闰年
{
    if((year%4==0&&year%100!=0)||year%400==0)//百度一下你就知道为何这样判断闰年
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int DaysofMonth(int year,int month)//返回这个月有几天
{
    if(month>0&&month<=7)//1-7月
    {
        if(month%2==1)
        {
            return 31;//1,3,5,7月都是31天
        }
        else
        {
            if(month==2)//2月有点特殊
                return 28+IsLeapyear(year);
            else return 30;//4,6月30天
        }
    }
    else if(month>7&&month<=12)//8-12月
    {
        if(month%2==0)
            return 31;//8,10,12月31天
        else return 30;//9,11月30天
    }
    else//不合法
    {
        return 0;
    }

}
int DayInYear(int year,int month,int day)//返回这一年的第几天
{
    int days=day;
    month--;
    while(month)
    {
        days+=DaysofMonth(year,month);
        month--;
    }
    return days;
}
int IFDate(int year,int month,int day){//判断是否是合法日期
    if(year<0||month<1||month>12||day>31||day<1){
        return 0;
    }else{
        if(day>DaysofMonth(year,month)){
            return 0;
        }
    }
    return 1;
}
int WeekDay(int year,int month,int day){//以今天:2020/10/27,星期二为基准
    int days,before=1;
    //days为距今的天数,今天之前before=-1;今天及之后before=1
    if(year<2020){//2020年以前
        before=-1;
        days=365-DayInYear(year,month,day)+IsLeapyear(year);
        for(int i=year+1;i<2020;i++){
            if(IsLeapyear(i)){
                days+=366;
            }else{
                days+=365;
            }
        }
        days+=301;//DayInYear(2020,10,27)=301
    }else if(year==2020){
        if(month<10){
            before=-1;
            days=DaysofMonth(year,month)-day;
            for(int i=month+1;i<10;i++){
                days+=DaysofMonth(year,i);
            }
            days+=27;
        }else if(month==10){
            if(day>=27){
                days=day-27;
            }
            else {
                before=-1;
                days=27-day;
            }
        }else{
            days=DayInYear(year,month,day)-301;
        }
    }else{//2020年以后
        days=65;//366-301=65
        for(int i=2021;i<year;i++){
            if(IsLeapyear(i)){
                days+=366;
            }else{
                day+=365;
            }
        }
        days+=DayInYear(year,month,day);
    }
    days*=before;
    int weekday=days%7+2;
    //直觉告诉我这么做,至于为什么我不会解释,规律好像就是这样
    if(weekday<=0){//小于0,加7
        weekday+=7;
    }
    if(weekday>7){//大于7,减去7
        weekday-=7;
    }
    return weekday;
}

void spiliteBy(string s,char by,string* sub){
    //字符串分割,分割符by,字串为sub
    int n=0;
    int last=0;
    int len=s.length();
    for(int i=0;i<len;i++){
        if(s[i]==by){
            sub[n]=s.substr(last,i-last);
            n++;
            last=i+1;
        }
    }
    sub[n]=s.substr(last,len-last);
}
int StringToInt(string s){//字符串转为整形,字符串必须全为整数型字符0-9
    int len=s.length();
    int a=0;
    int num=0;
    for(int i=0;i<len;i++){
        a=s[i]-'0';
        num*=10;
        num+=a;
    }
    return num;
}
int main()
{
    int testNum=1;
    while(testNum--)
    {
        int year,month,day;
        string s;
        cout<<"请输入日期,格式:2001/01/01"<<endl;
        cin>>s;
        string date[3];//默认分割为三个子串
        spiliteBy(s,'/',date);//分割
        year=StringToInt(date[0]);
        month=StringToInt(date[1]);
        day=StringToInt(date[2]);
        //cin>>year>>month>>day;
        cout<<year<<month<<day<<endl;
        if(IFDate(year,month,day))
        {
            cout<<year<<"年的第"<<DayInYear(year,month,day)<<"天,星期"<<WeekDay(year,month,day)<<endl;
        }
        else
        {
            cout<<"请输入合法日期"<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/lzp1467188465/article/details/109323572