给个建议,谢谢。
//输入一个日期,计算这天是今年的第几天。#include <stdio.h>
void main()
{
unsigned int _month={31,28,31,30,31,30,31,31,30,31,30,31};
unsigned int year,month,day;
char ch_1,ch_2;
printf("please input year/month/day: ");
scanf("%d%c%d%c%d",&year,&ch_1,&month,&ch_2,&day);
while((year==0) || (month==0 || month>12) || (day==0 || day>31))
{
printf("input error,please input year/month/day: ");
scanf("%d%c%d%c%d",&year,&ch_1,&month,&ch_2,&day);
}
if(year%4 == 0)
{
if(year%100 == 0)
{
if(year%400 == 0)
{
_month = 29;
}
}
else
{
_month=29;
}
}
while(1)
{
day =day + _month;
if(0 == (month-2))
{
break;
}
month--;
}
printf("this is this year NO.%d days\n",day);
}
#include <stdio.h>
#include <stdlib.h>
int whereday(int year,int month,int day)
{
int i = 0,p = 0;
unsigned int _month={31,28,31,30,31,30,31,31,30,31,30,31};
for(;i<month;i++)
p += _month;
return p+day;
}
int main()
{
int year=0,month=0,day=0;
printf("year:");
scanf("%d",&year);
printf("\nmonth:");
scanf("%d",&month);
printf("\nday:");
scanf("%d",&day);
printf("第%d天\n",whereday(year,month,day));
system("pause");
return 0;
}
平年和闰年就不搞上去 了 #include <stdio.h>
int leap(int i)
{
if(i%4 == 0 && i%100 != 0 || i%400 == 0)
return 1;
else
return 0;
}
void main()
{
int y,m,d,i,sum = 0;
int a = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int b = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("请输入年月日(用逗号分隔):");
scanf("%d,%d,%d",&y,&m,&d);
if(leap(y) == 1)
{
for(i=0;i<m-1;++i)
{
sum += b;
}
sum += d;
}
else
{
for(i=0;i<m-1;++i)
{
sum += a;
}
sum += d;
}
printf("%d年%d月%d天是今年的第%d天\n", y, m, d, sum);
} 迷雾少年 发表于 2015-5-1 07:51
平年和闰年就不搞上去 了
你的函数那里错了 迷雾少年 发表于 2015-5-1 07:51
平年和闰年就不搞上去 了
我修改了for(;i<month-1;i++)
p += _month; 矢志不渝 发表于 2015-5-2 02:17
我修改了for(;i
没错 啊,你改成这样后少一个月了因为我把数叔超标了,是为了当你输入1月1日是第一天 本帖最后由 迷雾少年 于 2015-5-2 06:40 编辑
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int whereday(int year,int month,int day)
{
int i = 0,p = 0;
unsigned int _month={0,31,28,31,30,31,30,31,31,30,31,30,31};//改成这样不会拿到垃圾数据;了
for(;i<month;i++)
p += _month;
return p+day;
}
int main()
{
int year=0,month=0,day=0;
printf("year:");
scanf("%d",&year);
printf("\nmonth:");
scanf("%d",&month);
printf("\nday:");
scanf("%d",&day);
printf("第%d天\n",whereday(year,month,day));
system("pause");
return 0;
}
页:
[1]