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

c語言的時間表源代碼

發布時間: 2022-12-27 00:05:14

Ⅰ 求C語言編日歷源代碼的詳細說明

/*
稍微改了下對齊格式,加了注釋
*/
/*
1、閏年的演算法:
如果某年能被4整除但不能被100整除,
或者能被400整除,
則該年是閏年.
用表達式表示就是
(year
%4
==
0
&&
year%100
!=
0)
||
(year%400
==
0)
2、計算某一天是星期幾:
已知1900年的1月1號為星期一,
然後就可以用某一天和1900年的1月1號相差的天數對7取余來求星期,
本題是用的公元1年的1月1號作為基準
*/
#include
<stdio.h>
#include<conio.h>
#include<stdlib.h>
int
IsLeapYear(int);
//函數定義
void
main()
{
int
i;
int
day;
int
year;
int
temp;
int
temp_i;
long
int
Year_days
=
0;
int
Year_Start
=
1;
int
Per_Year_Days;
int
month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};
printf("Please
enter
the
year:
");
scanf("%d",&year);
//輸入年份
while(Year_Start
<
year)
//從公元1年開始執行while循環,
該年的一月一號為星期一
{
if(
IsLeapYear(
Year_Start
)
)
Per_Year_Days
=
366;
//如果是閏年,
則一年有366天
else
Per_Year_Days
=
365;
//如果不是閏年,
則一年有365天
Year_days
=
Year_days
+
Per_Year_Days;
//Year_days為從公元1年到輸入年份的前一年的天數的總和
Year_Start++;
}
for(
temp
=
1;
temp
<=12;
temp++
)
//temp從1到12,
對應一年內12個月
{
switch(
temp
)
//用switch語句將temp和12個月對應起來
{
case
1:
printf("
January(%d)\n",year);
//一月
break;
case
2:
printf("
February(%d)\n",year);
//二月
break;
case
3:
printf("
March(%d)\n",year);
//三月
break;
case
4:
printf("
April(%d)\n",year);
//四月
break;
case
5:
printf("
May(%d)\n",year);
//五月
break;
case
6:
printf("
June(%d)\n",year);
//六月
break;
case
7:
printf("
July(%d)\n",year);
//七月
break;
case
8:
printf("
August(%d)\n",year);
//八月
break;
case
9:
printf("
September(%d)\n",year);
//九月
break;
case
10:
printf("
October(%d)\n",year);
//十月
break;
case
11:
printf("
November(%d)\n",year);
//十一月
break;
case
12:
printf("
December(%d)\n",year);
//十二月
break;
}
i
=
Year_days
%
7;
//每個星期有7天,
故用每年的天數對7取余
printf("Mon\tTue\tWed\tThu\tFri\tSat\tSun\n");
if(
i
!=
0
)
//如果余數不為零
for(
temp_i
=
0;
temp_i
<
i;
temp_i++)
printf("\t");
//則列印空格(這里用\t代替空格,
更加美觀),
空格數為i
day
=
1;
//初始化day為1,
為下面的while循環做准備
if(
IsLeapYear(year)
&&
temp
==
2)
//如果輸入的年份是閏年,
並且月份為2
while(
day
<=
month_day[12]
)
//day為一循環變數,
取值為1-365(閏年的話為1-366)
{
if(
day
>1
)
//如果天數大於一
if(
Year_days
%
7
==
0
)
//如果是星期日,
則換行
printf("\n");
if(
day
>=
10
)
printf("%d\t",day);
//列印天數+空格
else
printf("%d\t",day);
Year_days++;
day++;
}
else
//如果不滿足"輸入的年份是閏年,
並且月份為2"
while
(day
<=
month_day[temp-1])
{
if(
day
>
1
)
if(
Year_days
%
7
==
0
)
printf("\n");
if(
day
>=10
)
printf("%d\t",day);
else
printf("%d\t",day);
Year_days++;
day++;
}
printf("\n");
if(
getch()
==
'q'
)
//如果輸入為q,
則退出程序
exit(0);
}
getch();
//每按一次鍵,
列印一個月份
}
int
IsLeapYear(
int
year
)
{
//判斷是否是閏年,
是則返回1,
否則返回0
if
((year
%4
==
0)
&&
(year
%
100
!=
0)
||
(year
%
400
==
0)
)
return
1;
else
return
0;
}

Ⅱ C語言編寫的計時器源代碼怎麼編寫

幾天前寫了個大概
你做個參考把
#include<stdio.h>
void
main()
{
float
x,y;
char
fuhao;
pintf("請輸入第一個數:\t請輸入運算符:\t,請輸入第二個數:\n");
scanf("%f,%c,%f",&x,&fuhao,&f);
if(fuhao=='+')printf("%f,%c,%f=%f\n",x,fuhao,y,x+y);
else
if(fuhao=='-')printf("%f,%c,%f=%f\n"x,fuhao,y,x-y);
else
if(fuhao=='*')printf("%f,%c,%f=%f\n"x,fuhao,y,x*y);
else
if(fuhao=='/')
{
if(y==0.0)printf("分母不能為0\n");
else
printf("%f,%c,%f=%f\n"x,fuhao,y,x/y);
}
}

Ⅲ C語言 電子時鍾源代碼程序 帶圖 謝謝 最好有注釋

#include <stdio.h>

#include <time.h>

#include"stdlib.h"

#include "windows.h"

void main ()

{

while(1)

{


time_t rawtime;

struct tm * timeinfo;


time ( &rawtime );

timeinfo = localtime ( &rawtime );

printf ( "07The current date/time is: %s", asctime (timeinfo) );


Sleep(1000);

system("cls");

}


}

Ⅳ 跪求C語言編寫的萬年歷原代碼,大哥們幫幫忙啊~

//-------------系統庫定義-------------------
#include "stdio.h"
#include "BIOS.h"
#include "dos.h"
#define X " Sun Mon Tue Wed Thu Fri Sat"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define PGUP 0x4900
#define PGDW 0x5100
#define ESC 0x011b
#define QUERY 0x1071
#define QUERYD 0x1051
//--------------------------------------------

struct date dm ;//時間結構定義
int Cyear,Cmonth,Cday,Cmonnum,Lmonnum ;//全局變數

int isleap(int year)//判斷閏年
{
if(year%4==0&&year%100||year%400==0)
return 1 ;
else
return 0 ;
}

int getday(int year,int month,int day)//計算X年X月X日是星期幾
{
int flag,s,i ;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} ;
int cont=0 ;
flag=isleap(year);
if(flag==1)
a[2]++;
for(i=1;i<month;i++)
{
cont=cont+a[i];
}
cont=cont+day ;
s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont ;
return s%7 ;
}

void printspace(int n)//輸出空格
{
int i ;
for(i=0;i<n;i++)printf(" ");
}

void spacer()//輸出分隔符(分隔線)
{
int i ;
for(i=0;i<30;i++)
{
printf("=");
}
printf("\n");
}

void keyinfo()//輸出鍵盤操作信息
{
printf("\n-------Operation manual-------\n");
printf("Year:Up key and Down key\n");
printf("Month:Left key and Right key\n");
printf("Day:PageUp key and PageDown key\n");
printf("Query date of the calendar:Q key\n");
printf("Exit calendar:Esc key\n");
}
void week()//輸出當前日期是星期幾
{
int day ;
day=getday(Cyear,Cmonth,Cday);
if(day==0)
printf("\n%d-%d-%d is Sunday!\n",Cyear,Cmonth,Cday);
if(day==1)
printf("\n%d-%d-%d is Monday!\n",Cyear,Cmonth,Cday);
if(day==2)
printf("\n%d-%d-%d is Tuesday!\n",Cyear,Cmonth,Cday);
if(day==3)
printf("\n%d-%d-%d is Wednesday!\n",Cyear,Cmonth,Cday);
if(day==4)
printf("\n%d-%d-%d is Thursday!\n",Cyear,Cmonth,Cday);
if(day==5)
printf("\n%d-%d-%d is Friday!\n",Cyear,Cmonth,Cday);
if(day==6)
printf("\n%d-%d-%d is Saturday!\n",Cyear,Cmonth,Cday);
}

void calendar(int year,int month,int day)//顯示日歷
{
int i,j,d,m,flag,week;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
m=0,d=0,week=0;
Cmonnum=a[month];//當前月的天數
Lmonnum=a[month-1];//上一月的天數
week=d=getday(year,month,1);//取得X年X月的1日是星期幾
flag=isleap(year);
if(flag==1)
{
a[2]++;
}
clrscr();
printf("\n The calendar of year %d\n",year);
spacer();
switch(month)
{
case 1 :
printf(" January 1 ");
break ;
case 2 :
printf(" February 2 ");
break ;
case 3 :
printf(" March 3 ");
break ;
case 4 :
printf(" April 4 ");
break ;
case 5 :
printf(" May 5 ");
break ;
case 6 :
printf(" June 6 ");
break ;
case 7 :
printf(" July 7 ");
break ;
case 8 :
printf(" August 8 ");
break ;
case 9 :
printf(" September 9 ");
break ;
case 10 :
printf(" October 10 ");
break ;
case 11 :
printf(" Nevember 11 ");
break ;
case 12 :
printf(" December 12");
break ;
}
printf("\n");
printf(X);
printf("\n");
for(i=0;i<6;i++)
{
if(i==0)//如果是第一個星期時,執行!
{
printspace(d*4);
for(j=0;j<7-d;j++)
{
if(m==day-1)//m到達day的前一天時,輸出[%d]
{
printf(" [%d]",++m);
}
else if(m==day && week!=0)//m到達當天時的,輸出%3d
{
printf("%3d",++m);
}
else
{
printf("%4d",++m);
}
week=(week<6)?week+1:0;//計算當天是星期幾
}

printf("\n");
}
else//不是第一個星期時,執行!
{
for(j=0;j<7;j++)
{
if(m<a[month])
{
if(m==day-1)//m到達day的前一天時,輸出[%d]
{
if(m<9)//代表只有一位數的數字如9,8,7.....等。
printf(" [%d]",++m);
else//代表只有兩位數的數字如10,11,12.....等。
printf(" [%d]",++m);
}
else if(m==day && week!=0)
{
printf("%3d",++m);
}
else
{
printf("%4d",++m);
}
}

week=(week<6)?week+1:0;//計算當天是星期幾
}
printf("\n");
if(m==a[month])break ;

}
}
spacer();
if(flag==1)
printf("\nThe year %d is leap year!\n",year);
if(flag==0)
printf("\nThe year %d is not leap year!\n",year);
week();
printf("\nThe current date is: %d-%d-%d\n",dm.da_year,dm.da_mon,dm.da_day);
keyinfo();
}

//keyboard Operation
void key()
{
int key ;
Cyear=dm.da_year,Cmonth=dm.da_mon,Cday=dm.da_day ;
calendar(Cyear,Cmonth,Cday);
while(1)
{
key=bioskey(0);
if(key==RIGHT)
{
if(Cmonth<12&&Cmonth>=1)
{
Cmonth++;
}
else
{
Cyear++;
Cmonth=1 ;
}
}
if(key==LEFT)
{
if(Cmonth<=12&&Cmonth>1)
{
Cmonth--;
}
else
{
Cyear--;
Cmonth=12 ;
}
}
if(key==UP)
{
Cyear++;
}
if(key==DOWN)
{
Cyear--;
}
if(key==PGUP)
{
if(Cday!=1)
{
Cday--;
}
else if(Cday==1&&Cmonth==1)
{
Cyear--;
Cmonth=12 ;
Cday=31 ;
}
else
{
Cmonth--;
Cday=Lmonnum ;
}
}
if(key==PGDW)
{
if(Cmonnum!=Cday)
{
Cday++;
}
else if(Cmonnum==Cday&&Cmonth==12)
{
Cyear++;
Cmonth=1 ;
Cday=1 ;
}
else
{
Cmonth++;
Cday=1 ;
}
}
if(key==QUERY||key==QUERYD)
{
printf("Input date format(YYYY,MM,DD):");
scanf("%d,%d,%d",&Cyear,&Cmonth,&Cday);
}
if(key==ESC)break ;
calendar(Cyear,Cmonth,Cday);
}
}

void main()
{
clrscr();
getdate(&dm);
key();
}