
#include <stdioh>
struct date
{
int year;
int month;
int day;
};
int main(void)
{
int isPrime(int year);
int dateDiff(struct date mindate,struct date maxdate);
struct date mindate,maxdate;
int days;
printf("please input the one date:");
scanf("%i-%i-%i",&mindateyear,&mindatemonth,&mindateday);
printf("please input other day:");
scanf("%i-%i-%i",&maxdateyear,&maxdatemonth,&maxdateday);
days=dateDiff(mindate,maxdate);
printf("the day is:%d\n",days);
return 0;
}
//
/ 判断闰年函数(4年一润,100年不润,400年再润) /
//
int isPrime(int year)
{
if ((year%4==0&&year%100!=0)||(year%400==0))
{
return 1;
}
else
{
return 0;
}
}
int dateDiff(struct date mindate,struct date maxdate)
{
int days=0, flag=1;
const int primeMonth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
const int notPrimeMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//
/ 交换两个日期函数,将小的日期给mindate,将大的日期给maxdate /
//
struct date tmp;
if ((mindateyear>maxdateyear)|| (mindateyear==maxdateyear&&mindatemonth>maxdatemonth)||(mindateyear==maxdateyear&&mindatemonth==maxdatemonth&&mindateday>maxdateday))
{
tmp=mindate;
mindate=maxdate;
maxdate=tmp;
}
int maxmonth,minmonth;
//
/ 主要思路:拿2002-8-8 2005-2-22来说 /
/ 将2004-8-8---2005-2-22----2005-7-8 /
/一前一后刚好N年,算出2005-2-22到2005-7-8的天数,然后用总年36(5|6)减掉) /
/ 2002-9-8 2005-11-22 /
/ 2002-9-8-----2005-9-8-----2005-11-22(这次是加上后面天数) /
/如何判断是加还是减呢年大而月小的,则是减,程序中用flag标示 /
//
if (maxdatemonth<mindatemonth)
{
maxmonth=mindatemonth;
minmonth=maxdatemonth;
flag=-1;
}
else
{
maxmonth=maxdatemonth;
minmonth=mindatemonth;
flag=1;
}
//
/ 从mindateyear开始累加到maxdateyear /
//
for(int j=mindateyear;j<maxdateyear;++j)
{
if (isPrime(j)==1)
{
days+=366;
}
else
days+=365;
}
//
/ 从minmonth累加到maxmonth,分闰年和平年 /
//
int day;
if(isPrime(maxdateyear)==1)
{
for(int i=minmonth;i<maxmonth;i++)
{
day=primeMonth[i-1]flag;
days=days+day;
}
days=days+maxdateday-mindateday;
}
else
{
for (int i=minmonth;i<maxmonth;i++)
{
day=notPrimeMonth[i-1]flag;
days=days+day;
}
days=days+maxdateday-mindateday;
}
return days;
}
给你上个较完整的程序,可以直接在KEIL中运行并观察输出
/
功能: 本程序在12M晶振模式下,通过定时器中断精确实现数字时钟计时 *** 作,并在KEIL
中实现输出。时、分、秒的变化在定时中断里处理。
说明: 因采用工作方式2,自动装入初值,所以此程序计时很精确,只是在KEIL中模拟输
出显示的变化速度很快,这点可不理会
/
#include <reg52h>
#include <stdioh>
#define TEST //此行用于KEIL输出显示,如果不需要显示可将其删除
typedef unsigned char uchar;
typedef unsigned int uint;
#define TH0TL0_INIT (256-250) //定时器8位自动装入模式下寄存器初值,025ms中断一次
char cHour; //时
char cMin; //分
char cSec; //秒
uint iCount; //秒计数,计数达到4000时1s,4000025ms =1000ms = 1s
bit bSecChanged; //秒发生变化标志,每秒送一次输出显示,送显完成后清0,提高主程序效率
//==============================================================================
//T0定时器中断服务程序,12M晶振下每025ms产生中断,本程序执行一次
//==============================================================================
void Timer0() interrupt 1
{
iCount++; //秒计数值+1
if(iCount==4000)
{//时间计数达到1S
iCount = 0; //重新开始下一秒计数
cSec++; //时钟:秒+1
bSecChanged = 1; //置秒发生变化标志
if(cSec==60)
{//计够60s
cSec = 0; //重新开始下一分计数
cMin++; //时钟:分+1
}
if(cMin==60)
{//计够60分钟
cMin = 0; //重新开始下一小时计数
cHour++; //时钟:小时+1
}
if(cHour==24)
{//计够24小时
cHour = 0; //重新开始第二天计数
}
}
}
//==============================================================================
//主程序
//==============================================================================
void main()
{
uchar outstr[10]; //输出字符串,我的编译器可能有问题,直接输出有错
TMOD = 0X02;//工作方式2,8位自动重装计时模式
TH0 = TH0TL0_INIT; //025ms中断一次
TL0 = TH0TL0_INIT; //025ms中断一次
#ifdef TEST
SCON = 0x50; / SCON: mode 1, 8-bit UART, enable rcvr /
TMOD |= 0x20; / TMOD: timer 1, mode 2, 8-bit reload /
TH1 = 221; / TH1: reload value for 1200 baud @ 16MHz /
TR1 = 1; / TR1: timer 1 run /
TI = 1; / TI: set TI to send first char of UART /
#endif
cHour = 0; //时
cMin = 0; //分
cSec = 0; //秒
iCount = 0; //秒计数
bSecChanged = 0;
outstr[2] = ':'; //时分分隔符
outstr[5] = ':'; //分秒分隔符
outstr[8] = 0; //字符串结束符
EA=1; //开总中断
ET0=1; //允许T0中断
TR0=1; //启动T0
while(1)
{
if(bSecChanged==1)
{//秒发生变化,将时间值转换为可显示字符串准备送显示
bSecChanged = 0;//清除标志,节省CPU资源
outstr[0] = cHour/10 + 0x30; //将秒转换为ASCII码
outstr[1] = cHour%10 + 0x30;
outstr[3] = cMin/10 + 0x30; //将分转换为ASCII码
outstr[4] = cMin%10 + 0x30;
outstr[6] = cSec/10 + 0x30; //将小时转换为ASCII码
outstr[7] = cSec%10 + 0x30;
#ifdef TEST
printf(" %s\r",outstr); //在KEIL中显示时钟
#endif
}
}
}
首先数据库就是存储数据的仓库(字面理解),所以任何可以存储数据的文本文件都可以称为数据库——包括txt文档。
下面是将一个集合从datagridview中导出到excel中:
DataTable datatable = initDataTable();
for (int i = 0; i < ulListCount; i++)
{
DataRow datarow = datatableNewRow();
datarow[0] = ulList[i]Time;
datarow[9] = ulList[i]Remark;
。。。。。。。
datatableRowsAdd(datarow);
}
DataSet dataset = new DataSet();
datasetTablesAdd(datatable);
ExportDataGridViewToExcel(datatable);
//ExportDataGridViewToExcel方法
private void ExportDataGridViewToExcel(DataTable dataGridTable)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialogFilter = "Execl files (xls)|xls";
saveFileDialogTitle = "导出Excel文件到";
DateTime now = DateTimeNow;
saveFileDialogFileName = "日志-" + nowYearToString()PadLeft(2) + nowMonthToString()PadLeft(2, '0') + nowDayToString()PadLeft(2, '0') + "-" + nowHourToString()PadLeft(2, '0') + nowMinuteToString()PadLeft(2, '0') + nowSecondToString()PadLeft(2, '0');
saveFileDialogShowDialog();
Stream myStream;
myStream = saveFileDialogOpenFile();
StreamWriter sw = new StreamWriter(myStream, SystemTextEncodingGetEncoding("gb2312"));
string str = "";
try
{
//写标题
for (int i = 0; i < arycolumnnameLength; i++)
{
if (i > 0)
{
str += "\t";
}
str += arycolumnname[i];
}
swWriteLine(str);
//写内容
for (int j = 0; j < dataGridTableRowsCount; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridTableColumnsCount; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridTableRows[j][k]ToString();
}
swWriteLine(tempStr);
}
swClose();
myStreamClose();
MessageBoxShow("导出成功");
}
catch (Exception e)
{
MessageBoxShow(eToString());
}
finally
{
swClose();
myStreamClose();
}
}
//上面用到文件流将其保存程excel文件,还有其他的方式,可以网上收一下——一大堆。
C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明。
在头文件中定义了三种类型:time_t,struct tm和clock_t。
下面列出了这些函数。
time_t time(time_t timer);
double difftime(time_t time1,time_t time2);
struct tm gmtime(const time_t timer);
struct tm localtime(const time_t timer);
char asctime(const struct tm timeptr);
char ctime(const time_t timer);
size_t strftime(char s,size_t maxsize,const char format,const struct tm timeptr);
time_t mktime(struct tm timeptr);
clock_t clock(void);
具体应用举例
asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#i nclude
定义函数
char asctime(const struct tm timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n"
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
范例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",asctime(gmtime(&timep)));
}
执行
Sat Oct 28 02:10:06 2000
ctime(将时间和日期以字符串格式表示)
相关函数
time,asctime,gmtime,localtime
表头文件
#i nclude
定义函数
char ctime(const time_t timep);
函数说明
ctime ()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993\n"。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值
返回一字符串表示目前当地的时间日期。
范例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
执行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#i nclude
#i nclude
定义函数
int gettimeofday ( struct timeval tv , struct timezone tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /秒/
long tv_usec; /微秒/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /和Greenwich 时间差了多少分钟/
int tz_dsttime; /日光节约时间的状态/
};
上述两个结构都定义在/usr/include/sys/timeh。tz_dsttime 所代表的状态如下
DST_NONE /不使用/
DST_USA /美国/
DST_AUST /澳洲/
DST_WET /西欧/
DST_MET /中欧/
DST_EET /东欧/
DST_CAN /加拿大/
DST_GB /大不列颠/
DST_RUM /罗马尼亚/
DST_TUR /土耳其/
DST_AUSTALT /澳洲(1986年以后)/
返回值
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例
#i nclude
#i nclude
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf("tv_sec; %d\n", tv,tv_sec) ;
printf("tv_usec; %d\n",tvtv_usec);
printf("tz_minuteswest; %d\n", tztz_minuteswest);
printf("tz_dsttime, %d\n",tztz_dsttime);
}
执行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
gmtime(取得目前时间和日期)
相关函数
time,asctime,ctime,localtime
表头文件
#i nclude
定义函数
struct tmgmtime(const time_ttimep);
函数说明
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC 时间
范例
#i nclude
main(){
char wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 8:15:38
localtime(取得当地目前时间和日期)
相关函数
time, asctime, ctime, gmtime
表头文件
#i nclude
定义函数
struct tm localtime(const time_t timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义请参考gmtime()。此函
数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例
#i nclude
main(){
char wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm p;
time(&timep);
p=localtime(&timep); /取得当地时间/
printf ("%d%d%d ", (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 11:12:22
mktime(将时间结构数据转换成经过的秒数)
相关函数
time,asctime,gmtime,localtime
表头文件
#i nclude
定义函数
time_t mktime(strcut tm timeptr);
函数说明
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值
返回经过的秒数。
范例
/ 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数/
#i nclude
main()
{
time_t timep;
strcut tm p;
time(&timep);
printf("time() : %d \n",timep);
p=localtime(&timep);
timep = mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
}
执行
time():974943297
time()->localtime()->mktime():974943297
settimeofday(设置目前时间)
相关函数
time,ctime,ftime,gettimeofday
表头文件
#i nclude
#i nclude
定义函数
int settimeofday ( const struct timeval tv,const struct timezone tz);
函数说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。
注意,只有root权限才能使用此函数修改时间。
返回值
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
time(取得目前的时间)
相关函数
ctime,ftime,gettimeofday
表头文件
#i nclude
定义函数
time_t time(time_t t);
函数说明
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,
此函数也会将返回值存到t指针所指的内存。
返回值
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例
#i nclude
mian()
{
int seconds= time((time_t)NULL);
printf("%d\n",seconds);
}
Date and Time Functions: <timeh>
The header <timeh> declares types and functions for manipulating date and time Some functions process local time,
which may differ from calendar time, for example because of time zone clock_t and time_t are arithmetic types
representing times, and struct tm holds the components
of a calendar time:
int tm_sec; seconds after the minute (0,61)
int tm_min; minutes after the hour (0,59)
int tm_hour; hours since midnight (0,23)
int tm_mday; day of the month (1,31)
int tm_mon; months since January (0,11)
int tm_year; years since 1900
int tm_wday; days since Sunday (0,6)
int tm_yday; days since January 1 (0,365)
int tm_isdst; Daylight Saving Time flag
tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available
clock_t clock(void)
clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable
clock()/CLK_PER_SEC is a time in
seconds
time_t time(time_t tp)
time returns the current calendar time or -1 if the time is not available If tp is not NULL,
the return value is also assigned to tp
double difftime(time_t time2, time_t time1)
difftime returns time2-time1 expressed in seconds
time_t mktime(struct tm tp)
mktime converts the local time in the structure tp into calendar time in the same representation used by time
The components will have values
in the ranges shown mktime returns the calendar time or -1 if it cannot be represented
The next four functions return pointers to static objects that may be overwritten by other calls
char asctime(const struct tm tp)
asctimetp into a string of the form
Sun Jan 3 15:14:13 1988\n\0
char ctime(const time_t tp)
ctime converts the calendar time tp to local time; it is equivalent to
asctime(localtime(tp))
struct tm gmtime(const time_t tp)
gmtime converts the calendar time tp into Coordinated Universal Time (UTC) It returns NULL if UTC is not available
The name gmtime has historical significance
struct tm localtime(const time_t tp)
localtime converts the calendar time tp into local time
size_t strftime(char s, size_t smax, const char fmt, const struct tm tp)
strftime formats date and time information from tp into s according to fmt, which is analogous to a printf format
Ordinary characters (including the terminating '\0') are copied into s Each %c is replaced as described below,
using values appropriate for the local environment
No more than smax characters are placed into s strftime returns the number of characters, excluding the '\0',
or zero if more than smax characters were produced
%a abbreviated weekday name
%A full weekday name
%b abbreviated month name
%B full month name
%c local date and time representation
%d day of the month (01-31)
%H hour (24-hour clock) (00-23)
%I hour (12-hour clock) (01-12)
%j day of the year (001-366)
%m month (01-12)
%M minute (00-59)
%p local equivalent of AM or PM
%S second (00-61)
%U week number of the year (Sunday as 1st day of week) (00-53)
%w weekday (0-6, Sunday is 0)
%W week number of the year (Monday as 1st day of week) (00-53)
%x local date representation
%X local time representation
%y year without century (00-99)
%Y year with century
%Z time zone name, if any
%% %
for(;chour< hour;chour++) //应改为<, 如果=的话,会再次执行chour++变为11点了,下同
{
for(;cminute< 60;cminute++)
{
for(;csecond< 60;csecond++);
}
}
for(cminute=0;cminute<minute;cminute++)
{
for(csecond=0;csecond<60;csecond++);
}
for(csecond=0;csecond<=second;csecond++)
//你应该初始化csecond, 因为分钟到了后,秒从0开始;
if(chour==hour&&cminute==minute&&csecond==second)
cout<<"the time to!!!get up!!the time is:"<<hour<<":"<<minute<<":"<<second<<endl;
定义1
严格地说,数据库是“按照数据结构来组织、存储和管理数据的仓库”。在经济管理的日常工作中,常常需要把某些相关的数据放进这样的“仓库”,并根据管理的需要进行相应的处理。例如,企业或事业单位的人事部门常常要把本单位职工的基本情况(职工号、姓名、年龄、性别、籍贯、工资、简历等)存放在表中,这张表就可以看成是一个数据库。有了这个"数据仓库"我们就可以根据需要随时查询某职工的基本情况,也可以查询工资在某个范围内的职工人数等等。这些工作如果都能在计算机上自动进行,那我们的人事管理就可以达到极高的水平。此外,在财务管理、仓库管理、生产管理中也需要建立众多的这种"数据库",使其可以利用计算机实现财务、仓库、生产的自动化管理。JMartin给数据库下了一个比较完整的定义:数据库是存储在一起的相关数据的集合,这些数据是结构化的,无有害的或不必要的冗余,并为多种应用服务;数据的存储独立于使用它的程序;对数据库插入新数据,修改和检索原有数据均能按一种公用的和可控制的方式进行。当某个系统中存在结构上完全分开的若干个数据库时,则该系统包含一个“数据库集合”。
定义2
数据库是依照某种数据模型组织起来并存放二级存储器中的数据集合。这种数据集合具有如下特点:尽可能不重复,以最优方式为某个特定组织的多种应用服务,其数据结构独立于使用它的应用程序,对数据的增、删、改和检索由统一软件进行管理和控制。从发展的历史看,数据库是数据管理的高级阶段,它是由文件管理系统发展起来的。
定义3
(伯尔尼公约议定书专家委员会的观点)所有的信息(数据率档)的编纂物,不论其是以印刷形式,计算机存储单元形式,还是其它形式存在,都应视为“数据库”。数字化内容选择的原因有很多,概括起来主要有:(1)存储空间的原因。数字化的产品是通过网络被广大用户存取利用,而大家都知道数字化产品是存放在磁盘阵列上的,磁盘阵列由服务器来管理,磁盘空间是有限的,服务器的能力也是有限的,不可能无限量地存入数字资源,这就需要我们对文献资源数字化内容进行选择。(2)解决数字化生产高成本和图书馆经费有限性之间矛盾的需要。几乎没有图书馆有充足的资源来对整个馆藏进行数字化,内容选择不可避免。(3)数字资源管理的需要。技术的快速发展使数字化项目所生成的数字资源的生命周期越来越短,投入巨资进行数字迁移是延长数字资源生命的1个重要途径,昂贵的维护成本就必须考虑数字化的内容选择。数据库发展史数据库技术从诞生到现在,在不到半个世纪的时间里,形成了坚实的理论基础、成熟的商业产品和广泛的应用领域,吸引越来越多的研究者加入。数据库的诞生和发展给计算机信息管理带来了一场巨大的革命。三十多年来,国内外已经开发建设了成千上万个数据库,它已成为企业、部门乃至个人日常工作、生产和生活的基础设施。同时,随着应用的扩展与深入,数据库的数量和规模越来越大,数据库的研究领域也已经大大地拓广和深化了。30年间数据库领域获得了三次计算机图灵奖(CWBachman,EFCodd,JGray),更加充分地说明了数据库是一个充满活力和创新精神的领域。就让我们沿着历史的轨迹,追溯一下数据库的发展历程。传统上,为了确保企业持续扩大的IT系统稳定运行,一般用户信息中心往往不仅要不断更新更大容量的IT运维软硬件设备,极大浪费企业资源;更要长期维持一支由数据库维护、服务器维护、机房值班等各种维护人员组成的运维大军,维护成本也随之节节高升。为此,企业IT决策者开始思考:能不能像拧水龙头一样按需调节的使用IT
以上就是关于c语言 用库函数计算两日期相差的天数全部的内容,包括:c语言 用库函数计算两日期相差的天数、C语言写单片机的时候,怎么写定时中断、C#怎样 *** 作文件型数据库最好有实例等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)