给定某个年、月、日,计算出这一天为该年的的第几天。 要求:(1)用子函数实现闰年的判断 (2)用子函数实现日期的计算


#include<iostream>
using namespace std;
int a,b,c;
int sum( int a)
{
    if(a%4==0||a%400==0)
        cout<<"此年是闰年"<<endl;
    else 
        cout<<"此年是平年"<<endl;
    return a;
}
int sum1(int s,int d)
{
    int x,z;
    if(a%4==0||a%400==0)
         int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
         for(x=0;x<b-1;x++)
                z=z+a[x];//这里有问题
    if(a%4!=0||a%400!=0)
        int a[12]={31,29,31,30,31,30,31,31,30,31,30,31};
        for(x=0;x<b-1;x++)
            z=z+a[x];//这里有问题
cout<<"今年到现在已经:"<<z<<endl;
return a ;
}
void main()
{
    int x,y,z;
    cout<<"输入年月日"<<endl;
    cin>>a>>b>>c;
    x=sum(a);
    y=sum(a,b);//这里有问题,问题是函数参数调用太多。
}
#include <iostream>
using namespace std;
struct y_m_d
{
    int year;
    int month;
    int day;
};
int is_runnian(int iyear)
{
    if((iyear%4==0&&iyear%100!=0)||(iyear%400==0))
        return 1;
    else
        return 0;
}
int sday(int num,int iyear)
{
    switch(num)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        return 31;
    case 4:
    case 6:
    case 9:
    case 11:
        return 30;
    case 2:
        if(is_runnian(iyear))
            return 29;
        else
            return 28;
    }
    return 0;
}
 
int days(y_m_d date)
{
    int i,s=0;
    for(i=1;i<=date.month;i++)
        s=s+sday(i,date.year);
        return s;
}
int main()
{
  y_m_d date;
  int days(y_m_d);
  int day_sum;
  cin>>date.year>>date.month>>date.day;
  day_sum=days(date);
  cout<<day_sum<<endl;
  return 0;
}

闰年:能被4整除但不能被100整除,或者能被400整除


#include<iostream>
using namespace std;
int a,b,c;
void sum( int a)
{
    if(a%4==0&&a%100!=0||a%400==0)
        cout<<"此年是闰年"<<endl;
    else 
        cout<<"此年是平年"<<endl;
    
}
int sum1(int s,int d)
{
    int i,z;
    int a[12];
    a[0]=31,a[2]=31,a[3]=30,a[4]=31,a[5]=30,a[6]=31,a[7]=31,a[8]=30,a[9]=31,a[10]=30,a[11]=31;
    z=0;
    if((s%4!=0)||(s%400!=0))
         a[1]=28;
    if((s%4==0)&&(s%100!=0)||(s%400==0))
          a[1]=29;

    for(i=0;i<d-1;i++)
        z+=a[i];
return z ;
}
void main()
{
    int y,n;
    cout<<"输入年月日"<<endl;
    cin>>a>>b>>c;
    sum(a);
    y=sum1(a,b);
    n=y+c;
    cout<<"此年已经过了"<<n<<"天"<<endl;
    if(b==1&&c==1)
        cout<<"元旦"<<endl;
    else if(b==2&&c==14)
        cout<<"情人"<<endl;
    else if(b==3&&c==8)
        cout<<"妇女"<<endl;
    else if(b==4&&c==1)
        cout<<"愚人"<<endl;
    else if(b==5&&c==1)
        cout<<"劳动"<<endl;
    else if(b==5&&c==4)
        cout<<"青年"<<endl;
    else if(b==6&&c==1)
        cout<<"儿童"<<endl;
    else if(b==10&&c==1)
        cout<<"国庆"<<endl;
    else if(b==12&&c==25)
        cout<<"圣诞"<<endl;
    else cout<<"不是公历节日"<<endl;
}

自己做的不报错