新手求助
/***************************************************输入一个某年某月某日,输出为这一天为这一天的第几天**
*************************************************/
#include <stdio.h>
int main()
{
int a ,b1 , b2 ,c ,leap ,sum;
printf("please input year/month/day: \n"); //输入年月份
scanf("%d%d%d",&a,&b1,&c);
if (a%4==0) //判断是否为闰年
{
if (a%100==0)
{
if(a%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
if(leap) //leap为判断闰年的标志
{
printf("%dis a leap year\n",a);
switch(b1) //闰年每个月天数的差别
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: b2 = 31 ;break;
case 2: b2 = 29 ;break;
case 4:
case 6:
case 9:
case 11: b2 = 30 ;break;
}
}
else //非闰年每个月份天数的差别
{
printf("%dis not a leap year\n",a);
switch(b1)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: b2 = 31 ;break;
case 2:b2 = 28 ;break;
case 4:
case 6:
case 9:
case 11: b2 = 30 ;break;
}
}
if (b1==1) //计算天数
sum = b2 + a;
else if (b1==2)
sum = b2 + a + 31;
else if (b1==3)
sum = b2 * 2 +a;
else if (b1==4)
sum = b2 + 31 * 2 + 30 + a;
else if (b1==5)
sum = b2
printf("%d年%d月%d日为%d年的第%d天\n",a ,b1 ,c ,a ,sum);
}
计算天数怎么计算
用数组 天数就是已经过去的月数里的天数总和加上当前月的天数 你可以通过我这种方法得到想要的结果
#include <stdio.h>
#include <conio.h>
void main()
{
int year, mouth, day, sum, leap;
printf("请输入年 月 日:\n");
scanf("%d %d %d", &year, &mouth, &day);
switch(mouth)
{
case 1: sum = 0;break;
case 2: sum = 31;break;
case 3: sum = 59;break;
case 4: sum = 90;break;
case 5: sum = 120;break;
case 6: sum = 151;break;
case 7: sum = 181;break;
case 8: sum = 212;break;
case 9: sum = 243;break;
case 10: sum = 273;break;
case 11: sum = 304;break;
case 12: sum = 334;break;
}
sum = sum + day;
if(year%4==0 && year%100!=0 || year%400==0)
leap=1;
else
leap=0;
if(leap==1 && mouth > 2)
sum=sum+1;
printf("这一天是这一年的第%d天\n", sum);
getch();
} #pragma warning(disable : 4996)
#include <stdio.h>
#include <windows.h>
#include <stdbool.h>
int main(void) {
int year, month, day, sum = 0;
bool n = false;
int Year[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
printf("please input year/month/day: ");
scanf("%d %d %d", &year,&month, &day);
if ((year % 4 == 0) && (year % 100 != 0)) {
n = true;
}
if (n) {
Year = 28;
}
for (int i = 0; i < month; i++) {
sum += Year;
}
sum = sum - Year + day;
printf("%d年 %d月 %d日 是%d年的第%d天\n", year,month,day,year,sum);
system("pause");
return 0;
}
kaiseryyd 发表于 2018-4-11 23:53
用数组
还没学流泪(╥╯^╰╥) BngThea 发表于 2018-4-12 09:03
天数就是已经过去的月数里的天数总和加上当前月的天数
之前的月数怎嘛区分大还是小 549444387@qq.co 发表于 2018-4-12 09:14
你可以通过我这种方法得到想要的结果
#include
嗯嗯 谢谢这个比较简单
页:
[1]