c语言编写 输入一个以秒为单位的时间值,将其转化成“时:分:秒”的形式输出,将转换操作定义成函数

刚学完函数调用这一章
2025-02-01 01:52:07
推荐回答(2个)
回答1:

#include
void trans(int sec){
int hour,min;
hour=sec/3600; //计算时 3600进制
min=(sec%3600)/60; //计算分 60进制
sec=(sec%3600)%60; //计算秒 余下的全为秒数
printf("%d时:%d分:%d秒\n",hour,min,sec);
}
int main(){
int sec;
printf("请输入秒数:\n");
scanf("%d",&sec);
trans(sec);
return 0;
}
可以自己试试

回答2:

#include
#include

void time_chang()
{
char time_in[10] = "";
int time_int = 0;
char hour,mind,sec;
gets(time_in);
time_int = atoi(time_in);
sec = time_int%100;
time_int/=100;
mind = time_int%100;
hour = time_int/100;
printf("%d:%d:%d\n",hour,mind,sec);
}

void main()
{
time_chang();
}