该计划的目标是计算开始时间和结束时间之间的时差。
开始和结束时间将作为4位数字整数输入,并且需要计算时间差。输入的时间表示为hh:mm,没有“:”。
function ValidName(){
var regex = /^[a-zA-Z]$/;
var name = $("#txtNome").val();
if(name == ""){
alert("Please, type your name.");
name.focus();
return false;
}
else if(name =! name.regex){
alert("Please, type your name.");
name.focus();
return false;
}
}
这是我的代码:
Example:
first time: 0800
second time: 1755
Time elapsed: 9 hr and 55 min
问题是它确实以小时为单位输出时间。任何有关如何改进该计划的建议将不胜感激。
答案 0 :(得分:0)
最好在几分钟内转换所有内容,计算差异,然后转换为小时和分钟:
#include <iostream>
int main()
{
int first_time;
int second_time;
std::cout << "Enter first time" << std::endl;
std::cin >> first_time;
std::cout << "Enter second time" << std::endl;
std::cin >> second_time;
int first_time_min = first_time / 100 * 60 + first_time % 100;
int second_time_min = second_time / 100 * 60 + second_time % 100;
int diff_time_min = second_time_min - first_time_min;
std::cout << "Time elapsed: " << diff_time_min / 60 << " hours " << diff_time_min % 60 << " minutes" << std::endl;
}
答案 1 :(得分:-1)
可能的解决方案:
#include <iostream>
int main() {
int first_time;
int second_time;
int dif_time_in_minutes;
int hours, minutes;
std::cout << "Enter first time:";
std::cin >> first_time;
std::cout << "Enter second time:";
std::cin >> second_time;
dif_time_in_minutes = (second_time / 100)*60 + (second_time % 100) -
(first_time / 100) * 60 + (first_time % 100);
hours = dif_time_in_minutes / 60;
minutes = dif_time_in_minutes % 60;
std::cout << "Time elapsed: " << hours << " hours " << minutes << " minutes" << std::endl;
}