简单日历

​ 主要目的是学习函数模块划分,成品大概是这样,加了一些花里胡哨的东西(/▽\)

分三个模块,主函数.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);
​```

用c写的简单的日历(学习模块划分)的更多相关文章

  1. 使用JAVA写一个简单的日历

    JAVA写一个简单的日历import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateF ...

  2. js写一个简单的日历

    思路:先写一个结构和样式,然后写本月的时间,之后计算上下月份的关系 <!DOCTYPE html> <html lang="en"> <head> ...

  3. 使用SSI框架写的简单Demo(查询模块)

    在网上看到好多个版本,自己有时间索性就写个Demo记录下整个框架的逻辑流程: 1.首先拷贝整个框架所需要的jar包到WEB-INF/lib包下(这个网上都可以搜到的) 2.配置文件的配置, 2.1.在 ...

  4. 【UI插件】简单的日历插件(下)—— 学习MVC思想

    前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么 ...

  5. laravel学习:php写一个简单的ioc服务管理容器

    php写一个简单的ioc服务管理容器 原创: 陈晨 CoderStory 2018-01-14 最近学习laravel框架,了解到laravel核心是一个大容器,这个容器负责几乎所有服务组件的实例化以 ...

  6. 用C#Winform写个简单的批量清空文件内容和删除文件的小工具

    用C#Winform写个简单的批量清空文件内容和删除文件的小工具 本文介绍这个简单得不能再简单的小项目.做这个项目,有以下目的. 1 当然是做个能用的工具 2 学习使用Github 关于用VS2013 ...

  7. 自己用js写的两个日历控件

    前一阵写了两个日历控件,做了简单的封装,发出来共朋友们参考. 第一个日历控件,条状的日历. (使用方法:调用initBarTime(id,evn),第一个参数是要渲染div的id,第二个参数是点击日期 ...

  8. (原创)如何使用boost.asio写一个简单的通信程序(一)

    boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...

  9. jstorm开发指南-写个简单的jstorm应用

    jstorm开发指南-写个简单的jstorm应用 发表于 2015-07-18   |   分类于 大数据   |   暂无评论 jstorm 是阿里巴巴开源的基于storm采用Java重写的一套分布 ...

随机推荐

  1. Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入

    模拟实际开发过程的调用过程XML版-Setter方式注入 源码获取github [TOC] 1.项目结构 2.jar包跟上个一样 3.重写set方法 UserServiceImpl.java 1234 ...

  2. Hive Functions

    函数的分类 内置函数 操作符 复杂对象 UDF函数 数学函数 类型转换函数 日期函数 条件函数 UDTF函数 常用UDTF函数 explode posexplode inline stack json ...

  3. 本地开启https服务

    ### ##自签名证书 ##配置Apache服务器SSL ##自己作为CA签发证书 ###这里是OpenSSL和HTTPS的介绍 OpenSSL HTTPS 开启HTTPS配置前提是已在Mac上搭建A ...

  4. CS229 Lesson 13 高斯混合模型

    课程视频地址:http://open.163.com/special/opencourse/machinelearning.html 课程主页:http://cs229.stanford.edu/ 更 ...

  5. 直播内容大面积偏轨:都是high点的错?

    当下的直播行业看似火爆,却是外强中干.直播平台数量的暴增.主播人数的飙升.直播内容同质化严重等问题,都在成为新的行业症结.而面对复杂的情况,不仅刚入行的小主播,就连爆红的大主播都感到寒冬的难熬.为了能 ...

  6. Spring Boot从入门到精通(五)多数据源配置实现及源码分析

    多数据源配置在项目软件中是比较常见的开发需求,Spring和Spring Boot中对此都有相应的解决方案可供大家参考.在Spring Boot中,如MyBatis.JdbcTemplate以及Jpa ...

  7. STL标准库中的容器

    容器:顾名思义,我的理解就是把同一种数据类型括起来,作为一捆.如vector<int> ,vector就是个容器,里面全是一个个的int型数据. 容器包括三大块: 顺序型容器: (1)ve ...

  8. 系统级编程(csapp)

    系统级编程漫游 系统级编程提供学生从用户级.程序员的视角认识处理器.网络和操作系统,通过对汇编器和汇编代码.程序性能评测和优化.内存组织层次.网络协议和操作以及并行编程的学习,理解底层计算机系统对应用 ...

  9. 使用移动自适应布局+easy mock实现移动界面的简单实现

    一.使用easy mock模拟数据 easy mock链接地址 二.自己写移动自适应布局 自己编写主要是利用rem进行宽度栅格布局: html { /* 相当于一个界面适配器,pc以及移动端都可以进行 ...

  10. js移动端滑倒顶部加载历史消息解决方案!

    最近做了一个语音直播聊天的项目,有一个功能是当没有直播时,进入房间可以查看历史消息,滑动到顶部加载之前的历史消息,我用jquery scroll事件,来判断是否滚动到顶部,问题来了: 首先触发请求事件 ...