tailieunhanh - A Complete Guide to Programming in C++ part 37

A Complete Guide to Programming in C++ part 37. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | SOLUTIONS 339 Outputs the numbers cout The sorted numbers n endl for i 0 i cnt i cout setw 10 number i cout endl return 0 Exercise 2 ----------------------------------------------------- The class DayTime represents the time in hours minutes and seconds. ----------------------------------------------------- ifndef _DAYTIME_ define _DAYTIME_ include iostream include iomanip using namespace std class DayTime private short hour minute second bool overflow public DayTime int h 0 int m 0 int s 0 overflow false if setTime h m s this- setTime . hour minute second 0 bool setTime int hour int minute int second 0 if hour 0 hour 24 minute 0 minute 60 second 0 second 60 this- hour short hour this- minute short minute this- second short second return true else return false 340 CHAPTER 16 ARRAYS int getHour const return hour int getMinute const return minute int getSecond const return second int asSeconds const Daytime in seconds return 60 60 hour 60 minute second bool isLess DayTime t const Compares this and t. return asSeconds this- sSeconds void print const cout cout setfill 0 setw 2 setw 2 setw 2 setfill hour minute second Uhr endl void swap DayTime t Just one parameter DayTime temp t t this Swaps this and t this temp endif _DAYTIME ------------------------------------------------------- An array containing objects of class DayTime. ------------------------------------------------------- include Definition of class DayTime include iostream using namespace std char header n n Table with Daytimes n n int main DayTime timeTab 4 18 DayTime 10 25 DayTime 14 55 30 int i timeTab 3 .setTime 8 40 50 Last element. cout header endl SOLUTIONS 341 Output for i 0 i 4 i timeTab i .print cout endl To compute shortest and longest time int i_min 0 i_max 0 Indices for shortest and longest elements. for i 1 i 4 i if timeTab i .isLess timeTab i_min i_min i if timeTab i_max .isLess timeTab i i_max i cout nShortest time timeTab i_min .print .

TỪ KHÓA LIÊN QUAN