返回列表 發帖

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. }
複製代碼

  1. /**2.計算兩個時間(時:分:秒)的間隔時間**/
  2. #include<iostream>
  3. #include<cstdlib>
  4. using namespace std;

  5. int main()
  6. {  int h1,m1,s1,total1;
  7.    int h2,m2,s2,total2;
  8.    int time,hour,min,sec;
  9.    cout << "Please enter first time:" << endl ;
  10.    cout << "Hour:" << endl;
  11.    cin >> h1;
  12.    cout << "Minute:" << endl;
  13.    cin >> m1;
  14.    cout << "Second:" << endl;
  15.    cin >> s1;
  16.    
  17.    cout << "Please enter second time:" << endl ;
  18.    cout << "Hour:" << endl;
  19.    cin >> h2;
  20.    cout << "Minute:" << endl;
  21.    cin >> m2;
  22.    cout << "Second:" << endl;
  23.    cin >> s2;
  24.    
  25.    total1=h1*3600+m1*60+s1;
  26.    total2=h2*3600+m2*60+s2;
  27.    if(total1>total2)
  28.    {
  29.       time=total1-total2;               
  30.    }
  31.    else if(total2>total1)
  32.    {
  33.       time=total2-total1;               
  34.    }
  35.    else if(total2=total1)
  36.    {
  37.       cout << "兩數相差零秒" << endl;               
  38.    }
  39.    hour=time/3600;
  40.    time=time-(hour*3600);
  41.    min=time/60;
  42.    time=time-(min*60);
  43.    
  44.    cout << "兩數相差" << hour << ":" << min << ":" << time <<endl;
  45.    
  46.    system("pause");
  47.    return 0;
  48. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main(void){
  5.    
  6.     int h , min , s , ss , min2 , s2;
  7.    
  8.     cout << "Please enter a second : " << endl;
  9.     cin >> ss;
  10.     s = ss % 60;
  11.     s2 = ss / 60;
  12.     min = s2 % 60;
  13.     min2 = s2 / 60;
  14.     h = min2 % 24;
  15.    
  16.     cout << h <<" : "<< min <<" : "<< s << endl;
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼
分數掛蛋的心情像空白的紙,再次期望著奇蹟的到來。

TOP

  1. /**1. 將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40。 */
  2. #include <iostream>
  3. using namespace std;
  4. int main(void){
  5.   
  6.   int s;
  7.   cin >> s ; //輸入秒數
  8.   int h = s / 3600; //算出小時
  9.   int m = s / 60 - h * 60 ;//算出分鐘
  10.   int se = s - (h * 3600 + m * 60);//算出剩餘秒數
  11.   cout << h << ":" << m << ":" << se << endl ;
  12.     system("pause");
  13.     return 0;
  14. }   
複製代碼

TOP

  1. #include <iostream>
  2. using namespace std;
  3. int main(void){
  4.    
  5.   int h;
  6.   int m;
  7.   int s;
  8.   cout << "輸入小時";
  9.   cin >> h;
  10.   cout << "輸入分鐘";
  11.   cin >> m;
  12.   cout << "輸入秒";
  13.   cin >> s;
  14. cout << h << ":" << m << ":" << s << endl ;
  15.   int h2;
  16.   int m2;
  17.   int s2;
  18.   cout << "輸入小時";
  19.   cin >> h2;
  20.   cout << "輸入分鐘";
  21.   cin >> m2;
  22.   cout << "輸入秒";
  23.   cin >> s2;
  24. cout << h2 << ":" << m2 << ":" << s2 << endl ;
  25. int hh = (h - h2) * 3600;
  26. int mm = (m - m2) * 60 ;
  27. int ss = s - s2;
  28. int t = hh + mm + ss;
  29. cout << "秒數差:";
  30. if(t < 0){
  31.       int tt = t * -1 ;
  32.       cout << tt;
  33. }else{
  34.       cout <<  t;
  35.       }
  36.            
  37.       
  38.     system("pause");
  39.     return 0;
  40. }   
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main(void){

  5.     int s1;
  6.     int m1;
  7.     int h1;
  8.     int s2;
  9.     int m2;
  10.     int h2;


  11.     //cout << "輸入秒數" << endl;
  12.     cin >> h1;
  13.     cin >> m1;
  14.     cin >> s1;
  15.    
  16.     cout << h1 << ":" << m1 << ":" << s1 << endl;
  17.    
  18.     cin >> h2;
  19.     cin >> m2;
  20.     cin >> s2;

  21.     // h1 = s1 / 3600;
  22.     //m1 = (s1-h1*3600) / 60;
  23.     //s1 = s1 - (h1*3600) - (m1*60);

  24.   
  25.     cout << h2 << ":" << m2 << ":" << s2 << endl;

  26.     int hh = (h1 - h2) * 60;
  27.     int mm = (m1 - m2) * 3600;
  28.     int ss = s1 - h2;
  29.     int t = hh + mm + ss;
  30.    
  31.     cout << "秒數差:" <<endl;
  32.    
  33.     if(t<0){
  34.         int tt = t* -1;
  35.         cout << tt;
  36.     }else{
  37.         cout << t;  
  38.     }
  39.    
  40.     system("pause");

  41.     return 0;

  42. }
複製代碼
明輝

TOP

  1. Please enter first time :
  2. Hours :
  3. 1
  4. Minutes :
  5. 0
  6. Seconds :
  7. 1
  8. Please enter senond time :
  9. Hours :
  10. 0
  11. Minutes :
  12. 0
  13. Seconds :
  14. 3601
  15. 兩個時間相差零秒。
  16. 兩個時間相差
  17. 1113 : 3 : 52請按任意鍵繼續 . . .
複製代碼
程式錯誤
我是吳先生SIR

TOP

返回列表