- /**2.計算兩個時間(時:分:秒)的間隔時間**/
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- { int h1,m1,s1,total1;
- int h2,m2,s2,total2;
- int time,hour,min,sec;
- cout << "Please enter first time:" << endl ;
- cout << "Hour:" << endl;
- cin >> h1;
- cout << "Minute:" << endl;
- cin >> m1;
- cout << "Second:" << endl;
- cin >> s1;
-
- cout << "Please enter second time:" << endl ;
- cout << "Hour:" << endl;
- cin >> h2;
- cout << "Minute:" << endl;
- cin >> m2;
- cout << "Second:" << endl;
- cin >> s2;
-
- total1=h1*3600+m1*60+s1;
- total2=h2*3600+m2*60+s2;
- if(total1>total2)
- {
- time=total1-total2;
- }
- else if(total2>total1)
- {
- time=total2-total1;
- }
- else if(total2=total1)
- {
- cout << "兩數相差零秒" << endl;
- }
- hour=time/3600;
- time=time-(hour*3600);
- min=time/60;
- time=time-(min*60);
-
- cout << "兩數相差" << hour << ":" << min << ":" << time <<endl;
-
- system("pause");
- return 0;
- }
複製代碼 |