返回列表 發帖
  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

返回列表