猿问

c++编程,给出年月日,计算该日是该年的第几天?

c++编程,给出年月日,计算该日是该年的第几天


红糖糍粑
浏览 2073回答 3
3回答

红颜莎娜

&nbsp;1#include&nbsp;<iostream>&nbsp;1using&nbsp;namespace&nbsp;std;&nbsp;12345int&nbsp;main(){&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;sum_day(int&nbsp;month,int&nbsp;day);&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;is_leap(int&nbsp;year);&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;year,month,day,days;&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;cout<<"Enter&nbsp;date(for&nbsp;example:2015&nbsp;1&nbsp;14):\n";&nbsp;&nbsp;&nbsp;&nbsp;cin>>year>>month>>day;&nbsp;123&nbsp;&nbsp;&nbsp;&nbsp;days=sum_day(month,day);&nbsp;&nbsp;&nbsp;&nbsp;if(is_leap(year)&&month>2)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;days+=1;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;<<year<<"/"<<month<<"/"<<day<<"is&nbsp;the&nbsp;"<<days<<"&nbsp;day&nbsp;in&nbsp;this&nbsp;year!\n";&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;}&nbsp;12345678int&nbsp;sum_day(int&nbsp;month,int&nbsp;day){&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i<month;i++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day+=day_tab[i];&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;day;}&nbsp;12345int&nbsp;is_leap(int&nbsp;year){&nbsp;&nbsp;&nbsp;return&nbsp;((year%4==0&&year%100!=0)&nbsp;||year%400==0);}我在code&nbsp;blocks上运行没有问题

慕桂英546537

123456789101112131415161718192021222324252627282930313233#include<iostream>using&nbsp;namespace&nbsp;std;int&nbsp;d[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{31,29,31,30,31,30,31,31,30,31,30,31}&nbsp;};int&nbsp;days(int&nbsp;m,int&nbsp;a=0){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;sumday=0;&nbsp;for(int&nbsp;i=0;i<m-1;++i)&nbsp;&nbsp;sumday+=d[a][i];&nbsp;return&nbsp;sumday;}int&nbsp;main(){&nbsp;int&nbsp;day=0,month=0,year=0;&nbsp;cout<<"day&nbsp;&nbsp;&nbsp;:";cin>>day;&nbsp;cout<<"month&nbsp;:";cin>>month;&nbsp;cout<<"year&nbsp;&nbsp;:";cin>>year;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;a=0;&nbsp;//&nbsp;&nbsp;闰年判断&nbsp;if((year%4==0&&year%100!=0)||year%400==0)a=1;&nbsp;switch(month)&nbsp;{&nbsp;case&nbsp;1:case&nbsp;2:&nbsp;case&nbsp;3:case&nbsp;4:&nbsp;case&nbsp;5:case&nbsp;6:&nbsp;case&nbsp;7:case&nbsp;8:&nbsp;case&nbsp;9:case&nbsp;10:&nbsp;case&nbsp;11:case&nbsp;12:&nbsp;cout<<days(month,a)+day<<endl;&nbsp;default:&nbsp;break;&nbsp;}&nbsp;&nbsp;&nbsp;return&nbsp;0;}

互换的青春

# include <stdio.h># include <conio.h>int sum_day(int month, int day);int leap(int year);void main(){int year, month, day;int days;printf("请输入日期(年,月,日):");scanf("%d, %d, %d", &year, &month, &day);printf("%d年%d月%d日", year, month, day);days = sum_day(month, day);if(leap(year) && month>=3)days = days + 1;printf("是该年的第%d天.\n", days);getch();}static int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int sum_day(int month, int day){int i;for(i=1; i<month; i++)day = day + day_tab[i];return day;}int leap(int year){int leap;leap = (year%4==0&&year%100!=0)||(year%400==0);return leap;}没调试过,机子上暂时没工具,应该是没问题了。你试试
随时随地看视频慕课网APP
我要回答