c语言简单日历代码
#include <iostream>
#include<windows.h>
using namespace std;
void fun2(int firstday, int flag);//輸出日歷
int fun1(int year,int *flag);//計算所輸年份的第一天為星期幾并且判斷是否為閏年(閏年返回0,非閏年返回1)
int main()
{int year,firstday,flag;cout << "輸入年份:";cin >> year;firstday = fun1(year, &flag);fun2(firstday, flag);
}
int fun1(int year,int *flag)
{int sum = 0;for (int i = 1; i < year; i++)//計算有多少閏年{if (i % 400 == 0 || i % 4 == 0 && i % 100 != 0){sum++;}}if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//判斷這年是否為閏年{*flag = 0;}else*flag = 1;return ((year - sum) * 365 + sum * 366) % 7;
}
void fun2(int firstday, int flag)
{int sum = 0;//在每個月里記天數for (int a = 1; a <= 12; a++){cout << " " << a << "月" << endl;//輸出月份cout << "--------------------------" << endl;cout << "一 二 三 四 五 六 日" << endl;//輸出星期if (firstday == 0)//第一天為星期日前面應空多少格{for (int i = 1; i < 7; i++)cout << " ";}else for (int i = 1; i < firstday; i++)//第一天不為星期日前面應空多少格{cout << " ";}if (a == 1 || a == 3 || a == 5 || a == 7 || a == 8 || a == 10 || a == 12)//輸出大月{for (int j = 1; j <= 31; j++){printf("%-4d", j);sum++;if ((sum + firstday-1) % 7 == 0)cout << endl;}}else if (a == 4 || a == 6 || a == 9 || a == 11)//輸出小月{for (int j = 1; j <= 30; j++){printf("%-4d", j);sum++;if ((sum + firstday-1) % 7 == 0)cout << endl;}}else if (a == 2)//輸出二月{if(flag==1)//非閏年for (int j = 1; j <= 28; j++){printf("%-4d", j);sum++;if ((sum + firstday-1)%7 == 0)cout << endl;}else//閏年for (int j = 1; j <= 29; j++){printf("%-4d", j);sum++;if ((sum + firstday-1) % 7 == 0)cout << endl;}}firstday = (sum % 7 + firstday)%7;//判斷下一個月的一號是星期幾sum = 0;cout << endl;}
}
總結
- 上一篇: 修复桌面快捷方式箭头图标
- 下一篇: winhex恢复误GHOST系统造成的数