挑战C语言
Posted Ho哥在世界充满爱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了挑战C语言相关的知识,希望对你有一定的参考价值。
判断年月
先确定好该年是否闰年再写程序
#include<stdio.h>
void main(){
int year;
printf("Please enter year:");
scanf("%d",&year);
if(year%4==0&&year>=0){
if(year%100==0){
if(year%400==0)
printf("%d is leap year\n",year);
else
printf("%d is not leap year\n",year);
}
else
printf("%d is leap year\n",year);
}
else
printf("%d is not leap year\n",year);
}
void main()
{
int year,month,leap;//整数数据类型
printf("Please enter year,month:");//输出
scanf("%d,%d",&year,&month);//输入“year,month”前后数字中间加逗号
//leap year,leap=1代表闰年,leap=0代表平年(假设,1真,0假)
if(year%4==0&&year>=0)//判断数字是否被4整除以及大于0
{
if(year%100==0)//如果数字能被4整除并且大于0,再判断数字是否被100整除
{
if(year%400==0)//如果数字能被100整除,再判断是否被400整除
leap=1;//如果数字能被400整除(能被4整除,能被100整除),那数字1将赋值到变量leap
else
leap=0;//如果数字不能被400整除,能被4整除,能被100整除,那数字0将赋值到变量leap
}
else
leap=1;//如果数字不能被100整除并且能被4整除,那数字1将赋值到变量leap
}
else
leap=0;//如果数字不能被4整除,那数字0将赋值到变量leap
//是否闰年
if(leap==1)//判断
printf("%d is leap year\n",year);//如果leap等于1,那么输出是闰年
else//否则
printf("%d is not leap year\n",year); //如果leap不等于1,那么输出不是闰年
//spring:3,4,5;
//summer:6,7,8;
//autumn:9,10,11;
//winter:12,1,2;
switch(month)//判断选择,该月属于哪一季节
{
case 1: //month=1时,空的,下一句执行
case 2: printf("The season is winter\n");break; //month=2时输出“The season is spring”,break不可省略
//同理
case 3:
case 4:
case 5: printf("The season is spring\n");break;
case 6:
case 7:
case 8: printf("The season is summer\n");break;
case 9:
case 10:
case 11: printf("The season is autumn\n");break;
case 12:printf("The season is winter\n");break;
default:printf("The month is not exist\n");//default,不成立的时候输出“The month is not exist”
}
//31days:1,3,5,7,8,10,12;
//30days:4,6,9,11;
//leap year 29days:2;闰年中2月有29天
//not leap year 28days:2;平年中2月有28天
switch(month)//再判断选择,判断该月的天数
{
case 1:printf("The number of days of this month 31\n");break;//一月有31天
case 2:{ //2月比较特殊
if(leap==1)//判断
printf("The number of days of this month 29\n");//该年是闰年,输出2月有29天
else
printf("The number of days of this month 28\n");//该年是平年,输出是2月有28天
};break;
case 3:printf("The number of days of this month 31\n");break;
case 4:printf("The number of days of this month 30\n");break;
case 5:printf("The number of days of this month 31\n");break;
case 6:printf("The number of days of this month 30\n");break;
case 7:printf("The number of days of this month 31\n");break;
case 8:printf("The number of days of this month 31\n");break;
case 9:printf("The number of days of this month 30\n");break;
case 10:printf("The number of days of this month 31\n");break;
case 11:printf("The number of days of this month 30\n");break;
case 12:printf("The number of days of this month 31\n");break;
default:printf("The month is not exist\n");//不符合以上条件的时候就输出该季节不存在
}
}
以上是关于挑战C语言的主要内容,如果未能解决你的问题,请参考以下文章
我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段
我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段