前言
C语言写一个函数将以秒计数的时间转换为以时、分、秒计数的时间,函数返回用指针类型。
函数原型为:char *seconds_to(int seconds)。编写main调用测试它。
**输入格式要求:"%d" 提示信息:“请输入时间(秒):\n”
**输出格式要求:"%d秒合计%s!\n" “%d小时%d分钟%d秒”
程序源码
#include <stdio.h>
char *seconds_to(int seconds);
int main ()
{
int t;
char *t1;
printf("请输入时间(秒):\n");
scanf("%d", &t);
t1 = seconds_to(t);
printf("%d小时%d分钟%d秒\n", *(t1 + 2), *(t1 + 1), *(t1 + 0));
return 0;
}
char *seconds_to(int seconds)
{
static char time[3];
time[0] = seconds % 60;
time[1] = ( (seconds - time[0]) / 60) % 60; //分秒之间是60进位,可以根据进位原则求出分
time[2] = ( (seconds / 60) - time[1]) / 60; // 时分之间也是60进位
return &time[0];
}