本人写的最傻逼的程序
<div></div>本人自学了几周,这是我打的第一个最长,最傻逼的程序,希望能给好的建议
#include <stdio.h>
void main()
{
int year, leap, month, day, o;
scanf("%d%d%d", &year, &month, &day);
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
leap = 1;
}
else
{
leap = 0;
}
}
else
{
leap = 1;
}
}
else
{
leap = 0;
}
if(leap == 1)
{
if(month>3)
{
day += 1;
}
}
else if(leap == 0);
{
if(month>3)
{
day = day;
}
}
switch(leap)
{
case 1:
o = 29;
case 0:
o = 28;
}
if(month == 1)
{
printf(" %d the %d day", year, day);
}
else if(month == 2)
{
day = 31+day;
printf(" %d the %d day", year, day);
}
else if(month == 3)
{
day = 31+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 4)
{
day = 31*2+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 5)
{
day = 31*2+30+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 6)
{
day = 31*3+30+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 7)
{
day = 31*3+30*2+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 8)
{
day = 31*4+30*2+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 9)
{
day = 31*5+30*2+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 10)
{
day = 31*5+30*3+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 11)
{
day = 31*6+30*3+o+day;
printf(" %d the %d day", year, day);
}
else if(month == 12)
{
day = 31*6+30*4+o+day;
printf(" %d the %d day", year, day);
}
}
本帖最后由 jackz007 于 2020-11-24 00:26 编辑
代码逻辑正确,运行结果正确,对于初学者而言,很不容易,加油!
不过,代码写得很不简洁,这里给楼主提供一个简洁一些的版本,谨供楼主参考。
#include <stdio.h>
//判断闰年
int leap(int y)
{
if((! (y % 4) && y % 100) || ! (y % 400)) return 1 ;
else return 0 ;
}
//计算每个月的天数
int mdays(int y , int m)
{
int md = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31} , days ;
days = md ;
if(m == 2 && leap(y)) days ++ ;
return days ;
}
//计算某个日期是某一年的第几天
int thedays(int y , int m , int d)
{
int days , k ;
for(days = d , k = 1; k < m ; k ++) days += mdays(y , k) ;
return days ;
}
int main(void)
{
int year , month , day ;
scanf("%d%d%d" , & year , & month , & day) ;
printf(" %d the %d day\n", year , thedays(year , month , day)) ;
}
编译、运行实况
D:\00.Excise\C>g++ -o days days.c
D:\00.Excise\C>days
2020 11 23
2020 the 328 day
D:\00.Excise\C>
页:
[1]