用c语言求出某一年的某一月有多少天,运用了switch语句和什么循环 是for循环还是while循环???

如题所述

什么循环都不需要,直接使用方法,我给你一个我写的万年历代码。你参考一下:
#include<stdio.h>
// 存储输入的年份
int year;
// 存储输入的月份
int mouth;
// 存储统计从1900年1月1日至今的天数
int totalDay;
//求当月第一天
int temp;
//存储这个月的第一天
int first;
//定义循环变量
int i,j;
// 判断输入的年份是平年还是闰年,如果返回的值是1,表示该年份是闰年,如果返回值是0,表示该年份是平年
int JudgeYear(int input) {
if ((input % 4 == 0 && input % 100 != 0) || input % 400 == 0) {
return 1;
}
return 0;
}
// 对输入的月份进行判断,也就是算出该月有多少天
int Inputday(int input,int inputMonth) {
if (inputMonth == 1 || inputMonth == 3 || inputMonth == 5
|| inputMonth == 7 || inputMonth == 8 || inputMonth == 10
|| inputMonth == 12) {
return 31;
} else if (inputMonth == 2) {
if (JudgeYear(input) == 1) {
return 29;
} else {
return 28;
}
} else {
return 30;
}
}
// 统计从1900年开始到输入年份的前一年一共有多少天,如果是闰年就是366天,如果是平年就是365天
int addDay(int input) {
if (JudgeYear(input) == 1) {
return 366;
} else {
return 365;
}
}
/**以下输入年份和月份两个方法在输入的时候如果不是整数,
就会出现异常,所以使用try..catch语句进行异常处理*/
//输入年份的方法
void inputYear() {
printf("请输入年份:");
scanf("%d",&year);
if (year < 1900) {
printf("年份必须大于1900年!\n");
printf("\n");
inputYear();
}
}
//输入月份的方法
void inputMonth() {
printf("请输入月份:");
scanf("%d",&mouth);
if (mouth > 12 || mouth < 1) {
printf("你输入的月份有误!\n");
printf("\n");
inputMonth();
}
}

// 输出结果的方法
void output() {
inputYear();
inputMonth();
for (i = 1900; i < year; i++) {
totalDay += addDay(i);
}
for (j = 1; j <= mouth; j++) {
if(j<mouth){
totalDay += Inputday(year,j);
}
}
temp = totalDay % 7 + 1;
if (temp==7) {
first=0;
}else {
first = temp;
}
printf("\n");
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
for (j = 0; j < first; j++) {
printf("\t");
}
for (i = 1; i <= Inputday(year,mouth); i++) {
printf(" %d\t",i);
if ((totalDay+i-1)%7==5) {
printf("\n");
}
}
}
void main() {
output();
printf("\n");
}
其中JudgeYear这个方法是判断平年和闰年的,Inputday这个方法就是判断你输入的月份有多少天追问

求出某一年的某一月有多少天是不是只用了switch语句??

温馨提示:答案为网友推荐,仅供参考
相似回答