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

A Complete Guide to Programming in C++ part 66. 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 629 float FloatArr operator int i throw Badlndex if i 0 i cnt throw Badlndex i return arrPtr i float FloatArr operator int i const throw Badlndex if i 0 i cnt throw Badlndex i return arrPtr i Append a float value or an array of floats. void FloatArr append float val if cnt 1 max expand cnt 1 arrPtr cnt val void FloatArr append const FloatArr v if cnt max expand cnt int count Necessary if v this. for int i 0 i count i arrPtr cnt i Inserts a float value or an array of floats. void FloatArr insert float val int pos throw Badlndex insert FloatArr 1 val pos void FloatArr insert const FloatArr v int pos throw BadIndex if pos 0 pos cnt Append is also possible. throw Badlndex pos if max cnt expand cnt int i for i cnt-1 i pos --i Shift up from arrPtr i arrPtr i position pos. for i 0 i i Fill the gap. arrPtr i pos i cnt cnt 630 CHAPTER 28 EXCEPTION HANDLING To delete void FloatArr remove int pos throw Badlndex if pos 0 pos cnt for int i pos i cnt-1 i arrPtr i arrPtr i 1 --cnt else throw Badlndex pos ----------------------------------------------------------- Tests exception handling for float arrays. ----------------------------------------------------------- include iostream include iomanip using namespace std include int main const FloatArr v 10 bool ok false while ok try cout Here is the constant array v n cout setw 8 v endl int i cout cout ok Index cin i nThe value read true v i endl catch BadIndex err cerr Error in reading. n nInvalid index endl SOLUTIONS 631 FloatArr w 20 Array w try 0 To write 1 3 Error w 10 Error 7 Error catch BadIndex err cerr nError in writing nInvalid index endl cout nHere is the array n cout setw 5 w endl return 0 Exercise 2 -------------------------------------------------------- A numeric class to represent fractions exception handling is included.

TỪ KHÓA LIÊN QUAN