实现一个函数判断year是否为闰年
1.以前写的代码,最简单易懂的方式
#include<stdio.h> #include<stdlib.h> int main() { int year; printf("请输入年份:"); scanf("%d", &year); //四年一闰百年不闰四百再闰 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) printf("%d是闰年\n", year); else printf("%d不是闰年\n",year); system("pause"); return 0; }
2.分装函数
#include<stdio.h> #include<stdlib.h> int judg_year(int year)//判断年份是否闰年 { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) printf("%d是闰年\n", year); else printf("%d不是闰年\n", year); }//功能应单一,尽量少出现printf等函数 int main() { int judg_year(int year); int year; printf("请输入年份:"); scanf("%d", &year); judg_year(year);//引用 system("pause"); return 0; }
3.分装函数(改)
#include<stdio.h> #include<stdlib.h> int is_leap(year) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) return 1; else return 0; } int main() { int year = 0; scanf("%d", &year); int ret = is_leap(year); if (1 == ret) printf("%d是闰年\n",year); if (0 == ret) printf("%d不是闰年\n", year); system("pause"); return 0; }