用c语言功能要求: (1)输入一个年份,输出是在屏幕上显示该年 的日历。假定输入的年份在1940-2040年之间。 (2)输入年月,输出该月的日历
时间: 2024-03-25 22:38:53 浏览: 29
好的,我可以为您提供相关的C语言代码。对于第一个要求,以下是代码:
```
#include <stdio.h>
int isLeapYear(int year){
return (year%4==0 && year%100!=0) || year%400==0; //判断是否是闰年
}
int getMonthDays(int year, int month){
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if(month==2 && isLeapYear(year)){
return 29; //如果是闰年二月份,天数为29
}
return days[month-1]; //其他月份直接返回天数
}
void printMonth(int year, int month){
printf(" %d年%d月\n", year, month); //打印年月
printf("日 一 二 三 四 五 六\n"); //打印星期
int firstDay = 1;
while(firstDay != getMonthDays(year, month)+1){ //循环打印每一天
for(int i=0;i<7;i++){
if(firstDay == 1 && i==0){
printf(" ");
}else if(firstDay == getMonthDays(year, month)+1){
break;
}else{
printf("%2d", firstDay);
}
firstDay++;
if(i!=6 && firstDay<=getMonthDays(year, month)+1){
printf(" ");
}
}
printf("\n");
}
}
void printYear(int year){
for(int i=1;i<=12;i++){
printMonth(year, i); //打印每个月
printf("\n");
}
}
int main(){
int year;
printf("请输入年份(1940-2040):");
scanf("%d", &year);
if(year<1940 || year>2040){
printf("年份输入错误!");
return 0;
}
printYear(year);
return 0;
}
```
对于第二个要求,以下是代码:
```
#include <stdio.h>
int isLeapYear(int year){
return (year%4==0 && year%100!=0) || year%400==0; //判断是否是闰年
}
int getMonthDays(int year, int month){
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if(month==2 && isLeapYear(year)){
return 29; //如果是闰年二月份,天数为29
}
return days[month-1]; //其他月份直接返回天数
}
void printMonth(int year, int month){
printf(" %d年%d月\n", year, month); //打印年月
printf("日 一 二 三 四 五 六\n"); //打印星期
int firstDay = 1;
while(firstDay != getMonthDays(year, month)+1){ //循环打印每一天
for(int i=0;i<7;i++){
if(firstDay == 1 && i==0){
for(int j=1;j<=6;j++){ //如果是第一天,需要先打印前面的空格
printf(" ");
}
printf("%2d", firstDay);
}else if(firstDay == getMonthDays(year, month)+1){
break;
}else{
printf("%2d", firstDay);
}
firstDay++;
if(i!=6 && firstDay<=getMonthDays(year, month)+1){
printf(" ");
}
}
printf("\n");
}
}
int main(){
int year, month;
printf("请输入年份(1940-2040)和月份(1-12):");
scanf("%d %d", &year, &month);
if(year<1940 || year>2040 || month<1 || month>12){
printf("年份或月份输入错误!");
return 0;
}
printMonth(year, month);
return 0;
}
```
希望对您有帮助!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)