日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

打印日历-C语言

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 打印日历-C语言 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//打印日歷 //方法一:先求每一年的第一天周幾,再求所求月份周幾 //方法二:直接求出這月之前總天數 然后求這個月第一天周幾 //兩個方法思路一樣 都是對7求余剩幾天 加上基數 #include <stdio.h> #include <stdlib.h> int months[2][13] = {{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, //非閏年的12個月{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } //閏年的12個月 }; int isLeap(int year); int first_day_of_month1(int year, int month); int first_day_of_month2(int year, int month); void show(int year, int month, int first); int main() {int year, month;printf("year/month: ");scanf("%d/%d", &year, &month);int first_month = first_day_of_month2(year, month);show(year, month, first_month);return 0; } int isLeap(int year) //判斷是否為閏年 {return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }//用戶輸入的年份----1 int first_day_of_month1(int year, int month) {int base_year = 1900;int base_first_day = 1;int year_total = 0, month_total = 0;int first_years;for (int i = base_year; i < year; i++) year_total += isLeap(i);//這一年的第一天 (基數星期幾+前一年剩下多少天)%7過了整數周剩的天數就是周幾 year_total = year_total % 7; //用于我的理解 ----前一年剩下幾天 first_years = (year_total + base_first_day) % 7; for (int i = 1; i < month; i++) month_total += months[isLeap(year)][i];month_total = month_total % 7;return (month_total + first_years) % 7; }//用戶輸入的年份----2 int first_day_of_month2(int year, int month) {int base_year = 1900;int base_first_day = 1;int total = 0;int first_years;for (int i = base_year; i < year; i++){if(isLeap(i))total += 366;elsetotal += 365;} for (int i = 1; i < month; i++) total += months[isLeap(year)][i];total = total % 7;return (total + base_first_day) % 7; } void show(int year, int month, int first) {printf("Sun Mon Tue Wed The Fri Sat\n");printf("---------------------------\n");for (int i = 0; i < first; i++)printf(" ");for (int i = 1; i <= months[isLeap(year)][month]; i++) {printf("%3d ", i);if ((i + first) % 7 == 0)printf("\n");} }

總結

以上是生活随笔為你收集整理的打印日历-C语言的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。