2016-11-21 59 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,這將是合適的。 –