指针与数组练习题
题3 编写下列函数:
void split_time(long int total_sec,int *hr,int *min,int *sec);
total_sec是从午夜计算的秒数表示的时间。hr、min和sec都是指向变量的指针,这些变量在函数中将分别存储着按小时算(0-23)、按分钟算(0-59)和按秒算(0-59)的等价的时间。
答:程序如下
#include<stdio.h>
#include<stdlib.h>
void time_split(long int total_sec,int *hr,int *min,int *sec)
{
int i,j;
i=total_sec/60;
j=total_sec-i*60;
*sec=j;
if(i<60)
{
*min=i;
*hr=0;
}
else
{
int k;
*hr=i/60;
k=i-*hr*60;
*min=k;
}
}
int main()
{
long int total_sec;
int hr,min,sec;
printf("Enter total_second:");
scanf("%ld",&total_sec);
time_split(total_sec,&hr,&min,&sec);
printf("hr=%d,min=%d,sec=%d\n",hr,min,sec);
system("pause");
return 0;
}