用c写的简单的日历(学习模块划分)
简单日历
主要目的是学习函数模块划分,成品大概是这样,加了一些花里胡哨的东西(/▽\)
分三个模块,主函数.c 显示.c 计算.c 与.h 文件
有两种实现方式,区别在于是否以数组在模块之间传递。
第一种-用数组进行保存日历页
1.主函数.c
输入信息并控制
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "calendar1.h"
#include <stdbool.h>
#include <string.h>
#define ON 1
#define OFF 0
int main( )
{
int year = 2019;
int month = 7;
char op[10] = { 0 };
int button = 1;
do
{
calen_cal(year,month);
calen_display(year, month,button);
scanf(" %s", op);
if( !strcmp(op, "ON") ) {
button = 1; continue;
}
if( !strcmp(op, "OFF") ) {
button = 0; continue;
}
if( *op == 'U' || *op == 'u' )
{
if( month == 1 )
{
--year;
month = 12;
}
else --month;
continue;
}
if( *op == 'D' || *op == 'd' )
{
if( month == 12 )
{
++year;
month = 1;
}
else ++month;
continue;
}
if( *op == 'R' || *op == 'r' )break;
printf("请按以下格式输入:2019-7\n");
int temY, temM;
scanf("%d-%d", &temY, &temM);
if( is_right(temY, temM) )
{
year = temY; month = temM;
}
} while( true );
}
2.计算函数相关.c
计算( •̀ ω •́ )✧
```c
#include <stdio.h>
#include "calendar.h"
int months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int is_leepyear(int year)//是否为闰年
{
return ( year % 4 == 0 && year % 100 != 0 || year % 400 == 0 );
}
int is_right(int year, int mon) //输入的年份或月份是否正确
{
return ( year > 1900 && mon <= 12 && mon >= 1 );
}
int first_day_index(int year, int mon)//返回该月第一天的下标 0-6
{
if( is_leepyear(year) )months[1] = 29;
else months[1] = 28;
int days = 0;
for( int i = mon - 1; i > 0; --i )
{
days += months[i - 1];
}
for( int i = year - 1; i >= 1900; --i )
{
if( i % 4 == 0 && i % 100 != 0 || i % 400 == 0 ) days += 366;
else days += 365;
}
return days % 7;
}
int Last_days(int year, int mon)// 返回上个月最后一天的天数,用来排日历中的上一月信息
{
if( mon == 1 )return 31;
else return months[mon - 2];
}
void calen_cal(int *calen,int year,int mon,int index)//将排好的信息放到数组中
{
int Last_day = Last_days(year, mon);
int count = 0;
//排上个月
for( int i = 1; i <= index; ++i, ++count )
{
calen[count]=Last_day - index + i;
}
//排本月
for( int i = 1; i <= months[mon - 1]; ++i, ++count )
{
calen[count] = i;
}
//排下月
for( int i = 1; count < 42; ++i, ++count )
{
calen[count] = i;
}
}
```
3. 打印显示.c
花里胡哨的显示
```c
#include <stdio.h>
#include <Windows.h>
#include "calendar.h"
void calen_print(int *calen, int button, int index)
{
//打印上一月
int i = 0;
for( i = 1; i <= index; ++i )
{
if( button == 1 )printf("%5d", calen[i - 1]);
else printf(" ");
if( i % 7 == 0 )putchar(10);
}
//打印本月
for( ; calen[i - 1] != 1; ++i )
{
printf("%5d", calen[i - 1]);
if( i % 7 == 0 )putchar(10);
}
//打印下一月
for( ; i <= 42; ++i )
{
if( button == 1 )printf("%5d", calen[i - 1]);
else printf(" ");
if( i % 7 == 0 )putchar(10);
}
}
void calen_ui(int *calen, int button, int index,int year,int mon)
{
system("cls");
//这块是更改控制台颜色
srand(time(NULL));
int rand_color;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
printf("%d-%d U/D\n", year, mon);
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
printf("Mon Tue Wed Thu Fri Sat Sun\n");
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
printf("--------------------------------------\n");
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
calen_print(calen, button,index);
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
printf("--------------------------------------\n");
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
printf("U/u 向上翻页,D/d向下翻页,R/r退出,ON/OFF是否显示其他天数,输入其他进入日期跳转模式\n");
rand_color = rand( ) % 14;
SetConsoleTextAttribute(hConsole, rand_color);
}
```
4. 头文件.h
头文件声明
```c
void calen_ui(int *calen, int button, int index, int year, int mon);
void calen_cal(int *calen, int year, int mon,int index);
int first_day_index(year, mon);
```