您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C語言怎么實現簡單萬年歷的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內容如下
#include <stdio.h> #include<stdlib.h> int year, month; int run[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; int ping[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; char week[7][10] = { "SUN","MON","TUE","WED","THU","FRI","SAT" }; bool IsLeap(int year) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //判斷閏年 { return true; } else { return false; } } void PrintStar() { printf("\t\t\t萬年歷\t\t\t\n"); printf("****************************************************\n"); for (int i = 0; i < 7; i++) { printf("%s\t", week[i]); } printf("\n"); } int DaySum(int year, int month) { int sum = 0; for (int i = 1990; i < year; i++) { if (IsLeap(i)) { sum += 366; } else { sum += 365; } } for (int i = 0; i < month - 1; i++) { if (IsLeap(year)) { sum += run[i]; } else { sum += ping[i]; } } return sum; } void PrintCal(int sum, int year, int month) { int nRes, nTmp; nRes = sum % 7+1;//前面空幾格 nTmp = 7 - nRes;//從第幾格開始打印 for (int i = 0; i < nRes; i++) { printf("\t"); } if (IsLeap(year)) { for (int i = 1; i < run[month - 1]+1; i++) { printf("%d\t", i); if (nTmp == i || (i - nTmp) % 7 == 0) { printf("\n"); } } printf("\n"); } else { for (int i = 1; i < ping[month - 1]+1; i++) { printf("%d\t", i); if (nTmp == i || (i - nTmp) % 7 == 0) { printf("\n"); } } printf("\n"); } } int main(void) { while (1) { scanf_s("%d %d", &year, &month); PrintStar(); PrintCal(DaySum(year, month), year, month); printf("****************************************************\n"); } return 0; }
感謝各位的閱讀!關于“C語言怎么實現簡單萬年歷”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。