提问人:Thelanie 提问时间:9/12/2023 最后编辑:chqrlieThelanie 更新时间:10/10/2023 访问量:119
将模量的剩余部分转换为分钟
Converting remainder of modulus to minutes
问:
我在下面包含了我的代码,试图从用户那里获得速度和他们需要旅行的距离,并计算旅行将花费多少小时和分钟。
但是我的分钟数一直在显示,我从变量中的模运算符中得到了正确的剩余部分。这是否与变量的转换/类型有关?0
Temp
我希望比我好一点的人能看到错误。
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Initialise the variables we will use
float Speed, Distance, Remainder = 0.0;
int Hours, Minutes, Temp = 0;
//Tell the user what the program does
printf("This program will take a distance and a speed and calculate your driving time!\n");
//Take user input
printf("please enter the speed you are driving at: ");
scanf("%f", &Speed);
printf("Enter the distance you will travel: ");
scanf("%f", &Distance);
//calculate the result`your text`
Hours = Distance / Speed;
Temp = (int)Distance % (int)Speed;
printf("temp = %d\n", Temp);
Minutes = (Temp / 100) * 60;
//Print out the result
printf("If you are traveling %.2f km at the speed %.2f km/h you will arrive in %d hours and %d minutes\n",
Distance, Speed, Hours, Minutes);
return 0;
}
尝试更改类型并以不同的方式转换一些变量,但只是继续交付.0
答:
OP 的方法独立于边缘和一致性问题确定并产生边缘和一致性问题。 没什么意义。Hours, Minutes
Distance
Speed
/ 100
通过将计算从同一个单个 FP 值派生的整数中移动,避免残差和舍入问题。Hours, Minutes
// Total time in minutes using FP math and then round.
long Total = lround(60.0 * Distance / Speed);
// Then separate with integer math
Hours = Total / 60;
Minutes = Total % 60;
在这里,形成一个通用的单个量,称为以分钟为单位的总时间并绕过错误。Hours, Minutes
Total
/ 100
此外,作为所寻求时间的用途可能最好四舍五入到最接近的分钟,而不是截断。Total = lround(60.0 * Distance / Speed)
lround()
简单地说:
long MinuteTime = lround(60.0 * Distance / Speed);
printf("If you are traveling %.2f km at the speed %.2f km/h"
" you will arrive in %ld hours and %ld minutes\n",
Distance, Speed, MinuteTime/60, MinuteTime%60);
测试您的代码时,问题出在此代码块中。
//calculate the result`your text`
Hours = Distance / Speed;
Temp = (int)Distance % (int)Speed;
printf("temp = %d\n", Temp);
Minutes = (Temp / 100) * 60;
我不确定将“Temp”除以“100”的理由是什么,但这并没有提供适当的剩余时间。
这样,下面是一个重构的代码块。
//calculate the result`your text`
Hours = Distance / Speed;
Temp = (int)Distance - Hours * (int)Speed; /* Get remaining distance to go */
printf("temp = %d\n", Temp);
Minutes = Temp * 60 / Speed; /* Derive remaining time to cover remainder */
需要注意的主要事项是剩余公里数的“温度”值的推导和剩余分钟的推导,将整数乘法按正确的顺序排列。重构这些位后,接下来是对程序的测试。
craig@Vera:~/C_Programs/Console/Speed/bin/Release$ ./Speed
This program will take a distance and a speed and calculate your driving time!
please enter the speed you are driving at: 90
Enter the distance you will travel: 120
temp = 30
If you are traveling 120.00 km at the speed 90.00 km/h you will arive in 1 hours and 20 minutes
试一试。
评论
Speed
Speed == 0.5
尝试:
Minutes = (Temp / Speed) * 60;
而不是:
Minutes = (Temp / 100) * 60;
代码的问题在于如何计算分钟数。您将 Temp 除以 100,这对于计算剩余分钟数是不正确的。
下面是代码的更正版本:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Initialize the variables we will use
float Speed, Distance;
int Hours, Minutes;
// Tell the user what the program does
printf("This program will take a distance and a speed and calculate your driving time!\n");
// Take user input
printf("Please enter the speed you are driving at: ");
scanf("%f", &Speed);
printf("Enter the distance you will travel: ");
scanf("%f", &Distance);
// Calculate the result
float timeInHours = Distance / Speed;
Hours = (int)timeInHours; // Get the whole hours
Minutes = (timeInHours - Hours) * 60; // Get the remaining minutes
// Print out the result
printf("If you are traveling %.2f km at the speed %.2f km/h you will arrive in %d hours and %d minutes\n",
Distance, Speed, Hours, Minutes);
return 0;
}
在这个更正后的代码中,我分别计算小时和分钟。我首先将总时间(以小时为单位)计算为浮点值 (timeInHours),然后从中提取整个小时和剩余分钟数。分钟的计算方法是从总时间(以小时为单位)中减去整个小时,然后乘以 60 将剩余部分转换为分钟。这可确保您获得小时和分钟的正确结果。
评论
Minutes = (Temp / 100) * 60;
:并且是 s。您需要先乘以 60,然后再除以 100。考虑一下如果说 80 会发生什么。不过还有更多问题。看看如果距离小于速度会发生什么。Minutes
Temp
int
Temp
float h = Distance / Speed; Hours = h; float m = (h - Hours) * 60; Minutes = m; float s = (m - Minutes) * 60; Seconds = s; printf("%d hours %d minutes %d seconds\n", Hours, Minutes, Seconds);
Seconds = 59.9
int t = round(60 * 60 * Distance / Speed);