標題:
C++第二題:計算兩個時間 (時:分:秒) 的間隔秒數。
[打印本頁]
作者:
stephen
時間:
2010-2-20 11:26
標題:
C++第二題:計算兩個時間 (時:分:秒) 的間隔秒數。
本帖最後由 stephen 於 2010-2-20 23:30 編輯
C++第二題:計算兩個時間 (時:分:秒) 的間隔秒數。
/*2.計算兩個時間 (時:分:秒) 的間隔秒數。*/
#include <iostream>
using namespace std;
int main(void){
int h1, h2, m1, m2, s1, s2, time_1, time_2, time;
cout << "Please enter first time : " << endl;
cout << "Hours : " << endl;
cin >> h1;
cout << "Minutes : " << endl;
cin >> m1;
cout << "Seconds : " << endl;
cin >> s1;
cout << "Please enter senond time : " << endl;
cout << "Hours : " << endl;
cin >> h2;
cout << "Minutes : " << endl;
cin >> m2;
cout << "Seconds : " << endl;
cin >> s2;
time_1 = h1*3600 + m1*60 + s1; // the total seconds
time_2 = h2*3600 + m2*60 + s2; // the total seconds
if(time_1 > time_2){
time = time_1 - time_2;
}else if(time_1 < time_2){
time = time_2 - time_1;
}else if(time_1 == time_2){
cout << "兩個時間相差零秒。" << endl;
}
cout << "兩個時間相差" << endl;
cout << time / 3600 << " : "; // get hours
time = time - ((time / 3600) * 3600); // get second cut hours
cout << time / 60 << " : "; // get Minutes
time = time - ((time / 60) * 60); // get second cut Minutes
cout << time; // seconds
system("pause");
return 0;
}
複製代碼
作者:
yachen392
時間:
2010-2-20 11:33
/**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;
}
複製代碼
作者:
p17johnny
時間:
2010-2-23 17:16
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void){
int h , min , s , ss , min2 , s2;
cout << "Please enter a second : " << endl;
cin >> ss;
s = ss % 60;
s2 = ss / 60;
min = s2 % 60;
min2 = s2 / 60;
h = min2 % 24;
cout << h <<" : "<< min <<" : "<< s << endl;
system("pause");
return 0;
}
複製代碼
作者:
tony
時間:
2010-4-16 19:01
/**1. 將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40。 */
#include <iostream>
using namespace std;
int main(void){
int s;
cin >> s ; //輸入秒數
int h = s / 3600; //算出小時
int m = s / 60 - h * 60 ;//算出分鐘
int se = s - (h * 3600 + m * 60);//算出剩餘秒數
cout << h << ":" << m << ":" << se << endl ;
system("pause");
return 0;
}
複製代碼
作者:
tony
時間:
2010-4-16 19:25
#include <iostream>
using namespace std;
int main(void){
int h;
int m;
int s;
cout << "輸入小時";
cin >> h;
cout << "輸入分鐘";
cin >> m;
cout << "輸入秒";
cin >> s;
cout << h << ":" << m << ":" << s << endl ;
int h2;
int m2;
int s2;
cout << "輸入小時";
cin >> h2;
cout << "輸入分鐘";
cin >> m2;
cout << "輸入秒";
cin >> s2;
cout << h2 << ":" << m2 << ":" << s2 << endl ;
int hh = (h - h2) * 3600;
int mm = (m - m2) * 60 ;
int ss = s - s2;
int t = hh + mm + ss;
cout << "秒數差:";
if(t < 0){
int tt = t * -1 ;
cout << tt;
}else{
cout << t;
}
system("pause");
return 0;
}
複製代碼
作者:
abc3806198
時間:
2010-4-16 19:25
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void){
int s1;
int m1;
int h1;
int s2;
int m2;
int h2;
//cout << "輸入秒數" << endl;
cin >> h1;
cin >> m1;
cin >> s1;
cout << h1 << ":" << m1 << ":" << s1 << endl;
cin >> h2;
cin >> m2;
cin >> s2;
// h1 = s1 / 3600;
//m1 = (s1-h1*3600) / 60;
//s1 = s1 - (h1*3600) - (m1*60);
cout << h2 << ":" << m2 << ":" << s2 << endl;
int hh = (h1 - h2) * 60;
int mm = (m1 - m2) * 3600;
int ss = s1 - h2;
int t = hh + mm + ss;
cout << "秒數差:" <<endl;
if(t<0){
int tt = t* -1;
cout << tt;
}else{
cout << t;
}
system("pause");
return 0;
}
複製代碼
作者:
johnsom127
時間:
2010-8-16 20:03
Please enter first time :
Hours :
1
Minutes :
0
Seconds :
1
Please enter senond time :
Hours :
0
Minutes :
0
Seconds :
3601
兩個時間相差零秒。
兩個時間相差
1113 : 3 : 52請按任意鍵繼續 . . .
複製代碼
程式錯誤
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2