首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关于日期的C++不完整代码,更好的解决方案是什么?

关于日期的C++不完整代码,更好的解决方案是什么?
EN

Stack Overflow用户
提问于 2013-05-16 13:39:20
回答 1查看 264关注 0票数 0

我正在写一段代码,如果你输入你的生日日期和其他日期,它会返回你活着的年数、月数和日数。

包括(闰年)双六分年。

对象2:对于无效日期,输出必须是“数据无效日期”(葡萄牙文中的无效日期)。

投入/产出:

日期格式采用巴西标准,格式为日/月/年。

第一个输入是您将要测试的输入数。

投入1: 29/02/2000

投入2: 01/03/2001

产出:1 0 1

投入1: 29/02/2000

投入2: 28/02/2001

产出:1 0 0

投入1: 29/12/2012

投入2: 13/01/2013

产出:0 0 15

投入1: 27/05/2012

投入2: 27/05/2013

产出:1 0 0

投入1: 01/2012

投入2: 05/01/2013

产出:1 0 4

投入1: 13/05/1966

投入2: 05/02/2015

产出: 48 8 23

投入1: 29/02/2003

投入2: 2012年5月4日

输出:数据失效

投入1: 14/13/1995

投入2: 1996年8月7日

输出:数据失效

“守则”:

代码语言:javascript
运行
复制
#include <iostream>
#include <cstdio>

using namespace std;

int verificar(int ano)
{
if (((ano % 4 == 0) && (ano % 100 != 0)) || (ano % 400 == 0))

    return 1;

else
    return 0;
}
int checkdia(int dia, int mes, int ano){    

if (dia>0)

    if (((mes==1)||(mes==3)||(mes==5)||(mes==7)||(mes==8)||(mes==10)||(mes==12)) && (dia<=31))
            return 1;

    else{

        if (((mes==4)||(mes==6)||(mes==9)||(mes==11)) && (dia<=30))

            return 1;

        else{

            if ((mes==2) && (dia<=28))

                return 1;

            else{

                if ((((verificar(ano))==true)&&(dia<=29))&&(mes==2))

                    return 1;

                else

                    return 0;
            }
        }
    }
else
return 0;
}

int checkmes(int mes)
{
if ((mes>0) && (mes<=12))
    return 1;
else
    return 0;
}

int checkano(int ano)
{
if ((ano>0) && (ano<11000))
    return 1;
else
    return 0;
}

int main(){

int numerodetestes, mes1, mes2, dia1, dia2, ano1, ano2, teste11, teste12, teste13, teste21, teste22, teste23;

cin>>numerodetestes;

for(int c=0;c<=numerodetestes;c++){

    scanf("%d/%d/%d", &dia1, &mes1, &ano1);
    scanf("%d/%d/%d", &dia2, &mes2, &ano2);

    teste11=checkano(ano1);
    teste12=checkdia(dia1,mes1,ano1);
    teste13=checkmes(mes1);
    teste21=checkano(ano2);
    teste22=checkdia(dia2,mes2,ano2);
    teste23=checkmes(mes2);

    if ((dia1==29)&&(mes1==02))
        dia1=28;

    if ((teste11+teste12+teste13+teste21+teste22+teste23)==6){
        total=((365*(ano2-ano1))+sexto);
                    //... incomplete part ...//
    }
    else
        cout<<"data invalida"<<endl;
}
return 0;
}

词汇表:

dia:日

mes:月份

ano:年份

数字睾丸:试验次数

验证:双六边形的功能

check (.):检查"X“的函数

teste"XX":int变量,它将接收检查函数的0或1。

问题是:我不知道如何有组织地计算它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-16 14:58:01

返回值应该使用bool而不是int

代码语言:javascript
运行
复制
bool verificar(int ano)
{
    return ((ano % 4 == 0) && (ano % 100 != 0)) || (ano % 400 == 0));
}

另外,您的check函数也可以大大简化:

代码语言:javascript
运行
复制
bool checkmes(int mes)  {
    return ( (mes > 0) && (mes <= 12) );
}

bool checkano(int ano) {
    return ( (ano > 0) && (ano < 11000) );
}

bool checkdia(int dia, int mes, int ano) {   

    if(dia < 1 || dia > 31) return false;
    if(mes%2 == 0 && dia >30) return false;
    if(mes == 2 && dia >28) return verificar(ano);
    return true;
}

然后你就可以写这样的东西:

代码语言:javascript
运行
复制
bool checkdata(int dia, int mes, int ano) {
    return ( checkano(ano) && checkmes(mes) && checkdia(dia, mes, ano) );
}

这样你就可以写:

代码语言:javascript
运行
复制
if( !checkdata(dia1,mes1,ano1) || !checkdata(dia2,mes2,ano2) ) {
    cout<< "data invalida" <<endl;
}

现在对于主要的问题来说,您可以很容易地得到两个日期之间的天数的估计,但是您不能很容易地得到真实的日期,因为日期只是逻辑的。你必须考虑到历史上所有的日历修改。

为了便于估计,我首先添加/减去偏移到1月1日的日期,然后再添加年份差异:

代码语言:javascript
运行
复制
bool isLeap(int year) {
     return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

int monthLengths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int monthLength(int month, int year) {
    int n = monthLengths[month-1];
    if(month == 2 && isLeap(year)) n += 1;
    return n;
}

int yearLength(int year) {
    return isLeap(year) ? 366 : 365;
}


int nDay = 0; /* day counter */
/* subtract data1 offset to 01/01 */
nDay -= dia1;
for(int i = mes1; i > 1; --i) {
    nDay -= monthLength(i - 1, ano1);
}
/* add data2 offset to 01/01 */
nDay += dia2;
for(int i = mes2; i > 1; --i) {
    nDay += monthLength(i - 1, ano2);
}
/* add year offset */
for(int i = ano2; i > ano1; --i) {
    nDay  += yearLength(i);
}

cout << "Difference = " << nDay << " days" << endl;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16589042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档