本文目录一览:
1、c语言如何计算两个时间相差多少
2、c语言两日期相差天数
3、C语言求两个日期相差的天数
c语言如何计算两个时间相差多少
/**
time.c
定义一个结构体实现两个时间的加减
*/
#include<stdio.h>
#include<string.h>
typedef struct
{
int seconds;
int minutes;
int hours;
}Time;
int checkTime(Time time);
void printTime(Time time);
void swap(Time *time1,Time *time2);//大的时间放在前面
Time subtract1(Time *first,Time *second);
Time subtract(Time *first,Time *second);//默认第一个时间比第二个大
int main()
{
Time time1;
Time time2;
Time time3;
char againch[5]="y";
while(strcmp(againch,"y")==0||strcmp(againch,"Y")==0)
{
int again=1;
while(again)
{
printf("输入时间1:");
scanf("%d:%d:%d",&time1.hours,&time1.minutes,&time1.seconds);
if(checkTime(time1))
{
printf("-----输入时间格式错误!请重新输入\n");
again=1;
}
else
again=0;
}
again=1;
while(again)
{
printf("输入时间2:");
scanf("%d:%d:%d",&time2.hours,&time2.minutes,&time2.seconds);
if(checkTime(time2))
{
printf("-----输入时间格式错误!请重新输入\n");
again=1;
}
else
again=0;
}
swap(&time1,&time2);
printf(" ");
printTime(time1);
printf(" - ");
printTime(time2);
time3=subtract(&time1,&time2);
printf(" = ");
printTime(time3);
printf("\n");
printf("继续[y/n]?:");
scanf("%s",againch);
}
return 0;
}
//检查时间的格式
int checkTime(Time time)
{
return ((time.hours>24||time.hours<0)||(time.minutes>=60||time.minutes<0)||(time.seconds>=60||time.seconds<0));
}
//输出按个数输出时间
void printTime(Time time)
{
printf("%d:%d:%d",time.hours,time.minutes,time.seconds);
}
//大的时间放到第一个变量,小的时间方法哦第二个变量
void swap(Time *time1,Time *time2)
{
//保证第一个时间永远大于第二个时间
if(time2->hours<time1->hours)
{
//交换两个时间的小时
time2->hours^=time1->hours;
time1->hours^=time2->hours;
time2->hours^=time1->hours;
//交换两个时间的分钟:
time1->minutes^=time2->minutes;
time2->minutes^=time1->minutes;
time1->minutes^=time2->minutes;
//交换两个时间的秒:
time1->seconds^=time2->seconds;
time2->seconds^=time1->seconds;
time1->seconds^=time2->seconds;
}
else if(time2->minutes<time1->minutes&&time1->hours==time2->hours)
{
//交换两个时间的分钟:
time1->minutes^=time2->minutes;
time2->minutes^=time1->minutes;
time1->minutes^=time2->minutes;
//交换两个时间的秒:
time1->seconds^=time2->seconds;
time2->seconds^=time1->seconds;
time1->seconds^=time2->seconds;
}
else if(time2->seconds<time1->seconds&&time1->minutes==time2->minutes)
{
//交换两个时间的秒:
time1->seconds^=time2->seconds;
time2->seconds^=time1->seconds;
time1->seconds^=time2->seconds;
}
}
//计算两个时间的差
Time subtract(Time *first,Time *second)//默认第一个时间比第二个大
{
Time result;
//先对秒进行相减
if(first->seconds>=second->seconds)//如果第一个秒大于或者等于
{
result.seconds=first->seconds-second->seconds;
}
else//如果第一个的秒数小的话
{
first->minutes=first->minutes-1;//借位
first->seconds=first->seconds+60;
result.seconds=first->seconds-second->seconds;
}
//接着对分钟相减
if(first->minutes>=second->minutes)//如果第一个秒大于或者等于
{
result.minutes=first->minutes-second->minutes;
}
else//如果第一个的秒数小的话
{
first->hours=first->hours-1;//借位
first->minutes=first->minutes+60;
result.minutes=first->minutes-second->minutes;
}
//交换后 默认第一个小时会大于第一个,没有借位的情况,不用
result.hours=first->hours-second->hours;
return result;
}
拓展资料
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。 尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。 二十世纪八十年代,为了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。
c语言两日期相差天数
#include <stdio.h>
#include <math.h>
int main()
{
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31},b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
{
if((y1%100!=0&&y1%4==0)||y1%400==0)
{
while(m1<m2)
{
s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
while(m1<m2)
{
s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
while(y1<y2)
{
if(m1<=2)
{
if((y1%100!=0&&y1%4==0)||y1%400==0)
{
s=366+s;
}
else
{
s=365+s;
}
y1=y1+1;
}
else
{
y1=y1+1;
if((y1%100!=0&&y1%4==0)||y1%400==0)
{
s=366+s;
}
else
{
s=365+s;
}
}
}
if(m1==m2)
{
if((y2%100!=0&&y2%4==0)||y2%400==0)
{
for(i=0;m1<m2;i++)
{
s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
for(i=0;m1<m2;i++)
{
s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
if((y2%100!=0&&y2%4==0)||y2%400==0)
{
while(m1<m2)
{
s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
while(m1<m2)
{
s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
}
return 0;
}
你试试看!
C语言求两个日期相差的天数
计算两个年月日之间的天数,思路是分别算出日期的总天数然后相减。 要考虑闰年的情况,判断闰年的口诀:4年一闰,100年不闰,400年再闰。
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
网上找了一个(偷懒= =!),修改下如下:
#include <stdio.h>
int sum(int y,int m,int d)
{
unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,s=0;
for(i=1;i<y;i++)
if(i%4==0 && i%100!=0 || i%400==0)
s+=366;//闰年
else
s+=365;//平年
if(y%4==0 && y%100!=0 || y%400==0)
x[2]=29;
for(i=1;i<m;i++)
s+=x[i];//整月的天数
s+=d;//日的天数
return s;//返回总天数,相对公元1年
}
void main()
{
unsigned char y1,m1,d1,y2,m2,d2;
int s1,s2;
printf("输入第一个年 月 日:");
scanf("%d %d %d",&y1,&m1,&d1);
printf("输入第二个年 月 日:");
scanf("%d %d %d",&y2,&m2,&d2);
s1=sum(y1,m1,d1);
s2=sum(y2,m2,d2);
if (s1 > s2)
printf("相差天数:%ld\n",s1-s2);
else
printf("相差天数:%ld\n",s2-s1);
}
以上代码VC6编译测试通过。 虽然这个思路显得有些笨,但是其它算法,代码太长太复杂,要考虑多种情况,不如直接算两个日期距离公元元年1月1日的天数,然后相减