简体   繁体   English

将时间输入转换为float以在C ++中启用数学函数

[英]Convert time input to a float to enable math functions in C++

I am asking the user to input a start time and an end time in this format: 15.45 (military time with a decimal instead of a colon) and I need to convert those times to a float to perform calculations on them. 我要求用户以这种格式输入开始时间和结束时间:15.45(带小数而不是冒号的军事时间)我需要将这些时间转换为浮点数来执行计算。 I am having trouble wrapping my mind around converting the 60 minutes of an hour to a decimal value. 我无法将60分钟的时间转换为小数值。 eg - User inputs start of 12.45 and end of 14.15, this should be converted to 12.75 and 14.25 respectively. 例如 - 用户输入12.45开始和14.15结束,应分别转换为12.75和14.25。 How would I go about this conversion? 我将如何进行此转换?

Also, something I believe I am more capable of figuring out, but curious anyway: how would I validate input so as not to allow a time greater than 23.59 and no time with the last two digits greater than 59? 另外,我认为我更有能力搞清楚,但无论如何好奇:我如何验证输入以便不允许大于23.59的时间和最后两位大于59的时间?

just use 只是用

double time = hours + minutes / 60.0;
bool hoursValid = hours < 24; // hours are unsigned, right?
bool minutesValid = minutes < 60;

Example: 例:

char buf[] = "9.45";
unsigned int hours, minutes;
sscanf(buf, "%d.%d", &hours, &minutes);
printf("%f", hours + minutes/60.0); // outputs 9.75

If you get double 9.45 as input, you need to #include <cmath> and split it as 如果你得到double 9.45作为输入,你需要#include <cmath>并将其拆分为

hours = floor(v);
minutes = (v - hours) * 100;

As soon as one of the values is double , then the arithmetic will be done in double , with double results. 只要其中一个值为double ,则算法将以double ,结果为double So you can convert the minutes to double , using any of the three C++ syntaxes: static_cast<double>(minutes) , double(minutes) or (double)minutes , or just divide by 60.0 , rather than the integer 60 . 因此,您可以使用以下三种C ++语法中的任何一种将minutes转换为doublestatic_cast<double>(minutes)double(minutes)(double)minutes ,或者只是除以60.0 ,而不是整数60 (I like to be explicit, so I'd write double(minutes) / 60.0 . And some people prefer static_cast , even in this case.) (我喜欢明确,所以我会写double(minutes) / 60.0 。有些人更喜欢static_cast ,即使在这种情况下。)

With regards to the validation, I'd do it before conversion; 关于验证,我会在转换之前完成; once you've added the minutes to the hours, it's too late anyway. 一旦你把时间分钟添加到了几个小时,那就太晚了。

You have to separate the integer and decimal components of the number. 您必须分隔数字的整数和小数部分。 You will keep the integer part intact, then add the decimal part divided by 60 to it. 您将保持整数部分不变,然后将小数部分除以60。

Validation is also simple once you separate into hours and minutes. 一旦分成小时和分钟,验证也很简单。

I'm going to assume your code reads the value in from the user as a string. 我将假设您的代码从用户读取值作为字符串。 You will need to parse the user input at the decimal point to get the hours and minutes as two separate values. 您需要在小数点处解析用户输入,以将小时和分钟作为两个单独的值。 Once you have that, then you can convert minutes into a fraction of an hour and add it to the number of hours. 完成后,您可以将分钟转换为一小时,然后将其添加到小时数。

bool military_time_valid(float a) {
    int t = (int)a;
    if (t >= 24)
        throw std::runtime_error("invalid hours");
    if (a-t >= .6)
        throw std::runtime_error("invalid minutes");
}
float military_to_decimal(float a) {
    int t = (int)a;
    return t + (a-t)/.6;
}
float decimal_to_military(float a) {
    int t = (int)a;
    return t + (a-t)*.6;
}
float t = 15.45;
int h = (int) t;
float m = 100 * (t - h);
printf("%f", h + m / 60);

gives output: 15.750000 给出输出: 15.750000

Check if h is between 0 and 23 and m is between 0 and 59 . 检查h是否在023之间, m059之间。

Is there some reason you don't want to create a MilTime class and overload the arithmetic operators? 是否有一些原因你不想创建一个MilTime类并重载算术运算符? That would save you from having to convert to/from all the time, and would more clearly state your intentions. 这样可以避免您不得不一直转换为/从而更清楚地表达您的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM