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

c語言顯示年月日

發布時間: 2022-06-24 07:25:48

A. c語言輸入一個年月日,要求輸出這一天是該年的第幾天

第1個:i<=12
第2個:break;
第3個:d=30;break;
第4個: (year%4!=0) || (year%100==0&&year%400!=0)
第5個:days

B. C語言年歷顯示程序設計

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char* month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nev","Dec"};
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

int j_leapyear(int year) //定義函數檢測輸入年份是否為閏年
{
if((/*year%400==0*/year%4==0&&year%100!=0)||(year%400==0))
return (1);
// else if(year%4==0&&year%100!=0)
// return (1);
else return 0;
}

int month_day(int year,int month) //檢測該年月份有多少天
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(j_leapyear(year)&&month==2)
return 29;
else return (mon_day[month-1]);
}

int j_week(int year,int month,int day) //檢測該天是星期幾
{
int d=0,i; //d表示該天在該年份的累計天數
int w;
/* int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(j_leapyear(year)==1)
month_day[1]=29;
for(i=0;i<month;i++)
d+=month_day[i];
d=d+day;*/
for(i=1;i<month;i++)
d+=month_day(year,i);
d+=day;
w=(year-1)+(int)(year-1)/4-(int)(year-1)/100+(int)(year-1)/400+d;//網上公式
return ((int)w%7);
}

int allyear(int year) //輸出全年日歷
{
int i,j,b;
printf("\n%d 日歷",year);
for(i=1;i<=12;i++)
{
printf("\n\n\t%s\n",month[i-1]);
// printf(" 7 1 2 3 4 5 6 \n");
printf(" S M T W T F S \n");
b=j_week(year,i,1);
for(j=1;j<=month_day(year,i)+b;j++)
{
if(j<=b)
printf(" ");
else if(j-b<10)
printf("%2d ",j-b);
else printf("%2d ",j-b);
//else printf("%2d ",j-b);
if(j%7==0)
putchar('\n');
}
putchar('\n');
}
return 0;
}
void month_print(int year,int month) //輸入一個年月,輸出這個月的日歷
{

int i,c,d;
c=month_day(year,month);
printf("\n\n %d年%d月日歷\n",year,month);

printf(" S M T W T F S \n");
d=j_week(year,month,1);
for(i=1;i<=c+d;i++)
{
if(i<=d)
printf(" ");
else printf("%2d ",i-d);
if(i%7==0)
putchar('\n');
}
putchar('\n');
}

int jisuan_day(int year,int month,int day,int x,int y,int z) //輸入年月日,輸出距x年y月z日有多少天,星期幾,以及是否是公歷節日
{
int i,a,b,c=0,d=0,days=0,m=0,n=0,t;
a=year>=x?year:x;
b=year<x?year:x;
if(a>b)
{
for(i=b+1;i<a;i++)
c+=j_leapyear(i);
}
else c=0;
if(year!=x)
d=a-b-1;
else d=0;
m=365*d+c;
if(year<x)
{
for(i=month;i<=12;i++)
n+=month_day(year,i);
days=m+n+z-day;
}
else if(year>x)
{
for(i=y;i<=12;i++)
n+=month_day(x,i);
for(i=1;i<month;i++)
n+=month_day(year,i);
days=m+n+day-z;
}
else
{
for(i=month>=y?y:month;i<(month>=y?month:y);i++)
n+=month_day(x,i);
days=abs(day-z)+n; //輸出距離y月z日有多少天
}
printf("這天離%d年%d月%d號有%d天\n",x,y,z,days);
t=j_week(year,month,day);
if(t==0) //輸出星期幾
printf("這天星期日");
else if(t==1)
printf("這天星期一");
else if(t==2)
printf("這天星期二");
else if(t==3)
printf("這天星期三");
else if(t==4)
printf("這天星期四");
else if(t==5)
printf("這天星期五");
else if(t==6)
printf("這天星期六");
putchar('\n');
if(month==1&&day==1)
printf("這天是元旦節");
else if(month==2&&day==14)
printf("這天是情人節");
else if(month==3&&day==8)
printf("這天是婦女節");
else if(month==3&&day==12)
printf("這天是植樹節");
else if(month==4&&day==1)
printf("這天是愚人節");
else if(month==4&&day==4)
printf("這天是清明節");
else if(month==5&&day==1)
printf("這天是勞動節");
else if(month==6&&day==1)
printf("這天是兒童節");
else if(month==8&&day==1)
printf("這天是建軍節");
else if(month==9&&day==10)
printf("這天是教師節");
else if(month==10&&day==1)
printf("這天是國慶節");
else if(month==12&&day==25)
printf("這天是聖誕節");
else printf("這天不是特殊節日");
putchar('\n');
return days;
}

void fish_or_net(int year,int month,int day) //判斷是打魚還是曬網
{
int q;
int i,a,b,c=0,d=0,days=0,m=0,n=0;
a=year>=1990?year:1990;
b=year<1990?year:1990;
if(a>b)
{
for(i=b+1;i<a;i++)
c+=j_leapyear(i);
}
else c=0;
if(year!=1990)
d=a-b-1;
else d=0;
m=365*d+c;
if(year<1990)
{
for(i=month;i<=12;i++)
n+=month_day(year,i);
days=m+n+1-day;
}
else if(year>1990)
{
for(i=1;i<=12;i++)
n+=month_day(1990,i);
for(i=1;i<month;i++)
n+=month_day(year,i);
days=m+n+day-1;
}
else
{
for(i=month>=1?1:month;i<(month>=1?month:1);i++)
n+=month_day(1990,i);
days=abs(day-1)+n; //先確定這天離1990年1月1日有多少天
}

q=days%5+1;
if(q==1||q==2||q==3) //判斷打魚還是曬網
printf("漁人今天打魚");
else printf("漁人今天曬網");
putchar('\n');
}

/*int main() //這部分內容用來校檢
{
int j_leapyear(int year);
int month_day(int year,int month);
int j_week(int year,int month,int day);
int allyear(int year);
void month_print(int year,int month);
int jisuan_day(int year,int month,int day,int x,int y,int z);
void fish_or_net(int year,int month,int day);
int x,y,z,year,month,day;
// allyear(2015);
//month_print(2016,12);
printf("請輸入今天的日期(按年,月,日輸入,如2016,1,14表示2016年1月14日):");
scanf("%d,%d,%d",&x,&y,&z);
printf("\n請隨便輸入一個日期,格式同上:");
scanf("%d,%d,%d",&year,&month,&day);
jisuan_day(year,month,day,x,y,z);
// fish_or_net(x,y,z);
return 0;
} // 這部分內容是用來檢測上述函數是否出錯的
*/

int main()
{
int j_leapyear(int year);
int month_day(int year,int month);
int j_week(int year,int month,int day);
int allyear(int year);
void month_print(int year,int month);
int jisuan_day(int year,int month,int day,int x,int y,int z);
void fish_or_net(int year,int month,int day); //函數聲明
int option,year,month,day,x,y,z;
char ny;
system("color 1f");
while (1)
{
printf("\n\n\t 歡迎來到由...編寫的程序\n\n");
printf(" 請選擇您需要的服務,輸入編號回車結束\n");
printf(" 1.輸入一個年份,輸出該年的日歷\n");
printf(" 2.輸入年月,輸出這個月的日歷。\n");
printf(" 3.輸入年月日,輸出據今天還有多久,星期幾,是否是公歷節日。\n");
printf(" 4.某人自1990年1月1日開始打魚,「三天打魚,兩天曬網」,輸入一個1990年以後的日期,輸出他這一天是打魚還是曬網。\n");
printf(" 5.退出\n");
scanf("%d",&option);
switch(option) //switch分支結構
{
case 1:
while(1) //while循環,後面的1是常數,表示一直到break才結束循環
{
printf("請輸入一個年份:");
scanf("%d",&year);
allyear(year);
printf("你想繼續查詢日歷么?(y表示繼續,n表示結束)");
scanf("%c",&ny);
if(ny=='n')
break;
}
break;
case 2:
while (1)
{
printf("輸入年月:");
scanf("%d,%d",&year,&month);
month_print(year,month);
printf("你想繼續查詢日歷么?(y表示繼續,n表示結束):");
scanf("%c",&ny);
if(ny=='n')
break;
}
break;
case 3:
while (1)
{
printf("輸入年月日:");
scanf("%d,%d,%d",&year,&month,&day);
printf("請輸入今天的日期:");
scanf("%d,%d,%d",&x,&y,&z);
jisuan_day(year,month,day,x,y,z);
printf("你想繼續查詢么?(y表示繼續,n表示結束):");
scanf("%c",&ny);
if(ny=='n')
break;
}
break;
case 4:
while (1)
{
printf("輸入1990年1月1日以後的一個日期:");
scanf("%d,%d,%d",&year,&month,&day);
fish_or_net(year,month,day);
printf("你想繼續查詢漁人是打魚還是曬網么?(y表示繼續,n表示結束):");
scanf("%c",&ny);
if(ny=='n')
break;
}
break;
case 5:
while (1)
{
printf("確認么?y表示是的,n表示不是");
scanf("%c",&ny);
if(ny=='y')
exit(1); //表示退出程序
else if(ny=='n')
break;
}
break;
default:printf("對不起,暫時沒有這個服務");
break;
}
}
return 0;
}

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

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

#include<iostream>

intday(int&year,int&month);

intmain()

{

intyear{};

intmonth{};

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

std::cin>>year>>month;

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

return0;

}

(3)c語言顯示年月日擴展閱讀

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

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

D. c語言問題 求年月日

這里要改一下:
if(year%4==0&&year%100!=0||year%400==0) {
a[1]=29;
}else{
a[1]=28;
}
最後要加個return 0;

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

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

F. C語言中有沒有能顯示系統日期和時間的函數

C語言中讀取系統時間的函數為time(),其函數原型為:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。可以調用ctime()函數進行時間轉換輸出:
char * ctime(const time_t *timer);
將日歷時間轉換成本地時間,按年月日格式,進行輸出,如:

Wed Sep 23 08:43:03 2015

C語言還提供了將秒數轉換成相應的時間結構的函數:
struct tm * gmtime(const time_t *timer); //將日歷時間轉化為世界標准時間(即格林尼治時間)
struct tm * localtime(const time_t * timer); //將日歷時間轉化為本地時間

將通過time()函數返回的值,轉換成時間結構struct tm :
struct tm {
int tm_sec; /* 秒 – 取值區間為[0,59] */
int tm_min; /* 分 - 取值區間為[0,59] */
int tm_hour; /* 時 - 取值區間為[0,23] */
int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */
int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */
int tm_year; /* 年份,其值等於實際年份減去1900 */
int tm_wday; /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
int tm_yday; /* 從每年的1月1日開始的天數 – 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
};
編程者可以根據程序功能的情況,靈活的進行日期的讀取與輸出了。
例如:
#include<time.h>
main()
{
time_t timep;
struct tm *p;

time (&timep);
p=gmtime(&timep);

printf("%d\n",p->tm_sec); /*獲取當前秒*/
printf("%d\n",p->tm_min); /*獲取當前分*/
printf("%d\n",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d\n",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d\n",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/

printf("%d\n",p->tm_yday); /*從今年1月1日算起至今的天數,范圍為0-365*/
}

G. 改c語言程序,要顯示今天的年月日時分秒,,在線等啊 ,急,,全部分了都用上了

我以前倒是做過,不過程序還有點問題,調時間的時候容易過界,但正常走時候就正常了。開發環境用的IAR,單片機用f149,顯示用的1602的四線模式。
你自己看著改程序吧,其實我也是51上移植過去的。

悲劇了,帖不下了.我帖在我博客里了啊,自己去找吧.
主程序
__________________________分隔線____________________________________

#include <msp430x14x.h>
#include "ds1302.h"
#include "LCD1602x4_mps.h"

#define DS1302_SECOND 0x81 //時鍾晶元的寄存器位置,存放時間
#define DS1302_MINUTE 0x83
#define DS1302_HOUR 0x85
#define DS1302_WEEK 0x8b
#define DS1302_DAY 0x87
#define DS1302_MONTH 0x89
#define DS1302_YEAR 0x8d

unsigned char DateString[11],TimeString[9],week_value[2],TempBuffer[7]; //
char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year;
char done,count,temp,flag,up_flag,down_flag;
//unsigned int temp_value=0,temp_max=0;temp_min=0; //溫度值

void DateToStr(void) //將時間年,月,日,星期數據轉換成液晶顯示字元串,放到數組里DateString[]
{ unsigned char Year,Month,Day,Week;
Year=rtc_getyear();
Month=rtc_getmon();
Day=rtc_getdate();
Week=rtc_getday();

if(hide_year<2) //這里的if,else語句都是判斷位閃爍,<2顯示數據,>2就不顯示,輸出字元串為 2007/07/22
{
DateString[0] = '2';
DateString[1] = '0';
DateString[2] = Year/10 + '0';
DateString[3] = Year%10 + '0';
}
else
{
DateString[0] = ' ';
DateString[1] = ' ';
DateString[2] = ' ';
DateString[3] = ' ';
}
DateString[4] = '/';
if(hide_month<2)
{
DateString[5] = Month/10 + '0';
DateString[6] = Month%10 + '0';
}
else
{
DateString[5] = ' ';
DateString[6] = ' ';
}
DateString[7] = '/';
if(hide_day<2)
{
DateString[8] = Day/10 + '0';
DateString[9] = Day%10 + '0';
}
else
{
DateString[8] = ' ';
DateString[9] = ' ';
}
if(hide_week<2)
{
week_value[0] = Week%10 + '0'; //星期的數據另外放到 week_value[]數組里,跟年,月,日的分開存放,因為等一下要在最後顯示
}
else
{
week_value[0] = ' ';
}
week_value[1] = '\0';

DateString[10] = '\0'; //字元串末尾加 '\0' ,判斷結束字元
}

void TimeToStr(void) //將時,分,秒數據轉換成液晶顯示字元放到數組 TimeString[];
{ unsigned char Hour,Minute,Second;
Hour=rtc_gethour();
Minute=rtc_getmin();
Second=rtc_getsec();
if(hide_hour<2)
{
TimeString[0] = Hour/10 + '0';
TimeString[1] = Hour%10 + '0';
}
else
{
TimeString[0] = ' ';
TimeString[1] = ' ';
}
TimeString[2] = ':';
if(hide_min<2)
{
TimeString[3] = Minute/10 + '0';
TimeString[4] = Minute%10 + '0';
}
else
{
TimeString[3] = ' ';
TimeString[4] = ' ';
}
TimeString[5] = ':';
if(hide_sec<2)
{
TimeString[6] = Second/10 + '0';
TimeString[7] = Second%10 + '0';
}
else
{
TimeString[6] = ' ';
TimeString[7] = ' ';
}
DateString[8] = '\0';
}

void show_time() //液晶顯示程序
{
TimeToStr(); //時間數據轉換液晶字元
DateToStr(); //日期數據轉換液晶字元
// ReadTemp(); //開啟溫度採集程序
// temp_to_str(); //溫度數據轉換成液晶字元
LCD_PutStr(TempBuffer,25); //顯示溫度
LCD_PutStr(DateString,0); //顯示日期
LCD_PutStr(week_value,15); //顯示星期
LCD_PutStr(" Week",10); //在液晶上顯示 字母 week
LCD_PutStr(TimeString,16); //顯示時間
}
////////////////////////////////////////////////////////////////////////////

void outkey() //跳出調整模式,返回默認顯示
{ unsigned char Second;
if (!(P1IN&BIT0))
{
count=0;
hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;
Second=dataread(DS1302_SECOND);
datawrite(0x8e,0x00); //寫入允許
datawrite(0x80,Second&0x7f);
datawrite(0x8E,0x80); //禁止寫入
done=0;//temp_max=0;sund=1;
while(!(P1IN&BIT0));
delay_nms(2);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Upkey()//升序按鍵
{
if(!(P1IN&BIT1))
{
switch(count)
{case 1:
temp=dataread(DS1302_SECOND); //讀取秒數
temp=temp+1; //秒數加1
up_flag=1; //數據調整後更新標志
if((temp&0x7f)>0x59) //超過59秒,清零
temp=0;
break;
case 2:
temp=dataread(DS1302_MINUTE); //讀取分數
temp=temp+1; //分數加1
up_flag=1;
if(temp>0x59) //超過59分,清零
temp=0;
break;
case 3:
temp=dataread(DS1302_HOUR); //讀取小時數
temp=temp+1; //小時數加1
up_flag=1;
if(temp>0x23) //超過23小時,清零
temp=0;
break;
case 4:
temp=dataread(DS1302_WEEK); //讀取星期數
temp=temp+1; //星期數加1
up_flag=1;
if(temp>0x7)
temp=1;
break;
case 5:
temp=dataread(DS1302_DAY); //讀取日數
temp=temp+1; //日數加1
up_flag=1;
if(temp>0x31)
temp=1;
break;
case 6:
temp=dataread(DS1302_MONTH); //讀取月數
temp=temp+1; //月數加1
up_flag=1;
if(temp>0x12)
temp=1;
break;
case 7:
temp=dataread(DS1302_YEAR); //讀取年數
temp=temp+1; //年數加1
up_flag=1;
if(temp>0x99)
temp=0;
break;
default:break;
}

while(!(P1IN&BIT1));
delay_nms(2);
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Downkey()//降序按鍵
{
if(!(P1IN&BIT2))
{
switch(count)
{case 1:
temp=dataread(DS1302_SECOND); //讀取秒數
temp=temp-1; //秒數減1
down_flag=1; //數據調整後更新標志
if((temp&0x7f)>0x59) //小於0秒,返回59秒
temp=0x59;
break;
case 2:
temp=dataread(DS1302_MINUTE); //讀取分數
temp=temp-1; //分數減1
down_flag=1;
if(temp>0x59)
temp=0x59; //小於0秒,返回59秒
break;
case 3:
temp=dataread(DS1302_HOUR); //讀取小時數
temp=temp-1; //小時數減1
down_flag=1;
if(temp==0x00)
temp=0x23;
break;
case 4:
temp=dataread(DS1302_WEEK); //讀取星期數
temp=temp-1; //星期數減1
down_flag=1;
if(temp==0x00)
temp=0x07;
break;
case 5:
temp=dataread(DS1302_DAY); //讀取日數
temp=temp-1; //日數減1
down_flag=1;
if(temp==0x00)
temp=0x31;
break;
case 6:
temp=dataread(DS1302_MONTH); //讀取月數
temp=temp-1; //月數減1
down_flag=1;
if(temp==0x00)
temp=0x12;
break;
case 7:
temp=dataread(DS1302_YEAR); //讀取年數
temp=temp-1; //年數減1
down_flag=1;
if(temp>0x99)
temp=0x99;
break;
default:break;
}

while(!(P1IN&BIT2));
delay_nms(2);
}
}

void Setkey()//模式選擇按鍵
{
if(!(P1IN&BIT3))
{
count=count+1; //Setkey按一次,count就加1
done=1; //進入調整模式
while(!(P1IN&BIT3));
delay_nms(2);
}

}

void keydone()//按鍵功能執行
{ unsigned char Second;
if(flag==0) //關閉時鍾,停止計時
{ datawrite(0x8e,0x00); //寫入允許
temp=dataread(DS1302_SECOND);
datawrite(0x80,temp|0x80);
datawrite(0x8e,0x80); //禁止寫入
flag=1;
}
Setkey(); //掃描模式切換按鍵
switch(count)
{
case 1:do //count=2,調整秒
{
outkey(); //掃描跳出按鈕
Upkey(); //掃描加按鈕
Downkey(); //掃描減按鈕
if(up_flag==1||down_flag==1) //數據更新,重新寫入新的數據
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x80,temp|0x80); //寫入新的秒數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}

hide_sec++; //位閃計數
if(hide_sec>3)
hide_sec=0;
show_time(); //液晶顯示數據
}while(count==2);break;
case 2:do //count=3,調整分
{
hide_sec=0;
outkey();
Upkey();
Downkey();
if(temp>0x60)
temp=0;
if(up_flag==1||down_flag==1)
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x82,temp); //寫入新的分數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}
hide_min++;
if(hide_min>3)
hide_min=0;
show_time();
}while(count==3);break;
case 3:do //count=4,調整小時
{
hide_min=0;
outkey();
Upkey();
Downkey();
if(up_flag==1||down_flag==1)
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x84,temp); //寫入新的小時數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}
hide_hour++;
if(hide_hour>3)
hide_hour=0;
show_time();
}while(count==4);break;
case 4:do //count=5,調整星期
{
hide_hour=0;
outkey();
Upkey();
Downkey();
if(up_flag==1||down_flag==1)
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x8a,temp); //寫入新的星期數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}
hide_week++;
if(hide_week>3)
hide_week=0;
show_time();
}while(count==5);break;
case 5:do //count=6,調整日
{
hide_week=0;
outkey();
Upkey();
Downkey();
if(up_flag==1||down_flag==1)
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x86,temp); //寫入新的日數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}
hide_day++;
if(hide_day>3)
hide_day=0;
show_time();
}while(count==6);break;
case 6:do //count=7,調整月
{
hide_day=0;
outkey();
Upkey();
Downkey();
if(up_flag==1||down_flag==1)
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x88,temp); //寫入新的月數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}
hide_month++;
if(hide_month>3)
hide_month=0;
show_time();
}while(count==7);break;
case 7:do //count=8,調整年
{
hide_month=0;
outkey();
Upkey();
Downkey();
if(up_flag==1||down_flag==1)
{
datawrite(0x8e,0x00); //寫入允許
datawrite(0x8c,temp); //寫入新的年數
datawrite(0x8e,0x80); //禁止寫入
up_flag=0;
down_flag=0;
}
hide_year++;
if(hide_year>3)
hide_year=0;
show_time();
}while(count==8);break;
case 8: count=0;hide_year=0; //count8, 跳出調整模式,返回默認顯示狀態
Second=dataread(0x80);
datawrite(0x8e,0x00); //寫入允許
datawrite(0x80,Second&0x7f);
datawrite(0x8E,0x80); //禁止寫入
done=0; //temp_max=0;sund=1;
break; //count=7,開啟中斷,標志位置0並退出
default:break;

}

}
////////////////////////////////////////////////////////////////////////////

void rtcinit ()
{
rtc_wp(0);
rtc_stop(0);
rtc_charger(1,1);
}

void sysinit ()
{ WDTCTL = WDTPW + WDTHOLD; //關閉看門狗
P4OUT = 0xff;
P4DIR = 0xff;
P5OUT = 0x0f;
P5DIR = 0xf0;
P6OUT = 0xfc;
P6DIR = 0xfc;
}

void main ()
{ unsigned char temp;
sysinit ();
rtcinit ();
LCD_init(); //液晶初始化
_EINT();
while (1)
{
while(done==1)
keydone(); //進入調整模式
while(done==0)
{
temp=rtc_getsec();
delay_nms(10);
if(temp!=rtc_getsec())
show_time(); //液晶顯示數據
flag=0;
Setkey(); //掃描各功能鍵
}

}

}

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

}

程序運行結果如下:



(8)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;

}

I. 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;
}

J. C語言:輸入身份證號,輸出出生年月日

C語言,數字不能和字元串運算。

如果僅為了這樣輸出,可以全部用字元數組變數。

char year[5],month[3],day[3];

...

charyears[5],month[3],day[3];
...
while(i>=6&&i<10)
{
years[i-6]=str[i];
i++;
}
while(i>=10&&i<12)
{
month[i-10]=str[i];
i++;
}
while(i>=12&&i<14)
{
day[i-12]=str[i];
i++;
}
printf("你是%s年%s月%s日出生的人 ",years,month,day);

這是沒有使用字元串操作函數也沒有使用指針的方法。