Directory
1 Problem Description
2 Solutions
1 problem description "Title description"
implement a time class. Stores hours, minutes, and seconds as type int member variables. The class is required to include a constructor, access function, a function to propel the current time, ADV (), a Reset current time (the current time is set to 00:00:00) function reset () and output result function print (). Note that the time is displayed in digital spreadsheet format, that is, hours, minutes, seconds, respectively, with two digits, if one of them is less than 10, then 0, such as 22:01:00 (without spaces in the middle), in addition, according to the format of the output time, minutes, seconds after the Endl end. When the input time is beyond the legal range (hint: notice the upper and lower bounds), please automatically follow the 24-hour conversion, it is important to note that when the seconds are negative when the processing, such as input 25:00:61, the output should be 01:01:01, input -1:-1:-1, should output 22:58:59.
The first step: Define the time class according to test instructions
class Time
{
//Please add
};
The second step: Test the time class written using the following test program.
int main ()
{
//Current time
int hour, minute, second;
//Time increment
int incr_hr, incr_min, incr_sec;
cin >> Hour >> minute >> second >> incr_hr >> incr_min >> incr_sec;
Time t (hour, minute, second);
t.print ();
t.adv (incr_hr, Incr_min, incr_sec);
t.print ();
T.reset ();
t.print ();
return 0;
}Input FormatEnter a row, 6 int type numbers, with a space interval. output Formatoutput Three line time, the time format is 24-hour system, XX:XX:XX, English punctuation, less than two bits 10 digits complement 0.
first Act initial time, second act ADV () after time, third act after reset () timeExample 1 input0 0 0 0 0-1Example 1 output00:00:00
23:59:59
00:00:00
Example 3:Example 2 input0 0 1Example 2 output02:04:01
02:04:02
00:00:00Example 3 input-50-24 0Example 3 Output21:36:00
00:22:30
00:00:00
2 Solutions
The main test class related definitions and applications, the following specific code for the time, minutes, seconds of data range processing part of the code, you can write a function to deal with, this can be reused code, but in order to meet the requirements of the problem, the code directly copied over the application.
The specific code is as follows:
ImportJava.util.Scanner; Public classMain { Public inthour; Public intminute; Public intsecond; Main (intHourintMinuteintsecond) { //Get seconds if(Second >= 60) {minute+ = SECOND/60; Second%= 60; This. Second =second; } Else if(Second < 0) { inttemp = SECOND/60; intTemp1 = Second% 60; if(Temp1 = = 0) { This. Second = 0; Minute+=temp; } Else { This. Second = 60 +Temp1; Minute+ = Temp-1; } } Else { This. Second =second; } //Get minutes if(Minute >= 60) {Hour+ = MINUTE/60; Minute%= 60; This. Minute =minute; } Else if(Minute < 0) { inttemp = MINUTE/60; intTemp1 = minute% 60; if(Temp1 = = 0) { This. Minute = 0; Hour+=temp; } Else { This. Minute = 60 +Temp1; Hour+ = Temp-1; } } Else { This. Minute =minute; } //Get Hours if(Hour >= 24) This. Hour = hour% 24; Else if(Hour < 0) This. Hour = + hour% 24; Else This. Hour =hour; } Public voidAdvintHourintMinuteintsecond) {Second+= This. Second; //GET request format seconds if(Second >= 60) {minute+ = SECOND/60; Second%= 60; This. Second =second; } Else if(Second < 0) { inttemp = SECOND/60; intTemp1 = Second% 60; if(Temp1 = = 0) { This. Second = 0; Minute+=temp; } Else { This. Second = 60 +Temp1; Minute+ = Temp-1; } } Else { This. Second =second; } minute+= This. minute; //GET request Format minutes if(Minute >= 60) {Hour+ = MINUTE/60; Minute%= 60; This. Minute =minute; } Else if(Minute < 0) { inttemp = MINUTE/60; intTemp1 = minute% 60; if(Temp1 = = 0) { This. Minute = 0; Hour+=temp; } Else { This. Minute = 60 +Temp1; Hour= Hour + temp-1; } } Else { This. Minute =minute; } Hour+= This. hour; //get required format hours if(Hour >= 24) This. Hour = hour% 24; Else if(Hour < 0) This. Hour = + hour% 24; Else This. Hour =hour; } Public voidReset () { This. Hour = 0; This. Minute = 0; This. Second = 0; } Public voidprint () {if(Hour >= 10) System.out.print (Hour+":"); Else{System.out.print ("0" +hour+ ":"); } if(Minute >= 10) System.out.print (minute+":"); ElseSystem.out.print ("0" +minute+ ":"); if(Second >= 10) System.out.print (second+ "\ n"); ElseSystem.out.print ("0" +second+ "\ n"); } Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intHour1 =In.nextint (); intMinute1 =In.nextint (); intSecond1 =In.nextint (); intHOUR2 =In.nextint (); intMinute2 =In.nextint (); intSecond2 =In.nextint (); Main Test=NewMain (Hour1, minute1, SECOND1); Test.print (); Test.adv (HOUR2, Minute2, Second2); Test.print (); Test.reset (); Test.print (); }}
Algorithmic note _092: Blue Bridge Cup exercise c++_ch04_02_ Revision (Java)