C语言 使用计算两个时间的差,使用结构体

来源:互联网 发布:源生活网络超市 编辑:程序博客网 时间:2024/05/19 02:05

任意输入两个24小时制的时间,输出两个时间的时间差

/**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;}
测试:

输入时间1:11:12:23输入时间2:10:10:18           11:12:23 - 10:10:18 = 1:2:5继续[y/n]?:y输入时间1:12:13:14输入时间2:23:24:56           23:24:56 - 12:13:14 = 11:11:42继续[y/n]?:nProcess returned 0 (0x0)   execution time : 41.902 sPress any key to continue.