永发信息网

用C++输出两个指定时间段内所有的日期,并保存为txt

答案:5  悬赏:0  手机版
解决时间 2021-02-05 02:16
例如
输入
19600101 19700101
然后枚举这两个时间的所有日期,输出txt文档
用C++怎么写?
最佳答案

#include 
#include 

struct date
{
    int year; //年; 
    int month;  //月; 
    int day;   //日; 
};

void ymd(date &x, const unsigned long long &begin);
bool judge(date &x);
void nextday(date &x);

int main()
{
    unsigned long long begin{};
    unsigned long long end{};    
    std::cout << "请输入开始日期和结束日期(空格隔开):"; 

    while(std::cin >> begin >> end) //检查; 
    {
        if(begin > end)
            std::cout << "开始日期大于结束日期,请重新输入:";
        else
            break; 
    }

    date start{};        
    date over{}; 
    ymd(start, begin);
    ymd(over, end);

    if(!judge(start) || !judge(over))  //判断日期正确性; 
    {
        std::cout << "不存在该日期,程序结束!\n";
        exit(1); 
    }

    std::ofstream file("C:\\Users\\Administrator\\Desktop\\测试.txt"); //放到了桌面上,程序正常结束后,请打开检查; 

    if(!file.is_open())  //检查文件是否打开; 
    {
        std::cout << "文件打开失败,程序结束!\n";
        exit(1); 
    }

    file << begin << '\n';

    while(!(start.year == over.year && start.month == over.month && start.day == over.day)) //循环到结束日期; 
    {
        nextday(start);
        file << start.year;

        if(start.month < 10)
            file << '0' << start.month;
        else
            file << start.month;

        if(start.day < 10)
            file << '0' << start.day << '\n';
        else
            file << start.day << '\n';
    }

    file << end << '\n';
    file.close();
    return 0;
}

void ymd(date &x, const unsigned long long &begin)  //将输入日期对应赋值到结构成员; 
{
    x.year = begin / 10000;
    x.month = (begin - x.year * 10000) / 100;
    x.day = (begin - x.year * 10000 - x.month * 100);
}

bool judge(date &x) //判断日期是否存在; 
{
    bool temp{true};

    if(x.month > 12)
        temp = false;

    else if(x.month == 1 || x.month == 3 || x.month == 5 || x.month == 7 || x.month == 8 || x.month == 10 || x.month == 12)
    {
        if(x.day > 31)
            temp = false;         
    }

    else if(x.month == 2)
    {
        if(((x.year % 4 == 0 && x.year % 100 != 0)||(x.year % 400 == 0)) && x.day > 29)
            temp = false;

        else if(x.day > 28)
            temp = false;        
    }

    else
    {
        if(x.day > 30)
            temp = false;
    }

    return temp;
}

void nextday(date &x)  //计算下一天的日期; 
{
    if(x.month == 1 || x.month == 3 || x.month == 5 || x.month == 7 || x.month == 8 || x.month == 10 || x.month == 12)
    {
        if(x.day == 31)
        {
            x.day = 1;
            ++x.month;
        }

        else
            ++x.day;
    }

    else if(x.month == 2)
    {
        if(x.day == 28)
        {
            if((x.year % 4 == 0 && x.year % 100 != 0)||(x.year % 400 == 0))
                ++x.day;

            else
            {
                x.day = 1;
                x.month = 3;
            }
        }

        else if(x.day == 29)
        {
            x.day = 1;
            x.month = 3;
        }

        else
            ++x.day;
    }

    else
    {
        if(x.day == 30)
        {
            x.day = 1;
            ++x.month;
        }

        else
            ++x.day;
    }

    if(x.month > 12)
    {
        x.month = 1;
        ++x.year;
    }
}
全部回答
ctrl+c复制ctrl+v粘贴
就是日期加1天, 注意判断大月、小月、闰年
就是日期加1天, 注意判断大月、小月、闰年 再看看别人怎么说的。
#include  #include  #include  #include  #include  using namespace std; int is_leapyear(int year)   //如果润年,返回1,否则返回0  { if((year&4==0 && year%100!=0) || year%400==0) { return 1; } else { return 0; } } int days_one_month(int year, int month)   //返回一个月的天数  { int leap,re=0; leap=is_leapyear(year);    //是否是润年 switch(month) { case 1:     //1,3,5,7,8,10,12月每月都是31天 case 3: case 5: case 7: case 8: case 10: case 12: re=31; break; case 4:      //4,6,9,11月每月都是30天 case 6: case 9: case 11: re=30; break; case 2: if(leap==1)  //2月判断是否是润年 { re=29;  //是润年,则是29天 } else { re=28;  //不是润年,则是28天 } break; default: printf("\nmonth is error!"); break; } return re; } int main(int argc, char** argv)  { char date1[10]={"\0"}; char date2[10]={"\0"}; int y1,y2,m1,m2,d1,d2; cout<<"input first date(ex: 19601001): "<>date1; cout<<"input second date(ex: 19801001): "<>date2; char s[10]={"\0"};  //以下分别把输入的数据转换为数值的 memset(s,'\0',10);  //年,月,日,分别存储于y1,m1,d1中 strncpy(s,date1,4); y1=atoi(s); memset(s,'\0',10); s[0]=date1[4]; s[1]=date1[5]; m1=atoi(s); memset(s,'\0',10); s[0]=date1[6]; s[1]=date1[7]; d1=atoi(s); memset(s,'\0',10); strncpy(s,date2,4); y2=atoi(s); memset(s,'\0',10); s[0]=date2[4]; s[1]=date2[5]; m2=atoi(s); memset(s,'\0',10); s[0]=date2[6]; s[1]=date2[7]; d2=atoi(s); ofstream ofile; ofile.open("d:\\www.txt);        //打开文件以便写入 while(1) { int n=0; n=days_one_month(y1,m1); //n为该月有多少天 if(d1>n)                 //d1如果大于n天 {                        //则月份加1 m1++; d1=1; } if(m1>12)                 //月份大于12,则年份加1 { y1++; m1=1; } memset(s,'\0',10); sprintf(s,"%d%02d%02d",y1,m1,d1); ofile<=y2 && m1>=m2 && d1>=d2) { break; } } ofile.close(); return 0; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
小马虎做减法,错吧减数45写成54,结果得19,哪
秉贻农家地址在哪,我要去那里办事
within easy reach 这个怎么解释
有长春的网约车司机吗?谁知道新政后卡罗拉双
板材隔墙与骨架隔墙每个检验批应至少抽查(),
抓狂是什么意思啊?
正装女裙看起来有光泽,摸起来光滑,是什么布
上海奥特鞋业我想知道这个在什么地方
【深远的意思】深远历史意义是什么意思
关于财务人员年终总结
马身上瘦肉多还是肥肉多?
send(sock,hello,sizeof(hello)/sizeof(T
中国移动通信手机大卖场地址有知道的么?有点
关于KT板做展板以及其排版安排
翟家湾梁在哪里啊,我有事要去这个地方
推荐资讯
520分能考黑龙江什么大学
西宁市家里安装哪种净水器合适?
桐城小关小学地址有知道的么?有点事想过去
驾驶证分为什么只反了4分啊
新颜值美容地址在什么地方,我要处理点事
阎谢村地址在什么地方,想过去办事
九江琦峰专业(体育舞蹈)拉丁舞蹈学校在什么地
为什么草原上蒙古包的底面是圆的
请问六安市有别名吗?谁能告诉我?
王军无意中知道了好朋友吴海的QQ号和密码,于
東田造型(望京店)怎么去啊,我要去那办事
怎么打出响指
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?