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

标签 c++ algorithm date calculus

我正在编写一个代码,如果您输入生日和任何其他日期,它会返回您还活着的年、月和日的总数。

Obs.:包括(闰)双相年。

观察点2:对于无效日期,输出必须是“data invalida”(葡萄牙语无效日期)。

输入/输出:

观察:日期格式为巴西标准,格式为日/月/年。


8//第一个输入是你要测试的输入数。


输入 1:29/02/2000

输入 2:2001 年 1 月 3 日

输出: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:2012 年 5 月 27 日

输入 2:27/05/2013

输出:1 0 0


输入 1:2012 年 1 月 1 日

输入 2:2013 年 5 月 1 日

输出:1 0 4


输入 1:13/05/1966

输入 2:2015 年 5 月 2 日

输出:48 8 23


输入 1:29/02/2003

输入 2:2012 年 4 月 5 日

输出:数据无效


输入 1:14/13/1995

输入 2:1996 年 7 月 8 日

输出:数据无效


代码:

#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;
}

词汇表:

直径:天

时间:月

时间:年份

numerodetestes: 测试次数

verificar:bissextile 函数

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

teste“XX”:将接收 0 或 1 的检查函数的 int 变量。

问题是:我不知道如何以有组织的方式计算它。

最佳答案

您应该使用 bool 而不是 int 作为您的返回值:

bool verificar(int ano)
{
    return ((ano % 4 == 0) && (ano % 100 != 0)) || (ano % 400 == 0));
}

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

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;
}

然后你可以这样写:

bool checkdata(int dia, int mes, int ano) {
    return ( checkano(ano) && checkmes(mes) && checkdia(dia, mes, ano) );
}

这将允许你写:

if( !checkdata(dia1,mes1,ano1) || !checkdata(dia2,mes2,ano2) ) {
    cout<< "data invalida" <<endl;
}

现在对于主要问题,您可以轻松地估计两个日期之间的天数,但您无法轻松获得真实数字,因为日期不过是合乎逻辑的。您必须考虑历史上所有的日历修改。

为了便于估算,我会先添加/减去一月一日的日期偏移量,然后添加年份差异:

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;

关于关于日期的 C++ 不完整代码,什么是更好的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589042/

相关文章:

c++ - 在 C++ 中求解 Ax = b, A = 下三角矩阵

c++ - 打印以在c++中在线的特定位置输出

algorithm - 为什么无法手动测试 AES 算法?

r - 如何将Excel日期格式转换为R中的正确日期

PHP/MySQL - 以 DD MMM YYYY 格式处理日期的最佳方式?

javascript - 使用 javascript 获取当前季度

java - 滚动 Pane 如何滚动

c++ - C++ 中另一个带有闭包的抽象类问题

c - 递归函数find(n)的时间复杂度

python - 一次性验证可比较项的二叉搜索树