本篇文章給大家分享的是有關(guān)C語(yǔ)言中怎么實(shí)現(xiàn)萬(wàn)年歷效果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來(lái)看看吧。
順昌網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái)到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
具體內(nèi)容如下
#include #include #include #define str " SUN MON TUE WED THU FRI SAT " void menu(); //操作菜單int leap(int year); //判斷某年是不是閏年int days_month(int year, int month); //返回某月的天數(shù)int someday(int year, int month, int day); //計(jì)算某天是星期幾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; //用于判斷輸入的數(shù)據(jù)和操作是否正確,1代表正確,0代表錯(cuò)誤 int a = 1; menu(); do { printf("請(qǐng)輸入你要進(jìn)行的操作(1-4):"); scanf_s("%d", &i); getchar(); switch (i) { case 1: printf("請(qǐng)輸入你要查詢的年份:"); 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("請(qǐng)輸入您所要查詢的相關(guān)信息.\n"); printf("年份:"); scanf_s("%d", &year); printf("月份:"); scanf_s("%d", &month); k1 = 1; while (k1) { if (month > 12 || month < 1) { printf("你輸入的月份有誤,請(qǐng)重新輸入!\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("請(qǐng)輸入你要查詢的日期\n"); printf("年份:"); scanf_s("%d", &year); printf("月份:"); scanf_s("%d", &month); k2 = 1; while (k2) { if (month > 12 || month < 1) { printf("你輸入的月份有誤,請(qǐng)重新輸入!\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)); 調(diào)試觀察輸入的日期是否有誤,可忽略 printf("日期:"); scanf_s("%d", &day); k3 = 1; while (k3) { if (day > days_month(year, month) || day < 1) { printf("你輸入的日期有誤,請(qǐng)重新輸入!\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("你輸入的數(shù)據(jù)有誤,請(qǐng)重新輸入你要進(jìn)行的操作(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. 退出系統(tǒng) |\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){ //該函數(shù)運(yùn)用了特殊的計(jì)算公式,詳細(xì)可參見 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; //用于獲得年份的后兩位數(shù) 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用于記錄某月的天數(shù) 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語(yǔ)言中怎么實(shí)現(xiàn)萬(wàn)年歷效果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)名稱:C語(yǔ)言中怎么實(shí)現(xiàn)萬(wàn)年歷效果
文章地址:
http://weahome.cn/article/jhjjeh.html