第十七周 【项目4 - 日期结构体】(1)
问题及代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*ALL right reserved
*文件名:日期结构体
*作者;童宇
*完成日期:2014年月日
*版本号v1.0
*问题描述:定义一个结构体变量(包括年、月、日)
*输入描述:输入年、月、日
*程序输出:该日是该年的第几天。
*/
#include <iostream>
using namespace std;
int A(int ,int ,int );
struct Date
{
int year;
int month;
int day;
};
int main()
{
Date date;
cout<<"input year,month,day:\n ";
cin>>date.year>>date.month>>date.day;
int days;
days=A(date.year,date.month,date.day);
cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days<<"天."<<endl;
return 0;
}
int A(int y,int m,int d)
{
int s=0;
while(m>0)
{
m--;
switch(m)
{
case 2:
s=s+28;
break;
case 4:
case 6:
case 9:
case 11:
s=s+30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
s=s+31;
break;
}
}
s=s+d;
if((y%4==0&&y%100!=0)||y%400==0)
{
s=s+1;
}
return s;
}
运行结果:
还没有评论,来说两句吧...