写一个函数将以秒计数的时间转换为以时、分、秒计数的时间。 函数原型为:char *seconds_to(int seconds)。 编写main调用测试它。 **输入格式要求:"%d" 提示信息:"请输入时间(秒):\n" **输出格式要求:"%d秒合计%s!\n" "%d小时%d分钟%d秒" 提示:此题需要使用sprintf()函数,"%d小时%d分钟%d秒"是sprintf的输出格式。
#include <stdio.h>
char *seconds_to(int seconds)
{
static char time[100];
sprintf(time, "%d小时%d分钟%d秒", seconds / 3600, seconds % 3600 / 60, seconds % 60);
return time;
}
int main()
{
int seconds;
printf("请输入时间(秒):\n");
scanf("%d", &seconds);
printf("%d秒合计%s!\n", seconds, seconds_to(seconds));
}
C库<stdio.h>函数 int sprintf(char *str, const char *format, ...) 发送格式化输出到 str 所指向的字符串。
- str -- 这是指向一个字符数组的指针,该数组存储了 C 字符串。
- format -- 这是字符串,包含了要被写入到字符串 str 的文本。它可以包含嵌入的 format 标签,format 标签可被随后的附加参数中指定的值替换,并按需求进行格式化。format 标签属性是 %[flags][width][.precision][length]specifier。