2016-11-21 70 views
-1

我写这应该询问用户当前时间的“时间旅行”计划,其目标行驶时间,转发这些值转换成以分钟为单位一个函数,然后根据在所述时间差过高,也不会允许时间旅行,或者如果它被允许,如果他们在未来或过去,它会打印。我的问题现在的问题是,目前的时间,这是唯一的应该是相关的第一次迭代,或者该程序的跳转发生后未更新的第一个“跳”,程序默认为它而不是目标时间,这应该是跳跃发生后的技术“当前时间”。我一直试图找出几个小时,但我不能,所以我希望有人能帮助我。程序计算两个时间之间的区别为“时间旅行”

谢谢您的时间

#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 

int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs,  int t_mins, bool t_am); 
void print_future(); 
void print_past(); 
void print_SecurityProtocol(); 
int main() 
{ 
int c_hrs, c_mins; 
int t_hrs, t_mins; 
int system_time2; 
int time_difference2; 
bool c_am = 0; 
bool t_am = 0; 
char c, c2, Y, N, y, n, jumpAgain; 
string am_or_pm_current, am_or_pm_target; 
bool g_stop = true; 
cout << "Welcome to Po Sled" << endl; 
cout << "\tSystem booting..." << endl; 
for (int i = 0; i<1; i++) //for loop to run once 
{ 
    cout << "\n\tEnter current time below: " << endl; 
    cout << "\t>Enter hour: "; //User inputs current time in hours 
    cin >> c_hrs; 
    while (c_hrs > 12 || c_hrs < 1) 
    { 
     cout << "Error: Please enter an hour in the range [1, 12]: "; 
     cin >> c_hrs; 
    } 
    cout << "\t>Enter minutes: "; //User inputs current time in minutes 
    cin >> c_mins; 
    while (c_mins > 59 || c_mins < 0) { 
     cout << "Error: Please enter minutes in the range [0, 59]: "; 
     cin >> c_mins; 
    } 
    cout << "\t>Is it AM or PM?: "; //Classifying if current time is AM or PM 
    cin >> am_or_pm_current; 
    while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") { //Checks if valid input, if not repeats message until valid 
     cout << "\tError: Please enter AM/am or PM/pm: "; 
     cin >> am_or_pm_current; 
    } 
    if ((am_or_pm_current == "am") || (am_or_pm_current == "AM")) 
    { 
     c_am = 1; 
    } 
    else if ((am_or_pm_current == "pm") || (am_or_pm_current == "PM")) 
    { 
     c_am = 0; 
    } 
    cout << "\n\tCurrent system time set to " << c_hrs << ":" << setw(2) << setfill('0') << c_mins << am_or_pm_current << endl; 
    cout << "\n\t\tIs this time correct (Y or N)? "; 
    cin >> c; 
    while (c != 'Y' && c != 'y' && c != 'n' && c != 'N') 
    { 
     cout << "\t\t\tError: Please enter Y/y or N/n: "; 
     cin >> c; 
    } 
    if (c == 'N' || c == 'n') 
    { 
     continue; 
    } 
    else 
    { 
     cout << "\n\tSystem initializing and TARDIS unit warming...." << endl; 
     cout << "\tThe Po Sled is engaged and ready for input." << endl; 
    } 
} 
do { 
     //Starts a loop for target jump 
     cout << "\n\tEnter target time below: " << endl; //Enters target time of jump 
     cout << "\t>Enter Hour: "; 
     cin >> t_hrs; 
     while (t_hrs > 12 || t_hrs < 1) { 
      cout << "Error: Please enter an hour in the range [1, 12]: "; 
      cin >> t_hrs; 
     } 
     cout << "\t>Enter minutes: "; 
     cin >> t_mins; 
     while (t_mins > 59 || t_mins < 0) { 
      cout << "\tError: Please enter minutes in the range [0, 59]: "; 
      cin >> t_mins; 
     } 
     cout << "\n\tIs it AM or PM?: "; 
     cin >> am_or_pm_target; //Classifying if target time is AM or PM 
     while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") 
     { //Validates input is AM or PM 
      cout << "\tError: Please enter AM/am or PM/pm: "; 
      cin >> am_or_pm_target; 
     } 
     if ((am_or_pm_target == "am") || (am_or_pm_target == "AM")) 
     { 
      t_am = 1; 
     } 
     else if ((am_or_pm_target == "pm") || (am_or_pm_target == "PM")) 
     { 
      t_am = 0; 
     } 
     cout << "\tTarget time set to " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target; //Sets target time 
     cout << "\n\t\tIs this time correct (Y or N)? "; //Validates if target time entered is correct 

     cin >> c2; 
     while (c2 != 'Y' && c2 != 'y' && c2 != 'n' && c2 != 'N') 
     { 
      cout << "\t\t\tError: Please enter Y/y or N/n: "; 
      cin >> c2; 
     } 
     time_difference2 = compute_time_difference(c_hrs, c_mins, c_am, t_hrs, t_mins, t_am); 

     if (time_difference2 > 360) //If time difference is greater than 6 hours prints error function 
     { 
      print_SecurityProtocol(); 
      continue; 
     } 
     if (c2 == 'N' || c2 == 'n') 
     { 
      continue; 
     } 
     cout << "\tJump was made, the current time is " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target << endl; 

     if (time_difference2 < 0 && time_difference2 > -360) //If time difference is less than 0 prints past function 
     { 
      print_past(); 
     } 
     else if (time_difference2 >= 0 && time_difference2 <= 360) //If time difference is ahead of current time prints future function 
     { 
      print_future(); 
     } 
     cout << "\tWould you like to jump again (Y/N)? "; 
     cin >> jumpAgain; 
     while (jumpAgain != 'Y' && jumpAgain != 'y' && jumpAgain != 'n' && jumpAgain != 'N') //Input validation 
     { 
      cout << "\t\t\tError: Please enter Y/y or N/n: "; 
      cin >> jumpAgain; 
     } 
     if (jumpAgain == 'n' || jumpAgain == 'N') //User exiting program 
     { 
      if (time_difference2 < 0) 
      { 
       cout << "\t\tSystem shutting down; enjoy the past.\n" << endl; 
      } 
      else if (time_difference2 >= 0 && time_difference2 < 360) 
      { 
       cout << "\t\tSystem shutting down; enjoy the future.\n" << endl; 
      } 
     } 
     if (jumpAgain == 'Y' || jumpAgain == 'y') 
     { 
      continue; 
     } 

} while (jumpAgain != 'n' && jumpAgain != 'N'); 
return 0; 
} 

int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs,  int t_mins, bool t_am) //Computes time differences. 
{ 
int currentTime_hours, currentTime_minutes, targetTime_hours, targetTime_minutes, currentTime, targetTime; 
int time_difference; 
if (c_am == 1) //if c_am is true and it is morning 
{ 
    if (c_hrs == 12) 
    { 
     currentTime_hours = c_hrs * 0; 
    } 
    else if (c_hrs != 12) 
    { 
     currentTime_hours = (c_hrs * 60); 
     currentTime_minutes = c_mins; 
     currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time 
    } 
} 
else if (c_am == 0) //if c_am is false and it is afternoon time 
{ 
    if (currentTime_hours == 12) 
    { 
     c_hrs = c_hrs * 60; 
    } 
    else if (currentTime_hours != 12) 
    { 
     currentTime_hours = ((c_hrs + 12) * 60); 
     currentTime_minutes = c_mins; 
     currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time 
    } 
} 
if (t_am == 1) //if t_am is true and it is morning time 
{ 
    if (targetTime_hours == 12) //if target hours equal to 12 special math 
    { 
     targetTime_hours = t_hrs*0; 
    } 
    else if (targetTime_hours != 12) //else do this math 
    { 
     targetTime_hours = ((t_hrs) * 60); 
     targetTime_minutes = t_mins; 
     targetTime = targetTime_hours + targetTime_minutes; 
    } 
} 
else if (t_am == 0) //if target time equal to pm then do this math 
{ 
    if (targetTime_hours == 12) 
    { 
     targetTime_hours = t_hrs * 60; 
    } 
    else if (targetTime_hours != 12) //else if target time not equal to 12 then do normal pm math 
    { 
     targetTime_hours = ((t_hrs + 12) * 60); 
     targetTime_minutes = t_mins; 
     targetTime = targetTime_hours + targetTime_minutes; 
    } 
} 
time_difference = targetTime - currentTime; 
cout << "the difference computed is " << time_difference; 
return time_difference; 
} 

void print_SecurityProtocol() //Function which prints security protocol error message 
{ 
cout << "\tSecurity Protocols Engaging" << endl; 
cout << "\t\tError: The time difference is greater than 6 hours." << endl; 
cout << "\t\t\tPlease re-enter target time." << endl; 
} 
void print_past() //Function that prints when a user is in the past 
{ 
cout << "\tHold onto your lower posterior regions" << endl; 
cout << "\n\t\tYou are now in the relative past" << endl; 
} 
void print_future() //Function that prints when a user is in the future 
{ 
cout << "\tHold onto your lower posterior regions" << endl; 
cout << "\n\t\tYou are now in the relative future " << endl; 
} 
+0

'c_hrs = c_hrs * 60;' - 这是不正确的?我也无法看到您将当前时间设置为新输入时间的位置。 –

+0

@ KENY-N遗憾的是变回{currentTime_hours = c_hrs * 60;}也许那仍然不正确?此外,这个问题我不知道如何将当前时间设置为此程序中新输入的时间。我是否应该在跳转完成后让它像c_hrs = t_hrs等等。 –

回答

0

你为什么不使用系统的时候调用,而不是让用户输入“当前时间”?此外,使用24小时时钟,而不是12小时,且必须移除需要您的过程中满足AM/PM。 (至少)60小时和(最多)24 * 60,除了横跨欧洲,俄罗斯,澳大利亚或北美地区的驱动器之外,其他地方的驱动器将不会超过24小时60分钟。过度记录时间。你不是指MINUTES而不是小时(c_mins * 60)?

+0

我正在写的程序必须使用12小时的输入系统,否则我一定会使用24小时制。另外,为了比较,我将小时值转换为总分钟数。 {c_hrs * 60}将小时值乘以60得到总分钟值。例如24 * 60将是整整一天的分钟,并且在下午1点将是13小时* 60,这将是合适的。 –

相关问题