用c语言实现打印日历

只要输入年份就能打印出相应的日历

#ifndef MAIN_H
#define MAIN_H

#include "stdio.h"
#include "math.h"
#include "stdlib.h"

//获取某一个月的最大天数 
int monthday(int, int);

//判断闰年 ,是返回1,不是返回0 
int isleapyear(int);

#endif 


int main()
{
    int year, month, days, weekday;
    int i, d;
    while (1)
    {
        printf("please input the year:\n");
        scanf_s("%d", &year);
        days = year - 1 + (year - 1) / 
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是用C语言实现单月日历的代码示例,其中使用了数组来存储每个月中每一天的日期: ```c #include <stdio.h> int main() { int month, year, days, start_day; int calendar[6][7] = {0}; // 6行7列的二维数组,用于存储日历 printf("Enter month and year (mm yyyy): "); scanf("%d %d", &month, &year); // 计算该月有多少天 if (month == 2) { if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { days = 29; } else { days = 28; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { days = 30; } else { days = 31; } // 计算该月第一天是星期几 start_day = (year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7; for (int i = 1; i < month; i++) { if (i == 2) { if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { start_day += 29; } else { start_day += 28; } } else if (i == 4 || i == 6 || i == 9 || i == 11) { start_day += 30; } else { start_day += 31; } } start_day %= 7; // 将日期填入日历数组中 int day = 1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < start_day) { continue; } if (day <= days) { calendar[i][j] = day++; } } } // 显示日历 printf(" Sun Mon Tue Wed Thu Fri Sat\n"); for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (calendar[i][j] != 0) { printf("%4d", calendar[i][j]); } else { printf(" "); } } printf("\n"); } return 0; } ``` 该代码首先通过用户输入的月份和年份计算出该月有多少天,以及该月第一天是星期几。然后,使用一个6行7列的二维数组来存储该月的日历,日期填入数组中后,再使用循环将日历打印出来。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值