首页 > 代码库 > UT源码 125

UT源码 125

NextDate函数问题 

NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性

       NextDate函数包含三个变量month、day和year,函数的输出为输入日期后一天的日期。 要求输入变量month、day和year均为整数值,并且满足下列条件:

   条件1  1≤ month ≤12  否则输出,月份超出范围

   条件2  1≤ day ≤31 否则输出,日期超出范围

   条件3  1912≤ year ≤2050  否则输出:年份超出范围

     String  nextdate(int m,int d,int y)

   注意返回值是字符串。

程序要求:

1)先显示“请输入日期”

2)不满足条件1,返回:“月份超出范围”;不满足条件2,返回:“日期超出范围”;不满足条件3,返回:“年份超出范围”;如果出现多个不满足,以最先出现不满足的错误返回信息。

3)条件均满足,则输出第二天的日期:格式“****年**月**日”(如果输入2050年12月31日,则正常显示2051年1月1日)

#include<iostream>
using namespace std;

string NextDate(int year, int month, int day)
{
    cout << "请输入日期:" << endl;
    cout << "year:";
    cin >> year;
    cout << "month:";
    cin >> month;
    cout << "day:";
    cin >> day;

    if (!(year >= 1912 && year <= 2050))//判断年份
    {
        cout << "年份超出范围!" << endl;
        return "";
    }
    if (month > 12 || month < 1)//判断月份
    {
        cout << "月份超出范围!" << endl;
        return "";
    }
    if (day > 31 || day < 1)//判断日期
    {
        cout << "日期超出范围!" << endl;
        return "";
    }
    if (month == 4 && day == 31 || month == 6 && day == 31 || month == 9 && day == 31 || month == 11 && day == 31)
    {
        cout << "日期超出范围!" << endl;
        return "";
    }

    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))//计算闰年的日期
    {
        int x = 0;
        x = day - 29;
        if (month == 2 && x > 0)
        {
            cout << "日期超出范围!" << endl;
            return "";
        }
        if (month == 2 && day == 29)
        {
            month = 3;
            day = 1;
        }
        else day++;
    }
    else day++;

    switch (month)//计算第二天日期
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
        if (day == 32)
        {
            month++;
            day = 1;
            cout << year << "" << month << "" << day << "" << endl;
        }
        break;
    case 2:
        if (day == 29)
        {
            month = 3;
            day = 1;
            cout << year << "" << month << "" << day << "" << endl;
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if (day == 31)
        {
            month++;
            day = 1;
            cout << year << "" << month << "" << day << "" << endl;
        }
        break;
    case 12:
        if (day == 32)
        {
            year++;
            month = 1;
            day = 1;
            cout << year << "" << month << "" << day << "" << endl;
        }
        break;
    }
    cout << year << "" << month << "" << day << "" << endl;
}

void main()
{
    int year = 0, month = 0, day = 0;
    NextDate(year, month, day);
}

 

UT源码 125