יעול התוכנית

emissary

New member
יעול התוכנית

רציתי לדעת אם יש לכם דרך ליעל את התוכנית. בכלליות היא קולטת שעה של יציאה, ואת זמן הנסיעה, ומחשבת את שעת ההגעה
// This program reades time of departure (hours, minutes and seconds), and the // time of travelling (in seconds). // The program calucates the time of arrival (hours, minutes and seconds). // developer: Alon Kukliansky, Date: 24/9/2002 #include<iostream.h> void main() { long start_hour, start_min, start_sec, starttime_sec, timeoftravel_sec, temp; long end_hour, end_min, end_sec; cout <<"This program caculates the time of arrival. \n"; cout <<"Enter the time of departure (hour/minute/second), "; cout <<"and enter the travel time in seconds.\n"; // Reading the departure time cin >> start_hour >> start_min >> start_sec >> timeoftravel_sec; cout <<"If you will leave at "<<start_hour<<":"<<start_min<<":"<<start_sec; cout <<".\nAnd you will drive "<<timeoftravel_sec<<" seconds.\n"; // Calculatting the arrival time starttime_sec = start_hour * 3600 + start_min * 60 + start_sec; temp = timeoftravel_sec + starttime_sec; end_sec = temp % 60; end_hour = (temp / 3600); end_min = ((temp / 60) % 60); // Printing the arrival time cout <<"You will arrive at "<<end_hour<<":"<<end_min<<":"<<end_sec<<"."; }​
זה
 

emissary

New member
תיקון טעות

בצורה הקודמת השעות לא היו נכנות עכשיו זה כן, אבל אני רוצה לדעת אם יש לכם אלוגריתם יותר טוב מזה, כי פה אני הופך שעות לשניות ובחזרה שוב לשניות שזה די בזבוז...
// This program reades time of departure (hours, minutes and seconds), and the // time of travelling (in seconds). // The program calucates the time of arrival (hours, minutes and seconds). // developer: Alon Kukliansky, Date: 24/9/2002 #include<iostream.h> void main() { long start_hour, start_min, start_sec, starttime_sec, timeoftravel_sec, temp; long end_hour, end_min, end_sec; cout <<"This program caculates the time of arrival. \n"; cout <<"Enter the time of departure (hour/minute/second), "; cout <<"and enter the travel time in seconds.\n"; // Reading the departure time cin >> start_hour >> start_min >> start_sec >> timeoftravel_sec; cout <<"If you will leave at "<<start_hour<<":"<<start_min<<":"<<start_sec; cout <<".\nAnd you will drive "<<timeoftravel_sec<<" seconds.\n"; // Calculatting the arrival time starttime_sec = start_hour * 3600 + start_min * 60 + start_sec; temp = timeoftravel_sec + starttime_sec; end_sec = temp % 60; end_hour = temp / 3600 - 24 * ((temp/3600) / 24); end_min = ((temp / 60) % 60); // Printing the arrival time cout <<"You will arrive at "<<end_hour<<":"<<end_min<<":"<<end_sec<<"."; }​
 

Zack DA

New member
זה לא באמת משנה כאן

ברמה העקרונית הפתרון שלך מתבסס על מספר סופי, קבוע וידוע מראש של פעולות (זה נקרא (1)O ). יתכן ואפשר אולי במשחק עם הזמן והפעולות לחסוך המרה אחת, אבל מבחינת ניתוח יעילות התוכנית שלך אופטימלית ממילא. אגב דרך העבודה שלך היא נוחה מאוד ולכן עשייה מסודרת שעולה לך בפעולה יותר היא עדיפה מדרך מתחכמת ומסורבלת שקל יותר לטעות בה. עד כאן. צחי
 
למעלה