برنامه مقایسه دو زمان time به زبان سی پلاس پلاس cpp
جمعه, ۱۶ مهر ۱۳۹۵، ۰۳:۱۷ ق.ظ
gameover.blog.ir
تئوری مثال زیر شاید در مرحله اول کمی پیچیه به نظر بیاد اما خیلی اسونه. میاد از یک ساختار struct ساده شامل ثانیه و دقیقه و ساعت استفاده می کنه و وقتی دیفرنس یا تفرق رو انجام میده چک می کنه اگه ثانیه زمان اول عددش از ثانیه زمان دوم کمتر بود از دقیقه یکی قرض می گیره و 60 تا به ثانیه زمان اول اضاف می کنه تا بدونه تفرق انجام بده و همینطور الی آخر(مثل تفرق عادی دو عدد که اونجا از رقم سمت چپ عدد بالایی یه دونه کم میشد و ده تا به رقم راست اضافه میشد اینجا 60 تا چون ثانیه ماکزیمم اش 60 هست):
#include <iostream>
using namespace std;
struct TIME{
int seconds;
int minutes;
int hours;
};
void Difference(struct TIME t1, struct TIME t2, struct TIME *diff);
int main(){
struct TIME t1,t2,diff;
cout << "Enter start time: " << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t1.hours >> t1.minutes >> t1.seconds;
cout << "Enter stop time: " << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t2.hours >> t2.minutes >> t2.seconds;
Difference(t1,t2,&diff);
cout << endl << "TIME DIFFERENCE: " << t1.hours << " : " << t1.minutes << " : " << t1.seconds;
cout << " - " << t2.hours << " : " << t2.minutes << " : " << t2.seconds;
cout << "= " << diff.hours << " : " << diff.minutes << " : " << diff.seconds;
return 0;
}
void Difference(struct TIME t1, struct TIME t2, struct TIME *differ){
if(t2.seconds>t1.seconds){
--t1.minutes;
t1.seconds+=60;
}
differ->seconds=t1.seconds-t2.seconds;
if(t2.minutes>t1.minutes){
--t1.hours;
t1.minutes+=60;
}
differ->minutes=t1.minutes-t2.minutes;
differ->hours=t1.hours-t2.hours;
}
Output
Enter start time: Enter hours, minutes and seconds respectively: 12 34 55 Enter stop time: Enter hours, minutes and seconds respectively:8 12 15 TIME DIFFERENCE: 12 : 34 : 55 - 8 : 12 : 15= 4 : 22 : 40
۹۵/۰۷/۱۶