结构指针:计算两个时刻之间的时间

| January 3, 2008 18:47 | timmy | Via Original

#include <stdio.h>
struct time_struct {
  int h;
  int m;
  int s;
};
void cal_time(struct time_struct *t1,struct time_struct *t2);
void main()
{
  struct time_struct time1,time2;
  printf("Input hour,minute,second of time1: ");
  scanf("%d:%d:%d",&time1.h,&time1.m,&time1.s);
  printf("Input hour,minute,second of time2: ");
  scanf("%d:%d:%d",&time2.h,&time2.m,&time2.s);
  cal_time(&time1,&time2);
}
void cal_time(struct time_struct *t1,struct time_struct *t2)
{
  int res_h,res_m,res_s,temp;
  temp=(t1->h-t2->h)*3600+(t1->m-t2->m)*60+t1->s-t2->s;
  if(temp<0)
    temp=-temp;
  res_h=temp/3600;
  temp%=3600;
  res_m=temp/60;
  res_s=temp%60;
  printf("%d:%d:%d\n",res_h,res_m,res_s);
}


Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(159)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember