关于时间计算器的一个编程问题

   有一天小明发现了time()函数,time()函数返回一个整数,表示从197011000秒到运行这一程序经过的秒数,当然关于求当前时刻还有很多相关的函数等待你去发现。现在m队长想要创建自己的mfc_time函数,mfc_time()表示自某个时刻开始经过t秒之后的时刻,例如200011000秒经过3600秒之后的时刻为200011100秒,我们以标准日期的形式进行输出,月,日,时间,年份。详细输出规则见输入输出样例。关于日期的写法:1号,2号,3号,21号,22号,23号,31号的缩写均为“日期数+stndrd(firstsecondthird的后两个字母) ”,其余的日子缩写均为“数字+th”。错误样例已经进行了修正,同学们可以重新进行提交。
   各个月份缩写:一月Jan,二月Feb,三月Mar,四月Apr,五月May,六月Jun,七月Jul,八月Aug,九月Sept,十月Oct,十一月Nov,十二月Dec

输入
第一行输出一个数字n,表示有n组测试数据。接下来n行,每行7个整数,前六个整数表示一个时刻(年月日时分秒),最后一个整数t,表示从这个时刻开始经过了t秒。
输出
输出n行,每行输出一个时刻(年月日时分秒),标准日期格式,详细格式参考输入输出样例。

样例输入
3
2000 1 1 0 0 0 3600
2000 2 28 23 59 59 1
2000 1 1 0 0 0 123456789
样例输出
Jan 1st 01:00:00 2000
Feb 29th 00:00:00 2000
Nov 29th 21:33:09 2003

提示
数据1:2000年1月1日0时0分0秒经过3600秒以后是2000年1月1日1时0分0秒
数据2:2000年2月28日23时59分59秒经过1秒以后是2000年2月29日0时0分0秒,因为2000年是闰年。
我们保证读入的初始时间是合法的,年份在公元1000年到公元2022年之间。经过秒数t<=1010。

运行结果:

img

代码:

#include <iostream>
using namespace std;
int isleapyear(int y)
{
    if(y%4==0 && y%100!=0 || y%400==0)
        return 1;
    else
        return 0;
}
int main()
{
    int i,n;
    int y,m,d,h,mi,s;
    int t;
    char mon[13][10] ={"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    int dd[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    char days[32][10]={"","1st","2nd","3rd","4th","5th","6th",
        "7th","8th","9th","10th","11th","12th","13th",
        "14th","15th","16th","17th","18th","19th",
        "20th","21st","22nd","23rd","24th","25th",
        "26th","27th","28th","29th","30th","31st"};
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> y >> m>>d >>h>>mi>>s >> t;
        s+=t;
        mi += s/60;
        s = s%60;
        h += mi/60;
        mi = mi%60;
        d += h/24;
        h = h%24;
        if(isleapyear(y))
            dd[2] = 29;
        else
            dd[2] = 28;
        while(d > dd[m])
        {
            if(isleapyear(y))
                dd[2] = 29;
            else
                dd[2] = 28;
            d -= dd[m];
            m++;
            if(m>12)
            {
                y +=1;
                m = 1;
            }
        }
        printf("%s %s %02d:%02d:%02d %d\n",mon[m],days[d],h,mi,s,y);
    }
    return 0;
}
 

c++代码如下:

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

int main()
{
    int n;
    cin >> n; // 输入测试数据组数

    for (int i = 0; i < n; i++)
    {
        // 输入初始时间和增加的秒数
        int year, month, day, hour, minute, second, secondsToAdd;
        cin >> year >> month >> day >> hour >> minute >> second >> secondsToAdd;

        // 将输入的时间转换为 time_t 类型
        tm time;
        time.tm_year = year - 1900;
        time.tm_mon = month - 1;
        time.tm_mday = day;
        time.tm_hour = hour;
        time.tm_min = minute;
        time.tm_sec = second;
        time_t initialTime = mktime(&time);

        // 增加秒数
        initialTime += secondsToAdd;

        // 将增加秒数后的时间转换回 tm 类型
        tm *finalTime = localtime(&initialTime);

        // 输出格式化后的日期
        cout << finalTime->tm_year + 1900 << "-";
        cout << setw(2) << setfill('0') << finalTime->tm_mon + 1 << "-";
        cout << setw(2) << setfill('0') << finalTime->tm_mday << " ";
        cout << setw(2) << setfill('0') << finalTime->tm_hour << ":";
        cout << setw(2) << setfill('0') << finalTime->tm_min << ":";
        cout << setw(2) << setfill('0') << finalTime->tm_sec << endl;
    }

    return 0;
}


执行效果:

img

C++时间计算器
https://blog.csdn.net/qq_35960743/article/details/127817457

已完成代码、详细注释、测试用例结果截图如下,有任何问题可以再沟通帮忙处理解决,望采纳

#include <iostream>
#include <cstdio>
#include <ctime>

using namespace std;

// 定义常量
const int SECONDS_PER_MINUTE = 60;
const int MINUTES_PER_HOUR = 60;
const int HOURS_PER_DAY = 24;
const int DAYS_PER_WEEK = 7;
const int DAYS_PER_NORMAL_YEAR = 365;
const int DAYS_PER_LEAP_YEAR = 366;
const int MONTHS_PER_YEAR = 12;

// 定义每月的天数
const int DAYS_PER_MONTH[MONTHS_PER_YEAR+1] = {
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

// 定义每月的缩写
const string MONTH_NAMES[MONTHS_PER_YEAR+1] = {
    "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"
};

// 定义一个结构体来表示时刻
struct Time {
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
};

// 判断给定的年份是否为闰年
bool isLeapYear(int year) {
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}

// 计算给定时刻所对应的秒数
int toSeconds(Time t) {
    int seconds = t.second;
    seconds += MINUTES_PER_HOUR * t.minute;
    seconds += HOURS_PER_DAY * t.hour;

    // 累加年份中的秒数
    for (int y = 1970; y < t.year; y++) {
        seconds += isLeapYear(y) ? DAYS_PER_LEAP_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE :
                                   DAYS_PER_NORMAL_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
    }

// 累加月份中的秒数
for (int m = 1; m < t.month; m++) {
seconds += DAYS_PER_MONTH[m] * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
}

// 累加天数中的秒数
for (int d = 1; d < t.day; d++) {
    seconds += HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
}

return seconds;
}

// 根据给定的秒数,求出时刻
Time fromSeconds(int seconds) {
Time t;

// 从1970年开始算
t.year = 1970;

// 计算年份
while (true) {
    int daysInYear = isLeapYear(t.year) ? DAYS_PER_LEAP_YEAR : DAYS_PER_NORMAL_YEAR;
    if (seconds >= daysInYear * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE) {
        seconds -= daysInYear * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
        t.year++;
    } else {
        break;
    }
}

// 计算月份
t.month = 1;
while (true) {
    int daysInMonth = DAYS_PER_MONTH[t.month];
    if (isLeapYear(t.year) && t.month == 2) {
        daysInMonth++; // 闰年2月有29天
    }

    if (seconds >= daysInMonth * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE) {
        seconds -= daysInMonth * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
        t.month++;
    } else {
        break;
    }
}

// 计算天数
t.day = 1;
while (true) {
    if (seconds >= HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE) {
        seconds -= HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
        t.day++;
    } else {
        break;
    }
}

// 计算小时数
t.hour = 0;
while (true) {
if (seconds >= MINUTES_PER_HOUR * SECONDS_PER_MINUTE) {
seconds -= MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
t.hour++;
} else {
break;
}
}

// 计算分钟数
t.minute = 0;
while (true) {
    if (seconds >= SECONDS_PER_MINUTE) {
        seconds -= SECONDS_PER_MINUTE;
        t.minute++;
    } else {
        break;
    }
}

// 剩余的就是秒数
t.second = seconds;

return t;
}

// 格式化时刻
string format(Time t) {
string daySuffix = "th";
if (t.day == 1 || t.day == 21 || t.day == 31) {
daySuffix = "st";
} else if (t.day == 2 || t.day == 22) {
daySuffix = "nd";
} else if (t.day == 3 || t.day == 23) {
daySuffix = "rd";
}

char buf[1024];
sprintf(buf, "%s %d%s %04d %02d:%02d:%02d", MONTH_NAMES[t.month].c_str(), t.day, daySuffix.c_str(), t.year, t.hour, t.minute, t.second);
return buf;
}

int main() {
int n;
cin >> n;

while (n--) {
    Time t;
    cin >> t.year >> t.month >> t.day >> t.hour >> t.minute >> t.second;

    int seconds;
    cin >> seconds;

    int totalSeconds = toSeconds(t) + seconds;
    Time result = fromSeconds(totalSeconds);

    cout << format(result) << endl;
}

return 0;
}

img