定义一个结构体变量(包括年、月、日),要求输入年、月、日,计算输出该日是该年的第几天。
/* * Copyright (c) 2014,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:demo.cpp * 完成时间:2014年12月16日 * 版本号: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; //计算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.year % 100 == 0)) { if (date.month>2) days--; } cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days<<"天."<<endl; return 0; }运行结果:
@ Mayuko