用c写的简单的日历(学习模块划分)

简单日历

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

1512048-20190724215752374-1728607177.png

1512048-20190724215744450-651283592.png

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

转载于:https://www.cnblogs.com/starrys/p/11236077.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言课程设计是计算机专业的基础课程之一,它可以帮助学生掌握C语言的基本语法和程序设计思维。在课程设计中,通常会对整个设计过程进行模块划分,以便学生能够将问题分解为多个较小的模块,更加有条理地进行设计和编码。 在C语言课程设计的模块划分中,一般包括以下几个重要的模块: 1. 系统需求分析:这一模块是课程设计的第一步,学生需要对系统的需求进行深入的分析和理解。包括功能需求、性能需求、接口需求等,以确定整个系统的设计目标和功能。 2. 系统设计:在系统设计模块中,学生需要根据需求分析的结果,设计整个系统的框架和模块结构。包括确定系统的入口、输出、数据组织方式等,并绘制系统结构图或流程图。 3. 模块设计:模块设计是课程设计中的核心部分。学生需要将系统的功能进行细化,划分为多个相对独立的模块。每个模块包括输入、输出、具体实现逻辑等。学生需要定义每个模块功能、数据结构和接口规范。 4. 模块编码:在模块编码模块中,学生需要将模块设计的结果转化为具体的C语言代码。学生需要按照模块设计的规范进行编码,注重代码的可读性和可维护性。 5. 系统测试和调试:在系统测试和调试模块中,学生需要对整个系统进行测试,并对出现的问题进行调试和修复。可以采用黑盒测试和白盒测试等方法,确保整个系统的功能和性能的正确和正常。 总的来说,C语言课程设计模块划分可以帮助学生全面理解和掌握C语言的设计和实现方法。通过模块化的设计思想,学生可以更好地组织和管理课程设计的过程,提高代码的质量和可维护性。同时,这也为学生今后进行更复杂的软件设计和开发奠定了良好的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值