您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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("\n\n---------******* %d *******------------\n", a); search_month(year, a); } printf("---------*********************------------\n\n\n"); 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("---------*********************------------\n\n\n"); 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("---------*********************------------\n\n\n"); break; case 4:exit(0); printf("---------*********************------------\n\n\n"); 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("\n\n\t\t----------------------------------------\n"); printf("\t\t|--------------------------------------|\n"); printf("\t\t| %d. 輸出某年的日歷 |\n", i); printf("\t\t| %d. 輸出某月的日歷 |\n", i + 1); printf("\t\t| %d. 查詢某日是星期幾 |\n", i + 2); printf("\t\t| %d. 退出系統 |\n", i + 3); printf("\t\t|--------------------------------------|\n"); printf("\t\t----------------------------------------\n\n"); } 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("你所要查詢的日期是%s\n", 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("%s\n", 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語言中怎么實現萬年歷效果,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。