我编写了这个程序来以小时、分钟和秒为单位打印时间
#include<stdio.h>
int main()
{
float a, b,x,mina,minb,hrsa,hrsb;
float seca,secb;
FILE *fp = fopen("/proc/uptime", "r");
fscanf(fp, "%f %f", &a, &b);
seca=(a)%60;
mina=seca/60;
hrsa=mina/60;
secb=(b)%60;
minb=secb/60;
hrsb=minb/60;
printf("Uptime=%f hrs %f min %f sec",hrsa,mina,seca);
printf("Idealtime =%f hrs %f min %f sec",hrsb,minb,secb);
return 0;
}
我在第 12 行和第 16 行收到错误“二进制操作数无效”。
最佳答案
你不能对 float 做 mod (%
)。尝试((long)a)%60;
甚至(long long)
,但是1<<31秒提供的68年应该足够了.. ..
此外,您还需要对分钟进行另一个模运算,否则 3601 秒将变为“1 小时 60 分 1 秒”,而不是“1 小时 0 分 1 秒”。
关于C 程序以小时、分钟和秒为单位打印正常运行时间。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707806/