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)
{
// printf("小时格式错误:%d\n",(time.hours>=24||time.hours<0));
// printf("分钟格式错误:%d\n",(time.minutes>=60||time.minutes<0));
// printf("秒格式错误 :%d\n",(time.seconds>=60||time.minutes<0));
return ((time.hours>24||time.hours<0)||(time.minutes>=60||time.minutes<0)||(time.seconds>=60||time.minutes<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)//如果有time
{
//交换两个时间的小时
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;