课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565
【项目2-日期结构体】定义一个结构体变量(包括年、月、日),要求输入年、月、日,计算输出该日是该年的第几天?
/* * 程序的版权和版本声明部分 * Copyright (c)2013, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: date.cpp * 作 者: * 完成日期: 年 月 日 * 版本号: v1.0 * 输入描述:年月日 * 问题描述:输出对应的那一天是这一年的第几天 * 输出:第几天 */ #include <iostream> using namespace std; struct Date { int year; int month; int day; }; int main() { Date date; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; int days; cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days<<"天."<<endl; return 0; }
参考解答一:
#include <iostream> using namespace std; struct Date { int year; int month; int day; }; int main() { Date date; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; int days; switch(date.month) { case 1: days=date.day; break; case 2: days=date.day+31; break; case 3: days=date.day+59; break; case 4: days=date.day+90; break; case 5: days=date.day+120; break; case 6: days=date.day+151; break; case 7: days=date.day+181; break; case 8: days=date.day+212; break; case 9: days=date.day+243; break; case 10: days=date.day+273; break; case 11: days=date.day+304; break; case 12: days=date.day+334; break; } if ((date.year %4== 0 && date.year % 100 != 0 ||date.year % 400 == 0) && date.month >=3) days+=1; cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days <<"天."<<endl; return 0; }
参考解答二:
#include <iostream> using namespace std; struct Date { int year; int month; int day; }; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { Date date; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; int days=0,i; for(i=1;i<date.month;++i) days+=d[i]; //当月之前的天数 days+=date.day; //加上本月天数 if ((date.year %4== 0 && date.year % 100 != 0 ||date.year % 400 == 0) && date.month >=3) days+=1; //闰年的2月要加1天 cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days <<"天."<<endl; return 0; }
参考解答三: 函数版1——用结构体的分量当形参
#include <iostream> using namespace std; struct Date { int year; int month; int day; }; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int days(int,int,int); int main() { Date date; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"; cout<<days(date.year,date.month,date.day)<<"天."<<endl; return 0; } int days(int yy,int mm,int dd) { int n=0,i; for(i=1;i<mm;++i) n+=d[i]; //当月之前的天数 n+=dd; //加上本月天数 if ((yy %4== 0 && yy % 100 != 0 ||yy % 400 == 0) && mm >=3) n+=1; //闰年的2月要加1天 return n; }
参考解答四:函数版2——用结构体整体作为参数
#include <iostream> using namespace std; struct Date { int year; int month; int day; }; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int days(Date); int main() { Date date; cout<<"input year,month,day:"; cin>>date.year>>date.month>>date.day; cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"; cout<<days(date)<<"天."<<endl; return 0; } int days(Date ymd) { int n=0,i; for(i=1;i<ymd.month;++i) n+=d[i]; //当月之前的天数 n+=ymd.day; //加上本月天数 if ((ymd.year %4== 0 && ymd.year % 100 != 0 ||ymd.year % 400 == 0) && ymd.month >=3) n+=1; //闰年的2月要加1天 return n; }
时间: 2024-09-19 20:34:39