概述
本文实例为大家分享了C语言实现万年历的具体代码,供大家参考,具体内容如下
#include <stdio.h> #include <stdlib.h> #include <string.h> #define str " SUN MON TUE WED THU FRI SAT " void menu(); //操作菜单 int leap(int year); //判断某年是不是闰年 int days_month(int year, int month); //返回某月的天数 int someday(int year, int month, int day); //计算某天是星期几 void search_month(int year, int month); //输出某月的日历 void search_someday(int year, int month, int day); //查询某日是星期几 int main() { int i, year, month, day; int k1, k2, k3, k4; //用于判断输入的数据和操作是否正确,1代表正确,0代表错误 int a = 1; menu(); do { printf("请输入你要进行的操作(1-4):"); scanf_s("%d", &i); getchar(); switch (i) { case 1: printf("请输入你要查询的年份:"); scanf_s("%d", &year); for (a; a <= 12; a++) { printf("nn---------******* %d *******------------n", a); search_month(year, a); } printf("---------*********************------------nnn"); break; case 2: printf("请输入您所要查询的相关信息.n"); printf("年份:"); scanf_s("%d", &year); printf("月份:"); scanf_s("%d", &month); k1 = 1; while (k1) { if (month > 12 || month < 1) { printf("你输入的月份有误,请重新输入!n"); printf("月份:"); scanf_s("%d", &month); if (month < 1 || month>12) k1 = 1; } else k1 = 0; } search_month(year, month); printf("---------*********************------------nnn"); break; case 3: printf("请输入你要查询的日期n"); printf("年份:"); scanf_s("%d", &year); printf("月份:"); scanf_s("%d", &month); k2 = 1; while (k2) { if (month > 12 || month < 1) { printf("你输入的月份有误,请重新输入!n"); printf("月份:"); scanf_s("%d", &month); if (month < 1 || month>12) k2 = 1; } else k2 = 0; } // printf("%d年%d月有%d天n",year,month,days_month(year,month)); 调试观察输入的日期是否有误,可忽略 printf("日期:"); scanf_s("%d", &day); k3 = 1; while (k3) { if (day > days_month(year, month) || day < 1) { printf("你输入的日期有误,请重新输入!n"); printf("日期:"); scanf_s("%d", &day); if (day > days_month(year, month) || day < 1) k3 = 1; } else k3 = 0; } search_someday(year, month, day); printf("---------*********************------------nnn"); break; case 4:exit(0); printf("---------*********************------------nnn"); break; default: k4 = 1; while (k4) { printf("你输入的数据有误,请重新输入你要进行的操作(1-4):"); scanf_s("%d", &i); if (i < 1 || i>4) k4 = 1; else k4 = 0; } break; } } while (i > 0 && i < 5); return 0; } void menu() { int i = 1; printf("nntt----------------------------------------n"); printf("tt|--------------------------------------|n"); printf("tt| %d. 输出某年的日历 |n", i); printf("tt| %d. 输出某月的日历 |n", i + 1); printf("tt| %d. 查询某日是星期几 |n", i + 2); printf("tt| %d. 退出系统 |n", i + 3); printf("tt|--------------------------------------|n"); printf("tt----------------------------------------nn"); } int leap(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1; else return 0; } int days_month(int year, int month) { int days_month; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days_month = 31; break; case 4: case 6: case 9: case 11:days_month = 30; break; case 2: if (leap(year) == 1) { days_month = 29; break; } else days_month = 28; break; } return days_month; } int someday(int year, int month, int day) { //该函数运用了特殊的计算公式,详细可参见 http://blog.163.com/hexin_mars_blog/blog/static/248215040201571351115699/ int someday, m, n, i; //someday表示查询的日子是星期几 m = year - year / 1000 * 1000; n = m - m / 100 * 100 - 1; //用于获得年份的后两位数 int days = 0; for (i = 1; i < month; i++) days = days + days_month(year, i); days = days + day; someday = (n + n / 4 - n / 100 + n / 400 + days) % 7; return someday; } void search_someday(int year, int month, int day) { char weekname[20]; switch (someday(year, month, day)) { case 0:strcpy_s(weekname, "星期天"); break; case 1:strcpy_s(weekname, "星期一"); break; case 2:strcpy_s(weekname, "星期二"); break; case 3:strcpy_s(weekname, "星期三"); break; case 4:strcpy_s(weekname, "星期四"); break; case 5:strcpy_s(weekname, "星期五"); break; case 6:strcpy_s(weekname, "星期六"); break; } printf("你所要查询的日期是%sn", weekname); } void search_month(int year, int month) { int i = 0, j, k = 0, m; //j用于记录某月的第一天是星期几 m用于记录某月的天数 k用于换行提示 char x[10] = " "; m = days_month(year, month); j = someday(year, month, 1); //输出某月的日历 printf("------------------------------------------n"); printf("%sn", str); for (; i < j; i++) { printf("%s", x); k++; } for (i = 1; i <= m; i++) { k++; if (k % 7 == 0) { if (i < 10) { printf(" %d ", i); printf("n"); } else if (i >= 10) { printf(" %d ", i); printf("n"); } } else { if (i < 10) { printf(" %d ", i); } else if (i >= 10) { printf(" %d ", i); } } } printf("n------------------------------------------n"); } void exit() { exit(0); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是粗暴爆米花为你收集整理的C语言实现万年历效果的全部内容,希望文章能够帮你解决C语言实现万年历效果所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复