שאלה 3
Back Up Next

 

פתרון

/*
  Module: tr3_q3.cpp
  Author: Yariv Amar
  What? This program gets time adds time in seconds to a givven time.
  Input : Starting time format HH MM SS and duration in seconds.
  Output: Accurate time of arrivel.
  Assumptions on input: No assumptions cheacking all.
  How?
  Algoritem: Converting starting time into real number using the 
  trigonometric circle principle (the time is like an angle). Then adding
  duration time, and finally converting the number back into regular 
  known format of time.
*/
#include <iostream.h>
void main()
{
	int HH , MM , SS; //declering on Hours,Minuts,Seconds
	int Duration;  //duration time in seconds
	int	Error_flag=0; //logic flag that indicates of an error.
	double Temp_time; //temporary filed to help in converting
	cout<<"Please enter starting time in format HH MM SS and duration in seconds:\n";
	cout<<"For example if starting time is 12:31:10 and duration is 4004 sec,\n";
	cout<<"type 12 31 10 4004.\n";
	cout<<"----------------------------------------------------------------------\n";
	//getting data and checking type validate
	if (cin>>HH>>MM>>SS>>Duration == NULL) 
	{
		Error_flag=1;
		cout<<"\nYou have entered string or char instead of a number!\n";
	}
	else	//checking data only if type is valid
	{
		if (HH<0 || HH>23) //Checks errors in hour
		{
			Error_flag=2; 
			cout<<"You've entered invalid hour.\n";
		}
		if (MM<0 || MM>59) //Checks errors in minuts
		{
			Error_flag=2; 
			cout<<"You've entered invalid minutes.\n";
		}
		if (SS<0 || SS>59) //Checks errors in secons
		{
			Error_flag=2;
			cout<<"You've entered invalid seconds.\n";
		}
		if (Duration<0) //Checks errors in duration
		{
			Error_flag=2; 
			cout<<"Duration time can't be negative.\n";
		}
	}
	
	// in case of valid input start to convert and add time.
	if (!Error_flag)
	{
		cout<<"\nFor starting time: "<<HH<<":"<<MM<<":"<<SS;
		cout<<"\nAnd duration of: "<<Duration<<" seconds";
		Temp_time = HH + double(MM)/60 + double(SS)/3600;//converting time to real number
		Temp_time = Temp_time + double(Duration)/3600; //adding Duration to time
		HH = int(Temp_time); //converting back to hours
		Temp_time = Temp_time - HH;
		Temp_time = Temp_time * 60;
		MM = int(Temp_time); //converting back to minuts
		Temp_time = Temp_time - MM;
		Temp_time = Temp_time * 60;
		SS = int(Temp_time); //converting back to seconds
		if (HH>=24)	HH = (HH % 24); //fixing hours more than 24
		cout<<"\nThe arrivel time will be: "<<HH<<":"<<MM<<":"<<SS<<"\n";
	}
	else
		cout<<"\nPlease correct the data and try again later.\n\n";
}

פלטים מיצגים

פלט לדוגמה:

Please enter starting time in format HH MM SS and duration in seconds:

For example if starting time is 12:31:10 and duration is 4004 sec,

type 12 31 10 4004.

----------------------------------------------------------------------

 12 31 10 4004

For starting time: 12:31:10

And duration of: 4004 seconds

The arrivel time will be: 13:37:54

 

פלט לדוגמה:

 

Please enter starting time in format HH MM SS and duration in seconds:

For example if starting time is 12:31:10 and duration is 4004 sec,

type 12 31 10 4004.

----------------------------------------------------------------------

13 k 5 1200

You have entered string or char instead of a number!

 

Please correct the data and try again later.

 

פלט לדוגמה:

 

Please enter starting time in format HH MM SS and duration in seconds:

For example if starting time is 12:31:10 and duration is 4004 sec,

type 12 31 10 4004.

----------------------------------------------------------------------

24 61 60 1200

You've entered invalid hour.

You've entered invalid minutes.

You've entered invalid seconds.

 

Please correct the data and try again later.

 

 

 

Back Up Next

 

 

© Copyright YAProductions 2000