❶ 輸入年份月份 輸出顯示該月份的天數 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 <stdio.h>
main()
{
int i,year,month[12] ={31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d",&year);
if((year%4==0)&&(year%100!=0)||(year%400==0))month[1]++;year=0;
for (i=0;i<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語句寫也可以。
編程盡量自己寫,別人寫出來雖然你能看懂,但是要你自己寫還是寫不出的。