當前位置:首頁 » 編程語言 » c語言怎樣寫年月日代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言怎樣寫年月日代碼

發布時間: 2022-04-28 02:46:44

A. c語言中年月日之類的代碼

int year = 1230;
int month = 4;
int day = 46;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0&&year%100!=0||year%400==0)
a[2]++;
if(month>12||month<1||day<1||day>a[month])
{
printf("date error!\n");
return 0;
}

B. c語言中如何輸入年月日

就這么寫的,沒調試,你自己調一下好吧

#include <stdio.h>
void main()
{
int year,month,date;
scanf("%d-%d-%d",&year,&month,&date);
printf("%d-%d-%d",year,month,date);

if(year<0||year>3000)
printf("Error!");
if(month>12||month<1)
printf("Error!");
switch(month)
{
case 1: if(date>31||date<1) printf("Error!");break;
case 2: if((year%4==0)&&(year%100!=0)||(year%400==0))
{
if(date>29||date<1) printf("Error!");
}
else
if(date>28||date<1) printf("Error!");
break;
case 3: if(date>31||date<1) printf("Error!");break;
case 4: if(date>30||date<1) printf("Error!");break;
case 5: if(date>31||date<1) printf("Error!");break;
case 6: if(date>30||date<1) printf("Error!");break;
case 7: if(date>31||date<1) printf("Error!");break;
case 8: if(date>31||date<1) printf("Error!");break;
case 9: if(date>30||date<1) printf("Error!");break;
case 10: if(date>31||date<1) printf("Error!");break;
case 11: if(date>30||date<1) printf("Error!");break;
case 12: if(date>31||date<1) printf("Error!");break;
}
}

C. 用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;

}

D. C語言數組實現輸入年月日

根據題意可得代碼:

#include<stdio.h>
intisLeapYear(intyear)
{
if((year%4==0&&year%100!=0)||year%400==0)return1;
return0;
}

intmain()
{
intdays_of_month[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
intyear,month,date,days=0,i;
scanf("%d%d%d",&year,&month,&date);
if(isLeapYear(year)){
for(i=0;i<month-1;++i){
days+=days_of_month[1][i];
}
days+=date;
}
else{
for(i=0;i<month-1;++i){
days+=days_of_month[0][i];
}
days+=date;
}
printf("%d ",days);
return0;
}

E. C語言中如何把月日年輸出為年月日

這要看原來的程序中,月日年是怎麼組織的?如果分別存放在三個變數中,那麼只要把這三個變數的輸出順序交換一下就可以了。
例如,
int y,m,dm,d;
printf("請輸入月 日 年:");
scanf("%d%d%d",&m,&d,&y);
printf("%d年%d月%d日",y,m,d);

F. C語言編寫程序實現:從鍵盤輸入一個年月日,要求出該天是該年的第幾天。(注意判斷該年是否是閏年。)

#include<stdio.h>
int main(void)
{
int month,year,day,sum,i=0;
printf("輸入年月日:\n");
scanf("%d %d %d",&year,&month,&day);
switch(month)
{
case 12: i=i+30;
case 11: i=i+31;
case 10: i=i+30;
case 9: i=i+31;
case 8: i=i+31;
case 7: i=i+30;
case 6: i=i+31;
case 5: i=i+30;
case 4: i=i+31;
case 3: i=i+28;
case 2: i=i+31;
case 1: ;
default:
;

}
sum=i+day;
if(((year%4==0&&year%100!=0)||(year%400==0))&&(month>2))
sum=sum+1;
printf("這一天是 %d 年 第 %d 天\n",year,sum);

}

G. C語言輸入年份和天數輸出對應的年月日

C語言輸入年份和天數輸出對應的年月日的源代碼如下:

#include<iostream>

intday(int&year,int&month);

intmain()

{

intyear{};

intmonth{};

std::cout<<"請輸入年和月(空格隔開):";

std::cin>>year>>month;

std::cout<<"該月天數:"<<day(year,month)<<' ';

return0;

}

(7)c語言怎樣寫年月日代碼擴展閱讀

1、C++ 標准庫沒有提供所謂的日期類型。C++ 繼承了 C 語言用於日期和時間操作的結構和函數。為了使用日期和時間相關的函數和結構,需要在 C++ 程序中引用 <ctime> 頭文件。

2、有四個與時間相關的類型:clock_t、time_t、size_t和tm。類型 clock_t、size_t 和 time_t 能夠把系統時間和日期表示為某種整數。

H. c語言怎麼編寫要求輸入當前年月日,輸入生日年月日,然後計算多少歲(要注意平年二月跟閏年二月的天數

#include<stadio.h>
main()
{ int year,year1,month,month1,day,day1,age;
printf("請輸入您的生日:");
scanf("%d %d %d",&year,&month,&day);
printf("請輸入當前日期:");
scanf("%d %d %d",&year1,&month1,&day1);
if(year1==year)
age=0;
else
{age=year1-year;
if(month1<month||(month1==month&&day1<day))
age=age-1;
}
printf("您的年齡是:%d",age);
}
大概就是這個樣子,我沒有調試,可能有錯誤,但應該都是小問題,你自己調試一下,覺得哪兒不妥,可以自己改動。

I. 編寫一個c語言程序,輸入一個年月日,輸出這個日期是這一年的第幾天。(考慮閏年)

#include <stdio.h>

void main()

{

int year=0,month=0,day=0,i=0,sum=0;

while(1)

{

printf("請輸入年份:");

scanf("%d",&year);

printf("請輸入月份:");

scanf("%d",&month);

printf("請輸入日期:");

scanf("%d",&day);

i=0;//i變數是為了控制月和日的格式是否輸入正確;

switch(month)//判斷每個月的日期是否輸入正確;

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

if(day>0&&day<=31)

{

i=1;

}

break;

case 4:

case 6:

case 9:

case 11:

if(day>0&&day<=30)

{

i=1;

}

break;

case 2://2月閏年可以輸入29日;

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

{

if(day>0&&day<=29)

{

i=1;

}

}

else

{

if(day>0&&day<=28)

{

i=1;

}

}

break;

default://如果輸入不是1-12月份,那麼月份輸入有誤;

i=0;

break;

}

if(i)//如果i變數為1,那麼輸入正確,否則重新輸入;

{

break;

}

else

{

printf("請輸入正確的年月日!!! ");

}

}

i=0;

switch(month)//把每個月的日期累加;

{

case 12:

i=i+30;

case 11:

i=i+31;

case 10:

i=i+30;

case 9:

i=i+31;

case 8:

i=i+31;

case 7:

i=i+30;

case 6:

i=i+31;

case 5:

i=i+30;

case 4:

i=i+31;

case 3:

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

{

i=i+29;

}

else

{

i=i+28;

}

case 2:

i=i+31;

case 1:

;

}

sum=i+day;

printf("%d年%d月%d日是%d年的第%d天 !",year,month,day,year,sum);

}

拓展資料

C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。

盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。