//c语言写的
//定义一个时间结构体,成员包括时,分,秒编写一个函数用来比较两个时间的大小
#include <stdio.h>
void main()
{
struct ho_mi_se
{
int hour;
int min;
int second;
};
struct ho_mi_se ho_mi_se1;
struct ho_mi_se ho_mi_se2;
printf("输入第一时间的时(0--23):");
scanf("%d/n",&(ho_mi_se1.hour));
printf("输入第一时间的分(0--59):");
scanf("%d/n",&(ho_mi_se1.min));
printf("输入第一时间的秒(0--59):");
scanf("%d/n",&(ho_mi_se1.second));
printf("输入第二时间的时(0--23):");
scanf("%d/n",&(ho_mi_se2.hour));
printf("输入第二时间的分(0--59):");
scanf("%d/n",&(ho_mi_se2.min));
printf("输入第二时间的秒(0--59):");
scanf("%d/n",&(ho_mi_se2.second));
if(ho_mi_se2.hour>ho_mi_se1.hour)
{
printf("第二时间比第一时间大:%d:%d:%d>%d:%d:%d",ho_mi_se2.hour, ho_mi_se2.min,
ho_mi_se2.second,ho_mi_se1.hour,ho_mi_se1.min,ho_mi_se1.second);
}
else if(ho_mi_se2.min>ho_mi_se1.min)
{
printf("第二时间比第一时间大:%d:%d:%d>%d:%d:%d",ho_mi_se2.hour,ho_mi_se2.min,
ho_mi_se2.second,ho_mi_se1.hour,ho_mi_se1.min,ho_mi_se1.second);
}
else if(ho_mi_se2.second>ho_mi_se1.second)
{
printf("第二时间比第一时间大:%d:%d:%d>%d:%d:%d",ho_mi_se2.hour,ho_mi_se2.min,
ho_mi_se2.second,ho_mi_se1.hour,ho_mi_se1.min,ho_mi_se1.second);
}else
{
if((ho_mi_se2.hour==ho_mi_se1.hour)&&(ho_mi_se2.min==ho_mi_se1.min)&&(ho_mi_se2.second==ho_mi_se1.second))
{
printf("第二时间和第一时间相等:%d:%d:%d=%d:%d:%d",ho_mi_se2.hour,ho_mi_se2.min,
ho_mi_se2.second,ho_mi_se1.hour,ho_mi_se1.min,ho_mi_se1.second);
}
else
{
printf("第二时间比第一时间小:%d:%d:%d<%d:%d:%d",ho_mi_se2.hour,ho_mi_se2.min,
ho_mi_se2.second,ho_mi_se1.hour,ho_mi_se1.min,ho_mi_se1.second);
}
}
system("pause");
}
2013-04-26
请问输入时间时有什么要求吗?
#include<stdio.h>
struct time
{
int hour;
int minute;
int sec;
};
time t1,t2;
int main()
{
scanf("%d %d %d",&t1.hour,&t1.minute,&t1.sec);
scanf("%d %d %d",&t2.hour,&t2.minute,&t2.sec);
if(t1.hour>t2.hour)
{
printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" > ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
else if(t1.hour<t2.hour)
{
printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" < ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
else if(t1.minute>t2.minute)
{
printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" > ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
else if(t1.minute<t2.minute)
{
printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" < ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
else if(t1.sec>t2.sec)
{
printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" > ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
else if(t1.sec<t2.sec)
{
printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" < ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
else
{printf("(%d:%d:%d)",t1.hour,t1.minute,t1.sec);
printf(" = ");
printf("(%d:%d:%d)",t2.hour,t2.minute,t2.sec);
}
}
struct MyTime
{
int m_isec; //秒
int m_imin; //分
int m_ihour; //时
int GetSecond() //将时间格式转换成秒
{
return m_isec+m_imin*60+m_ihour*3600;
}
friend bool operator < (const MyTime&,const MyTime&);
friend bool operator > (const MyTime&,const MyTime&);
};
bool operator < (const MyTime & argtm1,const MyTime &argtm2)
{
MyTime tm1,tm2;
tm1=argtm1;
tm2=argtm2;
return tm1.GetSecond()-tm2.GetSecond()<0;
}
bool operator > (const MyTime & argtm1,const MyTime & argtm2)
{
MyTime tm1,tm2;
tm1=argtm1;
tm2=argtm2;
return tm1.GetSecond()-tm2.GetSecond()>0;
}
// 定义一个时间结构体,成员包括时,分,秒编写一个函数用来比较两个时间的大小
#include "stdlib.h"
typedef struct{
int hour; // >0
int minute; // 0-59
int second; // 0-59
}time_t;
// if t1 > t2, return > 0
// if t1 = t2, return = 0
// if t1 < t2, return < 0
int timecmp( time_t t1, time_t t2 )
{
int hour_diff = t1.hour - t2.hour;
int minute_diff = t1.minute - t2.minute;
int second_diff = t1.second - t2.second;
// equal
if( !hour_diff && !minute_diff && !second_diff ){
return 0;
}
// big
if( (hour_diff > 0)
|| ((!hour_diff)&&(minute_diff>0))
|| ((!hour_diff)&&(!minute_diff)&&(second_diff>0)) ){
return 1;
}
// small
if( (hour_diff < 0)
|| ((!hour_diff)&&(minute_diff<0))
|| ((!hour_diff)&&(!minute_diff)&&(second_diff<0)) ){
return -1;
}
}
void timecmpprt( time_t t1, time_t t2 )
{
int result = timecmp(t1,t2);
printf("%d:%d:%d", t1.hour, t1.minute, t1.second);
if( result > 0 ){
printf(" > ");
}else if( result == 0 ){
printf(" = ");
}else{
printf(" < ");
}
printf("%d:%d:%d\n", t2.hour, t2.minute, t2.second);
}
int main(int argc, char const *argv[])
{
time_t t1 = {100,24,53};
time_t t2 = {100,24,53};
time_t t3 = {150,24,53};
time_t t4 = {100,16,53};
timecmpprt(t1,t2);
timecmpprt(t1,t3);
timecmpprt(t1,t4);
return 0;
}