返回列表 發帖

C++第二題:計算兩個時間 (時:分:秒) 的間隔秒數。

本帖最後由 stephen 於 2010-2-20 23:30 編輯

C++第二題:計算兩個時間 (時:分:秒) 的間隔秒數。
  1. /*2.計算兩個時間 (時:分:秒) 的間隔秒數。*/
  2. #include <iostream>
  3. using namespace std;
  4. int main(void){
  5.    
  6.     int h1, h2, m1, m2, s1, s2, time_1, time_2, time;
  7.    
  8.     cout << "Please enter first time : " << endl;
  9.     cout << "Hours : " << endl;
  10.     cin >> h1;
  11.     cout << "Minutes : " << endl;
  12.     cin >> m1;
  13.     cout << "Seconds : " << endl;
  14.     cin >> s1;
  15.    
  16.     cout << "Please enter senond time : " << endl;
  17.     cout << "Hours : " << endl;
  18.     cin >> h2;
  19.     cout << "Minutes : " << endl;
  20.     cin >> m2;
  21.     cout << "Seconds : " << endl;
  22.     cin >> s2;
  23.    
  24.     time_1 = h1*3600 + m1*60 + s1;  // the total seconds
  25.     time_2 = h2*3600 + m2*60 + s2;  // the total seconds
  26.    
  27.     if(time_1 > time_2){
  28.              time = time_1 - time_2;
  29.     }else if(time_1 < time_2){
  30.                 time = time_2 - time_1;
  31.     }else if(time_1 == time_2){
  32.           cout << "兩個時間相差零秒。" << endl;
  33.     }
  34.    
  35.     cout << "兩個時間相差" << endl;
  36.     cout << time / 3600 << " : ";  // get hours
  37.     time = time - ((time / 3600) * 3600);  // get second cut hours
  38.     cout << time / 60 << " : ";  // get Minutes
  39.     time = time - ((time / 60) * 60);  // get second cut Minutes
  40.     cout << time;  // seconds
  41.    
  42.     system("pause");
  43.     return 0;
  44. }
複製代碼

返回列表