程序应该将用户输入的分钟数转换为天,小时和分钟
#include <stdio.h>
int main(void) {
int input_minutes, minutes, r_minutes;
int days, hours;
float r_days, r_hours;
printf("Enter a number of minutes: ");
scanf("%d", &input_minutes);
days = input_minutes / 1440;
这里是我希望将input_minutes的剩余部分除以1440,而是在编译时得到160.00007。
r_days = input_minutes % 1440;
hours = r_days * 24;
r_hours = (r_days * 1440) - hours;
minutes = r_hours * 60;
printf("r_days = %f", r_days);
printf("This represents:%d days, %d hours, %d minutes\n",days, hours, minutes);
答案 0 :(得分:4)
此处没有合理的理由使用float
类型(除了整数模数外,不涉及除法)。使用浮动只会导致精度损失和问题代表&amp;正如您所经历的那样正确显示整数。
的quickfix:
int r_days, r_hours;
和
printf("r_days = %d", r_days);
答案 1 :(得分:2)
您可能想要更改计算小时和分钟的逻辑。天计算似乎没问题。
r_days = input_minutes % 1440;
hours = r_days/60;
minutes = r_days % 60;
printf("This represents:%d days, %d hours, %d minutes\n",days, hours, minutes);