用c++算出某一年一月一日到该年某一天的天数

来源:百度知道 编辑:UC知道 时间:2024/05/17 06:03:04
结果是输入一个日期后会自动生成到该日期的天数

用得着这么复杂吗?看我的!
#include <stdio.h>
void main()
{ int year,month,days;
printf("请输入年月日:");
scanf("%d %d %d", &year,&month,&days);
for(int i=1;i<month;i++)
switch(i)
{ case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:if((year % 4 ==0) && (year % 100 !=0) ||(year % 400==0))
days+=29;
else days+=28;
}
printf("今天是今年的第%d天",days);
}

Cdata.h 基本能满足你的要求,如果不行,自己再写个什么函数就OK

#pragma once

class Date
{
public:
enum Month { jan = 1, feb, mar, apr, may, jun,
jul, aug, sep, oct, nov, dec };
class BadDate{ };
Date(int dd = 1, Month mm = Month(1), int yy = 2007);
int day( ) const;
Month month( ) const;
int year( ) const;