当前位置:首页 » 编程语言 » 输入年份和天数c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

输入年份和天数c语言

发布时间: 2023-08-20 15:05:13

❶ 输入年份月份 输出显示该月份的天数 c语言编写

scanf("%d%d",year,month);错了
应该是:
scanf("%d%d",&year,&month);
还有啊:case后面有空格的,是case
1,不是case1。
switch...case在执行时具有贯穿性,会从第一个匹配的项开始一路向下执行,知道遇见break或语句块结束符,如
case
1:
//do
a
执行完do
a后继续执行下一条case(do
b)
case
2:
//do
b
执行完do
b后继续执行下一条case(do
c)
case
3:
//do
c
执行完do
c后遇到break;直接跳出switch...case语句块
break;
case
4:
...

❷ c语言输入一个天数,输出对应的年月天

#include<stdio.h>
intmain()
{
inti,n=0,year=0,month=0,day=0;
printf("请输入天数<输入-1退出测试>:");
scanf("%d",&n);
while(n!=-1)
{
while(n>=360)
{
n-=360;
year++;
}
while(n>=30)
{
n-=30;
month++;
}
while(n>0)
{
n--;
day++;
}
printf("%d年%d月%d日 ",year,month,day);
year=month=day=0;
printf("请输入天数<输入-1退出测试>:");
scanf("%d",&n);
}
return0;
}

❸ 用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。

源程序如下:

#include "pch.h"

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

using namespace std;

int main()

{

int s[2][13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31,

0,31,29,31,30,31,30,31,31,30,31,30,31, };

int year = 0;

int month = 0;

int day = 0;

int n, i, daytemp;

int flag = 0;

int nyear = 0, nmonth = 0, nday = 0;

printf("输入年月日 ");

scanf("%d%d%d", &year, &month, &day);

//printf("输入天数 ");

//scanf("%d",&n);

n = 1;

if (year < 0 || month < 0 || month>12 || day < 1 || n < 0)

{

printf("输入数据错误 ");

return 1;

}

daytemp = day + n;//累加天数

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//闰年置1

flag = 1;

if (day > s[flag][month])

{

printf("输入日期与年月不符. ");

return 1;

}

if (daytemp <= s[flag][month])//当前日期加天数在本月

{

nyear = year;

nmonth = month;

nday = daytemp;

}

else

{

i = month;

nyear = year;

while (daytemp > s[flag][i])//循环递减,直到当前日期加天数在本月

{

daytemp = daytemp - s[flag][i];

i++;

if (i > 12)//超过一年,年累加核姿

{

nyear++;

if ((nyear % 4 == 0 && nyear % 100 != 0) || nyear % 400 == 0)

flag = 1;

else

flag = 0;

i = i - 12;

}

}

nmonth = i;

nday = daytemp;

}

printf("%d年%d月%d日第%d天后是: ", year, month, day, n);

printf("%d年%d月%d日 ", nyear, nmonth, nday);

return 0;

}

程序运行结果如下:



(3)输入年份和天数c语言扩展阅读:

其他实现方式:

int monthsize(int year, int month) {

int days;

if (month == 2) {

正旦if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

return 29;

return 28;

}

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:days = 31; break;

case 4:

case 6:

case 9:

case 11: days = 30; break;

}

return days;

}

int main()

int year, month, day, days;

printf("年 月 日:"改清绝);

while (scanf("%d%d%d", &year, &month, &day) == 3) {

days = monthsize(year, month);

if (days == day) {

if (month == 12) {

++year;

month = 1;

day = 1;

}

else ++month;

}

else ++day;

printf("第二天是:%d/%02d/%02d ", year, month, day);

printf("年 月 日(q to quit):");

}

return 0;

}

❹ C语言输入年份月份,输出天数。

#include<stdio.h>

main()
{
intyear,day,d,month,leap;
inti;
intMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("输入年份: ");
scanf("%d",&year);
printf("输入月份: ");
scanf("%d",&month);
if((year%4!=0)||((year%100==0)&&(year%400!=0)))
Month[2]=29;printf("%d年%d月有%d天",year,month,Month[month]);
}

❺ C语言:输入年份和天数,输出对应的年、月、日

下面是这段代码,配合注释体会一下吧

#include <stdio.h>

void MonthDay(int year,int yearDay,int *pMonth,int *pDay);

int main()
{
int year,yearDay,month,day;
scanf("%d%d",&year,&yearDay);//输入年份和天数
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d",year,month,day);
return 0;
}

void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};//这个数组保存每月的天数
if(year%4==0&&year%100!=0||year%400==0)//判断是否为闰年
days[1]=29;//闰年的二月有29天
*pMonth=1;//从一月开始看
while(yearDay>days[*pMonth-1])//如果哪一天超出这个月
{
yearDay-=days[*pMonth-1];//求出超出此月的天数
(*pMonth)++;//继续考虑下一月
}
*pDay=yearDay;
}

❻ c语言;输入一个年份,输出该年各月份天数以及总天数。

#include &lt;stdio.h&gt;
main()
{
int i,year,month[12] ={31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d",&amp;year);
if((year%4==0)&amp;&amp;(year%100!=0)||(year%400==0))month[1]++;year=0;
for (i=0;i&lt;12;i++)
printf("month[%d]:day[%d] ",i+1,month[i]),year+=month[i];
printf("year:[%d]days ",year);
}

❼ 用C语言编译程序:输入制定年月的月份天数

c语言忘记了,给你一个大概的思路
首先输入年份,scanf,然后判断是否闰年if
year/400,如果是闰年的话
下面接一个switch
,1,3,5,7,8,10
12输出31天,2,4,6,9,11是30天,2月28天
或者你用if语句写也可以。
编程尽量自己写,别人写出来虽然你能看懂,但是要你自己写还是写不出的。