【C 语言之项目实战】判断闰年及计算天数(详细版)
目錄
1.項目要求
2.定義模塊函數(shù)
3.各模塊函數(shù)實現(xiàn)
4.項目源代碼
5.項目總結(jié)
1. 項目要求
1.1 首先判斷用戶輸入的年份是否為閏年;
1.2 計算一年中每個月份的天數(shù);
1.3 用戶輸入日期(年月日),計算輸出天數(shù) 。
2. 定義函數(shù)
2.1 主函數(shù):main()
2.2 判斷閏年函數(shù):Is_Leap()
2.3 計算月份天數(shù):Get_YM_day()
2.4 輸入日期計算天數(shù):Get_YMD_Total()
3. 各模塊函數(shù)實現(xiàn)
3.1 判斷閏年函數(shù):Is_Leap()
???? ①. 算法思想:閏年判斷條件是,輸入的任意年份滿足能被 4 整除但不能被 100 整除或者滿足能被 400 整除即可。
???? ②. 源程序
/** 判斷是否為閏年 */ bool Is_Leap(int year) {/**bool leap; // bool 類型初始值默認為 falseif (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){leap = true;}return leap;*/// 等價于 ==> return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0); }3.2 計算月份天數(shù):Get_YM_day()
???? ①. 算法思想:月份天數(shù)基本上是固定不變的,除了 2 月份,閏年 29 天,非閏年 28 天。所以采用數(shù)組實現(xiàn),通過查表法,用戶輸入月份就可得到相應(yīng)的天數(shù)。月份即為數(shù)組下標(biāo),因為數(shù)組下表是從 0 開始,所以下表 0 表示閏年的 2 月天數(shù) 29 天,下標(biāo) 1 – 12 表示非閏年 1 - 12 月 對應(yīng)的天數(shù)。
???? ②. 源程序
/** 計算一年中月份的天數(shù) Get_YM_day */ int Get_YM_Day(int year, int month) {// 靜態(tài)常性數(shù)組 static 只初始化一次 const 且只可讀不可寫// 主函數(shù)每調(diào)用一次 都會開辟一次數(shù)組空間 調(diào)用結(jié)束后釋放空間 // 用 static 靜態(tài)關(guān)鍵字 作用域在函數(shù)內(nèi)部 生存期在數(shù)據(jù)區(qū)(.data)避免每次調(diào)用每次開辟空間static const int days[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// 0 1 2 3 4 5 6 7 8 9 10 11 12if (month == 2 && Is_Leap(year)){month = 0;}return days[month]; }???? ③. 總結(jié):靜態(tài)常性數(shù)組 static 只初始化一次,const 只可讀不可寫,主函數(shù)每調(diào)用一次都會開辟一次數(shù)組空間,調(diào)用結(jié)束后釋放空間;用 static 靜態(tài)關(guān)鍵字,作用域在函數(shù)內(nèi)部,生存期在數(shù)據(jù)區(qū)(.data),避免每次調(diào)用每次開辟空間。
???? 關(guān)鍵字詳情請參考 《C語言之初識關(guān)鍵字》,點擊此處即可閱讀
3.3 輸入日期計算天數(shù):Get_YMD_Total()
???? ①. 算法思想:循環(huán)疊加用戶輸入月份的前幾個月的天數(shù) + 當(dāng)月的天數(shù),如:用戶輸入 6 月 8 日 計算前 5 個月的天數(shù) + 6 月的 8 天,最后還需判斷用戶輸入的年月日是否正確。
/** 輸出日期 計算天數(shù) Get_YMD_Total */ int Get_YMD_Total(int year, int month, int day) {// 循環(huán)疊加用戶輸入月份的前幾個月的天數(shù) + 當(dāng)月的天數(shù) 如:用戶輸入 6 月 8 日 計算前 5 個月的天數(shù) + 8 天int sum = 0;if (year < 1) return YEAR_ERROR; // 輸入的年份出錯if (month < 1 || month > 12) return MONTH_ERROR; // 輸入的月份出錯if (day < 1 || day > Get_YM_Day(year, month)) return DAY_ERROR; // 輸入的天數(shù)出錯for (int i = 1; i < month; ++i) // 累加求輸入月份的前幾個月的天數(shù){sum += Get_YM_Day(year, i); }sum += day; // 加上當(dāng)月天數(shù)return sum; }3.4 主函數(shù):main()
/** 主函數(shù) */ int main() {int year, month, day, day1;char ch = '\0';do {printf("Input (year month day): \n");scanf_s("%d %d %d", &year, &month, &day);// 計算一年中月份的天數(shù)day1 = Get_YM_Day(year, month);// 輸出日期 計算天數(shù)int total = Get_YMD_Total(year, month, day);switch (total){case YEAR_ERROR:printf("year input error! \n");break;case MONTH_ERROR:printf("month input error! \n");break;case DAY_ERROR:printf("day input error! \n");break;default:if (Is_Leap(year)) {printf("%d年是閏年!\n", year);}else{printf("%d年不是閏年!\n", year);}printf("%d年的%d月==>%d天\n", year, month, day1);printf("%d年%d月%d日==>%d天\n", year, month, day, total);break;}printf("是否繼續(xù)(y/n)... \n");// 清除緩沖區(qū) // fflush(stdin); 在 vs 2015 年之后就失效了// 清除緩沖區(qū)的作用:鍵盤錄入時會有 ‘\n’ 為了避免 ‘\n’的干擾需清除緩沖區(qū)rewind(stdin); ch = getchar();} while (ch == 'Y' || ch == 'y');return 0; }4. 項目源代碼
#include<stdio.h> #include<stdlib.h> #include<string.h>#define YEAR_ERROR -1 #define MONTH_ERROR -2 #define DAY_ERROR -3/** 項目: * 判斷是否為閏年 Is_Leap * 計算一年中月份的天數(shù) Get_YM_day * 輸出日期 計算天數(shù) Get_YMD_Total *//** 判斷是否為閏年 */ bool Is_Leap(int year) {return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0); }/** 計算一年中月份的天數(shù) Get_YM_day */ int Get_YM_Day(int year, int month) {// 靜態(tài)常性數(shù)組 static 只初始化一次 const 且只可讀不可寫static const int days[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// 0 1 2 3 4 5 6 7 8 9 10 11 12if (month == 2 && Is_Leap(year)){month = 0;}return days[month]; }/** 輸出日期 計算天數(shù) Get_YMD_Total */ int Get_YMD_Total(int year, int month, int day) {// 循環(huán)疊加用戶輸入月份的前幾個月的天數(shù) + 當(dāng)月的天數(shù) 如:用戶輸入 6 月 8 日 計算前 5 個月的天數(shù) + 8 天int sum = 0;if (year < 1) return YEAR_ERROR; // 輸入的年份出錯if (month < 1 || month > 12) return MONTH_ERROR; // 輸入的月份出錯if (day < 1 || day > Get_YM_Day(year, month)) return DAY_ERROR; // 輸入的天數(shù)出錯for (int i = 1; i < month; ++i){sum += Get_YM_Day(year, i); }sum += day;return sum; }/** 主函數(shù) */ int main() {int year, month, day, day1;char ch = '\0';do {printf("Input (year month day): \n");scanf_s("%d %d %d", &year, &month, &day);// 計算一年中月份的天數(shù)day1 = Get_YM_Day(year, month);// 輸出日期 計算天數(shù)int total = Get_YMD_Total(year, month, day);switch (total){case YEAR_ERROR:printf("year input error! \n");break;case MONTH_ERROR:printf("month input error! \n");break;case DAY_ERROR:printf("day input error! \n");break;default:if (Is_Leap(year)) {printf("%d年是閏年!\n", year);}else{printf("%d年不是閏年!\n", year);}printf("%d年的%d月==>%d天\n", year, month, day1);printf("%d年%d月%d日==>%d天\n", year, month, day, total);break;}printf("是否繼續(xù)(y/n)... \n");// 清除緩沖區(qū) // fflush(stdin); 在 vs 2015 年之后就失效了// 清除緩沖區(qū)的作用:鍵盤錄入時會有 ‘\n’ 為了避免 ‘\n’的干擾需清除緩沖區(qū)rewind(stdin); ch = getchar();} while (ch == 'Y' || ch == 'y');return 0; }運行結(jié)果
5. 項目總結(jié)
???? 在做項目之前,先分析項目需求,設(shè)置完成項目實例所需要的各模塊函數(shù);思考各模塊函數(shù)代碼實現(xiàn)的算法思想,通過算法思想一步步完成各模塊函數(shù)代碼的實現(xiàn)并進行優(yōu)化。通過調(diào)試逐步完善代碼,最終實現(xiàn)項目實例代碼的編寫。
總結(jié)
以上是生活随笔為你收集整理的【C 语言之项目实战】判断闰年及计算天数(详细版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dos命令行连接操作ORACLE数据库
- 下一篇: 华为手机备忘录怎么转移到OPPO新手机