Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;关于日期的代码不完整,有什么更好的解决方案?_C++_Algorithm_Date_Calculus - Fatal编程技术网

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

C++ C++;关于日期的代码不完整,有什么更好的解决方案?,c++,algorithm,date,calculus,C++,Algorithm,Date,Calculus,我在写一个代码,如果你输入你的生日和任何其他日期,它会返回你活着的年、月和日的总数 Obs.:包括(闰)六分之一年 备注2:对于无效日期,输出必须为“数据无效”(葡萄牙语中的无效日期) 输入/输出: Obs:日期格式为巴西标准,格式为日/月/年 8//第一个输入是要测试的输入数 投入1:29/02/2000 投入2:01/03/2001 产出:101 投入1:29/02/2000 投入2:28/02/2001 产出:100 投入1:2012年12月29日 投入2:2013年1月13日 输

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

Obs.:包括(闰)六分之一年

备注2:对于无效日期,输出必须为“数据无效”(葡萄牙语中的无效日期)

输入/输出: Obs:日期格式为巴西标准,格式为日/月/年


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


投入1:29/02/2000

投入2:01/03/2001

产出:101


投入1:29/02/2000

投入2:28/02/2001

产出:100


投入1:2012年12月29日

投入2:2013年1月13日

输出:0 0 15


投入1:27/05/2012

投入2:27/05/2013

产出:100


输入1:2012年1月1日

输入2:05/01/2013

产出:104


投入1:1966年5月13日

输入2:2015年2月5日

产出:48 823


投入1:29/02/2003

投入2:2012年5月4日

产出:数据无效


投入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;
}
#包括
#包括
使用名称空间std;
int验证车(int ano)
{
如果((ano%4==0)和&(ano%100!=0))| |(ano%400==0))
返回1;
其他的
返回0;
}
int checkdia(int dia,int mes,int ano){
如果(直径>0)

如果((mes==1)| |(mes==3)| | |(mes==5)| | |(mes==7)| |(mes==8)| |(mes==10)| | | |(mes==12))&(dia您应该使用
bool
而不是
int
作为返回值:

bool verificar(int ano)
{
    return ((ano % 4 == 0) && (ano % 100 != 0)) || (ano % 400 == 0));
}
您的
检查功能也可以大大简化:

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;
}
这样你就可以写:

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

cout对于返回值,您应该使用
bool
而不是
int

bool verificar(int ano)
{
    return ((ano % 4 == 0) && (ano % 100 != 0)) || (ano % 400 == 0));
}
您的
检查功能也可以大大简化:

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;
}
这样你就可以写:

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

这是为了获得编码经验,或者你是在寻找现成的东西吗?这是另一个很好的选择。这是为了获得编码经验,我正在上大学的第一学期。我想在未来几年学习算法和应用程序开发。你能给我推荐一本好书、网站或其他学习这方面的资源吗对象?(我习惯于访问Udacity、Coursera和edX)。我想我还没有准备好在我的代码中实现QDateTime,我需要更多的练习。但是,谢谢你的建议。这是为了获得编码经验,还是你可能正在寻找类似的现成工具?这是另一个非常好的选择。这是为了获得编码经验,我正在上大学的第一学期。我想学习算法和应用未来几年的发展。你能给我推荐一本好书、一个网站或其他学习该学科的来源吗?(我习惯于访问Udacity、Coursera和edX)。我想我还没有准备好在我的代码中实现QDateTime,我需要进行更多的练习。但是,谢谢你的建议。@Nemo是的,这是一个错误,此外,一个模不足以做到这一点。“你必须考虑历史上所有的日历修改。”-一般来说,这是正确的,但如果你看最近的日期(可能是从大约开始,尽管我不是专家),日历是可以预测的-只是很难得出计算结果。特别是,请参见。Ops,1月被归类为30天的一个月(在我的错误代码中)不是31,抱歉这个错误,谢谢你的观察!@Nemo是的,这是一个错误,此外,模不足以完成这个任务。“你必须考虑历史上所有的日历修改。”-这是正确的,但如果你看的是最近的日期(大概从大约开始,虽然我不是专家)日历是可以预测的——计算起来很尴尬。请特别注意。Ops,一月被归类为30天的月份(在我错误的代码中)而不是31天,很抱歉这个错误,感谢您的观察!