From 3240e1a777e76306fc18f54bba842df96ab14144 Mon Sep 17 00:00:00 2001 From: Michael Cambria Date: Wed, 4 May 2016 19:04:50 -0400 Subject: [PATCH 1/1] Lippman C++ 5th Ed Source Code Initial Commit (GCC_4_7_0.zip) --- 1/README | 14 ++ 1/Sales_item.h | 142 +++++++++++++++ 1/add.cc | 42 +++++ 1/add_item.cc | 41 +++++ 1/add_item2.cc | 47 +++++ 1/avg_price.cc | 59 +++++++ 1/data/add | 1 + 1/data/add_item | 2 + 1/data/book_sales | 10 ++ 1/data/mysum | 1 + 1/data/occurs | 1 + 1/for_ex.cc | 38 ++++ 1/forcount.cc | 43 +++++ 1/item_io.cc | 43 +++++ 1/main_only.cc | 33 ++++ 1/makefile | 10 ++ 1/mysum.cc | 42 +++++ 1/occurs.cc | 55 ++++++ 1/runpgms | 9 + 1/whilecount.cc | 45 +++++ 10/README | 24 +++ 10/absInt.cc | 70 ++++++++ 10/accum.cc | 79 +++++++++ 10/accum4.cc | 44 +++++ 10/avg_price.cc | 61 +++++++ 10/bind2.cc | 80 +++++++++ 10/data/absInt | 15 ++ 10/data/accum | 1 + 10/data/book_sales | 10 ++ 10/data/count-size | 1 + 10/data/iostream_iter | 1 + 10/data/outFile1 | 1 + 10/data/outFile2 | 1 + 10/data/rcomma | 1 + 10/data/readme | 24 +++ 10/data/reverse-iter | 1 + 10/data/use_find | 3 + 10/elimDups.cc | 109 ++++++++++++ 10/equal.cc | 60 +++++++ 10/equiv-istream-iter.cc | 50 ++++++ 10/inserters.cc | 79 +++++++++ 10/iostream_iter.cc | 63 +++++++ 10/lambda.cc | 100 +++++++++++ 10/makefile | 15 ++ 10/newcount-size.cc | 149 ++++++++++++++++ 10/ostreamIter2.cc | 54 ++++++ 10/outrunpgms | 0 10/rcomma.cc | 58 ++++++ 10/rev-iters.cc | 45 +++++ 10/reverse-iter.cc | 77 ++++++++ 10/reverse.cc | 89 ++++++++++ 10/runpgms | 18 ++ 10/sortSI.cc | 59 +++++++ 10/use-istream-iter.cc | 67 +++++++ 10/use_find.cc | 85 +++++++++ 11/README | 15 ++ 11/data/Alice | 1 + 11/data/rules | 8 + 11/data/simple_query | 2 + 11/data/storyDataFile | 10 ++ 11/data/text | 3 + 11/data/word_count | 7 + 11/erasemap.cc | 67 +++++++ 11/makefile | 17 ++ 11/map1.cc | 103 +++++++++++ 11/multimap.cc | 79 +++++++++ 11/pair.cc | 125 +++++++++++++ 11/restricted_count.cc | 60 +++++++ 11/runpgms | 9 + 11/setOps.cc | 82 +++++++++ 11/unorderedWordCount.cc | 96 ++++++++++ 11/wcEx.cc | 66 +++++++ 11/word_count.cc | 68 +++++++ 11/word_transform.cc | 117 ++++++++++++ 12/Foo.h | 48 +++++ 12/QueryResult.h | 58 ++++++ 12/README | 14 ++ 12/StrBlob.h | 170 ++++++++++++++++++ 12/TextQuery.cc | 156 ++++++++++++++++ 12/TextQuery.h | 64 +++++++ 12/UP.cc | 50 ++++++ 12/allocPtr.cc | 67 +++++++ 12/allocSP.cc | 68 +++++++ 12/data/Alice | 1 + 12/data/alloc | 1 + 12/data/storyDataFile | 10 ++ 12/makefile | 17 ++ 12/oknew.cc | 97 ++++++++++ 12/querymain.cc | 73 ++++++++ 12/release.cc | 61 +++++++ 12/runpgms | 7 + 12/unique.cc | 50 ++++++ 12/useBlob.cc | 49 ++++++ 12/usealloc.cc | 96 ++++++++++ 12/usealloc2.cc | 69 ++++++++ 12/weak.cc | 44 +++++ 13/Folder.cc | 221 +++++++++++++++++++++++ 13/Folder.h | 104 +++++++++++ 13/FolderMain.cc | 133 ++++++++++++++ 13/HasPtrUseCount.cc | 115 ++++++++++++ 13/HasPtrValueLike.cc | 81 +++++++++ 13/README | 9 + 13/StrFolder.cc | 220 +++++++++++++++++++++++ 13/StrFolder.h | 104 +++++++++++ 13/StrFolderMain.cc | 132 ++++++++++++++ 13/StrVec.cc | 44 +++++ 13/StrVec.h | 236 +++++++++++++++++++++++++ 13/String.cc | 147 ++++++++++++++++ 13/String.h | 104 +++++++++++ 13/copyNoMove.cc | 47 +++++ 13/data/storyDataFile | 10 ++ 13/makefile | 27 +++ 13/moveHP.cc | 76 ++++++++ 13/runpgms | 5 + 13/serial.cc | 52 ++++++ 13/small_SI.h | 72 ++++++++ 13/substr.cc | 57 ++++++ 13/useStrVec.cc | 102 +++++++++++ 14/README | 13 ++ 14/Sales_data.cc | 104 +++++++++++ 14/Sales_data.h | 89 ++++++++++ 14/absInt.cc | 80 +++++++++ 14/add.cc | 53 ++++++ 14/calc.cc | 87 +++++++++ 14/data/absInt | 15 ++ 14/data/add_item | 9 + 14/data/count-size | 1 + 14/data/word_echo | 2 + 14/directcall.cc | 55 ++++++ 14/fcnobj.cc | 99 +++++++++++ 14/makefile | 17 ++ 14/newcount-size.cc | 156 ++++++++++++++++ 14/readStr.cc | 80 +++++++++ 14/runpgms | 9 + 15/Basket.cc | 84 +++++++++ 15/Basket.h | 74 ++++++++ 15/Basket_main.cc | 80 +++++++++ 15/Query.cc | 113 ++++++++++++ 15/Query.h | 154 ++++++++++++++++ 15/Quote.cc | 72 ++++++++ 15/Quote.h | 142 +++++++++++++++ 15/README | 15 ++ 15/andQueryTest.cc | 71 ++++++++ 15/and_orQueryTest.cc | 61 +++++++ 15/data/querymain | 3 + 15/data/storyDataFile | 10 ++ 15/data/three | 3 + 15/data/two | 2 + 15/get_print.cc | 79 +++++++++ 15/makefile | 44 +++++ 15/runpgms | 7 + 15/sample.cc | 64 +++++++ 15/scope.cc | 55 ++++++ 15/useQuote.cc | 48 +++++ 15/vecBasket.cc | 69 ++++++++ 15/virtualsNscope.cc | 85 +++++++++ 15/wordQueryTest.cc | 70 ++++++++ 16/Array.cc | 103 +++++++++++ 16/Blob.h | 333 +++++++++++++++++++++++++++++++++++ 16/DebugDelete.h | 50 ++++++ 16/README | 12 ++ 16/SP.cc | 69 ++++++++ 16/Sales_data.cc | 118 +++++++++++++ 16/Sales_data.h | 101 +++++++++++ 16/Vec.h | 228 ++++++++++++++++++++++++ 16/Vecmain.cc | 75 ++++++++ 16/array-parms4.cc | 61 +++++++ 16/build.cc | 41 +++++ 16/compare.cc | 66 +++++++ 16/compare.h | 52 ++++++ 16/compareDef.cc | 57 ++++++ 16/compareSpec.cc | 83 +++++++++ 16/data/Vecmain | 7 + 16/data/book_sales | 8 + 16/data/book_trans | 2 + 16/debug.cc | 77 ++++++++ 16/debug_rep.h | 151 ++++++++++++++++ 16/expansion.cc | 96 ++++++++++ 16/flip.cc | 78 ++++++++ 16/makefile | 50 ++++++ 16/multiset.cc | 59 +++++++ 16/overSpec.cc | 76 ++++++++ 16/qm.cc | 54 ++++++ 16/runpgms | 12 ++ 16/special.cc | 69 ++++++++ 16/trail-ret.cc | 82 +++++++++ 16/typealias.cc | 56 ++++++ 16/useBlob.cc | 53 ++++++ 16/useChcking.cc | 82 +++++++++ 16/variadic.cc | 98 +++++++++++ 16/variadic3.cc | 122 +++++++++++++ 17/README | 18 ++ 17/REerrs.cc | 58 ++++++ 17/badgetc.cc | 45 +++++ 17/bits.cc | 181 +++++++++++++++++++ 17/ccpgms.cc | 67 +++++++ 17/data/badgetc | 3 + 17/data/bits | 1 + 17/data/copyOut | 9 + 17/data/files | 35 ++++ 17/data/findDups | 1 + 17/data/findDupsV2 | 1 + 17/data/findgerunds | 4 + 17/data/game | 1 + 17/data/no-skipws | 2 + 17/data/phones | 3 + 17/data/phones2 | 3 + 17/data/phonesFinal | 11 ++ 17/data/skipws | 2 + 17/data/store1 | 10 ++ 17/data/store2 | 8 + 17/data/store3 | 10 ++ 17/data/store4 | 10 ++ 17/data/validatePhones | 8 + 17/findbook.cc | 135 ++++++++++++++ 17/game.cc | 62 +++++++ 17/getc.cc | 45 +++++ 17/getput.cc | 41 +++++ 17/makefile | 24 +++ 17/manips.cc | 75 ++++++++ 17/manips11.cc | 48 +++++ 17/no-skipws.cc | 44 +++++ 17/normalInts.cc | 82 +++++++++ 17/phones.cc | 89 ++++++++++ 17/phonesFinal.cc | 83 +++++++++ 17/point.cc | 43 +++++ 17/prec.cc | 56 ++++++ 17/rand1.cc | 109 ++++++++++++ 17/rand2.cc | 96 ++++++++++ 17/rand4.cc | 77 ++++++++ 17/rand6.cc | 87 +++++++++ 17/regex1.cc | 85 +++++++++ 17/runpgms | 19 ++ 17/sci_dec.cc | 63 +++++++ 17/seed.cc | 67 +++++++ 17/seek.cc | 73 ++++++++ 17/setw.cc | 69 ++++++++ 17/skipws.cc | 41 +++++ 17/subexpr.cc | 69 ++++++++ 17/tuple.cc | 77 ++++++++ 17/validatePhones.cc | 98 +++++++++++ 17/zips.cc | 70 ++++++++ 18/Animal.cc | 59 +++++++ 18/Animal.h | 138 +++++++++++++++ 18/Animal_virtual_baseVers.h | 153 ++++++++++++++++ 18/README | 10 ++ 18/Sales_data.cc | 123 +++++++++++++ 18/book.cc | 50 ++++++ 18/bookexcept.h | 52 ++++++ 18/class.cc | 44 +++++ 18/data/book_sales | 10 ++ 18/ex-over.cc | 59 +++++++ 18/inherit.cc | 54 ++++++ 18/makefile | 18 ++ 18/mi-scope.cc | 72 ++++++++ 18/over.cc | 64 +++++++ 18/over2.cc | 53 ++++++ 18/runpgms | 6 + 18/spec.cc | 58 ++++++ 18/usingDirective.cc | 73 ++++++++ 18/virt-inherit.cc | 52 ++++++ 19/.useScreen.cc.swp | Bin 0 -> 16384 bytes 19/README | 2 + 19/Screen.cc | 38 ++++ 19/Screen.h | 89 ++++++++++ 19/TextQuery.h | 99 +++++++++++ 19/Token.h | 140 +++++++++++++++ 19/bitfields.cc | 90 ++++++++++ 19/calc.cc | 105 +++++++++++ 19/dyn_cast.cc | 98 +++++++++++ 19/enum-match.cc | 69 ++++++++ 19/enum.cc | 79 +++++++++ 19/makefile | 15 ++ 19/memFN-bind.cc | 61 +++++++ 19/out2 | 36 ++++ 19/runpgms | 4 + 19/tok2.cc | 54 ++++++ 19/typeid.cc | 60 +++++++ 19/useScreen.cc | 115 ++++++++++++ 2/README | 10 ++ 2/Sales_data.cc | 70 ++++++++ 2/Sales_data.h | 40 +++++ 2/compounddecls.cc | 56 ++++++ 2/convs.cc | 57 ++++++ 2/data/add_item | 2 + 2/data/data/add_item | 2 + 2/dbl_ptr.cc | 54 ++++++ 2/decl-ex.cc | 52 ++++++ 2/escape.cc | 39 ++++ 2/inits.cc | 49 ++++++ 2/literals.cc | 50 ++++++ 2/makefile | 11 ++ 2/ref-ex.cc | 45 +++++ 2/runpgms | 6 + 2/runpgms.bat | 7 + 2/scope_levels.cc | 53 ++++++ 2/unsigned.cc | 89 ++++++++++ 3/README | 21 +++ 3/add_using.cc | 47 +++++ 3/arrayScores.cc | 62 +++++++ 3/cctype.cc | 81 +++++++++ 3/char_repl.cc | 126 +++++++++++++ 3/cstring_hdr.cc | 86 +++++++++ 3/data/add | 1 + 3/data/grades | 14 ++ 3/data/hexify | 1 + 3/data/iterarith | 1 + 3/data/ptr_traversal2 | 1 + 3/data/string_io | 1 + 3/data/vecStrings2 | 9 + 3/data/word_echo | 2 + 3/getline.cc | 45 +++++ 3/hexify.cc | 56 ++++++ 3/makefile | 15 ++ 3/multidim.cc | 141 +++++++++++++++ 3/ptr_traversal2.cc | 75 ++++++++ 3/runpgms | 17 ++ 3/square.cc | 47 +++++ 3/strcompare.cc | 47 +++++ 3/string_add.cc | 51 ++++++ 3/string_io.cc | 44 +++++ 3/string_io2.cc | 44 +++++ 3/string_size.cc | 45 +++++ 3/string_size2.cc | 49 ++++++ 3/string_size3.cc | 46 +++++ 3/vecScores.cc | 78 ++++++++ 3/vecStrings2.cc | 58 ++++++ 3/vecSubs.cc | 93 ++++++++++ 3/vec_decls.cc | 91 ++++++++++ 3/word_echo.cc | 42 +++++ 4/README | 10 ++ 4/arith-ex.cc | 48 +++++ 4/arraysizeof.cc | 48 +++++ 4/cond.cc | 58 ++++++ 4/data/oofe | 1 + 4/data/vecSubs | 14 ++ 4/incr.cc | 43 +++++ 4/int-div.cc | 44 +++++ 4/makefile | 11 ++ 4/oofe.cc | 82 +++++++++ 4/overflow.cc | 41 +++++ 4/prec.cc | 52 ++++++ 4/runpgms | 6 + 4/shift-prec.cc | 61 +++++++ 4/sizeof_pgm.cc | 77 ++++++++ 4/unaryOps.cc | 44 +++++ 4/vec_init.cc | 66 +++++++ 4/warn_dbltoint.cc | 40 +++++ 5/README | 15 ++ 5/add_item2.cc | 65 +++++++ 5/data/add_item_data | 10 ++ 5/data/doWhile | 4 + 5/data/grades | 14 ++ 5/data/guesses | 3 + 5/data/readme | 14 ++ 5/doWhile.cc | 53 ++++++ 5/guess.cc | 61 +++++++ 5/ifgrades.cc | 116 ++++++++++++ 5/makefile | 10 ++ 5/othercnt.cc | 56 ++++++ 5/rangefor.cc | 54 ++++++ 5/runpgms | 8 + 5/vowels.cc | 67 +++++++ 6/LocalMath.cc | 61 +++++++ 6/LocalMath.h | 38 ++++ 6/README | 11 ++ 6/arrRet.cc | 93 ++++++++++ 6/count-calls.cc | 47 +++++ 6/data/mainmath | 1 + 6/data/refparms | 15 ++ 6/errMsg_initList.cc | 110 ++++++++++++ 6/fact.cc | 46 +++++ 6/fcnptrRet.cc | 82 +++++++++ 6/good_printmain.cc | 55 ++++++ 6/inline_shorter.cc | 55 ++++++ 6/mainmath.cc | 52 ++++++ 6/mainret.cc | 42 +++++ 6/make_plural.h | 50 ++++++ 6/makefile | 21 +++ 6/mk_plural.cc | 50 ++++++ 6/printFcns.h | 45 +++++ 6/ref-fcn.cc | 51 ++++++ 6/refparms.cc | 108 ++++++++++++ 6/refret-ex.cc | 47 +++++ 6/reset.cc | 68 +++++++ 6/runpgms | 9 + 6/stl-arr-fcns.cc | 69 ++++++++ 6/use_mk_plural.cc | 54 ++++++ 6/usefcnptr.cc | 71 ++++++++ 6/wdebug.cc | 72 ++++++++ 7/Account.cc | 42 +++++ 7/Account.h | 54 ++++++ 7/Debug.cc | 34 ++++ 7/Debug.h | 71 ++++++++ 7/README | 11 ++ 7/Sales_data.cc | 82 +++++++++ 7/Sales_data.h | 70 ++++++++ 7/Screen.h | 96 ++++++++++ 7/WindowMgr.h | 90 ++++++++++ 7/add_item.cc | 48 +++++ 7/avg_price.cc | 54 ++++++ 7/data/add_item | 2 + 7/data/book_sales | 10 ++ 7/makefile | 30 ++++ 7/runpgms | 4 + 7/useAccount.cc | 46 +++++ 7/useDebug.cc | 58 ++++++ 7/useScreen.cc | 67 +++++++ 8/README | 14 ++ 8/add_item.cc | 48 +++++ 8/add_itemV2.cc | 48 +++++ 8/buf.cc | 53 ++++++ 8/clearIO.cc | 74 ++++++++ 8/data/add_item | 2 + 8/data/sstream | 3 + 8/fileIO.cc | 72 ++++++++ 8/makefile | 18 ++ 8/runpgms | 6 + 8/sstream.cc | 109 ++++++++++++ 9/README | 10 ++ 9/capacity.cc | 77 ++++++++ 9/data/erase | 7 + 9/erase1.cc | 83 +++++++++ 9/erase2.cc | 101 +++++++++++ 9/find-str.cc | 45 +++++ 9/find_ops.cc | 70 ++++++++ 9/makefile | 11 ++ 9/refreshIter.cc | 66 +++++++ 9/runpgms | 5 + 9/stack.cc | 61 +++++++ 9/str_assignOps.cc | 106 +++++++++++ 9/substr.cc | 50 ++++++ 9/useConvs.cc | 60 +++++++ CompilerNotes.pdf | Bin 0 -> 26405 bytes GCC 4.7 README.pdf | Bin 0 -> 43695 bytes GNU_makefile_template | 56 ++++++ Version_test.h | 105 +++++++++++ makefile | 65 +++++++ noinput | 5 + runpgms | 7 + 440 files changed, 23504 insertions(+) create mode 100644 1/README create mode 100644 1/Sales_item.h create mode 100644 1/add.cc create mode 100644 1/add_item.cc create mode 100644 1/add_item2.cc create mode 100644 1/avg_price.cc create mode 100644 1/data/add create mode 100644 1/data/add_item create mode 100644 1/data/book_sales create mode 100644 1/data/mysum create mode 100644 1/data/occurs create mode 100644 1/for_ex.cc create mode 100644 1/forcount.cc create mode 100644 1/item_io.cc create mode 100644 1/main_only.cc create mode 100644 1/makefile create mode 100644 1/mysum.cc create mode 100644 1/occurs.cc create mode 100644 1/runpgms create mode 100644 1/whilecount.cc create mode 100644 10/README create mode 100644 10/absInt.cc create mode 100644 10/accum.cc create mode 100644 10/accum4.cc create mode 100644 10/avg_price.cc create mode 100644 10/bind2.cc create mode 100644 10/data/absInt create mode 100644 10/data/accum create mode 100644 10/data/book_sales create mode 100644 10/data/count-size create mode 100644 10/data/iostream_iter create mode 100644 10/data/outFile1 create mode 100644 10/data/outFile2 create mode 100644 10/data/rcomma create mode 100644 10/data/readme create mode 100644 10/data/reverse-iter create mode 100644 10/data/use_find create mode 100644 10/elimDups.cc create mode 100644 10/equal.cc create mode 100644 10/equiv-istream-iter.cc create mode 100644 10/inserters.cc create mode 100644 10/iostream_iter.cc create mode 100644 10/lambda.cc create mode 100644 10/makefile create mode 100644 10/newcount-size.cc create mode 100644 10/ostreamIter2.cc create mode 100644 10/outrunpgms create mode 100644 10/rcomma.cc create mode 100644 10/rev-iters.cc create mode 100644 10/reverse-iter.cc create mode 100644 10/reverse.cc create mode 100644 10/runpgms create mode 100644 10/sortSI.cc create mode 100644 10/use-istream-iter.cc create mode 100644 10/use_find.cc create mode 100644 11/README create mode 100644 11/data/Alice create mode 100644 11/data/rules create mode 100644 11/data/simple_query create mode 100644 11/data/storyDataFile create mode 100644 11/data/text create mode 100644 11/data/word_count create mode 100644 11/erasemap.cc create mode 100644 11/makefile create mode 100644 11/map1.cc create mode 100644 11/multimap.cc create mode 100644 11/pair.cc create mode 100644 11/restricted_count.cc create mode 100644 11/runpgms create mode 100644 11/setOps.cc create mode 100644 11/unorderedWordCount.cc create mode 100644 11/wcEx.cc create mode 100644 11/word_count.cc create mode 100644 11/word_transform.cc create mode 100644 12/Foo.h create mode 100644 12/QueryResult.h create mode 100644 12/README create mode 100644 12/StrBlob.h create mode 100644 12/TextQuery.cc create mode 100644 12/TextQuery.h create mode 100644 12/UP.cc create mode 100644 12/allocPtr.cc create mode 100644 12/allocSP.cc create mode 100644 12/data/Alice create mode 100644 12/data/alloc create mode 100644 12/data/storyDataFile create mode 100644 12/makefile create mode 100644 12/oknew.cc create mode 100644 12/querymain.cc create mode 100644 12/release.cc create mode 100644 12/runpgms create mode 100644 12/unique.cc create mode 100644 12/useBlob.cc create mode 100644 12/usealloc.cc create mode 100644 12/usealloc2.cc create mode 100644 12/weak.cc create mode 100644 13/Folder.cc create mode 100644 13/Folder.h create mode 100644 13/FolderMain.cc create mode 100644 13/HasPtrUseCount.cc create mode 100644 13/HasPtrValueLike.cc create mode 100644 13/README create mode 100644 13/StrFolder.cc create mode 100644 13/StrFolder.h create mode 100644 13/StrFolderMain.cc create mode 100644 13/StrVec.cc create mode 100644 13/StrVec.h create mode 100644 13/String.cc create mode 100644 13/String.h create mode 100644 13/copyNoMove.cc create mode 100644 13/data/storyDataFile create mode 100644 13/makefile create mode 100644 13/moveHP.cc create mode 100644 13/runpgms create mode 100644 13/serial.cc create mode 100644 13/small_SI.h create mode 100644 13/substr.cc create mode 100644 13/useStrVec.cc create mode 100644 14/README create mode 100644 14/Sales_data.cc create mode 100644 14/Sales_data.h create mode 100644 14/absInt.cc create mode 100644 14/add.cc create mode 100644 14/calc.cc create mode 100644 14/data/absInt create mode 100644 14/data/add_item create mode 100644 14/data/count-size create mode 100644 14/data/word_echo create mode 100644 14/directcall.cc create mode 100644 14/fcnobj.cc create mode 100644 14/makefile create mode 100644 14/newcount-size.cc create mode 100644 14/readStr.cc create mode 100644 14/runpgms create mode 100644 15/Basket.cc create mode 100644 15/Basket.h create mode 100644 15/Basket_main.cc create mode 100644 15/Query.cc create mode 100644 15/Query.h create mode 100644 15/Quote.cc create mode 100644 15/Quote.h create mode 100644 15/README create mode 100644 15/andQueryTest.cc create mode 100644 15/and_orQueryTest.cc create mode 100644 15/data/querymain create mode 100644 15/data/storyDataFile create mode 100644 15/data/three create mode 100644 15/data/two create mode 100644 15/get_print.cc create mode 100644 15/makefile create mode 100644 15/runpgms create mode 100644 15/sample.cc create mode 100644 15/scope.cc create mode 100644 15/useQuote.cc create mode 100644 15/vecBasket.cc create mode 100644 15/virtualsNscope.cc create mode 100644 15/wordQueryTest.cc create mode 100644 16/Array.cc create mode 100644 16/Blob.h create mode 100644 16/DebugDelete.h create mode 100644 16/README create mode 100644 16/SP.cc create mode 100644 16/Sales_data.cc create mode 100644 16/Sales_data.h create mode 100644 16/Vec.h create mode 100644 16/Vecmain.cc create mode 100644 16/array-parms4.cc create mode 100644 16/build.cc create mode 100644 16/compare.cc create mode 100644 16/compare.h create mode 100644 16/compareDef.cc create mode 100644 16/compareSpec.cc create mode 100644 16/data/Vecmain create mode 100644 16/data/book_sales create mode 100644 16/data/book_trans create mode 100644 16/debug.cc create mode 100644 16/debug_rep.h create mode 100644 16/expansion.cc create mode 100644 16/flip.cc create mode 100644 16/makefile create mode 100644 16/multiset.cc create mode 100644 16/overSpec.cc create mode 100644 16/qm.cc create mode 100644 16/runpgms create mode 100644 16/special.cc create mode 100644 16/trail-ret.cc create mode 100644 16/typealias.cc create mode 100644 16/useBlob.cc create mode 100644 16/useChcking.cc create mode 100644 16/variadic.cc create mode 100644 16/variadic3.cc create mode 100644 17/README create mode 100644 17/REerrs.cc create mode 100644 17/badgetc.cc create mode 100644 17/bits.cc create mode 100644 17/ccpgms.cc create mode 100644 17/data/badgetc create mode 100644 17/data/bits create mode 100644 17/data/copyOut create mode 100644 17/data/files create mode 100644 17/data/findDups create mode 100644 17/data/findDupsV2 create mode 100644 17/data/findgerunds create mode 100644 17/data/game create mode 100644 17/data/no-skipws create mode 100644 17/data/phones create mode 100644 17/data/phones2 create mode 100644 17/data/phonesFinal create mode 100644 17/data/skipws create mode 100644 17/data/store1 create mode 100644 17/data/store2 create mode 100644 17/data/store3 create mode 100644 17/data/store4 create mode 100644 17/data/validatePhones create mode 100644 17/findbook.cc create mode 100644 17/game.cc create mode 100644 17/getc.cc create mode 100644 17/getput.cc create mode 100644 17/makefile create mode 100644 17/manips.cc create mode 100644 17/manips11.cc create mode 100644 17/no-skipws.cc create mode 100644 17/normalInts.cc create mode 100644 17/phones.cc create mode 100644 17/phonesFinal.cc create mode 100644 17/point.cc create mode 100644 17/prec.cc create mode 100644 17/rand1.cc create mode 100644 17/rand2.cc create mode 100644 17/rand4.cc create mode 100644 17/rand6.cc create mode 100644 17/regex1.cc create mode 100644 17/runpgms create mode 100644 17/sci_dec.cc create mode 100644 17/seed.cc create mode 100644 17/seek.cc create mode 100644 17/setw.cc create mode 100644 17/skipws.cc create mode 100644 17/subexpr.cc create mode 100644 17/tuple.cc create mode 100644 17/validatePhones.cc create mode 100644 17/zips.cc create mode 100644 18/Animal.cc create mode 100644 18/Animal.h create mode 100644 18/Animal_virtual_baseVers.h create mode 100644 18/README create mode 100644 18/Sales_data.cc create mode 100644 18/book.cc create mode 100644 18/bookexcept.h create mode 100644 18/class.cc create mode 100644 18/data/book_sales create mode 100644 18/ex-over.cc create mode 100644 18/inherit.cc create mode 100644 18/makefile create mode 100644 18/mi-scope.cc create mode 100644 18/over.cc create mode 100644 18/over2.cc create mode 100644 18/runpgms create mode 100644 18/spec.cc create mode 100644 18/usingDirective.cc create mode 100644 18/virt-inherit.cc create mode 100644 19/.useScreen.cc.swp create mode 100644 19/README create mode 100644 19/Screen.cc create mode 100644 19/Screen.h create mode 100644 19/TextQuery.h create mode 100644 19/Token.h create mode 100644 19/bitfields.cc create mode 100644 19/calc.cc create mode 100644 19/dyn_cast.cc create mode 100644 19/enum-match.cc create mode 100644 19/enum.cc create mode 100644 19/makefile create mode 100644 19/memFN-bind.cc create mode 100644 19/out2 create mode 100644 19/runpgms create mode 100644 19/tok2.cc create mode 100644 19/typeid.cc create mode 100644 19/useScreen.cc create mode 100644 2/README create mode 100644 2/Sales_data.cc create mode 100644 2/Sales_data.h create mode 100644 2/compounddecls.cc create mode 100644 2/convs.cc create mode 100644 2/data/add_item create mode 100644 2/data/data/add_item create mode 100644 2/dbl_ptr.cc create mode 100644 2/decl-ex.cc create mode 100644 2/escape.cc create mode 100644 2/inits.cc create mode 100644 2/literals.cc create mode 100644 2/makefile create mode 100644 2/ref-ex.cc create mode 100644 2/runpgms create mode 100644 2/runpgms.bat create mode 100644 2/scope_levels.cc create mode 100644 2/unsigned.cc create mode 100644 3/README create mode 100644 3/add_using.cc create mode 100644 3/arrayScores.cc create mode 100644 3/cctype.cc create mode 100644 3/char_repl.cc create mode 100644 3/cstring_hdr.cc create mode 100644 3/data/add create mode 100644 3/data/grades create mode 100644 3/data/hexify create mode 100644 3/data/iterarith create mode 100644 3/data/ptr_traversal2 create mode 100644 3/data/string_io create mode 100644 3/data/vecStrings2 create mode 100644 3/data/word_echo create mode 100644 3/getline.cc create mode 100644 3/hexify.cc create mode 100644 3/makefile create mode 100644 3/multidim.cc create mode 100644 3/ptr_traversal2.cc create mode 100644 3/runpgms create mode 100644 3/square.cc create mode 100644 3/strcompare.cc create mode 100644 3/string_add.cc create mode 100644 3/string_io.cc create mode 100644 3/string_io2.cc create mode 100644 3/string_size.cc create mode 100644 3/string_size2.cc create mode 100644 3/string_size3.cc create mode 100644 3/vecScores.cc create mode 100644 3/vecStrings2.cc create mode 100644 3/vecSubs.cc create mode 100644 3/vec_decls.cc create mode 100644 3/word_echo.cc create mode 100644 4/README create mode 100644 4/arith-ex.cc create mode 100644 4/arraysizeof.cc create mode 100644 4/cond.cc create mode 100644 4/data/oofe create mode 100644 4/data/vecSubs create mode 100644 4/incr.cc create mode 100644 4/int-div.cc create mode 100644 4/makefile create mode 100644 4/oofe.cc create mode 100644 4/overflow.cc create mode 100644 4/prec.cc create mode 100644 4/runpgms create mode 100644 4/shift-prec.cc create mode 100644 4/sizeof_pgm.cc create mode 100644 4/unaryOps.cc create mode 100644 4/vec_init.cc create mode 100644 4/warn_dbltoint.cc create mode 100644 5/README create mode 100644 5/add_item2.cc create mode 100644 5/data/add_item_data create mode 100644 5/data/doWhile create mode 100644 5/data/grades create mode 100644 5/data/guesses create mode 100644 5/data/readme create mode 100644 5/doWhile.cc create mode 100644 5/guess.cc create mode 100644 5/ifgrades.cc create mode 100644 5/makefile create mode 100644 5/othercnt.cc create mode 100644 5/rangefor.cc create mode 100644 5/runpgms create mode 100644 5/vowels.cc create mode 100644 6/LocalMath.cc create mode 100644 6/LocalMath.h create mode 100644 6/README create mode 100644 6/arrRet.cc create mode 100644 6/count-calls.cc create mode 100644 6/data/mainmath create mode 100644 6/data/refparms create mode 100644 6/errMsg_initList.cc create mode 100644 6/fact.cc create mode 100644 6/fcnptrRet.cc create mode 100644 6/good_printmain.cc create mode 100644 6/inline_shorter.cc create mode 100644 6/mainmath.cc create mode 100644 6/mainret.cc create mode 100644 6/make_plural.h create mode 100644 6/makefile create mode 100644 6/mk_plural.cc create mode 100644 6/printFcns.h create mode 100644 6/ref-fcn.cc create mode 100644 6/refparms.cc create mode 100644 6/refret-ex.cc create mode 100644 6/reset.cc create mode 100644 6/runpgms create mode 100644 6/stl-arr-fcns.cc create mode 100644 6/use_mk_plural.cc create mode 100644 6/usefcnptr.cc create mode 100644 6/wdebug.cc create mode 100644 7/Account.cc create mode 100644 7/Account.h create mode 100644 7/Debug.cc create mode 100644 7/Debug.h create mode 100644 7/README create mode 100644 7/Sales_data.cc create mode 100644 7/Sales_data.h create mode 100644 7/Screen.h create mode 100644 7/WindowMgr.h create mode 100644 7/add_item.cc create mode 100644 7/avg_price.cc create mode 100644 7/data/add_item create mode 100644 7/data/book_sales create mode 100644 7/makefile create mode 100644 7/runpgms create mode 100644 7/useAccount.cc create mode 100644 7/useDebug.cc create mode 100644 7/useScreen.cc create mode 100644 8/README create mode 100644 8/add_item.cc create mode 100644 8/add_itemV2.cc create mode 100644 8/buf.cc create mode 100644 8/clearIO.cc create mode 100644 8/data/add_item create mode 100644 8/data/sstream create mode 100644 8/fileIO.cc create mode 100644 8/makefile create mode 100644 8/runpgms create mode 100644 8/sstream.cc create mode 100644 9/README create mode 100644 9/capacity.cc create mode 100644 9/data/erase create mode 100644 9/erase1.cc create mode 100644 9/erase2.cc create mode 100644 9/find-str.cc create mode 100644 9/find_ops.cc create mode 100644 9/makefile create mode 100644 9/refreshIter.cc create mode 100644 9/runpgms create mode 100644 9/stack.cc create mode 100644 9/str_assignOps.cc create mode 100644 9/substr.cc create mode 100644 9/useConvs.cc create mode 100644 CompilerNotes.pdf create mode 100644 GCC 4.7 README.pdf create mode 100644 GNU_makefile_template create mode 100644 Version_test.h create mode 100644 makefile create mode 100644 noinput create mode 100644 runpgms diff --git a/1/README b/1/README new file mode 100644 index 0000000..c4aaafd --- /dev/null +++ b/1/README @@ -0,0 +1,14 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + book_sales avg_price, item_io + add_item add_item, add_item2 + add add + my_sum my_sum + occurs occurs + +Programs not listed above print output and do +not read any input + diff --git a/1/Sales_item.h b/1/Sales_item.h new file mode 100644 index 0000000..ff86669 --- /dev/null +++ b/1/Sales_item.h @@ -0,0 +1,142 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +/* This file defines the Sales_item class used in chapter 1. + * The code used in this file will be explained in + * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators) + * Readers shouldn't try to understand the code in this file + * until they have read those chapters. +*/ + +#ifndef SALESITEM_H +// we're here only if SALESITEM_H has not yet been defined +#define SALESITEM_H + +// Definition of Sales_item class and related functions goes here +#include +#include + +class Sales_item { +// these declarations are explained section 7.2.1, p. 270 +// and in chapter 14, pages 557, 558, 561 +friend std::istream& operator>>(std::istream&, Sales_item&); +friend std::ostream& operator<<(std::ostream&, const Sales_item&); +friend bool operator<(const Sales_item&, const Sales_item&); +friend bool +operator==(const Sales_item&, const Sales_item&); +public: + // constructors are explained in section 7.1.4, pages 262 - 265 + // default constructor needed to initialize members of built-in type + Sales_item() = default; + Sales_item(const std::string &book): bookNo(book) { } + Sales_item(std::istream &is) { is >> *this; } +public: + // operations on Sales_item objects + // member binary operator: left-hand operand bound to implicit this pointer + Sales_item& operator+=(const Sales_item&); + + // operations on Sales_item objects + std::string isbn() const { return bookNo; } + double avg_price() const; +// private members as before +private: + std::string bookNo; // implicitly initialized to the empty string + unsigned units_sold = 0; // explicitly initialized + double revenue = 0.0; +}; + +// used in chapter 10 +inline +bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) +{ return lhs.isbn() == rhs.isbn(); } + +// nonmember binary operator: must declare a parameter for each operand +Sales_item operator+(const Sales_item&, const Sales_item&); + +inline bool +operator==(const Sales_item &lhs, const Sales_item &rhs) +{ + // must be made a friend of Sales_item + return lhs.units_sold == rhs.units_sold && + lhs.revenue == rhs.revenue && + lhs.isbn() == rhs.isbn(); +} + +inline bool +operator!=(const Sales_item &lhs, const Sales_item &rhs) +{ + return !(lhs == rhs); // != defined in terms of operator== +} + +// assumes that both objects refer to the same ISBN +Sales_item& Sales_item::operator+=(const Sales_item& rhs) +{ + units_sold += rhs.units_sold; + revenue += rhs.revenue; + return *this; +} + +// assumes that both objects refer to the same ISBN +Sales_item +operator+(const Sales_item& lhs, const Sales_item& rhs) +{ + Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return + ret += rhs; // add in the contents of (|rhs|) + return ret; // return (|ret|) by value +} + +std::istream& +operator>>(std::istream& in, Sales_item& s) +{ + double price; + in >> s.bookNo >> s.units_sold >> price; + // check that the inputs succeeded + if (in) + s.revenue = s.units_sold * price; + else + s = Sales_item(); // input failed: reset object to default state + return in; +} + +std::ostream& +operator<<(std::ostream& out, const Sales_item& s) +{ + out << s.isbn() << " " << s.units_sold << " " + << s.revenue << " " << s.avg_price(); + return out; +} + +double Sales_item::avg_price() const +{ + if (units_sold) + return revenue/units_sold; + else + return 0; +} +#endif diff --git a/1/add.cc b/1/add.cc new file mode 100644 index 0000000..5bd0d25 --- /dev/null +++ b/1/add.cc @@ -0,0 +1,42 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() +{ + // prompt user to enter two numbers + std::cout << "Enter two numbers:" << std::endl; + int v1 = 0, v2 = 0; + std::cin >> v1 >> v2; + std::cout << "The sum of " << v1 << " and " << v2 + << " is " << v1 + v2 << std::endl; + return 0; +} + diff --git a/1/add_item.cc b/1/add_item.cc new file mode 100644 index 0000000..8a378ac --- /dev/null +++ b/1/add_item.cc @@ -0,0 +1,41 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include "Sales_item.h" + +int main() +{ + Sales_item item1, item2; + + std::cin >> item1 >> item2; //read a pair of transactions + std::cout << item1 + item2 << std::endl; //print their sum + + return 0; +} diff --git a/1/add_item2.cc b/1/add_item2.cc new file mode 100644 index 0000000..d43748c --- /dev/null +++ b/1/add_item2.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include "Sales_item.h" + +int main() +{ + Sales_item item1, item2; + + std::cin >> item1 >> item2; + // first check that item1 and item2 represent the same book + if (item1.isbn() == item2.isbn()) { + std::cout << item1 + item2 << std::endl; + return 0; // indicate success + } else { + std::cerr << "Data must refer to same ISBN" + << std::endl; + return -1; // indicate failure + } +} diff --git a/1/avg_price.cc b/1/avg_price.cc new file mode 100644 index 0000000..05d6d3e --- /dev/null +++ b/1/avg_price.cc @@ -0,0 +1,59 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include "Sales_item.h" + +int main() +{ + Sales_item total; // variable to hold data for the next transaction + + // read the first transaction and ensure that there are data to process + if (std::cin >> total) { + Sales_item trans; // variable to hold the running sum + // read and process the remaining transactions + while (std::cin >> trans) { + // if we're still processing the same book + if (total.isbn() == trans.isbn()) + total += trans; // update the running total + else { + // print results for the previous book + std::cout << total << std::endl; + total = trans; // total now refers to the next book + } + } + std::cout << total << std::endl; // print the last transaction + } else { + // no input! warn the user + std::cerr << "No data?!" << std::endl; + return -1; // indicate failure + } + + return 0; +} diff --git a/1/data/add b/1/data/add new file mode 100644 index 0000000..a07a471 --- /dev/null +++ b/1/data/add @@ -0,0 +1 @@ +3 7 diff --git a/1/data/add_item b/1/data/add_item new file mode 100644 index 0000000..92fd61c --- /dev/null +++ b/1/data/add_item @@ -0,0 +1,2 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/1/data/book_sales b/1/data/book_sales new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/1/data/book_sales @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/1/data/mysum b/1/data/mysum new file mode 100644 index 0000000..3c2bb02 --- /dev/null +++ b/1/data/mysum @@ -0,0 +1 @@ +3 4 5 6 diff --git a/1/data/occurs b/1/data/occurs new file mode 100644 index 0000000..dde16e2 --- /dev/null +++ b/1/data/occurs @@ -0,0 +1 @@ +42 42 42 42 42 55 55 62 100 100 100 diff --git a/1/for_ex.cc b/1/for_ex.cc new file mode 100644 index 0000000..3e3da2f --- /dev/null +++ b/1/for_ex.cc @@ -0,0 +1,38 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +int main() +{ + int sum = 0; + for (int i = -100; i <= 100; ++i) + sum += i; + std::cout << sum << std::endl; + return 0; +} diff --git a/1/forcount.cc b/1/forcount.cc new file mode 100644 index 0000000..8ced398 --- /dev/null +++ b/1/forcount.cc @@ -0,0 +1,43 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() +{ + int sum = 0; + + // sum values from 1 through 10 inclusive + for (int val = 1; val <= 10; ++val) + sum += val; // equivalent to sum = sum + val + std::cout << "Sum of 1 to 10 inclusive is " + << sum << std::endl; + return 0; +} + diff --git a/1/item_io.cc b/1/item_io.cc new file mode 100644 index 0000000..8e46602 --- /dev/null +++ b/1/item_io.cc @@ -0,0 +1,43 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include "Sales_item.h" + +int main() +{ + Sales_item book; + + // read ISBN, number of copies sold, and sales price + std::cin >> book; + // write ISBN, number of copies sold, total revenue, and average price + std::cout << book << std::endl; + + return 0; +} diff --git a/1/main_only.cc b/1/main_only.cc new file mode 100644 index 0000000..b3a362e --- /dev/null +++ b/1/main_only.cc @@ -0,0 +1,33 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +int main() +{ + return 0; +} diff --git a/1/makefile b/1/makefile new file mode 100644 index 0000000..30cb6d9 --- /dev/null +++ b/1/makefile @@ -0,0 +1,10 @@ +# executable files for this directory +OBJECTS = add.exe add_item.exe add_item2.exe avg_price.exe \ + for_ex.exe forcount.exe item_io.exe \ + main_only.exe mysum.exe occurs.exe whilecount.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +add_item.exe add_item2.exe avg_price.exe item_io.exe: Sales_item.h diff --git a/1/mysum.cc b/1/mysum.cc new file mode 100644 index 0000000..d8f1e27 --- /dev/null +++ b/1/mysum.cc @@ -0,0 +1,42 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() +{ + int sum = 0, value = 0; + + // read until end-of-file, calculating a running total of all values read + while (std::cin >> value) + sum += value; // equivalent to sum = sum + value + + std::cout << "Sum is: " << sum << std::endl; + return 0; +} diff --git a/1/occurs.cc b/1/occurs.cc new file mode 100644 index 0000000..66830fa --- /dev/null +++ b/1/occurs.cc @@ -0,0 +1,55 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() +{ + // currVal is the number we're counting; we'll read new values into val + int currVal = 0, val = 0; + + // read first number and ensure that we have data to process + if (std::cin >> currVal) { + int cnt = 1; // store the count for the current value we're processing + while (std::cin >> val) { // read the remaining numbers + if (val == currVal) // if the values are the same + ++cnt; // add 1 to cnt + else { // otherwise, print the count for the previous value + std::cout << currVal << " occurs " + << cnt << " times" << std::endl; + currVal = val; // remember the new value + cnt = 1; // reset the counter + } + } // while loop ends here + // remember to print the count for the last value in the file + std::cout << currVal << " occurs " + << cnt << " times" << std::endl; + } // outermost if statement ends here + return 0; +} diff --git a/1/runpgms b/1/runpgms new file mode 100644 index 0000000..1370a6b --- /dev/null +++ b/1/runpgms @@ -0,0 +1,9 @@ +echo "add:" && ./add < data/add +echo "add_item:" && ./add_item < data/add_item +echo "add_item2:" && ./add_item2 < data/add_item +echo "avg_price:" && ./avg_price < data/book_sales +echo "item_io:" && ./item_io < data/book_sales +echo "occurs:" && ./occurs < data/occurs +echo "mysum:" && ./mysum < data/mysum + +../noinput for_ex forcount main_only whilecount diff --git a/1/whilecount.cc b/1/whilecount.cc new file mode 100644 index 0000000..76cf572 --- /dev/null +++ b/1/whilecount.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() +{ + int sum = 0, val = 1; + // keep executing the while as long as val is less than or equal to 10 + while (val <= 10) { + sum += val; // assigns sum + val to sum + ++val; // add 1 to val + } + std::cout << "Sum of 1 to 10 inclusive is " + << sum << std::endl; + + return 0; +} + diff --git a/10/README b/10/README new file mode 100644 index 0000000..377713c --- /dev/null +++ b/10/README @@ -0,0 +1,24 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + absInt absInt + accum accum + count-size elimDups + count-size bind2 + count-size newcount-size + book_sales avg_price + book_sales sortSI + iostream_iter accum4 + iostream_iter equiv-istream + iostream_iter iostream_iter + iostream_iter ostreamIter2 + iostream_iter use-istream-iter + rcomma rcomma + reverse-iter reverse-iter + use_find use_find + +Programs not listed above print output and do +not read any input + diff --git a/10/absInt.cc b/10/absInt.cc new file mode 100644 index 0000000..9a21f9e --- /dev/null +++ b/10/absInt.cc @@ -0,0 +1,70 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::inserter; + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::for_each; using std::transform; + +int main() { + vector vi; + int i; + while (cin >> i) + vi.push_back(i); + // pass a lambda to for_each to print each element in vi + for_each(vi.begin(), vi.end(), [](int i) { cout << i << " "; }); + cout << endl; + + vector orig = vi; // save original data in vi + + // replace negative values by their absolute value + transform(vi.begin(), vi.end(), vi.begin(), + [](int i) { return i < 0 ? -i : i; }); + // print the elements now in vi + for_each(vi.begin(), vi.end(), [](int i) { cout << i << " "; }); + cout << endl; + + vi = orig; // start afresh + // use a lambda with a specified return type to transform vi + transform(vi.begin(), vi.end(), vi.begin(), + [](int i) -> int + { if (i < 0) return -i; else return i; }); + // print the elements now in vi + for_each(vi.begin(), vi.end(), [](int i) { cout << i << " "; }); + cout << endl; + + return 0; +} diff --git a/10/accum.cc b/10/accum.cc new file mode 100644 index 0000000..aed0301 --- /dev/null +++ b/10/accum.cc @@ -0,0 +1,79 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::fill; using std::fill_n; + +#include +using std::accumulate; + +#include +using std::back_inserter; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + vector vec(10); // default initialized to 0 + fill(vec.begin(), vec.end(), 1); // reset each element to 1 + + // sum the elements in vec starting the summation with the value 0 + int sum = accumulate(vec.cbegin(), vec.cend(), 0); + cout << sum << endl; + + // set a subsequence of the container to 10 + fill(vec.begin(), vec.begin() + vec.size()/2, 10); + cout << accumulate(vec.begin(), vec.end(), 0) << endl; + + // reset the same subsequence to 0 + fill_n(vec.begin(), vec.size()/2, 0); + cout << accumulate(vec.begin(), vec.end(), 0) << endl; + + // create 10 elements on the end of vec each with the value 42 + fill_n(back_inserter(vec), 10, 42); + cout << accumulate(vec.begin(), vec.end(), 0) << endl; + + // concatenate elements in a vector of strings and store in sum + vector v; + string s; + while (cin >> s) + v.push_back(s); + string concat = accumulate(v.cbegin(), v.cend(), string("")); + cout << concat << endl; + + return 0; +} + diff --git a/10/accum4.cc b/10/accum4.cc new file mode 100644 index 0000000..9b0426a --- /dev/null +++ b/10/accum4.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::accumulate; + +#include +using std::istream_iterator; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + istream_iterator in(cin), eof; + cout << accumulate(in, eof, 0) << endl; + return 0; +} diff --git a/10/avg_price.cc b/10/avg_price.cc new file mode 100644 index 0000000..9f94861 --- /dev/null +++ b/10/avg_price.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::istream_iterator; using std::ostream_iterator; + +#include +using std::cin; using std::cout; + +#include "Sales_item.h" + +int main() +{ + // iterators that can read and write Sales_items + istream_iterator item_iter(cin), eof; + ostream_iterator out_iter(cout, "\n"); + + // store the first transaction in sum and read the next record + Sales_item sum = *item_iter++; + + while (item_iter != eof) { + // if the current transaction (which is in item_iter) + // has the same ISBN + if (item_iter->isbn() == sum.isbn()) + sum += *item_iter++; // add it to sum + // and read the next transaction + else { + out_iter = sum; // write the current sum + sum = *item_iter++; // read the next transaction + } + } + out_iter = sum; // remember to print the last set of records + + return 0; +} diff --git a/10/bind2.cc b/10/bind2.cc new file mode 100644 index 0000000..ce1cda0 --- /dev/null +++ b/10/bind2.cc @@ -0,0 +1,80 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::for_each; + +#include +using std::bind; using namespace std::placeholders; +using std::ref; + +#include +using std::istream_iterator; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::cout; using std::cin; using std::endl; +using std::ostream; + +#include +using std::ifstream; using std::ofstream; + +ostream &print(ostream &os, const string &s, char c) +{ + return os << s << c; +} + +int main() +{ + string s; + vector words; + while (cin >> s) + words.push_back(s); + + for_each(words.begin(), words.end(), + bind(print, ref(cout), _1, ' ')); + cout << endl; + + ofstream os("data/outFile1"); + for_each(words.begin(), words.end(), + bind(print, ref(os), _1, ' ')); + os << endl; + + ifstream is("data/outFile1"); + istream_iterator in(is), eof; + for_each(in, eof, bind(print, ref(cout), _1, '\n')); + cout << endl; + + return 0; +} diff --git a/10/data/absInt b/10/data/absInt new file mode 100644 index 0000000..9726e80 --- /dev/null +++ b/10/data/absInt @@ -0,0 +1,15 @@ +-42 +65 +-95 +100 +39 +-67 +95 +76 +-88 +76 +83 +92 +76 +-93 +0 diff --git a/10/data/accum b/10/data/accum new file mode 100644 index 0000000..5bfcb72 --- /dev/null +++ b/10/data/accum @@ -0,0 +1 @@ +now is the time for all good fellows to come to the aid of the party diff --git a/10/data/book_sales b/10/data/book_sales new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/10/data/book_sales @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/10/data/count-size b/10/data/count-size new file mode 100644 index 0000000..da448a3 --- /dev/null +++ b/10/data/count-size @@ -0,0 +1 @@ +the quick red fox jumps over the slow red turtle diff --git a/10/data/iostream_iter b/10/data/iostream_iter new file mode 100644 index 0000000..2451c69 --- /dev/null +++ b/10/data/iostream_iter @@ -0,0 +1 @@ +23 109 45 89 6 34 12 90 34 23 56 23 8 89 23 diff --git a/10/data/outFile1 b/10/data/outFile1 new file mode 100644 index 0000000..e2b4b0d --- /dev/null +++ b/10/data/outFile1 @@ -0,0 +1 @@ +the quick red fox jumps over the slow red turtle diff --git a/10/data/outFile2 b/10/data/outFile2 new file mode 100644 index 0000000..5095c75 --- /dev/null +++ b/10/data/outFile2 @@ -0,0 +1 @@ +6 8 12 23 23 23 23 34 34 45 56 89 89 90 109 diff --git a/10/data/rcomma b/10/data/rcomma new file mode 100644 index 0000000..bc224c0 --- /dev/null +++ b/10/data/rcomma @@ -0,0 +1 @@ +FIRST,MIDDLE,LAST diff --git a/10/data/readme b/10/data/readme new file mode 100644 index 0000000..b2faf07 --- /dev/null +++ b/10/data/readme @@ -0,0 +1,24 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + SI_data arrow + absInt absInt + count-size bind + count-size bind2 + count-size newcount-size + book_sales avg_price + iostream_iter accum4 + iostream_iter equiv-istream + iostream_iter iostream_iter + iostream_iter ostreamIter2 + iostream_iter use-istream-iter + parms parms + readme readStr + reverse-iter reverse-iter + use_find use_find + +Programs not listed above print output and do +not read any input + diff --git a/10/data/reverse-iter b/10/data/reverse-iter new file mode 100644 index 0000000..23ee009 --- /dev/null +++ b/10/data/reverse-iter @@ -0,0 +1 @@ +1 7 3 5 9 diff --git a/10/data/use_find b/10/data/use_find new file mode 100644 index 0000000..d544c40 --- /dev/null +++ b/10/data/use_find @@ -0,0 +1,3 @@ +42 +47 +83 diff --git a/10/elimDups.cc b/10/elimDups.cc new file mode 100644 index 0000000..0e17e5b --- /dev/null +++ b/10/elimDups.cc @@ -0,0 +1,109 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::sort; using std::for_each; + +#include +using std::bind; +using namespace std::placeholders; + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +// comparison function to be used to sort by word length +bool isShorter(const string &s1, const string &s2) +{ + return s1.size() < s2.size(); +} + +bool LT(const string &s1, const string &s2) +{ + return s1 < s2; +} + +void print(const vector &words) +{ + for_each(words.begin(), words.end(), + [](const string &s) { cout << s << " "; }); + cout << endl; +} + +int main() +{ + vector words; + + // copy contents of each book into a single vector + string next_word; + while (cin >> next_word) { + // insert next book's contents at end of words + words.push_back(next_word); + } + print(words); + + vector cpy = words; // save the original data + + // uses string < to compare elements + // sort and print the vector + sort(words.begin(), words.end()); + + words = cpy; // return to the original data + // uses the LT function to compare elements + // should have the same output as the previous sort + sort(words.begin(), words.end(), LT); + print(words); + + words = cpy; // return to the original data + + // eliminate duplicates + sort(words.begin(), words.end()); + auto it = unique(words.begin(), words.end()); + words.erase(it, words.end()); + + // sort by length using a function + stable_sort(words.begin(), words.end(), isShorter); + print(words); + + words = cpy; // return to the original data + // sort the original input on word length, shortest to longest + sort(words.begin(), words.end(), isShorter); + print(words); + + // use bind to invert isShorter to sort longest to shortest + sort(words.begin(), words.end(), bind(isShorter, _2, _1)); + print(words); + + return 0; +} diff --git a/10/equal.cc b/10/equal.cc new file mode 100644 index 0000000..f90199b --- /dev/null +++ b/10/equal.cc @@ -0,0 +1,60 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::equal; + +#include +using std::list; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + list roster1; + vector roster2; + roster2 = {"hello", "so long", "tata"}; + roster1 = {"hello", "so long"}; + + auto b = + // roster2 should have at least as many elements as roster1 + equal(roster1.cbegin(), roster1.cend(), roster2.cbegin()); + + (b) ? cout << "true" : cout << "false"; + cout << endl; + + return 0; +} diff --git a/10/equiv-istream-iter.cc b/10/equiv-istream-iter.cc new file mode 100644 index 0000000..1454ff1 --- /dev/null +++ b/10/equiv-istream-iter.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::istream_iterator; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() { + // use istream_iterator to initialize a vector + istream_iterator in_iter(cin), eof; + vector vec(in_iter, eof); + + for (auto it : vec) + cout << it << " "; + cout << endl; + + return 0; +} + diff --git a/10/inserters.cc b/10/inserters.cc new file mode 100644 index 0000000..07d82da --- /dev/null +++ b/10/inserters.cc @@ -0,0 +1,79 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::copy; + +#include +using std::list; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::inserter; using std::front_inserter; + +void print(const string &label, const list &lst) +{ + cout << label << endl; + for (auto iter : lst) + cout << iter << " "; + cout << endl; +} + +int main() +{ + + list lst = {1,2,3,4}; + print("lst", lst); + + // after copy completes, lst2 contains 4 3 2 1 + list lst2, lst3; // empty lists + copy(lst.cbegin(), lst.cend(), front_inserter(lst2)); + + // after copy completes, lst3 contains 1 2 3 4 + copy(lst.cbegin(), lst.cend(), inserter(lst3, lst3.begin())); + + print("lst2", lst2); + print("lst3", lst3); + + vector v = {1,2,3,4,5}; + list new_lst = {6,7,8,9}; + auto it = new_lst.begin(); + copy(v.begin(), v.end(), inserter(new_lst, it)); + print("new_lst", new_lst); + + return 0; +} diff --git a/10/iostream_iter.cc b/10/iostream_iter.cc new file mode 100644 index 0000000..8730474 --- /dev/null +++ b/10/iostream_iter.cc @@ -0,0 +1,63 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::copy; using std::sort; using std::unique_copy; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::istream_iterator; using std::ostream_iterator; + +#include +using std::ofstream; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + istream_iterator int_it(cin); // reads ints from cin + istream_iterator int_eof; // end iterator value + vector v(int_it, int_eof); // initialize v by reading cin + + sort(v.begin(), v.end()); + ostream_iterator out(cout, " "); // write ints to cout + unique_copy(v.begin(), v.end(), out); // write unique elements to cout + cout << endl; // write a newline after the output + ofstream out_file("data/outFile2"); // writes int to named file + ostream_iterator out_iter(out_file, " "); + copy(v.begin(), v.end(), out_iter); + out_file << endl; // write a newline at end of the file + return 0; +} diff --git a/10/lambda.cc b/10/lambda.cc new file mode 100644 index 0000000..229618d --- /dev/null +++ b/10/lambda.cc @@ -0,0 +1,100 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include +using std::cout; using std::endl; using std::vector; using std::string; + +// five functions illustrating different aspects of lambda expressions +void fcn1() +{ + size_t v1 = 42; // local variable + // copies v1 into the callable object named f + auto f = [v1] { return v1; }; + v1 = 0; + auto j = f(); // j is 42; f stored a copy of v1 when we created it + cout << j << endl; +} + +void fcn2() +{ + size_t v1 = 42; // local variable + // the object f2 contains a reference to v1 + auto f2 = [&v1] { return v1; }; + v1 = 0; + auto j = f2(); // j is 0; f2 refers to v1; it doesn't store it + cout << j << endl; +} + +void fcn3() +{ + size_t v1 = 42; // local variable + // f can change the value of the variables it captures + auto f = [v1] () mutable { return ++v1; }; + v1 = 0; + auto j = f(); // j is 43 + cout << j << endl; +} + +void fcn4() +{ + size_t v1 = 42; // local variable + // v1 is a reference to a nonconst variable + // we can change that variable through the reference inside f2 + auto f2 = [&v1] { return ++v1; }; + v1 = 0; + auto j = f2(); // j is 1 + cout << j << endl; +} + +void fcn5() +{ + size_t v1 = 42; + // p is a const pointer to v1 + size_t* const p = &v1; + // increments v1, the objet to which p points + auto f = [p]() { return ++*p; }; + auto j = f(); // returns incremented value of *p + cout << v1 << " " << j << endl; // prints 43 43 + v1 = 0; + j = f(); // returns incremented value of *p + cout << v1 << " " << j << endl; // prints 1 1 +} + + +int main() +{ + fcn1(); + fcn2(); + fcn3(); + fcn4(); + fcn5(); + return 0; +} diff --git a/10/makefile b/10/makefile new file mode 100644 index 0000000..3e00b1a --- /dev/null +++ b/10/makefile @@ -0,0 +1,15 @@ +# executable files for this directory +OBJECTS = absInt.exe accum.exe accum4.exe \ + avg_price.exe bind2.exe elimDups.exe equal.exe \ + equiv-istream-iter.exe inserters.exe \ + iostream_iter.exe lambda.exe newcount-size.exe \ + ostreamIter2.exe rcomma.exe \ + reverse.exe reverse-iter.exe rev-iters.exe \ + sortSI.exe use_find.exe use-istream-iter.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 -I../6 + diff --git a/10/newcount-size.cc b/10/newcount-size.cc new file mode 100644 index 0000000..9c7043e --- /dev/null +++ b/10/newcount-size.cc @@ -0,0 +1,149 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::find_if; using std::for_each; +using std::sort; using std::stable_sort; +using std::unique; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::ifstream; + +#include +using std::size_t; + +#include +using std::bind; +using std::placeholders::_1; +using namespace std::placeholders; + +#include +using std::cin; using std::cout; using std::endl; + +#include "make_plural.h" + +// comparison function to be used to sort by word length +bool isShorter(const string &s1, const string &s2) +{ + return s1.size() < s2.size(); +} + +// determine whether a length of a given word is 6 or more +bool GT(const string &s, string::size_type m) +{ + return s.size() >= m; +} + +void elimDups(vector &words) +{ + // sort words alphabetically so we can find the duplicates + sort(words.begin(), words.end()); + for_each(words.begin(), words.end(), + [](const string &s) { cout << s << " "; }); + cout << endl; + + + // unique reorders the input so that each word appears once in the + // front part of the range + // returns an iterator one past the unique range + auto end_unique = unique(words.begin(), words.end()); + for_each(words.begin(), words.end(), + [](const string &s) { cout << s << " "; }); + cout << endl; + + // erase uses a vector operation to remove the nonunique elements + words.erase(end_unique, words.end()); + for_each(words.begin(), words.end(), + [](const string &s) { cout << s << " "; }); + cout << endl; +} + +void +biggies(vector &words, vector::size_type sz) +{ + elimDups(words); // put words in alphabetical order and remove duplicates + // sort words by size, but maintain alphabetical order for words of the same size + stable_sort(words.begin(), words.end(), + [](const string &a, const string &b) + { return a.size() < b.size();}); + + // get an iterator to the first element whose size() is >= sz + auto wc = find_if(words.begin(), words.end(), + [sz](const string &a) + { return a.size() >= sz; }); + + // compute the number of elements with size >= sz + auto count = words.end() - wc; + cout << count << " " << make_plural(count, "word", "s") + << " of length " << sz << " or longer" << endl; + + // print words of the given size or longer, each one followed by a space + for_each(wc, words.end(), + [](const string &s){cout << s << " ";}); + cout << endl; +} + +bool check_size(const string &s, string::size_type sz) +{ + return s.size() >= sz; +} + +int main() +{ + vector words; + + // copy contents of each book into a single vector + string next_word; + while (cin >> next_word) { + // insert next book's contents at end of words + words.push_back(next_word); + } + + biggies(words, 5); // biggies changes its first argument + + // alternative solution using bind and check_size function + // NB: words was changed inside biggies, + // at this point in the program words has only unique + // words and is in order by size + size_t sz = 5; + auto + wc = find_if(words.begin(), words.end(), + bind(check_size, std::placeholders::_1, sz)); + auto count = words.end() - wc; + cout << count << " " << make_plural(count, "word", "s") + << " of length " << sz << " or longer" << endl; + + return 0; +} diff --git a/10/ostreamIter2.cc b/10/ostreamIter2.cc new file mode 100644 index 0000000..4e3bf6a --- /dev/null +++ b/10/ostreamIter2.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::istream_iterator; using std::ostream_iterator; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() { + vector vec; + istream_iterator in_iter(cin); // read ints from cin + istream_iterator eof; // istream ``end'' iterator + + while (in_iter != eof) + vec.push_back(*in_iter++); + ostream_iterator out_iter(cout, " "); + + for (auto e : vec) + out_iter = e; // the assignment writes this element to cout + cout << endl; + + return 0; +} + diff --git a/10/outrunpgms b/10/outrunpgms new file mode 100644 index 0000000..e69de29 diff --git a/10/rcomma.cc b/10/rcomma.cc new file mode 100644 index 0000000..d126cab --- /dev/null +++ b/10/rcomma.cc @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::find; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + string line; + getline(cin, line); + + // find the first element in a comma-separated list + auto comma = find(line.cbegin(), line.cend(), ','); + cout << string(line.cbegin(), comma) << endl; + + // find the last element in a comma-separated list + auto rcomma = find(line.crbegin(), line.crend(), ','); + + // WRONG: will generate the word in reverse order + cout << string(line.crbegin(), rcomma) << endl; + + // ok: get a forward iterator and read to the end of line + cout << string(rcomma.base(), line.cend()) << endl; + + return 0; +} diff --git a/10/rev-iters.cc b/10/rev-iters.cc new file mode 100644 index 0000000..1a4da4b --- /dev/null +++ b/10/rev-iters.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + vector vec = {0,1,2,3,4,5,6,7,8,9}; + // reverse iterator of vector from back to front + for (auto r_iter = vec.crbegin(); // binds r_iter to the last element + r_iter != vec.crend(); // one before first element + ++r_iter) // decrements the iterator one element + cout << *r_iter << endl; // prints 9, 8, 7, . . . 0 + return 0; +} diff --git a/10/reverse-iter.cc b/10/reverse-iter.cc new file mode 100644 index 0000000..989d6db --- /dev/null +++ b/10/reverse-iter.cc @@ -0,0 +1,77 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::copy; using std::sort; + +#include +using std::istream_iterator; using std::ostream_iterator; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + vector vec; + istream_iterator in(cin), eof; + copy (in, eof, back_inserter(vec)); + + sort(vec.begin(), vec.end()); // sorts vec in ``normal'' order + vector vec2(vec); // vec2 is a copy of vec + + // sorts in reverse: puts the smallest element at the end of vec + sort(vec.rbegin(), vec.rend()); + + // prints vec + ostream_iterator out(cout, " "); + copy(vec.begin(), vec.end(), out); // reverse sort + cout << endl; + + // prints vec2 + copy(vec2.begin(), vec2.end(), out); // forward sort + cout << endl; + + // it refers to the first element in vec + auto it = vec.begin(); + + // rev_it refers one before the first element in vec + vector::reverse_iterator rev_it(it); + cout << *(rev_it.base()) << endl; // prints first element in vec + + // ways to print value at beginning of vec through it and rev_it + cout << *it << endl; + cout << *(rev_it.base()) << endl; + cout << *(rev_it - 1) << endl; + + return 0; +} + diff --git a/10/reverse.cc b/10/reverse.cc new file mode 100644 index 0000000..883701a --- /dev/null +++ b/10/reverse.cc @@ -0,0 +1,89 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::find; using std::find_if; using std::for_each; +using std::remove_copy_if; using std::reverse_copy; +using std::reverse; + +#include +using std::back_inserter; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + vector v1 = {0,1,2,3,4,5,6,7,8,9}; + vector v2; + vector v3 = v1; + + find(v1.begin(), v1.end(), 42); // find first element equal to 42 + find_if(v1.begin(), v1.end(), // find first odd element + [](int i) { return i % 2; }); + + // puts elements in v1 into reverse order + reverse(v1.begin(), v1.end()); + v1 = v3; // restore original data + + // copies elements from v1 into v2 in reverse order; v1 is unchanged + reverse_copy(v1.begin(), v1.end(), back_inserter(v2)); + + for (auto i : v1) + cout << i << " "; // prints 0 1 2 . . . 8 9 + cout << endl; + for (auto i : v2) + cout << i << " "; // prints 9 8 7 . . . 1 0 + cout << endl; + + // removes the odd elements from v1 + auto it = remove_if(v1.begin(), v1.end(), + [](int i) { return i % 2; }); + // prints 0 2 4 6 8 + for_each(v1.begin(), it, [](int i) { cout << i << " "; }); + cout << endl; + + v1 = v3; // restore original data + v2.clear(); // make v2 empty again + + // copies only the even elements from v1 into v2; v1 is unchanged + remove_copy_if(v1.begin(), v1.end(), back_inserter(v2), + [](int i) { return i % 2; }); + for (auto i : v2) + cout << i << " "; // prints 0 2 4 6 8 + cout << endl; + for (auto i : v1) // prints 0 1 2 . . . 8 9 + cout << i << " "; + cout << endl; + + return 0; +} diff --git a/10/runpgms b/10/runpgms new file mode 100644 index 0000000..0ac0b98 --- /dev/null +++ b/10/runpgms @@ -0,0 +1,18 @@ +echo "absInt:" && ./absInt.exe < data/absInt +echo "accum:" && ./accum.exe < data/accum +echo "accum4:" && ./accum4.exe < data/iostream_iter +echo "avg_price:" && ./avg_price.exe < data/book_sales +echo "bind2:" && ./bind2.exe < data/count-size +echo "elimDups: " && ./elimDups < data/count-size +echo "equiv-istream-iter:" && ./equiv-istream-iter.exe < data/iostream_iter +echo "iostream_iter:" && ./iostream_iter.exe < data/iostream_iter +echo "newcount-size:" && ./newcount-size.exe < data/count-size +echo "ostreamIter2:" && ./ostreamIter2.exe < data/iostream_iter +echo "rcomma:" && ./rcomma.exe < data/rcomma +echo "reverse-iter:" && ./reverse-iter.exe < data/reverse-iter +echo "sortSI:" && ./sortSI.exe < data/book_sales +echo "use_find:" && ./use_find.exe < data/use_find +echo "use-istream-iter:" && ./use-istream-iter.exe < data/iostream_iter + +../noinput equal.exe inserters.exe lambda.exe reverse.exe \ + rev-iters.exe diff --git a/10/sortSI.cc b/10/sortSI.cc new file mode 100644 index 0000000..1e323f9 --- /dev/null +++ b/10/sortSI.cc @@ -0,0 +1,59 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::sort; + +#include +using std::vector; + +#include +using std::cout; using std::cin; using std::endl; + +#include "Sales_item.h" + +int main() +{ + Sales_item trans; + vector file; + + while (std::cin >> trans) // read the transations + file.push_back(trans); + + for (auto i : file) // print what was read + cout << i << endl; + cout << "\n\n"; + + sort(file.begin(), file.end(), compareIsbn); // sort into ISBN order + for (auto i : file) // print in ISBN order + cout << i << endl; + + return 0; +} + diff --git a/10/use-istream-iter.cc b/10/use-istream-iter.cc new file mode 100644 index 0000000..88f8f83 --- /dev/null +++ b/10/use-istream-iter.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::copy; + +#include +using std::istream_iterator; using std::ostream_iterator; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() { + vector vec; + istream_iterator in_iter(cin); // read ints from cin + istream_iterator eof; // istream ``end'' iterator + + // use in_iter to read cin storing what we read in vec + while (in_iter != eof) // while there's valid input to read + // postfix increment reads the stream and + // returns the old value of the iterator + // we dereference that iterator to get + // the previous value read from the stream + vec.push_back(*in_iter++); + + // use an ostream_iterator to print the contents of vec + ostream_iterator out_iter(cout, " "); + copy(vec.begin(), vec.end(), out_iter); + cout << endl; + + // alternative way to print contents of vec + for (auto e : vec) + *out_iter++ = e; // the assignment writes this element to cout + cout << endl; + + return 0; +} + diff --git a/10/use_find.cc b/10/use_find.cc new file mode 100644 index 0000000..f0f0316 --- /dev/null +++ b/10/use_find.cc @@ -0,0 +1,85 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::find; + +#include +using std::begin; using std::end; + +#include +using std::vector; + +#include +using std::list; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + int ia[] = {27, 210, 12, 47, 109, 83}; + int val = 83; + int* result = find(begin(ia), end(ia), val); + cout << "The value " << val + << (result == end(ia) + ? " is not present" : " is present") << endl; + + // search starting from ia[1] up to but not including ia[4] + result = find(ia + 1, ia + 4, val); + + // initialize the vector with some values + vector vec = {27, 210, 12, 47, 109, 83}; + val = 42; // value we'll look for + + // result2 will denote the element we want if it's in vec, + // or vec.cend() if not + auto result2 = find(vec.cbegin(), vec.cend(), val); + + // report the result + cout << "The value " << val + << (result2 == vec.cend() + ? " is not present" : " is present") << endl; + + // now use find to look in a list of strings + list lst = {"val1", "val2", "val3"}; + + string sval = "a value"; // value we'll look for + // this call to find looks through string elements in a list + auto result3 = find(lst.cbegin(), lst.cend(), sval); + cout << "The value " << sval + << (result3 == lst.cend() + ? " is not present" : " is present") << endl; + + return 0; +} + diff --git a/11/README b/11/README new file mode 100644 index 0000000..ee650fa --- /dev/null +++ b/11/README @@ -0,0 +1,15 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + storyDatafile wcEx + storyDatafile erasemap + storyDatafile pair + storyDatafile word_count + word_count restricted_count + word_count unorderedWord_count + +Programs not listed above print output and do +not read any input + diff --git a/11/data/Alice b/11/data/Alice new file mode 100644 index 0000000..08b6485 --- /dev/null +++ b/11/data/Alice @@ -0,0 +1 @@ +Alice diff --git a/11/data/rules b/11/data/rules new file mode 100644 index 0000000..c63e9e3 --- /dev/null +++ b/11/data/rules @@ -0,0 +1,8 @@ +brb be right back +k okay? +y why +r are +u you +pic picture +thk thanks! +l8r later diff --git a/11/data/simple_query b/11/data/simple_query new file mode 100644 index 0000000..21e6e0a --- /dev/null +++ b/11/data/simple_query @@ -0,0 +1,2 @@ +data +compiler diff --git a/11/data/storyDataFile b/11/data/storyDataFile new file mode 100644 index 0000000..1f06b36 --- /dev/null +++ b/11/data/storyDataFile @@ -0,0 +1,10 @@ +Alice Emma has long flowing red hair. +Her Daddy says when the wind blows +through her hair, it looks almost alive, +like a fiery bird in flight. +A beautiful fiery bird, he tells her, +magical but untamed. +"Daddy, shush, there is no such thing," +she tells him, at the same time wanting +him to tell her more. +Shyly, she asks, "I mean, Daddy, is there?" diff --git a/11/data/text b/11/data/text new file mode 100644 index 0000000..c6fe124 --- /dev/null +++ b/11/data/text @@ -0,0 +1,3 @@ +where r u +y dont u send me a pic +k thk l8r diff --git a/11/data/word_count b/11/data/word_count new file mode 100644 index 0000000..bc13fed --- /dev/null +++ b/11/data/word_count @@ -0,0 +1,7 @@ +Although most programmers are familiar with data structures +such as vectors and lists, many have never used an +associative data structure. +Before we look at the details of how the library supports +these types, it will be helpful to start with +examples of how we can use +these containers. diff --git a/11/erasemap.cc b/11/erasemap.cc new file mode 100644 index 0000000..30f1d00 --- /dev/null +++ b/11/erasemap.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::map; + +#include +using std::string; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + map word_count; // empty map from string to size_t + string word; + while (cin >> word) + ++word_count[word]; + + string removal_word = "the"; + + // two ways to remove an entry from a map + // 1. by key + // erase on a key returns the number of elements removed + if (word_count.erase(removal_word)) + cout << "ok: " << removal_word << " removed\n"; + else cout << "oops: " << removal_word << " not found!\n"; + + // 2. by removing an iterator to the element we want removed + removal_word = "The"; // strings are case sensitive + map::iterator where; + where = word_count.find(removal_word); // should be gone + + if (where == word_count.end()) + cout << "oops: " << removal_word << " not found!\n"; + else { + word_count.erase(where); // erase iterator returns void + cout << "ok: " << removal_word << " removed!\n"; + } + return 0; +} diff --git a/11/makefile b/11/makefile new file mode 100644 index 0000000..530f735 --- /dev/null +++ b/11/makefile @@ -0,0 +1,17 @@ +# executable files for this directory +OBJECTS = erasemap.exe map1.exe \ + multimap.exe pair.exe \ + restricted_count.exe setOps.exe \ + unorderedWordCount.exe wcEx.exe \ + word_count.exe word_transform.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +# directory 7 has Sales_data code, 6 has make_plural function +LOCFLAGS = -I../7 -I../6 + +map1.exe: map1.o ../7/Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) map1.o ../7/Sales_data.o -o map1.exe + diff --git a/11/map1.cc b/11/map1.cc new file mode 100644 index 0000000..06ab7c3 --- /dev/null +++ b/11/map1.cc @@ -0,0 +1,103 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::multiset; + +#include +using std::map; + +#include +using std::string; + +#include +using std::pair; + +#include +using std::cout; using std::endl; + +#include "Sales_data.h" +#include "make_plural.h" + +// SDComp points to a function that compares two Sales_data objects +typedef bool(*SDComp)(const Sales_data&, const Sales_data&); + +// bookstore can have several transactions with the same ISBN +// elements in bookstore will be in ISBN order +multiset + bookstore(compareIsbn); + +// alternative way to declare bookstore using a lambda +multiset + bookstore2([](const Sales_data &l, const Sales_data &r) + { return l.isbn() < r.isbn(); }); + +int main() +{ + map word_count; // empty map + + // insert a value-initialized element with key Anna; + // assign 1 to the value of that element + word_count["Anna"] = 1; + + // fetch the element indexed by Anna; prints 1 + cout << word_count["Anna"] << endl; + + ++word_count["Anna"]; // fetch the element and add 1 to it + cout << word_count["Anna"] << endl; // prints 2 + + // various ways to add word to word_count + string word; + word_count.insert({word, 1}); + word_count.insert(make_pair(word, 1)); + word_count.insert(pair(word, 1)); + word_count.insert(map::value_type(word, 1)); + + typedef map::value_type valType; + word_count.insert(valType(word, 1)); + + // use value returned by insert + pair::iterator, bool> insert_ret; + + // if Anna not already in word_count, insert it with value 1 + insert_ret = word_count.insert({"Anna", 1}); + + if (insert_ret.second == false) // Anna was already in the map + insert_ret.first->second++; // increment current value + cout << word_count["Anna"] << endl; + + // get an iterator to an element in word_count + auto map_it = word_count.begin(); + + // *map_it is a reference to a pair object + cout << map_it->first; // prints the key for this element + cout << " " << map_it->second; // prints the value of the element + ++map_it->second; // ok: we can change the value through an iterator + return 0; +} diff --git a/11/multimap.cc b/11/multimap.cc new file mode 100644 index 0000000..689d618 --- /dev/null +++ b/11/multimap.cc @@ -0,0 +1,79 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::multimap; + +#include +using std::string; + +#include +using std::pair; + +#include +using std::cout; using std::endl; + +int main() +{ + // map from author to title; there can be multiple titles per author + multimap authors; + // add data to authors + authors.insert({"Alain de Botton", "On Love"}); + authors.insert({"Alain de Botton", "Status Anxiety"}); + authors.insert({"Alain de Botton", "Art of Travel"}); + authors.insert({"Alain de Botton", + "Architecture of Happiness"}); + + string search_item("Alain de Botton"); // author we'll look for + auto entries = authors.count(search_item); // number of elements + auto iter = authors.find(search_item); // first entry for this author + + // loop through the number of entries there are for this author + while(entries) { + cout << iter->second << endl; // print each title + ++iter; // advance to the next title + --entries; // keep track of how many we've printed + } + + // definitions of authors and search_item as above + // beg and end denote the range of elements for this author + for (auto beg = authors.lower_bound(search_item), + end = authors.upper_bound(search_item); + beg != end; ++beg) + cout << beg->second << endl; // print each title + + // definitions of authors and search_item as above + // pos holds iterators that denote the range of elements for this key + for (auto pos = authors.equal_range(search_item); + pos.first != pos.second; ++pos.first) + cout << pos.first->second << endl; // print each title + + return 0; +} + diff --git a/11/pair.cc b/11/pair.cc new file mode 100644 index 0000000..b32b321 --- /dev/null +++ b/11/pair.cc @@ -0,0 +1,125 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::list; + +#include +using std::vector; + +#include +using std::sort; + +#include +using std::istream_iterator; using std::ostream_iterator; + +#include +using std::pair; using std::make_pair; + +#include +using std::cin; using std::cout; using std::endl; + +#include "Sales_data.h" + +pair anon; // holds two strings +pair word_count; // holds a string and an size_t +pair> line; // holds string and vector + +// list initialize the members in a pair +pair author{"James", "Joyce"}; + +// ways to initialize a pair +typedef pair Author; +Author proust("Marcel", "Proust"); // construct a pair +Author joyce{"James", "Joyce"}; // list initialize a pair +Author austen = make_pair("Jane", "Austen"); // use make_pair + +void preliminaries(vector &v) +{ + // process v + istream_iterator input(cin), eof; + copy(input, eof, back_inserter(v)); + sort(v.begin(), v.end(), + [](const string &a, const string &b) + { return a.size() < b.size(); }); +} + +// three different ways to return a pair +// 1. list initialize the return value +pair +process(vector &v) +{ + if (!v.empty()) + return {v.back(), v.back().size()}; // list initialize + else + return pair(); // explicitly constructed return value +} + +// 2. use make_pair to generate the return value +pair +process2(vector &v) +{ + // process v + if (!v.empty()) + return make_pair(v.back(), v.back().size()); + else + return pair(); +} + +// 3. explicitly construct the return value +pair +process3(vector &v) +{ + // process v + if (!v.empty()) + return pair(v.back(), v.back().size()); + else + return pair(); +} + +int main() +{ + vector v; + string s; + while (cin >> s) + v.push_back(s); + + preliminaries(v); // sort v into size order + + // all three output statements should be the same + auto biggest = process(v); + cout << biggest.first << " " << biggest.second << endl; + biggest = process2(v); + cout << biggest.first << " " << biggest.second << endl; + biggest = process3(v); + cout << biggest.first << " " << biggest.second << endl; +} diff --git a/11/restricted_count.cc b/11/restricted_count.cc new file mode 100644 index 0000000..959218e --- /dev/null +++ b/11/restricted_count.cc @@ -0,0 +1,60 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::map; + +#include +using std::set; + +#include +using std::string; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + // count the number of times each word occurs in the input + map word_count; // empty map from string to size_t + set exclude = {"The", "But", "And", "Or", "An", "A", + "the", "but", "and", "or", "an", "a"}; + string word; + while (cin >> word) + // count only words that are not in exclude + if (exclude.find(word) == exclude.end()) + ++word_count[word]; // fetch and increment the counter for word + + for (const auto &w : word_count) // for each element in the map + // print the results + cout << w.first << " occurs " << w.second + << ((w.second > 1) ? " times" : " time") << endl; + +return 0; +} diff --git a/11/runpgms b/11/runpgms new file mode 100644 index 0000000..ed101d0 --- /dev/null +++ b/11/runpgms @@ -0,0 +1,9 @@ +echo "wcEx:" && ./wcEx.exe < data/storyDataFile +echo "erasemap:" && ./erasemap.exe < data/storyDataFile +echo "restricted_count:" && ./restricted_count.exe < data/word_count +echo "unorderedWordCount:" && ./unorderedWordCount.exe < data/word_count +echo "word_count:" && ./word_count.exe < data/storyDataFile +echo "pair:" && ./pair.exe < data/storyDataFile +echo "word_transform:" && ./word_transform.exe data/rules data/text + +../noinput map1.exe multimap.exe setOps.exe diff --git a/11/setOps.cc b/11/setOps.cc new file mode 100644 index 0000000..67a86ec --- /dev/null +++ b/11/setOps.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::set; using std::multiset; + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + // define a vector with 20 elements, + // holding two copies of each number from 0 to 9 + vector ivec; + for (vector::size_type i = 0; i != 10; ++i) { + ivec.push_back(i); + ivec.push_back(i); // duplicate copies of each number + } + + // iset holds unique elements from ivec; miset holds all 20 elements + set iset(ivec.cbegin(), ivec.cend()); + multiset miset(ivec.cbegin(), ivec.cend()); + + cout << ivec.size() << endl; // prints 20 + cout << iset.size() << endl; // prints 10 + cout << miset.size() << endl; // prints 20 + + // returns an iterator that refers to the element with key == 1 + iset.find(1); + iset.find(11); // returns the iterator == iset.end() + iset.count(1); // returns 1 + iset.count(11); // returns 0 + + // returns an iterator to the first element with key == 1 + iset.find(1); + iset.find(11); // returns the iterator == iset.end() + miset.count(1); // returns 2 + miset.count(11); // returns 0 + + set set1; // empty set + set1.insert("the"); // set1 now has one element + set1.insert("and"); // set1 now has two elements + + ivec = {2,4,6,8,2,4,6,8}; // ivec now has eight elements + set set2; // empty set + set2.insert(ivec.cbegin(), ivec.cend()); // set2 has four elements + set2.insert({1,3,5,7,1,3,5,7}); // set2 now has eight elements + + return 0; +} diff --git a/11/unorderedWordCount.cc b/11/unorderedWordCount.cc new file mode 100644 index 0000000..a37043d --- /dev/null +++ b/11/unorderedWordCount.cc @@ -0,0 +1,96 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::unordered_map; + +#include +using std::unordered_set; using std::unordered_multiset; + +#include +using std::string; + +using std::hash; + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::size_t; + +#include "Sales_data.h" + +// unordered_map version of the word count program +int main() +{ + // count occurrences, but the words won't be in alphabetical order + unordered_map word_count; + string word; + while (cin >> word) + ++word_count[word]; // fetch and increment the counter for word + + for (const auto &w : word_count) // for each element in the map + // print the results + cout << w.first << " occurs " << w.second + << ((w.second > 1) ? " times" : " time") << endl; + + return 0; +} + +// how to override default hash and equality operator on key_type +size_t hasher(const Sales_data &sd) +{ + return hash()(sd.isbn()); +} +bool eqOp(const Sales_data &lhs, const Sales_data &rhs) +{ + return lhs.isbn() == rhs.isbn(); +} + +// type alias using our functions in place of hash and == +using SD_multiset = unordered_multiset; + +// bookstore can hold multiple Sales_data with the same ISBN +// arguments are the bucket size +// and pointers to the hash function and equality operator +SD_multiset bookstore(42, hasher, eqOp); + +// how to override just the hash function; +// Foo must have == +struct Foo { string s; }; + +// we'll see how to define our own operators in chapter 14 +bool operator==(const Foo& l, const Foo&r) { return l.s == r.s; } + +size_t FooHash(const Foo& f) { return hash()(f.s); } + +// use FooHash to generate the hash code; Foo must have an == operator +unordered_set fooSet(10, FooHash); + diff --git a/11/wcEx.cc b/11/wcEx.cc new file mode 100644 index 0000000..f68f137 --- /dev/null +++ b/11/wcEx.cc @@ -0,0 +1,66 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::map; + +#include +using std::string; + +#include +using std::size_t; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + // count number of times each word occurs in the input + map word_count; // empty map from string to size_t + string word; + while (cin >> word) + ++word_count.insert({word, 0}).first->second; + + for(auto it = word_count.cbegin(); it != word_count.cend(); ++it) { + auto w = *it; + cout << w.first << " occurs " << w.second << " times" << endl; + } + + // get iterator positioned on the first element + auto map_it = word_count.cbegin(); + // for each element in the map + while (map_it != word_count.cend()) { + // print the element key, value pairs + cout << map_it->first << " occurs " + << map_it->second << " times" << endl; + ++map_it; // increment iterator to denote the next element + } + + return 0; +} diff --git a/11/word_count.cc b/11/word_count.cc new file mode 100644 index 0000000..94f697c --- /dev/null +++ b/11/word_count.cc @@ -0,0 +1,68 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::map; + +#include +using std::string; + +#include +using std::pair; + +#include +using std::size_t; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + // count the number of times each word occurs in the input + map word_count; // empty map from string to size_t + string word; + while (cin >> word) + ++word_count[word]; + + for (const auto &w : word_count) + cout << w.first << " occurs " << w.second << " times" << endl; + + // get an iterator positioned on the first element + auto map_it = word_count.cbegin(); + // compare the current iterator to the off-the-end iterator + while (map_it != word_count.cend()) { + // dereference the iterator to print the element key--value pairs + cout << map_it->first << " occurs " + << map_it->second << " times" << endl; + ++map_it; // increment the iterator to denote the next element + } + + return 0; +} + diff --git a/11/word_transform.cc b/11/word_transform.cc new file mode 100644 index 0000000..a758191 --- /dev/null +++ b/11/word_transform.cc @@ -0,0 +1,117 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include +#include +#include +#include +#include + +using std::map; using std::string; using std::vector; +using std::ifstream; using std::cout; using std::endl; +using std::getline; +using std::runtime_error; using std::istringstream; + +map buildMap(ifstream &map_file) +{ + map trans_map; // holds the transformations + string key; // a word to transform + string value; // phrase to use instead + // read the first word into key and the rest of the line into value + while (map_file >> key && getline(map_file, value)) + if (value.size() > 1) // check that there is a transformation + trans_map[key] = value.substr(1); // skip leading space + else + throw runtime_error("no rule for " + key); + return trans_map; +} + +const string & +transform(const string &s, const map &m) +{ + // the actual map work; this part is the heart of the program + auto map_it = m.find(s); + // if this word is in the transformation map + if (map_it != m.cend()) + return map_it->second; // use the replacement word + else + return s; // otherwise return the original unchanged +} + +// first argument is the transformations file; +// second is file to transform +void word_transform(ifstream &map_file, ifstream &input) +{ + auto trans_map = buildMap(map_file); // store the transformations + + // for debugging purposes print the map after its built + cout << "Here is our transformation map: \n\n"; + for (auto entry : trans_map) + cout << "key: " << entry.first + << "\tvalue: " << entry.second << endl; + cout << "\n\n"; + + // do the transformation of the given text + string text; // hold each line from the input + while (getline(input, text)) { // read a line of input + istringstream stream(text); // read each word + string word; + bool firstword = true; // controls whether a space is printed + while (stream >> word) { + if (firstword) + firstword = false; + else + cout << " "; // print a space between words + // transform returns its first argument or its transformation + cout << transform(word, trans_map); // print the output + } + cout << endl; // done with this line of input + } +} + +int main(int argc, char **argv) +{ + // open and check both files + if (argc != 3) + throw runtime_error("wrong number of arguments"); + + ifstream map_file(argv[1]); // open transformation file + if (!map_file) // check that open succeeded + throw runtime_error("no transformation file"); + + ifstream input(argv[2]); // open file of text to transform + if (!input) // check that open succeeded + throw runtime_error("no input file"); + + word_transform(map_file, input); + + return 0; // exiting main will automatically close the files +} diff --git a/12/Foo.h b/12/Foo.h new file mode 100644 index 0000000..16c5e7e --- /dev/null +++ b/12/Foo.h @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef FOO_H +#define FOO_H + +#include + +typedef int T; +struct Foo { // members are public by default + Foo(T t): val(t) { } + T val; +}; + +std::ostream& +print(std::ostream &os, const Foo &f) +{ + os << f.val; + return os; +} + +#endif diff --git a/12/QueryResult.h b/12/QueryResult.h new file mode 100644 index 0000000..b8b98f2 --- /dev/null +++ b/12/QueryResult.h @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef QUERYRESULT_H +#define QUERYRESULT_H +#include +#include +#include +#include +#include + +class QueryResult { +friend std::ostream& print(std::ostream&, const QueryResult&); +public: + typedef std::vector::size_type line_no; + typedef std::set::const_iterator line_it; + QueryResult(std::string s, + std::shared_ptr> p, + std::shared_ptr> f): + sought(s), lines(p), file(f) { } + std::set::size_type size() const { return lines->size(); } + line_it begin() const { return lines->cbegin(); } + line_it end() const { return lines->cend(); } + std::shared_ptr> get_file() { return file; } +private: + std::string sought; // word this query represents + std::shared_ptr> lines; // lines it's on + std::shared_ptr> file; //input file +}; + +std::ostream &print(std::ostream&, const QueryResult&); +#endif diff --git a/12/README b/12/README new file mode 100644 index 0000000..581ae8b --- /dev/null +++ b/12/README @@ -0,0 +1,14 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + storyDatafile usealloc + storyDatafile querymain + Alice querymain + alloc allocPtr + alloc allocSP + +Programs not listed above print output and do +not read any input + diff --git a/12/StrBlob.h b/12/StrBlob.h new file mode 100644 index 0000000..414c0fe --- /dev/null +++ b/12/StrBlob.h @@ -0,0 +1,170 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced. Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef STRBLOB_H +#define STRBLOB_H +#include +#include +#include +#include +#include + +// forward declaration needed for friend declaration in StrBlob +class StrBlobPtr; + +class StrBlob { + friend class StrBlobPtr; +public: + typedef std::vector::size_type size_type; + + // constructors + StrBlob() : data(std::make_shared>()) { } + StrBlob(std::initializer_list il); + + // size operations + size_type size() const { return data->size(); } + bool empty() const { return data->empty(); } + + // add and remove elements + void push_back(const std::string &t) { data->push_back(t); } + void pop_back(); + + // element access + std::string& front(); + std::string& back(); + + // interface to StrBlobPtr + StrBlobPtr begin(); // can't be defined until StrBlobPtr is + StrBlobPtr end(); +private: + std::shared_ptr> data; + // throws msg if data[i] isn't valid + void check(size_type i, const std::string &msg) const; +}; + +// constructor +inline +StrBlob::StrBlob(std::initializer_list il): + data(std::make_shared>(il)) { } + +// StrBlobPtr throws an exception on attempts to access a nonexistent element +class StrBlobPtr { + friend bool eq(const StrBlobPtr&, const StrBlobPtr&); +public: + StrBlobPtr(): curr(0) { } + StrBlobPtr(StrBlob &a, size_t sz = 0): wptr(a.data), curr(sz) { } + + std::string& deref() const; + StrBlobPtr& incr(); // prefix version + StrBlobPtr& decr(); // prefix version +private: + // check returns a shared_ptr to the vector if the check succeeds + std::shared_ptr> + check(std::size_t, const std::string&) const; + + // store a weak_ptr, which means the underlying vector might be destroyed + std::weak_ptr> wptr; + std::size_t curr; // current position within the array +}; + +inline +std::string& StrBlobPtr::deref() const +{ + auto p = check(curr, "dereference past end"); + return (*p)[curr]; // (*p) is the vector to which this object points +} + +inline +std::shared_ptr> +StrBlobPtr::check(std::size_t i, const std::string &msg) const +{ + auto ret = wptr.lock(); // is the vector still around? + if (!ret) + throw std::runtime_error("unbound StrBlobPtr"); + + if (i >= ret->size()) + throw std::out_of_range(msg); + return ret; // otherwise, return a shared_ptr to the vector +} + +// prefix: return a reference to the incremented object +inline +StrBlobPtr& StrBlobPtr::incr() +{ + // if curr already points past the end of the container, can't increment it + check(curr, "increment past end of StrBlobPtr"); + ++curr; // advance the current state + return *this; +} + +inline +StrBlobPtr& StrBlobPtr::decr() +{ + // if curr is zero, decrementing it will yield an invalid subscript + --curr; // move the current state back one element} + check(-1, "decrement past begin of StrBlobPtr"); + return *this; +} + +// begin and end members for StrBlob +inline +StrBlobPtr +StrBlob::begin() +{ + return StrBlobPtr(*this); +} + +inline +StrBlobPtr +StrBlob::end() +{ + auto ret = StrBlobPtr(*this, data->size()); + return ret; +} + +// named equality operators for StrBlobPtr +inline +bool eq(const StrBlobPtr &lhs, const StrBlobPtr &rhs) +{ + auto l = lhs.wptr.lock(), r = rhs.wptr.lock(); + // if the underlying vector is the same + if (l == r) + // then they're equal if they're both null or + // if they point to the same element + return (!r || lhs.curr == rhs.curr); + else + return false; // if they point to difference vectors, they're not equal +} + +inline +bool neq(const StrBlobPtr &lhs, const StrBlobPtr &rhs) +{ + return !eq(lhs, rhs); +} +#endif diff --git a/12/TextQuery.cc b/12/TextQuery.cc new file mode 100644 index 0000000..70feeb8 --- /dev/null +++ b/12/TextQuery.cc @@ -0,0 +1,156 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "TextQuery.h" +#include "make_plural.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using std::size_t; +using std::shared_ptr; +using std::istringstream; +using std::string; +using std::getline; +using std::vector; +using std::map; +using std::set; +using std::cerr; +using std::cout; +using std::cin; +using std::ostream; +using std::endl; +using std::ifstream; +using std::ispunct; +using std::tolower; +using std::strlen; +using std::pair; + +// read the input file and build the map of lines to line numbers +TextQuery::TextQuery(ifstream &is): file(new vector) +{ + string text; + while (getline(is, text)) { // for each line in the file + file->push_back(text); // remember this line of text + int n = file->size() - 1; // the current line number + istringstream line(text); // separate the line into words + string word; + while (line >> word) { // for each word in that line + word = cleanup_str(word); + // if word isn't already in wm, subscripting adds a new entry + auto &lines = wm[word]; // lines is a shared_ptr + if (!lines) // that pointer is null the first time we see word + lines.reset(new set); // allocate a new set + lines->insert(n); // insert this line number + } + } +} + +// not covered in the book -- cleanup_str removes +// punctuation and converts all text to lowercase so that +// the queries operate in a case insensitive manner +string TextQuery::cleanup_str(const string &word) +{ + string ret; + for (auto it = word.begin(); it != word.end(); ++it) { + if (!ispunct(*it)) + ret += tolower(*it); + } + return ret; +} + +QueryResult +TextQuery::query(const string &sought) const +{ + // we'll return a pointer to this set if we don't find sought + static shared_ptr> nodata(new set); + + // use find and not a subscript to avoid adding words to wm! + auto loc = wm.find(cleanup_str(sought)); + + if (loc == wm.end()) + return QueryResult(sought, nodata, file); // not found + else + return QueryResult(sought, loc->second, file); +} + +ostream &print(ostream & os, const QueryResult &qr) +{ + // if the word was found, print the count and all occurrences + os << qr.sought << " occurs " << qr.lines->size() << " " + << make_plural(qr.lines->size(), "time", "s") << endl; + + // print each line in which the word appeared + for (auto num : *qr.lines) // for every element in the set + // don't confound the user with text lines starting at 0 + os << "\t(line " << num + 1 << ") " + << *(qr.file->begin() + num) << endl; + + return os; +} + +// debugging routine, not covered in the book +void TextQuery::display_map() +{ + auto iter = wm.cbegin(), iter_end = wm.cend(); + + // for each word in the map + for ( ; iter != iter_end; ++iter) { + cout << "word: " << iter->first << " {"; + + // fetch location vector as a const reference to avoid copying it + auto text_locs = iter->second; + auto loc_iter = text_locs->cbegin(), + loc_iter_end = text_locs->cend(); + + // print all line numbers for this word + while (loc_iter != loc_iter_end) + { + cout << *loc_iter; + + if (++loc_iter != loc_iter_end) + cout << ", "; + + } + + cout << "}\n"; // end list of output this word + } + cout << endl; // finished printing entire map +} + diff --git a/12/TextQuery.h b/12/TextQuery.h new file mode 100644 index 0000000..f1957e2 --- /dev/null +++ b/12/TextQuery.h @@ -0,0 +1,64 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef TEXTQUERY_H +#define TEXTQUERY_H +#include +#include +#include +#include +#include +#include +#include "QueryResult.h" + +/* this version of the query classes includes two + * members not covered in the book: + * cleanup_str: which removes punctuation and + * converst all text to lowercase + * display_map: a debugging routine that will print the contents + * of the lookup mape +*/ + +class QueryResult; // declaration needed for return type in the query function +class TextQuery { +public: + using line_no = std::vector::size_type; + TextQuery(std::ifstream&); + QueryResult query(const std::string&) const; + void display_map(); // debugging aid: print the map +private: + std::shared_ptr> file; // input file + // maps each word to the set of the lines in which that word appears + std::map>> wm; + + // canonicalizes text: removes punctuation and makes everything lower case + static std::string cleanup_str(const std::string&); +}; +#endif diff --git a/12/UP.cc b/12/UP.cc new file mode 100644 index 0000000..ccd879a --- /dev/null +++ b/12/UP.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::unique_ptr; using std::shared_ptr; + +int main() +{ + // up points to an array of ten uninitialized ints + unique_ptr up(new int[10]); + for (size_t i = 0; i != 10; ++i) + up[i] = i; // assign a new value to each of the elements + up.release(); // automatically uses delete[] to destroy its pointer + + // to use a shared_ptr we must supply a deleter + shared_ptr sp(new int[10], [](int *p) { delete[] p; }); + // shared_ptrs don't have subscript operator + // and don't support pointer arithmetic + for (size_t i = 0; i != 10; ++i) + *(sp.get() + i) = i; // use get to get a built-in pointer + sp.reset(); // uses the lambda we supplied + // that uses delete[] to free the array +} + diff --git a/12/allocPtr.cc b/12/allocPtr.cc new file mode 100644 index 0000000..62f2b1d --- /dev/null +++ b/12/allocPtr.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::istream; using std::ostream; +using std::cin; using std::cout; using std::endl; + +#include "Foo.h" + +// factory returns a pointer to a dynamically allocated object +Foo* factory(T arg) +{ + // process arg as appropriate + return new Foo(arg); // caller is responsible for deleting this memory +} + +Foo* use_factory(T arg) +{ + Foo *p = factory(arg); + print(cout, *p); + cout << endl; + // use p + return p; // caller must delete the memory +} + +int main() +{ + T arg; + while (cin >> arg) { + auto p = use_factory(arg); + delete p; // remember to delete the memory from use_factory + } + + return 0; +} diff --git a/12/allocSP.cc b/12/allocSP.cc new file mode 100644 index 0000000..3b267c7 --- /dev/null +++ b/12/allocSP.cc @@ -0,0 +1,68 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::make_shared; using std::shared_ptr; + +#include +using std::istream; using std::ostream; +using std::cin; using std::cout; using std::endl; + +#include "Foo.h" + +// factory returns a shared_ptr to a dynamically allocated object +shared_ptr factory(T arg) +{ + // process arg as appropriate + // shared_ptr will take care of deleting this memory + return make_shared(arg); +} + +shared_ptr use_factory(T arg) +{ + shared_ptr p = factory(arg); + print(cout, *p); + cout << endl; + // use p + return p; // reference count is incremented when we return p +} + +int main() +{ + T arg; + while (cin >> arg) + use_factory(arg); +} + diff --git a/12/data/Alice b/12/data/Alice new file mode 100644 index 0000000..08b6485 --- /dev/null +++ b/12/data/Alice @@ -0,0 +1 @@ +Alice diff --git a/12/data/alloc b/12/data/alloc new file mode 100644 index 0000000..95a4bfe --- /dev/null +++ b/12/data/alloc @@ -0,0 +1 @@ +1 2 3 4 5 6 7 8 9 diff --git a/12/data/storyDataFile b/12/data/storyDataFile new file mode 100644 index 0000000..1f06b36 --- /dev/null +++ b/12/data/storyDataFile @@ -0,0 +1,10 @@ +Alice Emma has long flowing red hair. +Her Daddy says when the wind blows +through her hair, it looks almost alive, +like a fiery bird in flight. +A beautiful fiery bird, he tells her, +magical but untamed. +"Daddy, shush, there is no such thing," +she tells him, at the same time wanting +him to tell her more. +Shyly, she asks, "I mean, Daddy, is there?" diff --git a/12/makefile b/12/makefile new file mode 100644 index 0000000..71db9c1 --- /dev/null +++ b/12/makefile @@ -0,0 +1,17 @@ +# executable files for this directory +OBJECTS = allocPtr.exe allocSP.exe \ + oknew.exe querymain.exe release.exe \ + unique.exe UP.exe usealloc.exe usealloc2.exe \ + useBlob.exe weak.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +# 7 has Sales_data files, 6 has make_plural +LOCFLAGS = -I../7 -I../6 + +querymain.exe: querymain.o TextQuery.o + $(CC) $(CCFLAGS) $(LOCFLAGS) querymain.o TextQuery.o \ + -o querymain.exe + diff --git a/12/oknew.cc b/12/oknew.cc new file mode 100644 index 0000000..8ef4f8d --- /dev/null +++ b/12/oknew.cc @@ -0,0 +1,97 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::size_t; + +#include +using std::nothrow; + +size_t get_size() { return 42; } + +int main() +{ + // if allocation fails, new returns a null pointer + int *p1 = new int; // if allocation fails, new throws std::bad_alloc + int *p2 = new (nothrow) int; // if allocation fails, new returns a null pointer + + int i0; // named, uninitialized int variable + + int *p0 = new int; // pi points to a dynamically allocated, + // unnamed, uninitialized int + delete p0; // frees the memory to which pi points + + // named initialized variables + int i(1024); // value of i is 1024 + string s(10, '9'); // value of s is "9999999999" + vector v = {0,1,2,3,4,5,6,7,8,9}; + + // unnamed, initialized dynamically allocated objects + int *pi = new int(1024); // object to which pi points has value 1024 + string *ps = new string(10, '9'); // *ps is "9999999999" + // vector with ten elements with values from 0 to 9 + vector *pv = new vector{0,1,2,3,4,5,6,7,8,9}; + + cout << "*pi: " << *pi + << "\ti: " << i << endl + << "*ps: " << *ps + << "\ts: " << s << endl; + + for (auto b = pv->begin(); b != pv->end(); ++b) + cout << *b << " "; + cout << endl; + + // when we're done using the memory must delete the pointers + delete pi; // frees the memory to which pi points + delete ps; // frees the string to which ps points + // the string destructor frees the space used by its data + delete pv; // frees the memory for the vector + // which also destroys the elements in that vector + + // call get_size to determine how many ints to allocate + int *pia = new int[get_size()]; // pia points to the first of these ints + delete [] pia; // brackets used to delete pointer to element in an array + typedef int arrT[42]; // arrT names the type array of 42 ints + int *p = new arrT; // allocates an array of 42 ints; p points to the first one + delete [] p; // brackets are necessary because we allocated an array + + return 0; +} diff --git a/12/querymain.cc b/12/querymain.cc new file mode 100644 index 0000000..fd4a6d7 --- /dev/null +++ b/12/querymain.cc @@ -0,0 +1,73 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::ifstream; + +#include +using std::cin; using std::cout; using std::cerr; +using std::endl; + +#include // for EXIT_FAILURE + +#include "TextQuery.h" +#include "make_plural.h" + +void runQueries(ifstream &infile) +{ + // infile is an ifstream that is the file we want to query + TextQuery tq(infile); // store the file and build the query map + // iterate with the user: prompt for a word to find and print results + while (true) { + cout << "enter word to look for, or q to quit: "; + string s; + // stop if we hit end-of-file on the input or if a 'q' is entered + if (!(cin >> s) || s == "q") break; + // run the query and print the results + print(cout, tq.query(s)) << endl; + } +} + +// program takes single argument specifying the file to query +int main(int argc, char **argv) +{ + // open the file from which user will query words + ifstream infile; + // open returns void, so we use the comma operator XREF(commaOp) + // to check the state of infile after the open + if (argc < 2 || !(infile.open(argv[1]), infile)) { + cerr << "No input file!" << endl; + return EXIT_FAILURE; + } + runQueries(infile); + return 0; +} diff --git a/12/release.cc b/12/release.cc new file mode 100644 index 0000000..636f03e --- /dev/null +++ b/12/release.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::unique_ptr; + +#include +using std::cout; using std::endl; + +int main() +{ + unique_ptr p1(new string("Stegosaurus")); + + // transfers ownership from p1 + // (which points to the string Stegosaurus) to p2 + unique_ptr p2(p1.release()); // release makes p1 null + cout << *p2 << endl; // prints Stegosaurus + + unique_ptr p3(new string("Trex")); + + // reset deletes the memory to which p2 had pointed + // and transfers ownership from p3 to p2 + p2.reset(p3.release()); + cout << *p2 << endl; // prints Trex + + // p1 is null, p2 is steg, and p3 is trex + if (p1) + cout << "p1 not null?" << endl; + if (p3) + cout << "p2 not null?" << endl; + cout << *p2 << endl; +} diff --git a/12/runpgms b/12/runpgms new file mode 100644 index 0000000..2612892 --- /dev/null +++ b/12/runpgms @@ -0,0 +1,7 @@ +echo "querymain:" && ./querymain.exe data/storyDataFile < data/Alice +echo "allocPtr:" && ./allocPtr.exe < data/alloc +echo "allocSP:" && ./allocSP.exe < data/alloc +echo "usealloc:" && ./usealloc.exe < data/storyDataFile + +../noinput oknew.exe release.exe \ + unique.exe UP.exe usealloc2.exe useBlob.exe weak.exe diff --git a/12/unique.cc b/12/unique.cc new file mode 100644 index 0000000..fb7d0f4 --- /dev/null +++ b/12/unique.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::ostream; using std::cin; using std::cout; using std::endl; + +#include +using std::shared_ptr; + +int main() { + shared_ptr p(new string("Hello!")); + shared_ptr p2(p); // two users of the allocated string + string newVal; + if (!p.unique()) + p.reset(new string(*p)); // we aren't alone; allocate a new copy + *p += newVal; // now that we know we're the only pointer, okay to change this object + cout << *p << " " << *p2 << endl; +} diff --git a/12/useBlob.cc b/12/useBlob.cc new file mode 100644 index 0000000..246800d --- /dev/null +++ b/12/useBlob.cc @@ -0,0 +1,49 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced. Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ +#include +using std::cout; using std::endl; + +#include "StrBlob.h" + +int main() +{ + StrBlob b1; + { + StrBlob b2 = { "a", "an", "the" }; + b1 = b2; + b2.push_back("about"); + cout << b2.size() << endl; + } + cout << b1.size() << endl; + + for (auto it = b1.begin(); neq(it, b1.end()); it.incr()) + cout << it.deref() << endl; + + return 0; +} diff --git a/12/usealloc.cc b/12/usealloc.cc new file mode 100644 index 0000000..7bf9d3f --- /dev/null +++ b/12/usealloc.cc @@ -0,0 +1,96 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::allocator; + +#include +using std::size_t; + +#include +using std::cout; using std::endl; + +#include +using std::ifstream; + +int main() +{ + const size_t n = 100; + allocator alloc; // object that can allocate strings + auto p = alloc.allocate(n); // allocate n unconstructed strings + + auto q = p; // q will point to one past the last constructed element + alloc.construct(q++); // *q is the empty string + cout << *(q-1) << endl; + + alloc.construct(q++, 10, 'c'); // *q is cccccccccc + cout << *(q - 1) << endl; + + alloc.construct(q++, "hi"); // *q is hi! + cout << *(q - 1) << endl; + + cout << *p << endl; // ok: uses the string output operator + while (q != p) + alloc.destroy(--q); // free the strings we actually allocated + + alloc.deallocate(p, n); // return the memory we allocated + + p = alloc.allocate(n); // allocate n unconstructed strings + string s; + q = p; // q points to the memory for first string + ifstream in("data/storyDataFile"); + while (in >> s && q != p + n) + alloc.construct(q++, s); // construct only as many strings as we need + size_t size = q - p; // remember how many strings we read + + // use the array + + cout << "read " << size << " strings" << endl; + + for (q = p + size - 1; q != p; --q) + alloc.destroy(q); // free the strings we allocated + alloc.deallocate(p, n); // return the memory we allocated + + in.close(); + in.open("data/storyDataFile"); + p = new string[n]; // construct n empty strings + q = p; // q points to the first string + while (in >> s && q != p + n) + *q++ = s; // assign a new value to *q + size = q - p; // remember how many strings we read + + cout << "read " << size << " strings" << endl; + + // use the array + + delete[] p; // p points to an array; must remember to use delete[] +} diff --git a/12/usealloc2.cc b/12/usealloc2.cc new file mode 100644 index 0000000..592b3d5 --- /dev/null +++ b/12/usealloc2.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::string; + +#include +using std::vector; + +#include +using std::uninitialized_copy; +using std::allocator; using std::uninitialized_fill_n; + +#include +using std::cout; using std::endl; + +int main() +{ + vector vi{1,2,3,4,5,6,7,8,9}; + + allocator alloc; + // allocate twice as many elements as vi holds + auto p = alloc.allocate(vi.size() * 2); + + // construct elements starting at p as copies of elements in vi + auto q = uninitialized_copy(vi.begin(), vi.end(), p); + + // initialize the remaining elements to 42 + uninitialized_fill_n(q, vi.size(), 42); + + for (size_t i = 0; i != vi.size(); ++i) + cout << *(p + i) << " "; + cout << endl; + + for (size_t i = 0; i != vi.size(); ++i) + cout << *(q + i) << " "; + cout << endl; + + alloc.deallocate(p, vi.size()); +} diff --git a/12/weak.cc b/12/weak.cc new file mode 100644 index 0000000..c9aed14 --- /dev/null +++ b/12/weak.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::make_shared; using std::weak_ptr; using std::shared_ptr; + +int main() +{ + auto p = make_shared(42); + + weak_ptr wp(p); // wp weakly shares with p; use count in p is unchanged + + p.reset(); // assuming p.unique() was true, the int is deleted + + if (shared_ptr np = wp.lock()) { // true if np is not null + // inside the if, np shares its object with p + } +} diff --git a/13/Folder.cc b/13/Folder.cc new file mode 100644 index 0000000..e383e09 --- /dev/null +++ b/13/Folder.cc @@ -0,0 +1,221 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +// for move, we don't supply a using declaration for move + +#include +using std::cerr; using std::endl; + +#include +using std::set; + +#include +using std::string; + +#include "Folder.h" + +void swap(Message &lhs, Message &rhs) +{ + using std::swap; // not strictly needed in this case, but good habit + + // remove pointers to each Message from their (original) respective Folders + for (auto f: lhs.folders) + f->remMsg(&lhs); + for (auto f: rhs.folders) + f->remMsg(&rhs); + + // swap the contents and Folder pointer sets + swap(lhs.folders, rhs.folders); // uses swap(set&, set&) + swap(lhs.contents, rhs.contents); // swap(string&, string&) + + // add pointers to each Message to their (new) respective Folders + for (auto f: lhs.folders) + f->addMsg(&lhs); + for (auto f: rhs.folders) + f->addMsg(&rhs); +} + +Folder::Folder(Folder &&f) +{ + move_Messages(&f); // make each Message point to this Folder +} + +Folder& Folder::operator=(Folder &&f) +{ + if (this != &f) { + remove_from_Msgs(); // remove this Folder from the current msgs + move_Messages(&f); // make each Message point to this Folder + } + return *this; +} + +void Folder::move_Messages(Folder *f) +{ + msgs = std::move(f->msgs); // move the set from f to this Folder + f->msgs.clear(); // ensure that destroying f is harmless + for (auto m : msgs) { // for each Message in this Folder + m->remFldr(f); // remove the pointer to the old Folder + m->addFldr(this); // insert pointer to this Folder + } +} + +Message::Message(Message &&m): contents(std::move(m.contents)) +{ + move_Folders(&m); // moves folders and updates the Folder pointers +} + +Message::Message(const Message &m): + contents(m.contents), folders(m.folders) +{ + add_to_Folders(m); // add this Message to the Folders that point to m +} + +Message& Message::operator=(Message &&rhs) +{ + if (this != &rhs) { // direct check for self-assignment + remove_from_Folders(); + contents = std::move(rhs.contents); // move assignment + move_Folders(&rhs); // reset the Folders to point to this Message + } + return *this; +} + +Message& Message::operator=(const Message &rhs) +{ + // handle self-assignment by removing pointers before inserting them + remove_from_Folders(); // update existing Folders + contents = rhs.contents; // copy message contents from rhs + folders = rhs.folders; // copy Folder pointers from rhs + add_to_Folders(rhs); // add this Message to those Folders + return *this; +} + +Message::~Message() +{ + remove_from_Folders(); +} + +// move the Folder pointers from m to this Message +void Message::move_Folders(Message *m) +{ + folders = std::move(m->folders); // uses set move assignment + for (auto f : folders) { // for each Folder + f->remMsg(m); // remove the old Message from the Folder + f->addMsg(this); // add this Message to that Folder + } + m->folders.clear(); // ensure that destroying m is harmless +} + +// add this Message to Folders that point to m +void Message::add_to_Folders(const Message &m) +{ + for (auto f : m.folders) // for each Folder that holds m + f->addMsg(this); // add a pointer to this Message to that Folder +} + +// remove this Message from the corresponding Folders +void Message::remove_from_Folders() +{ + for (auto f : folders) // for each pointer in folders + f->remMsg(this); // remove this Message from that Folder + folders.clear(); // no Folder points to this Message + +} + +void Folder::add_to_Messages(const Folder &f) +{ + for (auto msg : f.msgs) + msg->addFldr(this); // add this Folder to each Message +} + +Folder::Folder(const Folder &f) : msgs(f.msgs) +{ + add_to_Messages(f); // add this Folder to each Message in f.msgs +} + +Folder& Folder::operator=(const Folder &f) +{ + remove_from_Msgs(); // remove this folder from each Message in msgs + msgs = f.msgs; // copy the set of Messages from f + add_to_Messages(f); // add this folder to each Message in msgs + return *this; +} + +Folder::~Folder() +{ + remove_from_Msgs(); +} + + +void Folder::remove_from_Msgs() +{ + while (!msgs.empty()) + (*msgs.begin())->remove(*this); +} +void Message::save(Folder &f) +{ + folders.insert(&f); // add the given Folder to our list of Folders + f.addMsg(this); // add this Message to f's set of Messages +} + +void Message::remove(Folder &f) +{ + folders.erase(&f); // take the given Folder out of our list of Folders + f.remMsg(this); // remove this Message to f's set of Messages +} + +void Folder::save(Message &m) +{ + // add m and add this folder to m's set of Folders + msgs.insert(&m); + m.addFldr(this); +} + +void Folder::remove(Message &m) +{ + // erase m from msgs and remove this folder from m + msgs.erase(&m); + m.remFldr(this); +} + +void Folder::debug_print() +{ + cerr << "Folder contains " << msgs.size() << " messages" << endl; + int ctr = 1; + for (auto m : msgs) { + cerr << "Message " << ctr++ << ":\n\t" << m->contents << endl; + } +} + +void Message::debug_print() +{ + cerr << "Message:\n\t" << contents << endl; + cerr << "Appears in " << folders.size() << " Folders" << endl; +} diff --git a/13/Folder.h b/13/Folder.h new file mode 100644 index 0000000..f27a37e --- /dev/null +++ b/13/Folder.h @@ -0,0 +1,104 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef FOLDER_H +#define FOLDER_H + +#include +#include + +class Folder; + +class Message { + friend void swap(Message&, Message&); + friend class Folder; +public: + // folders is implicitly initialized to the empty set + explicit Message(const std::string &str = ""): + contents(str) { } + + // copy control to manage pointers to this Message + Message(const Message&); // copy constructor + Message& operator=(const Message&); // copy assignment + ~Message(); // destructor + Message(Message&&); // move constructor + Message& operator=(Message&&); // move assignment + + // add/remove this Message from the specified Folder's set of messages + void save(Folder&); + void remove(Folder&); + void debug_print(); // print contents and it's list of Folders, + // printing each Folder as well +private: + std::string contents; // actual message text + std::set folders; // Folders that have this Message + + // utility functions used by copy constructor, assignment, and destructor + // add this Message to the Folders that point to the parameter + void add_to_Folders(const Message&); + void move_Folders(Message*); + // remove this Message from every Folder in folders + void remove_from_Folders(); + + // used by Folder class to add self to this Message's set of Folder's + void addFldr(Folder *f) { folders.insert(f); } + void remFldr(Folder *f) { folders.erase(f); } +}; +// declaration for swap should be in the same header as Message itself +void swap(Message&, Message&); + +class Folder { + friend void swap(Message&, Message&); + friend class Message; +public: + ~Folder(); // remove self from Messages in msgs + Folder(const Folder&); // add new folder to each Message in msgs + Folder& operator=(const Folder&); // delete Folder from lhs messages + // add Folder to rhs messages + Folder(Folder&&); // move Messages to this Folder + Folder& operator=(Folder&&); // delete Folder from lhs messages + // add Folder to rhs messages + + Folder() = default; // defaults ok + + void save(Message&); // add this message to folder + void remove(Message&); // remove this message from this folder + + void debug_print(); // print contents and it's list of Folders, +private: + std::set msgs; // messages in this folder + + void add_to_Messages(const Folder&);// add this Folder to each Message + void remove_from_Msgs(); // remove this Folder from each Message + void addMsg(Message *m) { msgs.insert(m); } + void remMsg(Message *m) { msgs.erase(m); } + void move_Messages(Folder*); // move Message pointers to point to this Folder +}; + +#endif diff --git a/13/FolderMain.cc b/13/FolderMain.cc new file mode 100644 index 0000000..a621acd --- /dev/null +++ b/13/FolderMain.cc @@ -0,0 +1,133 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include "Folder.h" + +int main() +{ + string s1("contents1"); + string s2("contents2"); + string s3("contents3"); + string s4("contents4"); + string s5("contents5"); + string s6("contents6"); + + // all new messages, no copies yet + Message m1(s1); + Message m2(s2); + Message m3(s3); + Message m4(s4); + Message m5(s5); + Message m6(s6); + + Folder f1; + Folder f2; + + m1.save(f1); m3.save(f1); m5.save(f1); + m1.save(f2); + m2.save(f2); m4.save(f2); m6.save(f2); + + m1.debug_print(); + f2.debug_print(); + + // create some copies + Message c1(m1); + Message c2(m2), c4(m4), c6(m6); + + m1.debug_print(); + f2.debug_print(); + + // now some assignments + m2 = m3; + m4 = m5; + m6 = m3; + m1 = m5; + + m1.debug_print(); + f2.debug_print(); + + // finally, self-assignment + m2 = m2; + m1 = m1; + + m1.debug_print(); + f2.debug_print(); + + vector vm; + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m1); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m2); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m3); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m4); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m5); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m6); + + vector vf; + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(f1); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(f2); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(Folder(f1)); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(Folder(f2)); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(Folder()); + + Folder f3; + f3.save(m6); + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(f3); + + + return 0; +} diff --git a/13/HasPtrUseCount.cc b/13/HasPtrUseCount.cc new file mode 100644 index 0000000..494c186 --- /dev/null +++ b/13/HasPtrUseCount.cc @@ -0,0 +1,115 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +// reference counted version of HasPtr +#include + +#include + +class HasPtr { +public: + // constructor allocates a new string and a new counter, + // which it sets to 1 + HasPtr(const std::string &s = std::string()): + ps(new std::string(s)), i(0), use(new std::size_t(1)) {} + + // copy constructor copies all three data members + // and increments the counter + HasPtr(const HasPtr &p): + ps(p.ps), i(p.i), use(p.use) { ++*use; } + + HasPtr& operator=(const HasPtr&); + + ~HasPtr(); + + // move constructor takes over the pointers from its argument + // and makes the argument safe to delete + HasPtr(HasPtr &&p): ps(p.ps), i(p.i), use(p.use) + { p.ps = 0; p.use = 0; } + + HasPtr &operator=(HasPtr&&); + +private: + std::string *ps; + int i; + std::size_t *use; // member to track how many objects share *ps +}; + +HasPtr::~HasPtr() +{ + if (--*use == 0) { // if the reference count goes to 0 + delete ps; // delete the string + delete use; // and the counter + } +} + +HasPtr & +HasPtr::operator=(HasPtr &&rhs) +{ + if (this != &rhs) { + if (--*use == 0) { // do the work of the destructor + delete ps; + delete use; + } + ps = rhs.ps; // do the work of the move constructor + i = rhs.i; + use = rhs.use; + ps = 0; use = 0; + } + return *this; +} + +HasPtr& HasPtr::operator=(const HasPtr &rhs) +{ + ++*rhs.use; // increment the use count of the right-hand operand + if (--*use == 0) { // then decrement this object's counter + delete ps; // if no other users + delete use; // free this object's allocated members + } + ps = rhs.ps; // copy data from rhs into this object + i = rhs.i; + use = rhs.use; + return *this; // return this object +} + +HasPtr f(HasPtr hp) // HasPtr passed by value, so it is copied +{ + HasPtr ret; + ret = hp; // assignment copies the given HasPtr + // proces ret + return ret; // ret and hp are destroyed +} + +int main() +{ + HasPtr h("hi mom!"); + HasPtr h2 = h; // no new memory is allocated, + // h and h2 share the same underlying string +} + diff --git a/13/HasPtrValueLike.cc b/13/HasPtrValueLike.cc new file mode 100644 index 0000000..824a9a8 --- /dev/null +++ b/13/HasPtrValueLike.cc @@ -0,0 +1,81 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +// value-like implementation of HasPtr +class HasPtr { + friend void swap(HasPtr&, HasPtr&); +public: + HasPtr(const std::string &s = std::string()): + ps(new std::string(s)), i(0) { } + + // each HasPtr has its own copy of the string to which ps points + HasPtr(const HasPtr &p): + ps(new std::string(*p.ps)), i(p.i) { } + + HasPtr& operator=(const HasPtr &); + + ~HasPtr() { delete ps; } +private: + std::string *ps; + int i; +}; + +inline +void swap(HasPtr &lhs, HasPtr &rhs) +{ + using std::swap; + swap(lhs.ps, rhs.ps); // swap the pointers, not the string data + swap(lhs.i, rhs.i); // swap the int members +} + +using std::string; +HasPtr& HasPtr::operator=(const HasPtr &rhs) +{ + auto newp = new string(*rhs.ps); // copy the underlying string + delete ps; // free the old memory + ps = newp; // copy data from rhs into this object + i = rhs.i; + return *this; // return this object +} + +HasPtr f(HasPtr hp) // HasPtr passed by value, so it is copied +{ + HasPtr ret = hp; // copies the given HasPtr + // process ret + return ret; // ret and hp are destroyed +} + +int main() +{ + HasPtr h("hi mom!"); // allocates a new copy of "hi mom!" + f(h); // copy constructor copies h in the call to f + // that copy is destroyed when f exits +} // h is destroyed on exit, which destroys its allocated memory diff --git a/13/README b/13/README new file mode 100644 index 0000000..d759066 --- /dev/null +++ b/13/README @@ -0,0 +1,9 @@ +No programs in this chapter read input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + +Programs not listed above print output and do +not read any input + diff --git a/13/StrFolder.cc b/13/StrFolder.cc new file mode 100644 index 0000000..27c58fb --- /dev/null +++ b/13/StrFolder.cc @@ -0,0 +1,220 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +// for std::move, but we don't supply a using declaration for move + +#include +using std::cerr; using std::endl; + +#include +using std::set; + +#include "StrFolder.h" + +void swap(Message &lhs, Message &rhs) +{ + using std::swap; // not strictly needed in this case, but good habit + + // remove pointers to each Message from their (original) respective Folders + for (auto f: lhs.folders) + f->remMsg(&lhs); + for (auto f: rhs.folders) + f->remMsg(&rhs); + + // swap the contents and Folder pointer sets + swap(lhs.folders, rhs.folders); // uses swap(set&, set&) + swap(lhs.contents, rhs.contents); // swap(String&, String&) + + // add pointers to each Message to their (new) respective Folders + for (auto f: lhs.folders) + f->addMsg(&lhs); + for (auto f: rhs.folders) + f->addMsg(&rhs); +} + +Folder::Folder(Folder &&f) +{ + move_Messages(&f); // make each Message point to this Folder +} + +Folder& Folder::operator=(Folder &&f) +{ + if (this != &f) { // direct check for self-assignment + remove_from_Msgs(); // remove this Folder from the current msgs + move_Messages(&f); // make each Message point to this Folder + } + return *this; +} + +void Folder::move_Messages(Folder *f) +{ + msgs = std::move(f->msgs); // move the set from f to this Folder + f->msgs.clear(); // ensure that destroying f is harmless + for (auto m : msgs) { // for each Message in this Folder + m->remFldr(f); // remove the pointer to the old Folder + m->addFldr(this); // insert pointer to this Folder + } +} + +Message::Message(Message &&m): contents(std::move(m.contents)) +{ + move_Folders(&m); // moves folders and updates the Folder pointers +} + +Message::Message(const Message &m): + contents(m.contents), folders(m.folders) +{ + add_to_Folders(m); // add this Message to the Folders that point to m +} + + +Message& Message::operator=(Message &&rhs) +{ + if (this != &rhs) { // direct check for self-assignment + remove_from_Folders(); + contents = std::move(rhs.contents); // move assignment + move_Folders(&rhs); // reset the Folders to point to this Message + } + return *this; +} + +Message& Message::operator=(const Message &rhs) +{ + // handle self-assignment by removing pointers before inserting them + remove_from_Folders(); // update existing Folders + contents = rhs.contents; // copy message contents from rhs + folders = rhs.folders; // copy Folder pointers from rhs + add_to_Folders(rhs); // add this Message to those Folders + return *this; +} + +Message::~Message() +{ + remove_from_Folders(); +} + +// move the Folder pointers from m to this Message +void Message::move_Folders(Message *m) +{ + folders = std::move(m->folders); // uses set move assignment + for (auto f : folders) { // for each Folder + f->remMsg(m); // remove the old Message from the Folder + f->addMsg(this); // add this Message to that Folder + } + m->folders.clear(); // ensure that destroying m is harmless +} + +// add this Message to Folders that point to m +void Message::add_to_Folders(const Message &m) +{ + for (auto f : m.folders) // for each Folder that holds m + f->addMsg(this); // add a pointer to this Message to that Folder +} + +// remove this Message from the corresponding Folders +void Message::remove_from_Folders() +{ + for (auto f : folders) // for each pointer in folders + f->remMsg(this); // remove this Message from that Folder + folders.clear(); // no Folder points to this Message + +} + +void Folder::add_to_Messages(const Folder &f) +{ + for (auto msg : f.msgs) + msg->addFldr(this); // add this Folder to each Message +} + +Folder::Folder(const Folder &f) : msgs(f.msgs) +{ + add_to_Messages(f); // add this Folder to each Message in f.msgs +} + +Folder& Folder::operator=(const Folder &f) +{ + remove_from_Msgs(); // remove this folder from each Message in msgs + msgs = f.msgs; // copy the set of Messages from f + add_to_Messages(f); // add this folder to each Message in msgs + return *this; +} + +Folder::~Folder() +{ + remove_from_Msgs(); +} + + +void Folder::remove_from_Msgs() +{ + while (!msgs.empty()) + (*msgs.begin())->remove(*this); +} + +void Message::save(Folder &f) +{ + folders.insert(&f); // add the given Folder to our list of Folders + f.addMsg(this); // add this Message to f's set of Messages +} + +void Message::remove(Folder &f) +{ + folders.erase(&f); // take the given Folder out of our list of Folders + f.remMsg(this); // remove this Message to f's set of Messages +} + +void Folder::save(Message &m) +{ + // add m and add this folder to m's set of Folders + msgs.insert(&m); + m.addFldr(this); +} + +void Folder::remove(Message &m) +{ + // erase m from msgs and remove this folder from m + msgs.erase(&m); + m.remFldr(this); +} + +void Folder::debug_print() +{ + cerr << "Folder contains " << msgs.size() << " messages" << endl; + int ctr = 1; + for (auto m : msgs) { + cerr << "Message " << ctr++ << ":\n\t" << m->contents << endl; + } +} + +void Message::debug_print() +{ + cerr << "Message:\n\t" << contents << endl; + cerr << "Appears in " << folders.size() << " Folders" << endl; +} diff --git a/13/StrFolder.h b/13/StrFolder.h new file mode 100644 index 0000000..0052c98 --- /dev/null +++ b/13/StrFolder.h @@ -0,0 +1,104 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef STRFOLDER_H +#define STRFOLDER_H + +#include "String.h" +#include + +class Folder; + +class Message { + friend void swap(Message&, Message&); + friend class Folder; +public: + // folders is implicitly initialized to the empty set + explicit Message(const String &str = ""): + contents(str) { } + + // copy control to manage pointers to this Message + Message(const Message&); // copy constructor + Message& operator=(const Message&); // copy assignment + ~Message(); // destructor + Message(Message&&); // move constructor + Message& operator=(Message&&); // move assignment + + // add/remove this Message from the specified Folder's set of messages + void save(Folder&); + void remove(Folder&); + void debug_print(); // print contents and it's list of Folders, + // printing each Folder as well +private: + String contents; // actual message text + std::set folders; // Folders that have this Message + + // utility functions used by copy constructor, assignment, and destructor + // add this Message to the Folders that point to the parameter + void add_to_Folders(const Message&); + void move_Folders(Message*); + // remove this Message from every Folder in folders + void remove_from_Folders(); + + // used by Folder class to add self to this Message's set of Folder's + void addFldr(Folder *f) { folders.insert(f); } + void remFldr(Folder *f) { folders.erase(f); } +}; +// declaration for swap should be in the same header as Message itself +void swap(Message&, Message&); + +class Folder { + friend void swap(Message&, Message&); + friend class Message; +public: + ~Folder(); // remove self from Messages in msgs + Folder(const Folder&); // add new folder to each Message in msgs + Folder& operator=(const Folder&); // delete Folder from lhs messages + // add Folder to rhs messages + Folder(Folder&&); // move Messages to this Folder + Folder& operator=(Folder&&); // delete Folder from lhs messages + // add Folder to rhs messages + + Folder() = default; // defaults ok + + void save(Message&); // add this message to folder + void remove(Message&); // remove this message from this folder + + void debug_print(); // print contents and it's list of Folders, +private: + std::set msgs; // messages in this folder + + void add_to_Messages(const Folder&);// add this Folder to each Message + void remove_from_Msgs(); // remove this Folder from each Message + void addMsg(Message *m) { msgs.insert(m); } + void remMsg(Message *m) { msgs.erase(m); } + void move_Messages(Folder*); // move Message pointers to point to this Folder +}; + +#endif diff --git a/13/StrFolderMain.cc b/13/StrFolderMain.cc new file mode 100644 index 0000000..d85b86a --- /dev/null +++ b/13/StrFolderMain.cc @@ -0,0 +1,132 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::vector; + +#include "StrFolder.h" + +#include "String.h" + +int main() +{ + String s1("contents1"); + String s2("contents2"); + String s3("contents3"); + String s4("contents4"); + String s5("contents5"); + String s6("contents6"); + + // all new messages, no copies yet + Message m1(s1); + Message m2(s2); + Message m3(s3); + Message m4(s4); + Message m5(s5); + Message m6(s6); + + Folder f1; + Folder f2; + + m1.save(f1); m3.save(f1); m5.save(f1); + m1.save(f2); + m2.save(f2); m4.save(f2); m6.save(f2); + + m1.debug_print(); + f2.debug_print(); + + // create some copies + Message c1(m1); + Message c2(m2), c4(m4), c6(m6); + + m1.debug_print(); + f2.debug_print(); + + // now some assignments + m2 = m3; + m4 = m5; + m6 = m3; + m1 = m5; + + m1.debug_print(); + f2.debug_print(); + + // finally, self-assignment + m2 = m2; + m1 = m1; + + m1.debug_print(); + f2.debug_print(); + + vector vm; + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m1); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m2); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m3); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m4); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m5); + + cout << "capacity: " << vm.capacity() << endl; + vm.push_back(m6); + + vector vf; + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(f1); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(f2); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(Folder(f1)); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(Folder(f2)); + + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(Folder()); + + Folder f3; + f3.save(m6); + cout << "capacity: " << vf.capacity() << endl; + vf.push_back(f3); + + + return 0; +} diff --git a/13/StrVec.cc b/13/StrVec.cc new file mode 100644 index 0000000..13ee9da --- /dev/null +++ b/13/StrVec.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "StrVec.h" + +#include +using std::string; + +#include +using std::allocator; + +// errata fixed in second printing -- +// StrVec's allocator should be a static member not an ordinary member + +// definition for static data member +allocator StrVec::alloc; + +// all other StrVec members are inline and defined inside StrVec.h diff --git a/13/StrVec.h b/13/StrVec.h new file mode 100644 index 0000000..fa7f201 --- /dev/null +++ b/13/StrVec.h @@ -0,0 +1,236 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef STRVEC_H +#define STRVEC_H + +#include +#include +#include +#include + +// simplified implementation of the memory allocation strategy for a vector-like class +class StrVec { +public: + // copy control members + StrVec(): + elements(nullptr), first_free(nullptr), cap(nullptr) { } + + StrVec(const StrVec&); // copy constructor + StrVec &operator=(const StrVec&); // copy assignment + + StrVec(StrVec&&) noexcept; // move constructor + StrVec &operator=(StrVec&&) noexcept; // move assignment + + ~StrVec() noexcept; // destructor + + // additional constructor + StrVec(std::initializer_list); + + void push_back(const std::string&); // copy the element + void push_back(std::string&&); // move the element + + // add elements + size_t size() const { return first_free - elements; } + size_t capacity() const { return cap - elements; } + + // iterator interface + std::string *begin() const { return elements; } + std::string *end() const { return first_free; } + + // operator functions covered in chapter 14 + StrVec &operator=(std::initializer_list); + + std::string& operator[](std::size_t n) + { return elements[n]; } + + const std::string& operator[](std::size_t n) const + { return elements[n]; } + + // emplace member covered in chapter 16 + template void emplace_back(Args&&...); +private: + static std::allocator alloc; // allocates the elements + + // utility functions: + // used by members that add elements to the StrVec + void chk_n_alloc() + { if (size() == capacity()) reallocate(); } + // used by the copy constructor, assignment operator, and destructor + std::pair alloc_n_copy + (const std::string*, const std::string*); + void free(); // destroy the elements and free the space + void reallocate(); // get more space and copy the existing elements + std::string *elements; // pointer to the first element in the array + std::string *first_free; // pointer to the first free element in the array + std::string *cap; // pointer to one past the end of the array +}; + +#include + +inline +StrVec::~StrVec() noexcept { free(); } + +inline +std::pair +StrVec::alloc_n_copy(const std::string *b, const std::string *e) +{ + // allocate space to hold as many elements as are in the range + auto data = alloc.allocate(e - b); + + // initialize and return a pair constructed from data and + // the value returned by uninitialized_copy + return {data, uninitialized_copy(b, e, data)}; +} + +inline +StrVec::StrVec(StrVec &&s) noexcept // move won't throw any exceptions + // member initializers take over the resources in s + : elements(s.elements), first_free(s.first_free), cap(s.cap) +{ + // leave s in a state in which it is safe to run the destructor + s.elements = s.first_free = s.cap = nullptr; +} + +inline +StrVec::StrVec(const StrVec &s) +{ + // call alloc_n_copy to allocate exactly as many elements as in s + auto newdata = alloc_n_copy(s.begin(), s.end()); + elements = newdata.first; + first_free = cap = newdata.second; +} + +inline +void StrVec::free() +{ + // may not pass deallocate a 0 pointer; if elements is 0, there's no work to do + if (elements) { + // destroy the old elements in reverse order + for (auto p = first_free; p != elements; /* empty */) + alloc.destroy(--p); + alloc.deallocate(elements, cap - elements); + } +} + +inline +StrVec &StrVec::operator=(std::initializer_list il) +{ + // alloc_n_copy allocates space and copies elements from the given range + auto data = alloc_n_copy(il.begin(), il.end()); + free(); // destroy the elements in this object and free the space + elements = data.first; // update data members to point to the new space + first_free = cap = data.second; + return *this; +} + +inline +StrVec &StrVec::operator=(StrVec &&rhs) noexcept +{ + // direct test for self-assignment + if (this != &rhs) { + free(); // free existing elements + elements = rhs.elements; // take over resources from rhs + first_free = rhs.first_free; + cap = rhs.cap; + // leave rhs in a destructible state + rhs.elements = rhs.first_free = rhs.cap = nullptr; + } + return *this; +} + +inline +StrVec &StrVec::operator=(const StrVec &rhs) +{ + // call alloc_n_copy to allocate exactly as many elements as in rhs + auto data = alloc_n_copy(rhs.begin(), rhs.end()); + free(); + elements = data.first; + first_free = cap = data.second; + return *this; +} + +inline +void StrVec::reallocate() +{ + // we'll allocate space for twice as many elements as the current size + auto newcapacity = size() ? 2 * size() : 1; + + // allocate new memory + auto newdata = alloc.allocate(newcapacity); + + // move the data from the old memory to the new + auto dest = newdata; // points to the next free position in the new array + auto elem = elements; // points to the next element in the old array + for (size_t i = 0; i != size(); ++i) + alloc.construct(dest++, std::move(*elem++)); + + free(); // free the old space once we've moved the elements + + // update our data structure to point to the new elements + elements = newdata; + first_free = dest; + cap = elements + newcapacity; +} + +inline +StrVec::StrVec(std::initializer_list il) +{ + // call alloc_n_copy to allocate exactly as many elements as in il + auto newdata = alloc_n_copy(il.begin(), il.end()); + elements = newdata.first; + first_free = cap = newdata.second; +} + +inline +void StrVec::push_back(const std::string& s) +{ + chk_n_alloc(); // ensure that there is room for another element + // construct a copy of s in the element to which first_free points + alloc.construct(first_free++, s); +} + +inline +void StrVec::push_back(std::string &&s) +{ + chk_n_alloc(); // reallocates the StrVec if necessary + alloc.construct(first_free++, std::move(s)); +} + + +// emplace member covered in chapter 16 +template +inline +void StrVec::emplace_back(Args&&... args) +{ + chk_n_alloc(); // reallocates the StrVec if necessary + alloc.construct(first_free++, std::forward(args)...); +} + +#endif diff --git a/13/String.cc b/13/String.cc new file mode 100644 index 0000000..089da48 --- /dev/null +++ b/13/String.cc @@ -0,0 +1,147 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::strlen; + +#include +using std::copy; + +#include +using std::size_t; + +#include +using std::ostream; + +#include +using std::swap; + +#include +using std::initializer_list; + +#include +using std::uninitialized_copy; + +#include "String.h" + +// define the static allocator member +std::allocator String::a; + +// copy-assignment operator +String & String::operator=(const String &rhs) +{ + // copying the right-hand operand before deleting the left handles self-assignment + auto newp = a.allocate(rhs.sz); // copy the underlying string from rhs + uninitialized_copy(rhs.p, rhs.p + rhs.sz, newp); + + if (p) + a.deallocate(p, sz); // free the memory used by the left-hand operand + p = newp; // p now points to the newly allocated string + sz = rhs.sz; // update the size + + return *this; +} + +// move assignment operator +String & String::operator=(String &&rhs) noexcept +{ + // explicit check for self-assignment + if (this != &rhs) { + if (p) + a.deallocate(p, sz); // do the work of the destructor + p = rhs.p; // take over the old memory + sz = rhs.sz; + rhs.p = 0; // deleting rhs.p is safe + rhs.sz = 0; + } + return *this; +} + +String& String::operator=(const char *cp) +{ + if (p) a.deallocate(p, sz); + p = a.allocate(sz = strlen(cp)); + uninitialized_copy(cp, cp + sz, p); + return *this; +} + +String& String::operator=(char c) +{ + if(p) a.deallocate(p, sz); + p = a.allocate(sz = 1); + *p = c; + return *this; +} + +String& String::operator=(initializer_list il) +{ + // no need to check for self-assignment + if (p) + a.deallocate(p, sz); // do the work of the destructor + p = a.allocate(sz = il.size()); // do the work of the copy constructor + uninitialized_copy(il.begin(), il.end(), p); + return *this; +} +// named functions for operators +ostream &print(ostream &os, const String &s) +{ + auto p = s.begin(); + while (p != s.end()) + os << *p++ ; + return os; +} + +String add(const String &lhs, const String &rhs) +{ + String ret; + ret.sz = rhs.size() + lhs.size(); // size of the combined String + ret.p = String::a.allocate(ret.sz); // allocate new space + uninitialized_copy(lhs.begin(), lhs.end(), ret.p); // copy the operands + uninitialized_copy(rhs.begin(), rhs.end(), ret.p + lhs.sz); + return ret; // return a copy of the newly created String +} + +// return plural version of word if ctr isn't 1 +String make_plural(size_t ctr, const String &word, + const String &ending) +{ + return (ctr != 1) ? add(word, ending) : word; +} + +// chapter 14 will explain overloaded operators +ostream &operator<<(ostream &os, const String &s) +{ + return print(os, s); +} + +String operator+(const String &lhs, const String &rhs) +{ + return add(lhs, rhs); +} + diff --git a/13/String.h b/13/String.h new file mode 100644 index 0000000..d377501 --- /dev/null +++ b/13/String.h @@ -0,0 +1,104 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef STRING_H +#define STRING_H + +#include +#include +#include +#include +#include +#include +#include + +class String { +friend String operator+(const String&, const String&); +friend String add(const String&, const String&); +friend std::ostream &operator<<(std::ostream&, const String&); +friend std::ostream &print(std::ostream&, const String&); + +public: + String() = default; + + // cp points to a null terminated array, + // allocate new memory & copy the array + String(const char *cp) : + sz(std::strlen(cp)), p(a.allocate(sz)) + { std::uninitialized_copy(cp, cp + sz, p); } + + // copy constructor: allocate a new copy of the characters in s + String(const String &s):sz(s.sz), p(a.allocate(s.sz)) + { std::uninitialized_copy(s.p, s.p + sz , p); } + + // move constructor: copy the pointer, not the characters, + // no memory allocation or deallocation + String(String &&s) noexcept : sz(s.size()), p(s.p) + { s.p = 0; s.sz = 0; } + + String(size_t n, char c) : sz(n), p(a.allocate(n)) + { std::uninitialized_fill_n(p, sz, c); } + + // allocates a new copy of the data in the right-hand operand; + // deletes the memory used by the left-hand operand + String &operator=(const String &); + // moves pointers from right- to left-hand operand + String &operator=(String &&) noexcept; + + // unconditionally delete the memory because each String has its own memory + ~String() noexcept { if (p) a.deallocate(p, sz); } + + // additional assignment operators + String &operator=(const char*); // car = "Studebaker" + String &operator=(char); // model = 'T' + String & + operator=(std::initializer_list); // car = {'a', '4'} + + const char *begin() { return p; } + const char *begin() const { return p; } + const char *end() { return p + sz; } + const char *end() const { return p + sz; } + + size_t size() const { return sz; } + void swap(String &s) + { auto tmp = p; p = s.p; s.p = tmp; + auto cnt = sz; sz = s.sz; s.sz = cnt; } +private: + std::size_t sz = 0; + char *p = nullptr; + static std::allocator a; +}; +String make_plural(size_t ctr, const String &, const String &); +inline +void swap(String &s1, String &s2) +{ + s1.swap(s2); +} + +#endif diff --git a/13/copyNoMove.cc b/13/copyNoMove.cc new file mode 100644 index 0000000..3225e33 --- /dev/null +++ b/13/copyNoMove.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include + +class Foo { +public: + Foo() = default; // default constructor needed because Foo has a copy constructor + Foo(const Foo&); // copy constructor + // other members, but Foo does not define a move constructor +}; + +Foo::Foo(const Foo&) { std::cout << "Foo copy ctor" << std::endl; } + +int main() +{ + Foo x; + Foo y(x); // copy constructor; x is an lvalue + Foo z(std::move(x)); // copy constructor, because there is no move constructor +} diff --git a/13/data/storyDataFile b/13/data/storyDataFile new file mode 100644 index 0000000..1f06b36 --- /dev/null +++ b/13/data/storyDataFile @@ -0,0 +1,10 @@ +Alice Emma has long flowing red hair. +Her Daddy says when the wind blows +through her hair, it looks almost alive, +like a fiery bird in flight. +A beautiful fiery bird, he tells her, +magical but untamed. +"Daddy, shush, there is no such thing," +she tells him, at the same time wanting +him to tell her more. +Shyly, she asks, "I mean, Daddy, is there?" diff --git a/13/makefile b/13/makefile new file mode 100644 index 0000000..99f8b28 --- /dev/null +++ b/13/makefile @@ -0,0 +1,27 @@ +# executable files for this directory +OBJECTS = FolderMain.exe HasPtrUseCount.exe \ + HasPtrValueLike.exe copyNoMove.exe \ + moveHP.exe serial.exe StrFolderMain.exe \ + useStrVec.exe substr.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 -I../7 + +Folder.o FolderMain.o: Folder.h +StrFolder.o StrFolderMain.o: StrFolder.h String.h +String.o: String.h +useStrVec.o StrVec.o: String.h StrVec.h + +useStrVec.exe: StrVec.o useStrVec.o + $(CC) $(CCFLAGS) $(LOCFLAGS) StrVec.o useStrVec.o -o useStrVec.exe + +FolderMain.exe: Folder.o FolderMain.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Folder.o FolderMain.o -o FolderMain.exe + +StrFolderMain.exe: StrFolder.o StrFolderMain.o String.o + $(CC) $(CCFLAGS) $(LOCFLAGS) StrFolder.o StrFolderMain.o String.o \ + -o StrFolderMain.exe + diff --git a/13/moveHP.cc b/13/moveHP.cc new file mode 100644 index 0000000..1ec3437 --- /dev/null +++ b/13/moveHP.cc @@ -0,0 +1,76 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include +// for swap but we do not provide a using declaration for swap + +// HasPtr with added move constructor +class HasPtr { + friend void swap(HasPtr&, HasPtr&); +public: + // default constructor and constructor that takes a string + HasPtr(const std::string &s = std::string()): + ps(new std::string(s)), i(0) { } + + // copy constructor + HasPtr(const HasPtr &p): + ps(new std::string(*p.ps)), i(p.i) { } + + // move constructor + HasPtr(HasPtr &&p) noexcept : ps(p.ps), i(p.i) {p.ps = 0;} + + // assignment operator is both the move- and copy-assignment operator + HasPtr& operator=(HasPtr rhs) + { swap(*this, rhs); return *this; } + + // destructor + ~HasPtr() { delete ps; } +private: + std::string *ps; + int i; +}; + +inline +void swap(HasPtr &lhs, HasPtr &rhs) +{ + using std::swap; + swap(lhs.ps, rhs.ps); // swap the pointers, not the string data + swap(lhs.i, rhs.i); // swap the int members +} + +int main() +{ + HasPtr hp("hi mom"); + HasPtr hp2(hp); + + hp = hp2; // hp2 is an lvalue; copy constructor used to copy hp2 + hp = std::move(hp2); // move constructor moves hp2 +} diff --git a/13/runpgms b/13/runpgms new file mode 100644 index 0000000..6bdbf11 --- /dev/null +++ b/13/runpgms @@ -0,0 +1,5 @@ + +../noinput FolderMain.exe HasPtrUseCount.exe \ + HasPtrValueLike.exe copyNoMove.exe \ + moveHP.exe serial.exe \ + StrFolderMain.exe substr.exe useStrVec.exe diff --git a/13/serial.cc b/13/serial.cc new file mode 100644 index 0000000..2f7f1cd --- /dev/null +++ b/13/serial.cc @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::size_t; + +struct numbered { + static size_t sn; + numbered() : mysn(sn++) { } + numbered(const numbered &) : mysn(sn++) { } + numbered &operator=(const numbered &) { return *this; } + size_t mysn; +}; + +size_t numbered::sn = 0; + +void f (numbered s) { cout << s.mysn << endl; } + +int main() +{ + numbered a, b = a, c = b; + f(a); f(b); f(c); +} diff --git a/13/small_SI.h b/13/small_SI.h new file mode 100644 index 0000000..74448c9 --- /dev/null +++ b/13/small_SI.h @@ -0,0 +1,72 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced. Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ +#ifndef SMALL_SI_H +#define SMALL_SI_H + +// This file isn't used in our code, but illustrates the +// definitions that are equivalent to Sales_data's +// synthesized copy control members + +#include + +class Sales_data { +public: + Sales_data(const std::string & s = ""): bookNo(s), + units_sold(0), revenue(0.0) { } + + Sales_data(const std::string &book, unsigned cnt, double price): + bookNo(book), units_sold(cnt), revenue(cnt * price) { } + + // equivalent to the synthesized copy constructor + Sales_data(const Sales_data &rhs): bookNo(rhs.bookNo), + units_sold(rhs.units_sold), revenue(rhs.revenue) { } + + // equivalent to the synthesized destructor + // no work to do other than destroying the members, + // which happens automatically + ~Sales_data() { } + + Sales_data& operator=(const Sales_data &); // assignment operator +private: + std::string bookNo; + int units_sold = 0; + double revenue = 0.0; +}; + +// equivalent to the synthesized copy-assignment operator +Sales_data& +Sales_data::operator=(const Sales_data &rhs) +{ + bookNo = rhs.bookNo; // calls the string::operator= + units_sold = rhs.units_sold; // uses the built-in int assignment + revenue = rhs.revenue; // uses the built-in double assignment + return *this; // return a reference to this object +} + +#endif diff --git a/13/substr.cc b/13/substr.cc new file mode 100644 index 0000000..6b27b7c --- /dev/null +++ b/13/substr.cc @@ -0,0 +1,57 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +string::size_type +position(const string &a, const string &b, const string &c) +{ + return (a + b).find(c); +} + +int main() +{ + string s1 = "a value", s2 = "another"; + auto x = position(s1, s2, "val"); + if (x == string::npos) + cout << "not found" << endl; + else if (x < s1.size()) + cout << "value is in first parameter" << endl; + else + cout << "value is in second parameter" << endl; + + auto n = (s1 + s2).find('a'); + cout << "n = " << n << endl; + s1 + s2 = "wow!"; +} + diff --git a/13/useStrVec.cc b/13/useStrVec.cc new file mode 100644 index 0000000..9698c99 --- /dev/null +++ b/13/useStrVec.cc @@ -0,0 +1,102 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "StrVec.h" + +#include +using std::string; + +#include +using std::cout; using std::endl; +using std::istream; + +#include +using std::ifstream; + +void print(const StrVec &svec) +{ + for (auto it : svec) + cout << it << " " ; + cout <> s) + svec.push_back(s); + return svec; +} + +int main() +{ + StrVec sv = {"one", "two", "three"}; + // run the string empty funciton on the first element in sv + if (!sv[0].empty()) + sv[0] = "None"; // assign a new value to the first string + + // we'll call getVec a couple of times + // and will read the same file each time + ifstream in("data/storyDataFile"); + StrVec svec = getVec(in); + print(svec); + in.close(); + + cout << "copy " << svec.size() << endl; + auto svec2 = svec; + print(svec2); + + cout << "assign" << endl; + StrVec svec3; + svec3 = svec2; + print(svec3); + + StrVec v1, v2; + v1 = v2; // v2 is an lvalue; copy assignment + + in.open("data/storyDataFile"); + v2 = getVec(in); // getVec(in) is an rvalue; move assignment + in.close(); + + StrVec vec; // empty StrVec + string s = "some string or another"; + vec.push_back(s); // calls push_back(const string&) + vec.push_back("done"); // calls push_back(string&&) + + // emplace member covered in chpater 16 + s = "the end"; + vec.emplace_back(10, 'c'); // adds cccccccccc as a new last element + vec.emplace_back(s); // uses the string copy constructor + string s1 = "the beginning", s2 = s; + vec.emplace_back(s1 + s2); // uses the move constructor + + return 0; +} + diff --git a/14/README b/14/README new file mode 100644 index 0000000..c7c82c4 --- /dev/null +++ b/14/README @@ -0,0 +1,13 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + add_item add + count-size newcount-size + count-size fcnobj + word_echo readStr + +Programs not listed above print output and do +not read any input + diff --git a/14/Sales_data.cc b/14/Sales_data.cc new file mode 100644 index 0000000..bfaa1d1 --- /dev/null +++ b/14/Sales_data.cc @@ -0,0 +1,104 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" +#include +using std::istream; using std::ostream; + +Sales_data::Sales_data(istream &is) +{ + is >> *this; // read a transaction from is into this object +} + +double Sales_data::avg_price() const +{ + if (units_sold) + return revenue/units_sold; + else + return 0; +} + +// member binary operator: left-hand operand is bound to the implicit this pointer +// assumes that both objects refer to the same book +Sales_data& Sales_data::operator+=(const Sales_data &rhs) +{ + units_sold += rhs.units_sold; + revenue += rhs.revenue; + return *this; +} + +// assumes that both objects refer to the same book +Sales_data +operator+(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum += rhs; // add rhs into sum + return sum; +} + +istream &operator>>(istream &is, Sales_data &item) +{ + double price; // no need to initialize; we'll read into price before we use it + is >> item.bookNo >> item.units_sold >> price; + if (is) // check that the inputs succeeded + item.revenue = item.units_sold * price; + else + item = Sales_data(); // input failed: give the object the default state + return is; +} + +ostream &operator<<(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} + +// operators replace these original named functions +istream &read(istream &is, Sales_data &item) +{ + double price = 0; + is >> item.bookNo >> item.units_sold >> price; + item.revenue = price * item.units_sold; + return is; +} +ostream &print(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} + +Sales_data add(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum += rhs; // add rhs into sum + return sum; +} + diff --git a/14/Sales_data.h b/14/Sales_data.h new file mode 100644 index 0000000..a77159c --- /dev/null +++ b/14/Sales_data.h @@ -0,0 +1,89 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef SALES_DATA_H +#define SALES_DATA_H + +#include +#include + +class Sales_data { +friend std::ostream &operator<< + (std::ostream&, const Sales_data&); +friend std::istream &operator>>(std::istream&, Sales_data&); +friend bool operator==(const Sales_data &, const Sales_data &); + +friend std::ostream &print(std::ostream&, const Sales_data&); +friend std::istream &read(std::istream&, Sales_data&); + +public: + // constructors + Sales_data() = default; + Sales_data(const std::string &s): bookNo(s) { } + Sales_data(const std::string &s, unsigned n, double p): + bookNo(s), units_sold(n), revenue(p*n) { } + Sales_data(std::istream &); + + std::string isbn() const { return bookNo; } + Sales_data& operator+=(const Sales_data&); +private: + double avg_price() const; + std::string bookNo; + unsigned units_sold = 0; + double revenue = 0.0; +}; + +// non-member Sales_data operations +inline +bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) +{ return lhs.isbn() < rhs.isbn(); } + +inline +bool operator==(const Sales_data &lhs, const Sales_data &rhs) +{ + return lhs.isbn() == rhs.isbn() && + lhs.units_sold == rhs.units_sold && + lhs.revenue == rhs.revenue; +} +inline +bool operator!=(const Sales_data &lhs, const Sales_data &rhs) +{ + return !(lhs == rhs); +} + +// old versions +Sales_data add(const Sales_data&, const Sales_data&); +std::ostream &print(std::ostream&, const Sales_data&); +std::istream &read(std::istream&, Sales_data&); + +// new operator functions +Sales_data operator+(const Sales_data&, const Sales_data&); +std::ostream &operator<<(std::ostream&, const Sales_data&); +std::istream &operator>>(std::istream&, Sales_data&); +#endif diff --git a/14/absInt.cc b/14/absInt.cc new file mode 100644 index 0000000..ac72b84 --- /dev/null +++ b/14/absInt.cc @@ -0,0 +1,80 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::inserter; + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::transform; + +struct absInt { + int operator()(int val) const { + return val < 0 ? -val : val; + } +}; + +int main() { + int i = -42; + absInt absObj; // object that has a function-call operator + unsigned ui = absObj(i); // passes i to absObj.operator() + cout << i << " " << ui << endl; + + // store collection of positive and negative integers in vi + vector vi; + while (cin >> i) + vi.push_back(i); + + // call absInt to store the absolute value of those ints in vu + vector vu; + transform(vi.begin(), vi.end(), back_inserter(vu), absInt()); + + // print contents of vu using a lambda + for_each(vu.begin(), vu.end(), [](unsigned i) { cout << i << " "; }); + cout << endl; + + vector vu2; + // similar transformation but using a lambda + transform(vi.begin(), vi.end(), back_inserter(vu2), + [](int i) { return i < 0 ? -i : i; }); + if (vu == vu2) + cout << "as expected" << endl; + else { + cout << "something's wrong, vectors differ" << endl; + for_each(vu.begin(), vu.end(), [](unsigned i) { cout << i << " "; }); + } + cout << endl; + + return 0; +} diff --git a/14/add.cc b/14/add.cc new file mode 100644 index 0000000..6fa4cc8 --- /dev/null +++ b/14/add.cc @@ -0,0 +1,53 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include "Sales_data.h" +#include "Sales_item.h" + +int main() +{ + Sales_item item1, item2; + cin >> item1 >> item2; //read a pair of transactions + // print the sum of two Sales_items + cout << item1 + item2 << endl; + + Sales_data data1, data2; + read(read(cin, data1), data2); //read a pair of transactions + print(cout, add(data1, data2)); // print the sum of two Sales_datas + cout << std::endl; + + cin >> data1 >> data2; // use input operator to read Sales_datas + // use operators to print the sum of two Sales_datas + cout << data1 + data2 << endl; + + return 0; +} diff --git a/14/calc.cc b/14/calc.cc new file mode 100644 index 0000000..05442d4 --- /dev/null +++ b/14/calc.cc @@ -0,0 +1,87 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; using std::ostream; + +#include +using std::map; + +#include +using std::string; + +#include +using std::bind; using std::function; +using namespace std::placeholders; + +// ordinary function +int add(int i, int j) { return i + j; } + +// lambda, which generates an unnamed function-object class +auto mod = [](int i, int j) { return i % j; }; + +// function-object class +// In the first printing we named this struct div, but that name conflicts with +// the name of a C library function. Compilers are permitted to put +// C library names in the global namespace. Future printings will +// change the name of this calss to divide. +struct divide { + int operator()(int denominator, int divisor) { + return denominator / divisor; + } +}; + +int main() +{ + function f1 = add; // function pointer + function f2 = divide(); // callable class type + function f3 = [](int i, int j) // lambda + { return i * j; }; + cout << f1(4,2) << endl; // prints 6 + cout << f2(4,2) << endl; // prints 2 + cout << f3(4,2) << endl; // prints 8 + + // table of callable objects corresponding to each binary operator + // all the callables must take two ints and return an int + // an element can be a function pointer, function object, or lambda + map> binops = { + {"+", add}, // function pointer + {"-", std::minus()}, // library function object + {"/", divide()}, // user-defined function object + {"*", [](int i, int j) { return i * j; }}, // unnamed lambda + {"%", mod} }; // named lambda object + + cout << binops["+"](10, 5) << endl; // calls add(10, 5) + cout << binops["-"](10, 5) << endl; // uses the call operator of the minus object + cout << binops["/"](10, 5) << endl; // uses the call operator of the divide object + cout << binops["*"](10, 5) << endl; // calls the lambda function object + cout << binops["%"](10, 5) << endl; // calls the lambda function object + + return 0; +} diff --git a/14/data/absInt b/14/data/absInt new file mode 100644 index 0000000..9726e80 --- /dev/null +++ b/14/data/absInt @@ -0,0 +1,15 @@ +-42 +65 +-95 +100 +39 +-67 +95 +76 +-88 +76 +83 +92 +76 +-93 +0 diff --git a/14/data/add_item b/14/data/add_item new file mode 100644 index 0000000..647b742 --- /dev/null +++ b/14/data/add_item @@ -0,0 +1,9 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 + +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 + +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 + diff --git a/14/data/count-size b/14/data/count-size new file mode 100644 index 0000000..da448a3 --- /dev/null +++ b/14/data/count-size @@ -0,0 +1 @@ +the quick red fox jumps over the slow red turtle diff --git a/14/data/word_echo b/14/data/word_echo new file mode 100644 index 0000000..e544f33 --- /dev/null +++ b/14/data/word_echo @@ -0,0 +1,2 @@ +Now is the time for all good boys +to come to the aid of the party diff --git a/14/directcall.cc b/14/directcall.cc new file mode 100644 index 0000000..6a4aa4c --- /dev/null +++ b/14/directcall.cc @@ -0,0 +1,55 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" + +#include +using std::cout; using std::endl; + +#include +using std::string; + +int main() +{ + Sales_data data1, data2; + std::cin >> data1 >> data2; // read Sales_data transactions + cout << data1 + data2 << endl; // write sum of Sales_data objects + cout << 42 + 5 << endl; // write sum of ints + + // equivalent calls to a nonmember operator function + data1 + data2; // normal expression + operator+(data1, data2); // equivalent function call + cout << operator+(data1, data2) << endl; + + data1 += data2; // expression-based ``call'' + data1.operator+=(data2); // equivalent call to a member operator function + cout << data1 << endl; + + return 0; +} diff --git a/14/fcnobj.cc b/14/fcnobj.cc new file mode 100644 index 0000000..e43f8ef --- /dev/null +++ b/14/fcnobj.cc @@ -0,0 +1,99 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::plus; using std::negate; +using std::function; using std::placeholders::_1; +using std::bind; using std::less_equal; + +#include +using std::cout; using std::endl; + +#include +using std::count_if; + +#include +using std::vector; + +#include +using std::cin; + +#include +using std::string; + +bool size_compare(const string &s, string::size_type sz) +{ + return s.size() >= sz; +} + +int main() { + + cout << plus()(3,4) << endl; // prints 7 + + plus intAdd; // object that can add two int values + negate intNegate; // object that can negate an int value + + // uses intAdd::operator(int, int) to add 10 and 20 + int sum = intAdd(10, 20); // equivalent to sum = 30 + cout << sum << endl; + + sum = intNegate(intAdd(10, 20)); // equivalent to sum = -30 + cout << sum << endl; + + // uses intNegate::operator(int) to generate -10 + // as the second argument to intAdd::operator(int, int) + sum = intAdd(10, intNegate(10)); // sum = 0 + + cout << sum << endl; + + vector vec = {0,1,2,3,4,5,16,17,18,19}; + + // bind second argument to less_equal + cout << count_if(vec.begin(), vec.end(), + bind(less_equal(), _1, 10)); + cout << endl; + + vector svec; + string in; + while (cin >> in) + svec.push_back(in); + + function fp1 = size_compare; + + //decltype(fp1)::result_type ret; + function fp2 = bind(size_compare, _1, 6); + cout << count_if(svec.begin(), svec.end(), fp2) + << endl; + cout << count_if(svec.begin(), svec.end(), + bind(size_compare, _1, 6)) + << endl; + + + return 0; +} diff --git a/14/makefile b/14/makefile new file mode 100644 index 0000000..574c95d --- /dev/null +++ b/14/makefile @@ -0,0 +1,17 @@ +# executable files for this directory +OBJECTS = absInt.exe add.exe calc.exe directcall.exe \ + fcnobj.exe newcount-size.exe readStr.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 -I../6 + +add.o directcall.o Sales_data.o: Sales_data.h + +add.exe: add.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) add.o Sales_data.o -o add.exe + +directcall.exe: directcall.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) directcall.o Sales_data.o -o directcall.exe diff --git a/14/newcount-size.cc b/14/newcount-size.cc new file mode 100644 index 0000000..87c581b --- /dev/null +++ b/14/newcount-size.cc @@ -0,0 +1,156 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "make_plural.h" + +#include +using std::cin; using std::cerr; using std::cout; using std::endl; +using std::ostream; + +#include +using std::for_each; using std::find_if; using std::stable_sort; + +#include +using std::vector; + +#include +using std::string; + +#include +using std::ifstream; + +#include +using std::size_t; + +// comparison function to be used to sort by word length +bool isShorter(const string &s1, const string &s2) +{ + return s1.size() < s2.size(); +} + +// determine whether a length of a given word is 6 or more +bool GT(const string &s, string::size_type m) +{ + return s.size() >= m; +} + +class SizeComp { +public: + SizeComp() = delete; // no default constructor + SizeComp &operator=(const SizeComp&) = delete; // no assignment + ~SizeComp() = default; + + // constructor with a parameter for each captured variable + SizeComp(size_t n): sz(n) { } + + // call operator with the same return type, + // parameters, and body as the lambda + bool operator()(const string &s) const { return s.size() >= sz; } +private: + size_t sz; // a data member for each variable captured by value +}; + +class PrintString { +public: + PrintString() = delete; // no default constructor + PrintString(ostream &o) : os(o) { } + void operator()(const string &s) const { os << s << " "; } +private: + ostream &os; +}; + +class ShorterString { +public: + bool operator()(const string &s1, const string &s2) const + { return s1.size() < s2.size(); } +}; + +void elimDups(vector &words) +{ + // sort words alphabetically so we can find the duplicates + sort(words.begin(), words.end()); + + // print the sorted contents + for_each(words.begin(), words.end(), PrintString(cerr)); + cerr << endl; + + // unique reorders the input so that each word appears once in the + // front part of the range + // returns an iterator one past the unique range; + auto end_unique = unique(words.begin(), words.end()); + + // erase uses a vector operation to remove the nonunique elements + words.erase(end_unique, words.end()); + + // print the reduced vector + for_each(words.begin(), words.end(), PrintString(cerr)); + cerr << endl; +} + +void biggies(vector &words, vector::size_type sz) +{ + elimDups(words); // puts words in alphabetic order and removes duplicates + + // sort words by size using object of type ShorterString + // maintaining alphabetic order for words of the same size + stable_sort(words.begin(), words.end(), ShorterString()); + + // use object of type SizeComp to find + // the first element whose size() is >= sz + auto wc = find_if(words.begin(), words.end(), SizeComp(sz)); + + // compute the number of elements with size >= sz + auto count = words.end() - wc; + + // print results + cout << count << " " << make_plural(count, "word", "s") + << " " << sz << " characters or longer" << endl; + + // use object of type PrintString + // to print the contents of words, each one followed by a space + for_each(wc, words.end(), PrintString(cout)); + cout << endl; +} + +int main() +{ + vector words; + + // copy contents of each book into a single vector + string next_word; + while (cin >> next_word) { + // insert next book's contents at end of words + words.push_back(next_word); + } + + biggies(words, 6); + + return 0; +} + diff --git a/14/readStr.cc b/14/readStr.cc new file mode 100644 index 0000000..8345aa3 --- /dev/null +++ b/14/readStr.cc @@ -0,0 +1,80 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::for_each; + +#include +using std::cin; using std::cout; using std::endl; using std::cerr; +using std::istream; using std::ostream; + +#include +using std::string; + +#include +using std::vector; + +class PrintString { +public: + PrintString(ostream &o = cout, char c = ' '): + os(o), sep(c) { } + void operator()(const string &s) const { os << s << sep; } +private: + ostream &os; // stream on which to write + char sep; // character to print after each output +}; + +class ReadLine { +public: + ReadLine() = delete; + ReadLine(istream &i) : is(i) { } + bool operator()(string &s) const { return getline(is, s); } +private: + istream &is; +}; + +int main() +{ + vector vs; + ReadLine rl(cin); // object that read lines from cin + string s; + while (rl(s)) // store what rl reads into s + vs.push_back(s); + + cout << "read : " << vs.size() << " elements" << endl; + PrintString printer; // uses the defaults; prints to cout + printer(s); // prints s followed by a space on cout + + PrintString errors(cerr, '\n'); + errors(s); // prints s followed by a newline on cerr + + cerr << "for_each printing to cerr" << endl; + for_each(vs.begin(), vs.end(), PrintString(cerr, '\n')); +} + diff --git a/14/runpgms b/14/runpgms new file mode 100644 index 0000000..d482966 --- /dev/null +++ b/14/runpgms @@ -0,0 +1,9 @@ +echo "absInt:" && ./absInt.exe < data/absInt +echo "add: " && ./add.exe < data/add_item +echo "fcnobj: " && ./fcnobj.exe < data/count-size +echo "directcall: " && ./directcall.exe < data/add_item +echo "newcount-size: " && ./newcount-size.exe < data/count-size +echo "readStr: " && ./readStr.exe < data/word_echo + +../noinput calc.exe + diff --git a/15/Basket.cc b/15/Basket.cc new file mode 100644 index 0000000..cb662d0 --- /dev/null +++ b/15/Basket.cc @@ -0,0 +1,84 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Quote.h" +#include "Basket.h" + +#include +using std::size_t; + +#include +using std::multiset; + +#include +using std::string; + +#include +using std::ostream; using std::endl; +using std::cout; + +// debugging routine to check contents in a Basket +void Basket::display(ostream &os) const +{ + os << "Basket size: " << items.size() << endl; + + // print each distinct ISBN in the Basket along with + // count of how many copies are ordered and what their price will be + // upper_bound returns an iterator to the next item in the set + for (auto next_item = items.cbegin(); + next_item != items.cend(); + next_item = items.upper_bound(*next_item)) + { + // we know there's at least one element with this key in the Basket + os << (*next_item)->isbn() << " occurs " + << items.count(*next_item) << " times" + << " for a price of " + << (*next_item)->net_price(items.count(*next_item)) + << endl; + } +} + +double Basket::total_receipt(ostream &os) const +{ + double sum = 0.0; // holds the running total + + + // iter refers to the first element in a batch of elements with the same ISBN + // upper_bound returns an iterator to the element just past the end of that batch + for (auto iter = items.cbegin(); + iter != items.cend(); + iter = items.upper_bound(*iter)) { + // we know there's at least one element with this key in the Basket + // print the line item for this book + sum += print_total(os, **iter, items.count(*iter)); + } + os << "Total Sale: " << sum << endl; // print the final overall total + return sum; +} + diff --git a/15/Basket.h b/15/Basket.h new file mode 100644 index 0000000..6e5d784 --- /dev/null +++ b/15/Basket.h @@ -0,0 +1,74 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef BASKET_H +#define BASKET_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Quote.h" + +// holds items being purchased +class Basket { +public: + // Basket uses synthesized default constructor and copy-control members + void add_item(const std::shared_ptr &sale) + { items.insert(sale); } + + void add_item(const Quote& sale) // copy the given object + { items.insert(std::shared_ptr(sale.clone())); } + + void add_item(Quote&& sale) // move the given object + { items.insert( + std::shared_ptr(std::move(sale).clone())); } + + // prints the total price for each book + // and the overall total for all items in the basket + double total_receipt(std::ostream&) const; + + // for debugging purposes, prints contents of the basket + void display (std::ostream&) const; +private: + // function to compare shared_ptrs needed by the multiset member + static bool compare(const std::shared_ptr &lhs, + const std::shared_ptr &rhs) + { return lhs->isbn() < rhs->isbn(); } + + // multiset to hold multiple quotes, ordered by the compare member + std::multiset, decltype(compare)*> + items{compare}; +}; + +#endif diff --git a/15/Basket_main.cc b/15/Basket_main.cc new file mode 100644 index 0000000..6116b81 --- /dev/null +++ b/15/Basket_main.cc @@ -0,0 +1,80 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::shared_ptr; using std::make_shared; +#include "Basket.h" +#include +using std::cout; using std::endl; + +int main() +{ + Basket sale; + sale.add_item(shared_ptr(new Quote("123", 45))); + sale.add_item(shared_ptr(new Quote("123", 45))); + sale.add_item(shared_ptr(new Quote("123", 45))); + sale.add_item(make_shared("345", 45, 3, .15)); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("345", 45, 3, .15))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Bulk_quote("678", 55, 5, .25))); + sale.add_item(shared_ptr(new Lim_quote("abc", 35, 2, .10))); + sale.add_item(shared_ptr(new Lim_quote("abc", 35, 2, .10))); + sale.add_item(shared_ptr(new Lim_quote("abc", 35, 2, .10))); + sale.add_item(shared_ptr(new Lim_quote("abc", 35, 2, .10))); + sale.add_item(shared_ptr(new Lim_quote("abc", 35, 2, .10))); + sale.add_item(shared_ptr(new Lim_quote("abc", 35, 2, .10))); + sale.add_item(shared_ptr(new Quote("def", 35))); + sale.add_item(shared_ptr(new Quote("def", 35))); + + sale.total_receipt(cout); + + Basket bsk; + // arguments are the ISBN, price, minimum quantity, and discount + bsk.add_item(shared_ptr(new Bulk_quote("0-201-82470-1", 50, 5, .19))); + bsk.add_item(shared_ptr(new Bulk_quote("0-201-82470-1", 50, 5, .19))); + bsk.add_item(shared_ptr(new Bulk_quote("0-201-82470-1", 50, 5, .19))); + bsk.add_item(shared_ptr(new Bulk_quote("0-201-82470-1", 50, 5, .19))); + bsk.add_item(shared_ptr(new Bulk_quote("0-201-82470-1", 50, 5, .19))); + bsk.add_item(shared_ptr(new Lim_quote("0-201-54848-8", 35, 2, .10))); + bsk.add_item(shared_ptr(new Lim_quote("0-201-54848-8", 35, 2, .10))); + bsk.add_item(shared_ptr(new Lim_quote("0-201-54848-8", 35, 2, .10))); + bsk.total_receipt(cout); +} diff --git a/15/Query.cc b/15/Query.cc new file mode 100644 index 0000000..140b4cb --- /dev/null +++ b/15/Query.cc @@ -0,0 +1,113 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Query.h" +#include "TextQuery.h" + +#include +using std::shared_ptr; using std::make_shared; + +#include +using std::set; + +#include +using std::set_intersection; + +#include +using std::ostream; + +#include +using std::size_t; + +#include +using std::inserter; + +// returns the lines not in its operand's result set +QueryResult +NotQuery::eval(const TextQuery& text) const +{ + // virtual call to eval through the Query operand + auto result = query.eval(text); + + // start out with an empty result set + auto ret_lines = make_shared>(); + + // we have to iterate through the lines on which our operand appears + auto beg = result.begin(), end = result.end(); + + // for each line in the input file, if that line is not in result, + // add that line number to ret_lines + auto sz = result.get_file()->size(); + for (size_t n = 0; n != sz; ++n) { + // if we haven't processed all the lines in result + // check whether this line is present + if (beg == end || *beg != n) + ret_lines->insert(n); // if not in result, add this line + else if (beg != end) + ++beg; // otherwise get the next line number in result if there is one + } + return QueryResult(rep(), ret_lines, result.get_file()); +} + +// returns the intersection of its operands' result sets +QueryResult +AndQuery::eval(const TextQuery& text) const +{ + // virtual calls through the Query operands to get result sets for the operands + auto left = lhs.eval(text), right = rhs.eval(text); + + // set to hold the intersection of left and right + auto ret_lines = make_shared>(); + + // writes the intersection of two ranges to a destination iterator + // destination iterator in this call adds elements to ret + set_intersection(left.begin(), left.end(), + right.begin(), right.end(), + inserter(*ret_lines, ret_lines->begin())); + return QueryResult(rep(), ret_lines, left.get_file()); +} + +// returns the union of its operands' result sets +QueryResult +OrQuery::eval(const TextQuery& text) const +{ + // virtual calls through the Query members, lhs and rhs + // the calls to eval return the QueryResult for each operand + auto right = rhs.eval(text), left = lhs.eval(text); + + // copy the line numbers from the left-hand operand into the result set + auto ret_lines = + make_shared>(left.begin(), left.end()); + + // insert lines from the right-hand operand + ret_lines->insert(right.begin(), right.end()); + // return the new QueryResult representing the union of lhs and rhs + return QueryResult(rep(), ret_lines, left.get_file()); +} + diff --git a/15/Query.h b/15/Query.h new file mode 100644 index 0000000..0f2fa87 --- /dev/null +++ b/15/Query.h @@ -0,0 +1,154 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef QUERY_H +#define QUERY_H +#include "TextQuery.h" +#include +#include +#include +#include +#include +#include + +// abstract class acts as a base class for concrete query types; all members are private +class Query_base { + friend class Query; +protected: + using line_no = TextQuery::line_no; // used in the eval functions + virtual ~Query_base() = default; +private: + // eval returns the QueryResult that matches this Query + virtual QueryResult eval(const TextQuery&) const = 0; + // rep is a string representation of the query + virtual std::string rep() const = 0; +}; + +// interface class to manage the Query_base inheritance hierarchy +class Query { + // these operators need access to the shared_ptr constructor + friend Query operator~(const Query &); + friend Query operator|(const Query&, const Query&); + friend Query operator&(const Query&, const Query&); +public: + Query(const std::string&); // builds a new WordQuery + + // interface functions: call the corresponding Query_base operations + QueryResult eval(const TextQuery &t) const + { return q->eval(t); } + std::string rep() const { return q->rep(); } +private: + Query(std::shared_ptr query): q(query) { } + std::shared_ptr q; +}; +inline +std::ostream & +operator<<(std::ostream &os, const Query &query) +{ + // Query::rep makes a virtual call through its Query_base pointer to rep() + return os << query.rep(); +} + +class WordQuery: public Query_base { + friend class Query; // Query uses the WordQuery constructor + WordQuery(const std::string &s): query_word(s) { } + + // concrete class: WordQuery defines all inherited pure virtual functions + QueryResult eval(const TextQuery &t) const + { return t.query(query_word); } + std::string rep() const { return query_word; } + std::string query_word; // word for which to search +}; + +inline +Query::Query(const std::string &s): q(new WordQuery(s)) { } + +class NotQuery: public Query_base { + friend Query operator~(const Query &); + NotQuery(const Query &q): query(q) { } + + // concrete class: NotQuery defines all inherited pure virtual functions + std::string rep() const {return "~(" + query.rep() + ")";} + QueryResult eval(const TextQuery&) const; + Query query; +}; + +class BinaryQuery: public Query_base { +protected: + BinaryQuery(const Query &l, const Query &r, std::string s): + lhs(l), rhs(r), opSym(s) { } + + // abstract class: BinaryQuery doesn't define eval + std::string rep() const { return "(" + lhs.rep() + " " + + opSym + " " + + rhs.rep() + ")"; } + + Query lhs, rhs; // right- and left-hand operands + std::string opSym; // name of the operator +}; + +class AndQuery: public BinaryQuery { + friend Query operator&(const Query&, const Query&); + AndQuery(const Query &left, const Query &right): + BinaryQuery(left, right, "&") { } + + // concrete class: AndQuery inherits rep and defines the remaining pure virtual + QueryResult eval(const TextQuery&) const; +}; + +class OrQuery: public BinaryQuery { + friend Query operator|(const Query&, const Query&); + OrQuery(const Query &left, const Query &right): + BinaryQuery(left, right, "|") { } + + QueryResult eval(const TextQuery&) const; +}; + +inline Query operator&(const Query &lhs, const Query &rhs) +{ + return std::shared_ptr(new AndQuery(lhs, rhs)); +} + +inline Query operator|(const Query &lhs, const Query &rhs) +{ + return std::shared_ptr(new OrQuery(lhs, rhs)); +} + +inline Query operator~(const Query &operand) +{ + return std::shared_ptr(new NotQuery(operand)); +} + +std::ifstream& open_file(std::ifstream&, const std::string&); +TextQuery get_file(int, char**); +bool get_word(std::string&); +bool get_words(std::string&, std::string&); +std::ostream &print(std::ostream&, const QueryResult&); + +#endif diff --git a/15/Quote.cc b/15/Quote.cc new file mode 100644 index 0000000..28f4783 --- /dev/null +++ b/15/Quote.cc @@ -0,0 +1,72 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Quote.h" + +#include +using std::min; + +#include +using std::size_t; + +#include +using std::ostream; using std::endl; +using std::cout; + +// calculate and print the price for the given number of copies, applying any discounts +double print_total(ostream &os, + const Quote &item, size_t n) +{ + // depending on the type of the object bound to the item parameter + // calls either Quote::net_price or Bulk_quote::net_price + double ret = item.net_price(n); + os << "ISBN: " << item.isbn() // calls Quote::isbn + << " # sold: " << n << " total due: " << ret << endl; + return ret; +} + +// if the specified number of items are purchased, use the discounted price +double Bulk_quote::net_price(size_t cnt) const +{ + if (cnt >= quantity) + return cnt * (1 - discount) * price; + else + return cnt * price; +} + +// use discounted price for up to a specified number of items +// additional items priced at normal, undiscounted price +double Lim_quote::net_price(size_t cnt) const +{ + size_t discounted = min(cnt, quantity); + size_t undiscounted = cnt - discounted; + return discounted * (1 - discount) * price + + undiscounted * price; +} + diff --git a/15/Quote.h b/15/Quote.h new file mode 100644 index 0000000..059c719 --- /dev/null +++ b/15/Quote.h @@ -0,0 +1,142 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef QUOTE_H +#define QUOTE_H + +#include "Version_test.h" + +#include +#include +#include +#include + +// Item sold at an undiscounted price +// derived classes will define various discount strategies +class Quote { +friend std::istream& operator>>(std::istream&, Quote&); +friend std::ostream& operator<<(std::ostream&, const Quote&); +public: + Quote() = default; + Quote(const std::string &book, double sales_price): + bookNo(book), price(sales_price) { } + + // virtual destructor needed + // if a base pointer pointing to a derived object is deleted + virtual ~Quote() = default; // dynamic binding for the destructor + + std::string isbn() const { return bookNo; } + + // returns the total sales price for the specified number of items + // derived classes will override and apply different discount algorithms + virtual double net_price(std::size_t n) const + { return n * price; } + + // virtual function to return a dynamically allocated copy of itself + +#ifdef REFMEMS + virtual Quote* clone() const & {return new Quote(*this);} + virtual Quote* clone() && {return new Quote(std::move(*this));} +#else + // without reference qualification on member functions + // we can't overloaded on rvalue reference and const lvalue reference + // so for now we just implement a single version that copies itself + virtual Quote* clone() const {return new Quote(*this);} +#endif +private: + std::string bookNo; // ISBN number of this item +protected: + double price = 0.0; // normal, undiscounted price +}; + +// abstract base class to hold the discount rate and quantity +// derived classes will implement pricing strategies using these data +class Disc_quote : public Quote { +public: + // other members as before + Disc_quote() = default; + Disc_quote(const std::string& book, double price, + std::size_t qty, double disc): + Quote(book, price), + quantity(qty), discount(disc) { } + + double net_price(std::size_t) const = 0; + + std::pair discount_policy() const + { return {quantity, discount}; } +protected: + std::size_t quantity = 0; // purchase size for the discount to apply + double discount = 0.0; // fractional discount to apply +}; + +// the discount kicks in when a specified number of copies of the same book are sold +// the discount is expressed as a fraction used to reduce the normal price + +class Bulk_quote : public Disc_quote { // Bulk_quote inherits from Quote +public: + Bulk_quote() = default; + Bulk_quote(const std::string& book, double p, + std::size_t qty, double disc) : + Disc_quote(book, p, qty, disc) { } + + // overrides the base version in order to implement the bulk purchase discount policy + double net_price(std::size_t) const override; + +#ifdef REFMEMS + Bulk_quote* clone() const & {return new Bulk_quote(*this);} + Bulk_quote* clone() && {return new Bulk_quote(std::move(*this));} +#else + Bulk_quote* clone() const {return new Bulk_quote(*this);} +#endif +}; + +// discount (a fraction off list) for only a specified number of copies, +// additional copies sold at standard price +class Lim_quote : public Disc_quote { +public: + Lim_quote(const std::string& book = "", + double sales_price = 0.0, + std::size_t qty = 0, double disc_rate = 0.0): + Disc_quote(book, sales_price, qty, disc_rate) { } + + // overrides base version so as to implement limited discount policy + double net_price(std::size_t) const; + +#ifdef REFMEMS + Lim_quote* clone() const & { return new Lim_quote(*this); } + Lim_quote* clone() && { return new Lim_quote(std::move(*this)); } +#else + Lim_quote* clone() const { return new Lim_quote(*this); } +#endif +}; + +double print_total(std::ostream &, const Quote&, std::size_t); + +#endif + diff --git a/15/README b/15/README new file mode 100644 index 0000000..970d20c --- /dev/null +++ b/15/README @@ -0,0 +1,15 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + storyDatafile andQueryTest + storyDatafile and_orQueryTest + storyDatafile wordQueryTest + two andQueryTest + three and_orQueryTest + querymain wordQueryTest + +Programs not listed above print output and do +not read any input + diff --git a/15/andQueryTest.cc b/15/andQueryTest.cc new file mode 100644 index 0000000..6e7a3a7 --- /dev/null +++ b/15/andQueryTest.cc @@ -0,0 +1,71 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Query.h" +#include "TextQuery.h" + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::set; + +int main(int argc, char **argv) +{ + // gets file to read and builds map to support queries + TextQuery file = get_file(argc, argv); + + do { + string sought1, sought2; + // stop if hit eof on input or a "q" is entered + if (!get_words(sought1, sought2)) break; + + // find all the occurrences of the requested string + Query andq = Query(sought1) & Query(sought2); + cout << "\nExecuting query for: " << andq << endl; + auto results = andq.eval(file); + // report matches + print(cout, results); + + results = Query(sought1).eval(file); + cout << "\nExecuted query: " << Query(sought1) << endl; + // report matches + print(cout, results); + + results = Query(sought2).eval(file); + cout << "\nExecuted query: " << Query(sought2) << endl; + // report matches + print(cout, results); + } while(true); + + return 0; +} diff --git a/15/and_orQueryTest.cc b/15/and_orQueryTest.cc new file mode 100644 index 0000000..41d5c44 --- /dev/null +++ b/15/and_orQueryTest.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Query.h" +#include "TextQuery.h" +#include +#include +#include + +using std::set; +using std::string; +using std::cin; using std::cout; using std::cerr; +using std::endl; + +int main(int argc, char **argv) +{ + // gets file to read and builds map to support queries + TextQuery file = get_file(argc, argv); + + // iterate with the user: prompt for a word to find and print results + while (true) { + string sought1, sought2, sought3; + if (!get_words(sought1, sought2)) break; + cout << "\nenter third word: " ; + cin >> sought3; + // find all the occurrences of the requested string + Query q = Query(sought1) & Query(sought2) + | Query(sought3); + cout << "\nExecuting Query for: " << q << endl; + const auto results = q.eval(file); + // report matches + print(cout, results); + } + return 0; +} diff --git a/15/data/querymain b/15/data/querymain new file mode 100644 index 0000000..d503daf --- /dev/null +++ b/15/data/querymain @@ -0,0 +1,3 @@ +Alice +container +Daddy diff --git a/15/data/storyDataFile b/15/data/storyDataFile new file mode 100644 index 0000000..1f06b36 --- /dev/null +++ b/15/data/storyDataFile @@ -0,0 +1,10 @@ +Alice Emma has long flowing red hair. +Her Daddy says when the wind blows +through her hair, it looks almost alive, +like a fiery bird in flight. +A beautiful fiery bird, he tells her, +magical but untamed. +"Daddy, shush, there is no such thing," +she tells him, at the same time wanting +him to tell her more. +Shyly, she asks, "I mean, Daddy, is there?" diff --git a/15/data/three b/15/data/three new file mode 100644 index 0000000..0c2b68c --- /dev/null +++ b/15/data/three @@ -0,0 +1,3 @@ +fiery +bird +wind diff --git a/15/data/two b/15/data/two new file mode 100644 index 0000000..59532a6 --- /dev/null +++ b/15/data/two @@ -0,0 +1,2 @@ +hair +Alice diff --git a/15/get_print.cc b/15/get_print.cc new file mode 100644 index 0000000..fa89d48 --- /dev/null +++ b/15/get_print.cc @@ -0,0 +1,79 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Query.h" +#include "TextQuery.h" + +#include +using std::string; + +#include +using std::cout; using std::cin; + +#include +using std::ifstream; + +#include +using std::runtime_error; + +// these functions are declared in Query.h +TextQuery get_file(int argc, char **argv) +{ + // get a file to read from which user will query words + ifstream infile; + if (argc == 2) + infile.open(argv[1]); + if (!infile) { + throw runtime_error("No input file!"); + } + + return TextQuery(infile); // builds query map +} + +bool get_word(string &s1) +{ + cout << "enter a word to search for, or q to quit: "; + cin >> s1; + if (!cin || s1 == "q") return false; + else return true; +} + +bool get_words(string &s1, string &s2) +{ + + // iterate with the user: prompt for a word to find and print results + cout << "enter two words to search for, or q to quit: "; + cin >> s1; + + // stop if hit eof on input or a "q" is entered + if (!cin || s1 == "q") return false; + cin >> s2; + return true; +} + diff --git a/15/makefile b/15/makefile new file mode 100644 index 0000000..69ca0e5 --- /dev/null +++ b/15/makefile @@ -0,0 +1,44 @@ +# executable files for this directory +OBJECTS = andQueryTest.exe and_orQueryTest.exe \ + Basket_main.exe wordQueryTest.exe \ + sample.exe scope.exe useQuote.exe \ + vecBasket.exe virtualsNscope.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../12 -I../6 + +sample.o useQuote.o Quote.o: Quote.h +vecBasket.o Basket_main.o Basket.o: Basket.h Quote.h + +get_print.o andQueryTest.o and_orQueryTest.o \ +wordQueryTest.o Query.o: Query.h ../12/TextQuery.h + +wordQueryTest.exe: Query.o wordQueryTest.o ../12/TextQuery.o get_print.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Query.o wordQueryTest.o \ + ../12/TextQuery.o get_print.o -o wordQueryTest.exe + +andQueryTest.exe: Query.o andQueryTest.o ../12/TextQuery.o get_print.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Query.o andQueryTest.o \ + ../12/TextQuery.o get_print.o -o andQueryTest.exe + +and_orQueryTest.exe: Query.o and_orQueryTest.o ../12/TextQuery.o get_print.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Query.o and_orQueryTest.o \ + ../12/TextQuery.o get_print.o -o and_orQueryTest.exe + +useQuote.exe: useQuote.o Quote.o + $(CC) $(CCFLAGS) $(LOCFLAGS) useQuote.o Quote.o -o useQuote.exe + +vecBasket.exe: Basket.o vecBasket.o Quote.o + $(CC) $(CCFLAGS) $(LOCFLAGS) vecBasket.o Basket.o Quote.o \ + -o vecBasket.exe + +Basket_main.exe: Basket.o Basket_main.o Quote.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Basket_main.o Basket.o Quote.o \ + -o Basket_main.exe + +sample.exe: sample.o Quote.o + $(CC) $(CCFLAGS) $(LOCFLAGS) sample.o Quote.o -o sample.exe + diff --git a/15/runpgms b/15/runpgms new file mode 100644 index 0000000..bc9bd8e --- /dev/null +++ b/15/runpgms @@ -0,0 +1,7 @@ +echo "andQueryTest: " && ./andQueryTest.exe data/storyDataFile < data/two +echo "and_orQueryTest: " && ./and_orQueryTest.exe data/storyDataFile < data/three +echo "wordQueryTest: " && ./wordQueryTest.exe data/storyDataFile < data/querymain + +../noinput Basket_main.exe sample.exe \ + scope.exe useQuote.exe vecBasket.exe \ + virtualsNscope.exe diff --git a/15/sample.cc b/15/sample.cc new file mode 100644 index 0000000..1e65cdf --- /dev/null +++ b/15/sample.cc @@ -0,0 +1,64 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +using std::size_t; + +using std::cout; +using std::endl; +#include "Quote.h" + +int main() +{ + Quote basic("0-201-54848-8", 45); + Bulk_quote bulk("0-201-82470-1", 45, 3, .15); + + // basic has type Quote; bulk has type Bulk_quote + print_total(cout, basic, 20); // calls Quote version of net_price + print_total(cout, bulk, 20); // calls Bulk_quote version of net_price + Quote base("0-201-82470-1", 50); + Bulk_quote derived("0-201-82470-1", 50, 5, .19); + cout << derived.net_price(20); // calls Bulk_quote::net_price + cout << endl; + + base = derived; // copies the Quote part of derived into base + cout << base.net_price(20); // calls Quote::net_price + cout << endl; + + Quote &item = derived; // dynamic and static types of item differ + cout << item.net_price(20); // calls Bulk_quote::net_price + + item.isbn(); // isbn is not virtual, calls Bulk::isbn + cout << endl; + + return 0; +} + + diff --git a/15/scope.cc b/15/scope.cc new file mode 100644 index 0000000..4465c15 --- /dev/null +++ b/15/scope.cc @@ -0,0 +1,55 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +struct Base { + Base(): mem(0) { } +protected: + int mem; +}; + +struct Derived : Base { + Derived(int i): mem(i) { } // initializes Derived::mem to i + // Base::mem is default initialized + int get_mem() { return mem; } // returns Derived::mem + int get_base_mem() { return Base::mem; } + // . . . +protected: + int mem; // hides mem in the base +}; + +int main() +{ + Derived d(42); + cout << d.get_mem() << endl; // prints 42 + cout << d.get_base_mem() << endl; // prints 0 +} + diff --git a/15/useQuote.cc b/15/useQuote.cc new file mode 100644 index 0000000..fcd231c --- /dev/null +++ b/15/useQuote.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::ostream; + +#include "Quote.h" + +int main() +{ + Quote base("0-201-82470-1", 50); + print_total(cout, base, 10); // calls Quote::net_price + + Bulk_quote derived("0-201-82470-1", 50, 5, .19); + print_total(cout, derived, 10); // calls Bulk_quote::net_price + + Quote *baseP = &derived; + + // calls the version from the base class + // regardless of the dynamic type of baseP + double undiscounted = baseP->Quote::net_price(42); +} diff --git a/15/vecBasket.cc b/15/vecBasket.cc new file mode 100644 index 0000000..b57d58c --- /dev/null +++ b/15/vecBasket.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::shared_ptr; using std::make_shared; + +#include "Quote.h" + +int main () +{ + Quote base("0-201-82470-1", 50); + Bulk_quote bulk("0-201-54848-8", 50, 10, .25); + + // ok: but probably not what is wanted--- + // the objects in basket have no derived members + vector basket; + basket.push_back(Quote("0-201-82470-1", 50)); + + // ok, but copies only the Quote part of the object into basket + basket.push_back(Bulk_quote("0-201-54848-8", 50, 10, .25)); + + // calls version defined by Quote, prints 750, i.e., 15 * $50 + cout << basket.back().net_price(15) << endl; + + // better approach---store shared_ptrs + vector> basket2; + + basket2.push_back(make_shared("0-201-82470-1", 50)); + basket2.push_back( + make_shared("0-201-54848-8", 50, 10, .25)); + + // calls the version defined by Quote; + // prints 562.5, i.e., 15 * $50 less the discount + cout << basket2.back()->net_price(15) << endl; + + return 0; +} diff --git a/15/virtualsNscope.cc b/15/virtualsNscope.cc new file mode 100644 index 0000000..92436ec --- /dev/null +++ b/15/virtualsNscope.cc @@ -0,0 +1,85 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +class Base { +public: + virtual int fcn(); +}; + +int Base::fcn() { cout << "Base::fcn()" << endl; return 0; } + +class D1 : public Base { +public: + // hides fcn in the base; this fcn is not virtual + // D1 inherits the definition of Base::fcn() + int fcn(int); // parameter list differs from fcn in Base + virtual void f2(); // new virtual function that does not exist in Base +}; + +int D1::fcn(int) { cout << "D1::fcn(int)" << endl; return 0; } +void D1::f2() { cout << "D1::f2()" << endl; } + +class D2 final : public D1 { +public: + int fcn(int); // nonvirtual function hides D1::fcn(int) + int fcn(); // overrides virtual fcn from Base + void f2(); // overrides virtual f2 from D1 +}; + +int D2::fcn(int) { cout << "D2::fcn(int)" << endl; return 0; } +int D2::fcn() { cout << "D2::fcn()" << endl; return 0; } +void D2::f2() { cout << "D2::f2()" << endl; } + +int main() +{ + D1 dobj, *dp = &dobj; + dp->fcn(42); // ok: static call to D1::fcn(int) + + Base bobj; D1 d1obj; D2 d2obj; + + Base *bp1 = &bobj, *bp2 = &d1obj, *bp3 = &d2obj; + bp1->fcn(); // virtual call, will call Base::fcn at run time + bp2->fcn(); // virtual call, will call Base::fcn at run time + bp3->fcn(); // virtual call, will call D2::fcn at run time + + D1 *d1p = &d1obj; D2 *d2p = &d2obj; + d1p->f2(); // virtual call, will call D1::f2() at run time + d2p->f2(); // virtual call, will call D2::f2() at run time + Base *p1 = &d2obj; D1 *p2 = &d2obj; D2 *p3 = &d2obj; + p2->fcn(42); // statically bound, calls D1::fcn(int) + p3->fcn(42); // statically bound, calls D2::fcn(int) + + Base* bp = &d1obj; D1 *dp1 = &d2obj; D2 *dp2 = &d2obj; + dp1->fcn(10); // static call to D1::fcn(int) + dp2->fcn(10); // static call to D2::fcn(int) +} + diff --git a/15/wordQueryTest.cc b/15/wordQueryTest.cc new file mode 100644 index 0000000..ecd5f4c --- /dev/null +++ b/15/wordQueryTest.cc @@ -0,0 +1,70 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Query.h" +#include "TextQuery.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using std::set; +using std::string; +using std::map; +using std::vector; +using std::cerr; +using std::cout; +using std::cin; +using std::ifstream; +using std::endl; + +int main(int argc, char **argv) +{ + TextQuery file = get_file(argc, argv); + + // iterate with the user: prompt for a word to find and print results + do { + string sought; + if (!get_word(sought)) break; + + // find all the occurrences of the requested string + // define synonym for the line_no set + Query name(sought); + const auto results = name.eval(file); + cout << "\nExecuting Query for: " << name << endl; + + // report no matches + print(cout, results) << endl; + } while (true); // loop indefinitely; the exit is inside the loop + return 0; +} diff --git a/16/Array.cc b/16/Array.cc new file mode 100644 index 0000000..c1d9f84 --- /dev/null +++ b/16/Array.cc @@ -0,0 +1,103 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::begin; using std::end; + +#include +using std::list; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::initializer_list; + +#include +using std::size_t; + +#include "Blob.h" + +int main() +{ + Blob ia; // empty Blob + Blob ia2 = {0,1,2,3,4}; // Blob with five elements + vector v1(10, 0); // ten elements initialized to 0 + Blob ia3(v1.begin(), v1.end()); // copy elements from v1 + cout << ia << "\n" << ia2 << "\n" << ia3 << endl; + + // these definitions instantiate two distinct Blob types + Blob names; // Blob that holds strings + Blob prices;// different element type + + // instantiates Blob class and its + // initializer_list constructor + Blob articles = {"a", "an", "the"}; // three elements + + // instantiates Blob and the initializer_list constructor + Blob squares = {0,1,2,3,4,5,6,7,8,9}; + + // instantiates Blob::size() const + cout << squares << endl; + for (size_t i = 0; i != squares.size(); ++i) + squares[i] = i*i; // instantiates Blob::operator[](size_t) + cout << squares << endl; + + // instantiates the Blob constructor that has + // two vector::iterator parameters + vector vl = {0,1,2,3,4,5,6,7,8,9}; + Blob a1(vl.begin(), vl.end()); // copy from a vector + + // instantiates the Blob class + // and the Blob constructor that has two int* parameters + int arr[] = {0,1,2,3,4,5,6,7,8,9}; + Blob a2(begin(arr), end(arr)); // copy from an array + + list li(10, 0); // 10 elements all zeros + Blob zeros(li.begin(), li.end()); // copy from a list + + cout << a1 << "\n" << zeros << endl; + + a1.swap(zeros); + cout << a1 << "\n" << zeros << endl; + + list w = {"now", "is", "the", "time"}; + + // instantiates the Blob class and the Blob + // constructor that has two (list::iterator parameters + Blob a3(w.begin(), w.end()); // copy from a list + + return 0; +} diff --git a/16/Blob.h b/16/Blob.h new file mode 100644 index 0000000..265974e --- /dev/null +++ b/16/Blob.h @@ -0,0 +1,333 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef BLOB_H +#define BLOB_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// forward declarations needed for friend declarations in Blob +template class BlobPtr; +template class Blob; // needed for parameters in operator== +template + bool operator==(const Blob&, const Blob&); + +template class Blob { + // each instantiation of Blob grants access to the version of + // BlobPtr and the equality operator instantiated with the same type + friend class BlobPtr; + friend bool operator== + (const Blob&, const Blob&); +public: + typedef T value_type; + typedef typename std::vector::size_type size_type; + + // constructors + Blob(); + Blob(std::initializer_list il); + template Blob(It b, It e); + Blob(T*, std::size_t); + + // return BlobPtr to the first and one past the last elements + BlobPtr begin() { return BlobPtr(*this); } + BlobPtr end() + { auto ret = BlobPtr(*this, data->size()); + return ret; } + + // number of elements in the Blob + size_type size() const { return data->size(); } + bool empty() const { return data->empty(); } + + // add and remove elements + void push_back(const T &t) {data->push_back(t);} + void push_back(T &&t) { data->push_back(std::move(t)); } + void pop_back(); + + // element access + T& front(); + T& back(); + T& at(size_type); + const T& back() const; + const T& front() const; + const T& at(size_type) const; + T& operator[](size_type i); + const T& operator[](size_type i) const; + + void swap(Blob &b) { data.swap(b.data); } +private: + std::shared_ptr> data; + + // throws msg if data[i] isn't valid + void check(size_type i, const std::string &msg) const; +}; + +// constructors +template +Blob::Blob(T *p, std::size_t n): + data(std::make_shared>(p, p + n)) { } + +template +Blob::Blob(): + data(std::make_shared>()) { } + +template // type parameter for the class +template // type parameter for the constructor + Blob::Blob(It b, It e): + data(std::make_shared>(b, e)) { } + +template +Blob::Blob(std::initializer_list il): + data(std::make_shared>(il)) { } + +// check member +template +void Blob::check(size_type i, const std::string &msg) const +{ + if (i >= data->size()) + throw std::out_of_range(msg); +} + +// element access members +template +T& Blob::front() +{ + // if the vector is empty, check will throw + check(0, "front on empty Blob"); + return data->front(); +} + +template +T& Blob::back() +{ + check(0, "back on empty Blob"); + return data->back(); +} + +template void Blob::pop_back() +{ + check(0, "pop_back on empty Blob"); + data->pop_back(); +} + +template +const T& Blob::front() const +{ + check(0, "front on empty Blob"); + return data->front(); +} + +template +const T& Blob::back() const +{ + check(0, "back on empty Blob"); + return data->back(); +} + +template +T& Blob::at(size_type i) +{ + // if i is too big, check will throw, preventing access to a nonexistent element + check(i, "subscript out of range"); + return (*data)[i]; // (*data) is the vector to which this object points +} + +template +const T& +Blob::at(size_type i) const +{ + check(i, "subscript out of range"); + return (*data)[i]; +} + +template +T& Blob::operator[](size_type i) +{ + // if i is too big, check will throw, preventing access to a nonexistent element + check(i, "subscript out of range"); + return (*data)[i]; +} + +template +const T& +Blob::operator[](size_type i) const +{ + check(i, "subscript out of range"); + return (*data)[i]; +} + +// operators +template +std::ostream& +operator<<(std::ostream &os, const Blob a) +{ + os << "< "; + for (size_t i = 0; i < a.size(); ++i) + os << a[i] << " "; + os << " >"; + return os; +} + +template +bool +operator==(const Blob lhs, const Blob rhs) +{ + if (rhs.size() != lhs.size()) + return false; + for (size_t i = 0; i < lhs.size(); ++i) { + if (lhs[i] != rhs[i]) + return false; + } + return true; +} + +// BlobPtr throws an exception on attempts to access a nonexistent element +template +bool operator==(const BlobPtr&, const BlobPtr&); + +template class BlobPtr : public std::iterator { + friend bool + operator==(const BlobPtr&, const BlobPtr&); +public: + BlobPtr(): curr(0) { } + BlobPtr(Blob &a, size_t sz = 0): + wptr(a.data), curr(sz) { } + + T &operator[](std::size_t i) + { auto p = check(i, "subscript out of range"); + return (*p)[i]; // (*p) is the vector to which this object points + } + + const T &operator[](std::size_t i) const + { auto p = check(i, "subscript out of range"); + return (*p)[i]; // (*p) is the vector to which this object points + } + + T& operator*() const + { auto p = check(curr, "dereference past end"); + return (*p)[curr]; // (*p) is the vector to which this object points + } + T* operator->() const + { // delegate the real work to the dereference operator + return & this->operator*(); + } + + // increment and decrement + BlobPtr& operator++(); // prefix operators + BlobPtr& operator--(); + + BlobPtr operator++(int); // postfix operators + BlobPtr operator--(int); + +private: + // check returns a shared_ptr to the vector if the check succeeds + std::shared_ptr> + check(std::size_t, const std::string&) const; + + // store a weak_ptr, which means the underlying vector might be destroyed + std::weak_ptr> wptr; + std::size_t curr; // current position within the array +}; + +// equality operators +template +bool operator==(const BlobPtr &lhs, const BlobPtr &rhs) +{ + return lhs.wptr.lock().get() == rhs.wptr.lock().get() && + lhs.curr == rhs.curr; +} + +template +bool operator!=(const BlobPtr &lhs, const BlobPtr &rhs) +{ + return !(lhs == rhs); +} + +// check member +template +std::shared_ptr> +BlobPtr::check(std::size_t i, const std::string &msg) const +{ + auto ret = wptr.lock(); // is the vector still around? + if (!ret) + throw std::runtime_error("unbound BlobPtr"); + if (i >= ret->size()) + throw std::out_of_range(msg); + return ret; // otherwise, return a shared_ptr to the vector +} + +// member operators +// postfix: increment/decrement the object but return the unchanged value +template +BlobPtr BlobPtr::operator++(int) +{ + // no check needed here; the call to prefix increment will do the check + BlobPtr ret = *this; // save the current value + ++*this; // advance one element; prefix ++ checks the increment + return ret; // return the saved state +} + +template +BlobPtr BlobPtr::operator--(int) +{ + // no check needed here; the call to prefix decrement will do the check + BlobPtr ret = *this; // save the current value + --*this; // move backward one element; prefix -- checks the decrement + return ret; // return the saved state +} + +// prefix: return a reference to the incremented/decremented object +template +BlobPtr& BlobPtr::operator++() +{ + // if curr already points past the end of the container, can't increment it + check(curr, "increment past end of BlobPtr"); + ++curr; // advance the current state + return *this; +} + +template +BlobPtr& BlobPtr::operator--() +{ + // if curr is zero, decrementing it will yield an invalid subscript + --curr; // move the current state back one element + check(-1, "decrement past begin of BlobPtr"); + return *this; +} +#endif diff --git a/16/DebugDelete.h b/16/DebugDelete.h new file mode 100644 index 0000000..93431c3 --- /dev/null +++ b/16/DebugDelete.h @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef DEBUGDELETE_H +#define DEBUGDELETE_H + +#include +#include +#include + +// function-object class that calls delete on a given pointer +class DebugDelete { +public: + DebugDelete(const std::string &s = "unique_ptr", + std::ostream &strm = std::cerr): os(strm), type(s) { } + // as with any function template, the type of T is deduced by the compiler + template void operator()(T *p) const + { os << "deleting " << type << std::endl; delete p; } +private: + std::ostream &os; // where to print debugging info + std::string type; // what type of smart pointer we're deleting +}; + +#endif diff --git a/16/README b/16/README new file mode 100644 index 0000000..830368d --- /dev/null +++ b/16/README @@ -0,0 +1,12 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + book_sales compareDef + book_trans multiset + Vecmainin Vecmain + +Programs not listed above print output and do +not read any input + diff --git a/16/SP.cc b/16/SP.cc new file mode 100644 index 0000000..3880413 --- /dev/null +++ b/16/SP.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::unique_ptr; using std::shared_ptr; + +// function-object class that calls delete on a given pointer +#include "DebugDelete.h" + +int main() +{ + double* p = new double; + // an object that can act like a delete expression + DebugDelete d("plain pointer"); + d(p); // calls DebugDelete::operator()(double*), which deletes p + + int* ip = new int; + // calls operator()(int*) on a temporary DebugDelete object + DebugDelete("plain pointer")(ip); + + // destroying the the object to which upi points + // instantiates DebugDelete::operator()(int *) + unique_ptr upi(new int, DebugDelete()); + + // destroying the the object to which ups points + // instantiates DebugDelete::operator()(string*) + unique_ptr ups(new string, DebugDelete()); + + // illustrate other types using DebugDelete as their deleter + shared_ptr sp1(new int(42), DebugDelete("shared_ptr")); + + return 0; +} + diff --git a/16/Sales_data.cc b/16/Sales_data.cc new file mode 100644 index 0000000..6fccd43 --- /dev/null +++ b/16/Sales_data.cc @@ -0,0 +1,118 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::istream; using std::ostream; + +#include "Sales_data.h" + +// define the hash interface for Sales_data + +namespace std { +size_t +hash::operator()(const Sales_data& s) const +{ + return hash()(s.bookNo) ^ + hash()(s.units_sold) ^ + hash()(s.revenue); +} +} // close the std namespace; note: no semicolon after the close curly + +// remaining members unchanged from chapter 14 +Sales_data::Sales_data(istream &is) +{ + is >> *this; // read a transaction from is into this object +} + +double Sales_data::avg_price() const +{ + if (units_sold) + return revenue/units_sold; + else + return 0; +} + +// member binary operator: left-hand operand is bound to the implicit this pointer +// assumes that both objects refer to the same book +Sales_data& Sales_data::operator+=(const Sales_data &rhs) +{ + units_sold += rhs.units_sold; + revenue += rhs.revenue; + return *this; +} + +// assumes that both objects refer to the same book +Sales_data +operator+(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum += rhs; // add rhs into sum + return sum; +} + +istream &operator>>(istream &is, Sales_data &item) +{ + double price; // no need to initialize; we'll read into price before we use it + is >> item.bookNo >> item.units_sold >> price; + if (is) // check that the inputs succeeded + item.revenue = item.units_sold * price; + else + item = Sales_data(); // input failed: give the object the default state + return is; +} + +ostream &operator<<(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} + +// operators replace these original named functions +istream &read(istream &is, Sales_data &item) +{ + double price = 0; + is >> item.bookNo >> item.units_sold >> price; + item.revenue = price * item.units_sold; + return is; +} +ostream &print(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} + +Sales_data add(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum += rhs; // add rhs into sum + return sum; +} + diff --git a/16/Sales_data.h b/16/Sales_data.h new file mode 100644 index 0000000..fc2182d --- /dev/null +++ b/16/Sales_data.h @@ -0,0 +1,101 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef SALES_DATA_H +#define SALES_DATA_H + +#include +#include + +// unchanged from chapter 14 except for added friend declaration for hash +class Sales_data { +friend class std::hash; +friend std::ostream &operator<< + (std::ostream&, const Sales_data&); +friend std::istream &operator>>(std::istream&, Sales_data&); +friend bool operator==(const Sales_data &, const Sales_data &); + +friend std::ostream &print(std::ostream&, const Sales_data&); +friend std::istream &read(std::istream&, Sales_data&); +public: + // constructors + Sales_data() = default; + Sales_data(const std::string &s): bookNo(s) { } + Sales_data(const std::string &s, unsigned n, double p): + bookNo(s), units_sold(n), revenue(p*n) { } + Sales_data(std::istream &); + std::string isbn() const { return bookNo; } + Sales_data& operator+=(const Sales_data&); +private: + double avg_price() const; + std::string bookNo; + unsigned units_sold = 0; + double revenue = 0.0; +}; + +namespace std { +template <> // we're defining a specialization with +struct hash // the template parameter of Sales_data +{ + // the type used to hash an unordered container must define these types + typedef size_t result_type; + typedef Sales_data argument_type; // by default, this type needs == + + size_t operator()(const Sales_data& s) const; + + // our class uses synthesized copy control and default constructor + // other members as before +}; +} // close the std namespace; note: no semicolon after the close curly + +// non-member Sales_data operations +inline +bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) +{ return lhs.isbn() < rhs.isbn(); } + +inline +bool operator==(const Sales_data &lhs, const Sales_data &rhs) +{ + return lhs.isbn() == rhs.isbn() && + lhs.units_sold == rhs.units_sold && + lhs.revenue == rhs.revenue; +} +inline +bool operator!=(const Sales_data &lhs, const Sales_data &rhs) +{ + return !(lhs == rhs); +} + +Sales_data add(const Sales_data&, const Sales_data&); +std::ostream &print(std::ostream&, const Sales_data&); +std::istream &read(std::istream&, Sales_data&); +Sales_data operator+(const Sales_data&, const Sales_data&); +std::ostream &operator<<(std::ostream&, const Sales_data&); +std::istream &operator>>(std::istream&, Sales_data&); +#endif diff --git a/16/Vec.h b/16/Vec.h new file mode 100644 index 0000000..3f0cd35 --- /dev/null +++ b/16/Vec.h @@ -0,0 +1,228 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef VEC_H +#define VEC_H + +#include +#include +#include +#include + +// simplified-implementation of memory allocation strategy for a vector-like class +template +class Vec { +public: + Vec() = default; + Vec(const Vec&); // copy constructor + Vec(Vec&&) noexcept; // move constructor + Vec &operator=(const Vec&); // copy assignment + Vec &operator=(Vec&&) noexcept; // move assignment + ~Vec() noexcept; // destructor + + // list assignment + Vec &operator=(std::initializer_list); + + // add elements + void push_back(const T&); + void push_back(T&&); + template void emplace_back(Args&&...); + + // size and capacity + size_t size() const { return first_free - elements; } + size_t capacity() const { return cap - elements; } + + // element access + T& operator[](size_t n) { return elements[n]; } + const T& operator[](size_t n) const { return elements[n]; } + + // iterator interface + T *begin() const { return elements; } + T *end() const { return first_free; } +private: + static std::allocator alloc; // allocates the elements + // used by functions that add elements to the Vec + void chk_n_alloc() { if (first_free == cap) reallocate(); } + + // utilities used by copy constructor, assignment operator, and destructor + std::pair + alloc_n_copy(const T*, const T*); + void free(); + void reallocate(); // get more space and copy existing elements + T* elements = nullptr; // pointer to first element in the array + T* first_free = nullptr; // pointer to first free element in the array + T* cap = nullptr; // pointer to one past the end of the array +}; + + +//definition for the static data member +template std::allocator Vec::alloc; + +template +inline +Vec::~Vec() noexcept { free(); } + +template +inline +std::pair +Vec::alloc_n_copy(const T *b, const T *e) +{ + auto data = alloc.allocate(e - b); + return {data, uninitialized_copy(b, e, data)}; +} + +template +inline +Vec::Vec(Vec &&s) noexcept : + // take over resources from s + elements(s.elements), first_free(s.first_free), cap(s.cap) +{ + // leave s in a state in which it is safe to run the destructor + s.elements = s.first_free = s.cap = nullptr; +} + +template +inline +Vec::Vec(const Vec &s) +{ + // call copy to allocate exactly as many elements as in s + auto newdata = alloc_n_copy(s.begin(), s.end()); + elements = newdata.first; + first_free = cap = newdata.second; +} + +template +inline +void Vec::free() +{ + // destroy the old elements in reverse order + for (auto p = first_free; p != elements; /* empty */) + alloc.destroy(--p); // destroy elements in reverse order + + // deallocate cannot be called on a 0 pointer + if (elements) + alloc.deallocate(elements, cap - elements); +} + +template +inline +Vec &Vec::operator=(std::initializer_list il) +{ + // copy allocates space and copies elements from the given range + auto data = alloc_n_copy(il.begin(), il.end()); + + free(); // destroy the elements in this object and free the space + + elements = data.first; // update data members to point to the new space + first_free = cap = data.second; + + return *this; +} + +template +inline +Vec &Vec::operator=(Vec &&rhs) noexcept +{ + // direct test for self-assignment + if (this != &rhs) + free(); // free existing elements if appropriate + + elements = rhs.elements; // take over resources from rhs + first_free = rhs.first_free; + cap = rhs.cap; + + // leave rhs in a destructible state + rhs.elements = rhs.first_free = rhs.cap = nullptr; + + return *this; +} + +template +inline +Vec &Vec::operator=(const Vec &rhs) +{ + // call copy to allocate exactly as many elements as in rhs + auto data = alloc_n_copy(rhs.begin(), rhs.end()); + free(); + elements = data.first; + first_free = cap = data.second; + return *this; +} + +template +inline +void Vec::reallocate() +{ + // we'll allocate space for twice as many elements as current size + auto newcapacity = size() ? 2 * size() : 2; + + // allocate new space + auto first = alloc.allocate(newcapacity); + auto dest = first; + auto elem = elements; + + // move the elements + for (size_t i = 0; i != size(); ++i) + alloc.construct(dest++, std::move(*elem++)); + free(); // free the old space once we've moved the elements + + // update our data structure point to the new elements + elements = first; + first_free = dest; + cap = elements + newcapacity; +} + +template +inline +void Vec::push_back(const T& s) +{ + chk_n_alloc(); // reallocates the Vec if necessary + // construct a copy s in the element to which first_free points + alloc.construct(first_free++, s); +} + +template +inline +void Vec::push_back(T&& s) +{ + chk_n_alloc(); // reallocates the Vec if necessary + alloc.construct(first_free++, std::move(s)); +} + +template +template +inline +void Vec::emplace_back(Args&&... args) +{ + // any space left? + chk_n_alloc(); // reallocates the Vec if necessary + alloc.construct(first_free++, std::forward(args)...); +} + +#endif diff --git a/16/Vecmain.cc b/16/Vecmain.cc new file mode 100644 index 0000000..482c8e0 --- /dev/null +++ b/16/Vecmain.cc @@ -0,0 +1,75 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Vec.h" + +#include +using std::string; + +#include +using std::cin; using std::cout; using std::endl; +using std::istream; + +void print(const Vec &svec) +{ + for (auto it : svec) + cout << it << " " ; + cout < getVec(istream &is) +{ + Vec svec; + string s; + while (is >> s) + svec.push_back(s); + return svec; +} + +int main() +{ + Vec svec = getVec(cin); + print(svec); + + cout << "copy " << svec.size() << endl; + auto svec2 = svec; + print(svec2); + + cout << "assign" << endl; + Vec svec3; + svec3 = svec2; + print(svec3); + + Vec v1, v2; + Vec getVec(istream &); + v1 = v2; // copy assignment + v2 = getVec(cin); // move assignment + + return 0; +} diff --git a/16/array-parms4.cc b/16/array-parms4.cc new file mode 100644 index 0000000..f0df1c2 --- /dev/null +++ b/16/array-parms4.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::size_t; + +// when we call print the compiler will instantiate a version of print +// with T replaced by the argument's element type, and N by that array's dimension +template +void print(T (&arr)[N]) +{ + for (auto elem : arr) + cout << elem << endl; +} + +int main() +{ + int a1[] = {0,1,2,3,4,5,6,7,8,9}; + int a2[] = {1,3,5}; + string a3[4]; + + print(a1); // instantiates print(int (&arr)[10]) + + print(a2); // instantiates print(int (&arr)[3]) + + print(a3); // instantiates print(string (&arr)[42]) + + return 0; +} diff --git a/16/build.cc b/16/build.cc new file mode 100644 index 0000000..ca5faa6 --- /dev/null +++ b/16/build.cc @@ -0,0 +1,41 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Blob.h" +#include "compare.h" + +#include +using std::string; + +// templateBuild.cc +// instantiation file must provide a (nonextern) definition for every +// type and function that other files declare as extern +template int compare(const int&, const int&); +template class Blob; // instantiates all members of the class template +template class Blob; // instantiates Blob diff --git a/16/compare.cc b/16/compare.cc new file mode 100644 index 0000000..8a5540d --- /dev/null +++ b/16/compare.cc @@ -0,0 +1,66 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::strcmp; + +#include +using std::cout; using std::endl; + +#include "compare.h" + +int main() +{ + // instantiates int compare(const int&, const int&) + cout << compare(1, 0) << endl; // T is int + + // instantiates int compare(const vector&, const vector&) + vector vec1{1, 2, 3}, vec2{4, 5, 6}; + cout << compare(vec1, vec2) << endl; // T is vector + + long l1, l2; + int i1, i2; + compare(i1, i2); // instantiate compare(int, int) + compare(l1, l2); // instantiate compare(long, long) + compare(i1, l2); // uses compare(int, int) + compare(i1, l2);// uses compare(long, long) + + const char *cp1 = "hi", *cp2 = "world"; + compare(cp1, cp2); // calls the specialization + compare(cp1, cp2); // converts arguments to string + + return 0; +} + diff --git a/16/compare.h b/16/compare.h new file mode 100644 index 0000000..0961292 --- /dev/null +++ b/16/compare.h @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef COMPARE_H +#define COMPARE_H +#include + +// implement strcmp-like generic compare function +// returns 0 if the values are equal, 1 if v1 is larger, -1 if v1 is smaller +template +int compare(const T &v1, const T &v2) +{ + if (v1 < v2) return -1; + if (v2 < v1) return 1; + return 0; +} + +// special version of compare to handle C-style character strings +template <> +inline +int compare(const char* const &v1, const char* const &v2) +{ + return std::strcmp(v1, v2); +} + +#endif diff --git a/16/compareDef.cc b/16/compareDef.cc new file mode 100644 index 0000000..af75a3b --- /dev/null +++ b/16/compareDef.cc @@ -0,0 +1,57 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" + +#include +using std::cout; using std::endl; using std::cin; + +#include +using std::less; using std::greater; + +// compare has a default template argument, less +// and a default function argument, F() +template > +int compare(const T &v1, const T &v2, F f = F()) +{ + if (f(v1, v2)) return -1; + if (f(v2, v1)) return 1; + return 0; +} + +int main() +{ + bool i = compare(0, 42); // uses less; i is -1 + + // result depends on the isbns in item1 and item2 + Sales_data item1(cin), item2(cin); + bool j = compare(item1, item2, compareIsbn); + + return 0; +} diff --git a/16/compareSpec.cc b/16/compareSpec.cc new file mode 100644 index 0000000..5f7cbd5 --- /dev/null +++ b/16/compareSpec.cc @@ -0,0 +1,83 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::less; + +#include +using std::strcmp; + +// version of compare that will be correct even if used on pointers +template int compare(const T &v1, const T &v2) +{ + cout << "compare(T)" << "\t"; + + if (less()(v1, v2)) return -1; + if (less()(v2, v1)) return 1; + + return 0; +} + +template +int compare(const char (&p1)[N], const char (&p2)[M]) +{ + cout << "compare(const char arrays)" << "\t"; + + return strcmp(p1, p2); +} + +template<> +int compare(const char *const& p1, const char *const& p2) +{ + cout << "compare(const char*const)" << "\t"; + + return strcmp(p1, p2); +} + +int main() +{ + int *p1 = new int(45); + int *p2 = new int(42); + + // because we're comparing pointer values, the result of + // this call may vary each time the program is run + cout << compare(*p1, *p2) << endl; + cout << compare(p1, p2) << endl; + + cout << strcmp("hi", "mom") << endl; + cout << compare("hi", "mom") << endl; + + const char *cp1 = "hi", *cp2= "mom"; + cout << compare(cp1, cp2) << endl; + + return 0; +} diff --git a/16/data/Vecmain b/16/data/Vecmain new file mode 100644 index 0000000..e440727 --- /dev/null +++ b/16/data/Vecmain @@ -0,0 +1,7 @@ +Although most programmers are familiar with data structures +such as |vector|s and |list|s, many have never used an +associative data structure. +Before we look at the details of how the library supports +these types, it will be helpful to start with +examples of how we can use +these containers. diff --git a/16/data/book_sales b/16/data/book_sales new file mode 100644 index 0000000..4224fa4 --- /dev/null +++ b/16/data/book_sales @@ -0,0 +1,8 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-999-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-999-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/16/data/book_trans b/16/data/book_trans new file mode 100644 index 0000000..5705960 --- /dev/null +++ b/16/data/book_trans @@ -0,0 +1,2 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 diff --git a/16/debug.cc b/16/debug.cc new file mode 100644 index 0000000..bc21220 --- /dev/null +++ b/16/debug.cc @@ -0,0 +1,77 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "debug_rep.h" + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + vector v = {1,2,3,4,5,6,7,8,9}; + string s("hi"); + cout << debug_rep(v) << endl; + cout << debug_rep(s) << endl; + cout << debug_rep("hi") << endl; + cout << debug_rep(&v[0]) << endl; + cout << debug_rep(&s) << endl; + const string *sp = &s; + cout << debug_rep(sp) << endl; + + char carr[] = "bye"; // calls pointer version if no overloads + cout << debug_rep(carr) << endl; + vector authors = {"Proust", "Shakespeare", "Barth"}; + vector authors2 = {"Proust", "Shakespeare", "Barth"}; + cout << debug_rep(authors) << endl; + cout << debug_rep(authors2) << endl; + cout << debug_rep(s) << endl; + s += "more stuff"; + cout << debug_rep(s) << endl; + s += "\\escape\"and quotes"; + cout << debug_rep(s) << endl; + + cout << debug_rep("hi world!") << endl; // calls debug_rep(T*) + + s = "hi"; + const char *cp = "bye"; + char arr[] = "world"; + + cout << debug_rep(s) << endl; // calls specialization debug_rep(const string& + cout << debug_rep(cp) << endl; // calls specialization debug_rep(const char* + cout << debug_rep(arr) << endl;// calls specialization debug_rep(char* + cout << debug_rep(&s) << endl; // calls template debug_rep(T*) + + return 0; +} diff --git a/16/debug_rep.h b/16/debug_rep.h new file mode 100644 index 0000000..757f409 --- /dev/null +++ b/16/debug_rep.h @@ -0,0 +1,151 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef DEBUG_REP_H +#define DEBUG_REP_H +#include +#include +#include +#include + +/* this file uses two preprocessor variables to control whether + * we use nontemplate functions or specializations for string + * data and to control whether we define versions of debug_rep + * that handle character pointers. + * + * SPECIALIZED is defined, then we define specializations + * for string, char*, and const char* and do not + * define nontemplate functions taking these types + * + * OVERCHAR only matters if SPECIALIZED is not defined. In this case + * if OVERCHAR is defined, we define one nontemplate function + * that takes a string; if OVERCHAR is not defined + * we also define nontemplate functions taking char* and + * const char* +*/ +#ifndef SPECIALIZED +std::string debug_rep(const std::string &s); +#ifndef OVERCHAR +std::string debug_rep(char *p); +std::string debug_rep(const char *cp); +#endif +#endif +// overloaded, not specialized, function templates +template std::string debug_rep(const T &t); +template std::string debug_rep(T *p); +template std::string debug_rep(T b, T e); +template std::string debug_rep(const std::vector &v); + +#ifdef SPECIALIZED +// specialized versions to handle strings and character pointers +// declarations for specializations should follow declarations for all overloaded templates +template <> std::string debug_rep(const std::string&); +template <> std::string debug_rep(const char*); +template <> std::string debug_rep(char*); +#endif + +// print any type we don't otherwise handle +template std::string debug_rep(const T &t) +{ +#ifdef DEBUG + std::cout << "const T&" << "\t"; +#endif + std::ostringstream ret; + ret << t; // uses T's output operator to print a representation of t + return ret.str(); // return a copy of the string to which ret is bound +} + +// print pointers as their pointer value +// followed by the object to which the pointer points +// NB: this function will not work properly with char* +template std::string debug_rep(T *p) +{ +#ifdef DEBUG + std::cout << "T*" << "\t"; +#endif + std::ostringstream ret; + ret << "pointer: " << p; // print the pointer's own value + if (p) + ret << " " << debug_rep(*p); // print the value to which p points + else + ret << " null pointer"; // or indicate that the p is null + return ret.str(); // return a copy of the string to which ret is bound +} +#ifndef SPECIALIZED +// print strings inside double quotes +std::string debug_rep(const std::string &s) +#else +template <> std::string debug_rep(const std::string &s) +#endif +{ +#ifdef DEBUG + std::cout << "const string &" << "\t"; +#endif + return '"' + s + '"'; +} + + +#ifndef OVERCHAR +// convert the character pointers to string and call the string version of debug_rep +std::string debug_rep(char *p) +{ + return debug_rep(std::string(p)); +} +std::string debug_rep(const char *p) +{ + return debug_rep(std::string(p)); +} +#endif +#ifdef SPECIALIZED +template<> std::string debug_rep(char *p) + { return debug_rep(std::string(p)); } +template <> std::string debug_rep(const char *cp) + { return debug_rep(std::string(cp)); } +#endif + +template std::string debug_rep(T b, T e) +{ + std::ostringstream ret; + for (T it = b; it != e; ++it) { + if (it != b) + ret << ","; // put comma before all but the first element + ret << debug_rep(*it); // print the element + } + return ret.str(); +} + +template std::string debug_rep(const std::vector &v) +{ + std::ostringstream ret; + ret << "vector: ["; + ret << debug_rep(v.begin(), v.end()); + ret << "]"; + return ret.str(); +} +#endif diff --git a/16/expansion.cc b/16/expansion.cc new file mode 100644 index 0000000..0936a3f --- /dev/null +++ b/16/expansion.cc @@ -0,0 +1,96 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +template +T accum(const T &t) +{ + return t; +} + +template +T accum(const T &t, Args... args) +{ + return t + accum(args...); +} + +// h adds its variadic arguments to the starting value of 42 +int h() +{ + return 42; // starting point for the accumulation +} + +template int h(int t, Args ... args) +{ + return t + h(args...); // sum of all the values in args plus 42 +} + +// produces the sum of up to 4 integral values +void f(int i, int j = 0, int k = 0, int l = 0) +{ + cout << i << " + " + << j << " + " + << k << " + " + << l << " = " + << i + j + k + l << endl; +} + +// expansion: applies the pattern to each member of the pack +// using a separator appropriate to the context +template void g(Args ... args) { + cout << sizeof...(Args) << endl; // number of type parameters + cout << sizeof...(args) << endl; // number of function parameters + // call f passing it the arguments from args + f(args...); // f(a1, a2, a3, ..., an) + + // call h passing it the arguments from args + cout << h(args...) << endl; // h(a1, a2, a3, ..., an) + + // the pattern is h(x), + // the expansion calls h on each argument in args + f(h(args) ...); // f(h(a1), h(a2), h(a3), ..., h(an)) + + // args is expanded in the call to h + f(h(args ...)); // f(h(a1, a2, a3, ..., an2) + + // pattern adds the argument value to result from calling h(5,6,7,8) + f(h(5,6,7,8 ) + args ...); // f(h(5,6,7,8) + a1, h(5,6,7,8) + a2, + // h(5,6,7,8) + a3, ..., h(5,6,7,8) + an) +} + +int main() +{ + cout << accum(1,2,3,4) << endl; + + g(1,2,3,4); + + return 0; +} diff --git a/16/flip.cc b/16/flip.cc new file mode 100644 index 0000000..ccef118 --- /dev/null +++ b/16/flip.cc @@ -0,0 +1,78 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +using std::cout; using std::endl; + +// template that takes a callable and two parameters +// and calls the given callable with the parameters ``flipped'' +template +void flip(F f, T1 &&t1, T2 &&t2) +{ + f(std::forward(t2), std::forward(t1)); +} + +void f(int v1, int &v2) // note v2 is a reference +{ + cout << v1 << " " << ++v2 << endl; +} + +void g(int &&i, int& j) +{ + cout << i << " " << j << endl; +} + +// flip1 is an incomplete implementation: top-level const and references are lost +template +void flip1(F f, T1 t1, T2 t2) +{ + f(t2, t1); +} + +template +void flip2(F f, T1 &&t1, T2 &&t2) +{ + f(t2, t1); +} + +int main() +{ + int i = 0, j = 0, k = 0, l = 0; + cout << i << " " << j << " " << k << " " << l << endl; + + f(42, i); // f changes its argument i + flip1(f, j, 42); // f called through flip1 leaves j unchanged + flip2(f, k, 42); // ok: k is changed + g(1, i); + flip(g, i, 42); // ok: rvalue-ness of the third argument is preserved + cout << i << " " << j << " " << k << " " << l << endl; + + return 0; +} diff --git a/16/makefile b/16/makefile new file mode 100644 index 0000000..53b05b5 --- /dev/null +++ b/16/makefile @@ -0,0 +1,50 @@ +# executable files for this directory +OBJECTS = Array.exe array-parms4.exe \ + compare.exe compareDef.exe compareSpec.exe \ + debug.exe expansion.exe flip.exe multiset.exe \ + overloaded.exe overSpec.exe qm.exe \ + SP.exe special.exe specialized.exe \ + trail-ret.exe typealias.exe \ + useBlob.exe useChcking.exe \ + variadic.exe variadic3.exe Vecmain.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = + +SP.o: DebugDelete.h + +compareDef.o multiset.o variadic.o variadic3.o Sales_data.o: Sales_data.h + +Vecmain.o: Vec.h + +compareDef.exe: compareDef.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) compareDef.o Sales_data.o \ + -o compareDef.exe + +variadic.exe: variadic.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) variadic.o Sales_data.o \ + -o variadic.exe + +variadic3.exe: variadic3.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) variadic3.o Sales_data.o \ + -o variadic3.exe + +multiset.exe: multiset.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) multiset.o Sales_data.o \ + -o multiset.exe + +qm.exe: qm.o build.o + $(CC) $(CCFLAGS) $(LOCFLAGS) qm.o build.o -o qm.exe + +specialized.exe: debug.cc + $(CC) -D SPECIALIZED $(CCFLAGS) $(LOCFLAGS) debug.cc -o specialized.exe + +overloaded.exe: debug.o + $(CC) -D OVERCHAR $(CCFLAGS) $(LOCFLAGS) debug.cc -o overloaded.exe + +debug.exe: debug.o + $(CC) -D DEBUG $(CCFLAGS) $(LOCFLAGS) debug.cc -o debug.exe + diff --git a/16/multiset.cc b/16/multiset.cc new file mode 100644 index 0000000..41c8683 --- /dev/null +++ b/16/multiset.cc @@ -0,0 +1,59 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::string; + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::unordered_multiset; + +#include + +#include "Sales_data.h" + +using std::hash; + +int main() +{ + // uses hash and Sales_data operator== + unordered_multiset SDset; + Sales_data item; + while (cin >> item) { + SDset.insert(item); + } + cout << SDset.size() << endl; + + return 0; +} diff --git a/16/overSpec.cc b/16/overSpec.cc new file mode 100644 index 0000000..2e2c431 --- /dev/null +++ b/16/overSpec.cc @@ -0,0 +1,76 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::strcmp; + +#include +using std::cout; using std::endl; + +// first version; can compare any two types +template int compare(const T&, const T&); + +// second version to handle string literals +template +int compare(const char (&)[N], const char (&)[M]); + +// specialized verson of the first template, handles character arrays +template <> +int compare(const char* const &, const char* const &); + +template int compare(const T& v1, const T& v2) +{ +cout << "base template" << endl; + if (v1 < v2) return -1; + if (v2 < v1) return 1; + return 0; +} + +template +int compare(const char (&p1)[N], const char (&p2)[M]) +{ +cout << "array template" << endl; + return strcmp(p1, p2); +} + + +// special version of compare to handle pointers to character arrays +template <> +int compare(const char* const &p1, const char* const &p2) +{ +cout << "specialized template" << endl; + return strcmp(p1, p2); +} + +int main() +{ + const char *p1 = "hi", *p2 = "mom"; + compare(p1, p2); // calls the first template + compare("hi", "mom"); // calls the template with two nontype parameters +} diff --git a/16/qm.cc b/16/qm.cc new file mode 100644 index 0000000..165e8e0 --- /dev/null +++ b/16/qm.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "compare.h" +#include "Blob.h" + +#include +using std::string; + +// Application.cc +// these template types must be instantiated elsewhere in the program +// instantion declaration and definition +extern template class Blob; +extern template int compare(const int&, const int&); + +int main() { + + Blob sa1, sa2; // instantiation will appear elsewhere + + // Blob and its initializer_list constructor + // are instantiated in this file + Blob a1 = {0,1,2,3,4,5,6,7,8,9}; + Blob a2(a1); // copy constructor instantiated in this file + + int i = compare(a1[0], a2[0]); // instantiation will appear elsewhere + + return 0; +} diff --git a/16/runpgms b/16/runpgms new file mode 100644 index 0000000..86c6019 --- /dev/null +++ b/16/runpgms @@ -0,0 +1,12 @@ +echo "compareDef: " && ./compareDef.exe < data/book_sales +echo "multiset: " && ./multiset.exe < data/book_trans +echo "Vecmain: " && ./Vecmain.exe < data/Vecmain + +../noinput Array.exe array-parms4.exe \ + compare.exe compareSpec.exe \ + debug.exe flip.exe expansion.exe \ + overloaded.exe overSpec.exe qm.exe \ + SP.exe special.exe specialized.exe \ + trail-ret.exe typealias.exe \ + useBlob.exe useChcking.exe \ + variadic.exe variadic3.exe diff --git a/16/special.cc b/16/special.cc new file mode 100644 index 0000000..82c8ff5 --- /dev/null +++ b/16/special.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +// declarations before definitions +template void f(T); +template void f(const T*); + +template void g(T); +template void g(T*); + +// definitions +template void f(T) { cout << "f(T)" << endl; } +template void f(const T*) +{ cout << "f(const T*)" << endl; } + +template void g(T) { cout << "g(T)" << endl; } +template void g(T*) { cout << "g(T*)" << endl; } + +int main() +{ + int i = 42; + f(i); // calls f(T), f(const T*) isn't viable + + f(&i); // calls f(T), which is an exact match, + // f(const T*) requires a conversion + + g(i); // calls g(T), g(T*) isn't viable + g(&i); // calls g(T*), both templates are viable, + // but g(T*) is more specialized + + int *p = &i; + const int ci = 0, *p2 = &ci; + g(42); g(p); g(ci); g(p2); + f(42); f(p); f(ci); f(p2); + + return 0; +} diff --git a/16/trail-ret.cc b/16/trail-ret.cc new file mode 100644 index 0000000..c90f946 --- /dev/null +++ b/16/trail-ret.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::remove_reference; + +#include +using std::vector; + +#include +using std::string; + +#include "Blob.h" + +// auto as function return type indicates we're using a trailing return type +// function that returns a reference to an element in the range +template +auto reffcn(It beg, It end) -> decltype(*beg) +{ + // process the range + return *beg; // return a copy of an element from the range +} + +// function that returns an element in the range by value +// must use typename to use a type member of a template parameter +template +auto valfcn(It beg, It end) -> + typename remove_reference::type +{ + // process the range + return *beg; // return a copy of an element from the range +} + +int main() +{ + vector vi = {1,2,3,4,5}; + Blob ca = { "hi", "bye" }; + + auto &i = reffcn(vi.begin(), vi.end()); // reffcn should return int& + auto &s = reffcn(ca.begin(), ca.end()); // reffcn should return string& + + vi = {1,2,3,4}; + for (auto i : vi) cout << i << " "; cout << endl; + auto &ref = reffcn(vi.begin(), vi.end()); // ref is int& + ref = 5; // changes the value of *beg to which ref refers + for (auto i : vi) cout << i << " "; cout << endl; + + auto val = valfcn(vi.begin(), vi.end()); // val is int + cout << val << endl; // will print 5 + + return 0; +} + diff --git a/16/typealias.cc b/16/typealias.cc new file mode 100644 index 0000000..8f396fc --- /dev/null +++ b/16/typealias.cc @@ -0,0 +1,56 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Blob.h" + +#include +using std::pair; + +#include +using std::string; + +typedef Blob StrBlob; + + +template using twin = pair; + +template using partNo = pair; + +int main() +{ + // authors is a pair + twin author("Mark", "Twain"); + + twin win_loss(2, 45); // win_loss is a pair + + typedef string Vehicle; + partNo car("volvo", 242); // car is a pair + + return 0; +} diff --git a/16/useBlob.cc b/16/useBlob.cc new file mode 100644 index 0000000..76b7e69 --- /dev/null +++ b/16/useBlob.cc @@ -0,0 +1,53 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include "Blob.h" + +int main() +{ + Blob b1; // empty Blob + cout << b1.size() << endl; + { // new scope + Blob b2 = {"a", "an", "the"}; + b1 = b2; // b1 and b2 share the same elements + b2.push_back("about"); + cout << b1.size() << " " << b2.size() << endl; + } // b2 is destroyed, but the elements it points to must not be destroyed + cout << b1.size() << endl; + for(auto p = b1.begin(); p != b1.end(); ++p) + cout << *p << endl; + + return 0; +} diff --git a/16/useChcking.cc b/16/useChcking.cc new file mode 100644 index 0000000..ce4b55d --- /dev/null +++ b/16/useChcking.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Blob.h" +#include +using std::string; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + vector v1(3, 43), v2(10); + Blob a1(v1.begin(), v1.end()), + a2 = {0,1,2,3,4,5,6,7,8,9}, + a3(v2.begin(), v2.end()); + + cout << a1 << "\n\n" << a2 << "\n\n" << a3 << endl; + + cout << "\ncopy" << "\n\n"; + Blob a5(a1); + cout << a5 << endl; + + cout << "\nassignment" << "\n\n"; + + a1 = a3; + cout << a1 << "\n\n" << a2 << "\n\n" << a3 << endl; + + cout << "\nelement assignment" << "\n\n"; + a1[0] = 42; + a1[a1.size() - 1] = 15; + cout << a1 << "\n\n" << a3 << endl; + + Blob s1 = {"hi", "bye", "now"}; + BlobPtr p(s1); // p points to the vector inside s1 + *p = "okay"; // assigns to the first element in s1 + cout << p->size() << endl; // prints 4, the size of the first element in s1 + cout << (*p).size() << endl; // equivalent to p->size() + + Blob s2{"one", "two", "three"}; + // run the string empty function in the first element in s2 + if (s2[0].empty()) + s2[0] = "empty"; // assign a new value to the first string in s2 + + cout << a1 << endl; + cout << a2 << endl; + a2.swap(a1); + cout << a1 << endl; + cout << a2 << endl; + + return 0; +} + diff --git a/16/variadic.cc b/16/variadic.cc new file mode 100644 index 0000000..84edca1 --- /dev/null +++ b/16/variadic.cc @@ -0,0 +1,98 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cerr; +using std::ostream; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::map; + +#include +using std::size_t; + +#include "Sales_data.h" +#include "debug_rep.h" + +// function to end the recursion and print the last element +template +ostream &print(ostream &os, const T &t) +{ + return os << t; // no separator after the last element in the pack +} + +template +ostream & +print(ostream &os, const T &t, const Args&... rest)//expand Args +{ + os << t << ", "; + return print(os, rest...); //expand rest +} + +// call debug_rep on each argument in the call to print +template +ostream &errorMsg(ostream &os, const Args&... rest) +{ + // print(os, debug_rep(a1), debug_rep(a2), ..., debug_rep(an) + return print(os, debug_rep(rest)...); +} + + +struct ErrorCode { + ErrorCode(size_t n = 0): e(n) { } + size_t e; + size_t num() const { return e; } + string lookup() const { return errors[e]; } + static map errors; +}; + +map +ErrorCode::errors = { {42, "some error"}, { 1024, "another error"} }; + +int main() +{ + Sales_data item("978-0590353403", 25, 15.99); + string fcnName("itemProcess"); + ErrorCode code(42); + string otherData("invalid ISBN"); + + errorMsg(cerr, fcnName, code.num(), otherData, "other", item); + cerr << endl; + + print(cerr, debug_rep(fcnName), debug_rep(code.num()), + debug_rep(otherData), debug_rep("otherData"), + debug_rep(item)); + cerr << endl; + + return 0; +} + diff --git a/16/variadic3.cc b/16/variadic3.cc new file mode 100644 index 0000000..471d568 --- /dev/null +++ b/16/variadic3.cc @@ -0,0 +1,122 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::initializer_list; + +#include +using std::ostream; using std::cout; using std::endl; + +#include +using std::min; + +#include +using std::string; + +#include "Sales_data.h" + +// function to end the recursion and print the last element +// this function must be declared before the variadic version of print is defined +template +ostream &print(ostream &os, const T &t) +{ + return os << t; // no separator after the last element in the pack +} + +// this version of print will be called for all but the last element in the pack + +template +ostream &print(ostream &os, const T &t, const Args&... rest) +{ + os << t << ", "; // print the first argument + return print(os, rest...); // recursive call; print the other arguments +} + +template bool bigger(const T &t, initializer_list il) +{ + if (il.size() == 0) + return false; + auto b = il.begin(); + while (b != il.end()) + if (t < *b) + return true; + else + ++b; + return false; +} + +// NB: elements in the list must all have the same type +template T min_elem(initializer_list il) +{ + if (il.size() == 0) + throw "empty"; + auto b = il.begin(); // we know there's at least one element in the list + T ret(*b++); // start off with the first element as the ``smallest'' + while (b != il.end()) // till we run out of elements + ret = min(ret, *b++); // update ret if we find a smaller value + return ret; // return the value we found +} + +// using variadic templates we can allow for conversions among the elements +// function to end the recursion, called when we have only one element left from the original pack +template T min_elem(const T &v) +{ + return v; +} + +template +T min_elem(T val, Back... back) +{ + // recursive call, "pops" the first element from back, which will be val in this call + T val2 = min_elem(back...); + + // requires that the types of val2 and val are comparable using < + return val < val2 ? val : val2; +} + +int main() +{ + // no braces, so calls variadic version of min_elem + cout << min_elem(1.0,2,3,4.5,0.0,5,6,7,8,9) << endl; + // calls min_element that takes a single argument + // of type initializer_list + cout << min_elem({1,2,3,4,0,5,6,7,8,9}) << endl; + + int i = 5; + cout << bigger(i, {1,2,3,4,0,5,6,7,8,9}) << endl; + + string s = "how now brown cow"; + print(cout, i, s, 42); // two parameters in the pack + print(cout, i, s); // one parameter in the pack + print(cout, i); // no parameters in the pack + cout << endl; + + return 0; +} + diff --git a/17/README b/17/README new file mode 100644 index 0000000..dc6a088 --- /dev/null +++ b/17/README @@ -0,0 +1,18 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + bits bits + game game + no-skipws no-skipws + skipws getc + skipws badgetc + skipws getput + skipws skipws + store1,store2, + store3,store4 findbook + +Programs not listed above print output and do +not read any input + diff --git a/17/REerrs.cc b/17/REerrs.cc new file mode 100644 index 0000000..358348a --- /dev/null +++ b/17/REerrs.cc @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::cout; using std::endl; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; +using std::regex_error; + +int main() +{ + try { + // missing close bracket after alnum; the constructor will throw + regex r("[[:alnum:]+\\.(cpp|cxx|cc)$", regex::icase); + } catch (regex_error e) + { cout << e.what() << "\ncode: " << e.code() << endl; } + + return 0; +} +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/17/badgetc.cc b/17/badgetc.cc new file mode 100644 index 0000000..65cebba --- /dev/null +++ b/17/badgetc.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include // for EOF +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + char ch; // using a char here invites disaster! + + // the return from cin.get is converted to char + // and then compared to an int + while ((ch = cin.get()) != EOF) + cout.put(ch); + cout << endl; + + return 0; +} diff --git a/17/bits.cc b/17/bits.cc new file mode 100644 index 0000000..17378af --- /dev/null +++ b/17/bits.cc @@ -0,0 +1,181 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include +using std::cout; using std::cin; +using std::endl; +using std::string; +using std::size_t; + +#include +using std::bitset; +int main() +{ + bitset<32> bitvec(1U); // 32 bits; low-order bit is 1, + // remaining bits are 0 + bool is_set = bitvec.any(); // true, one bit is set + bool is_not_set = bitvec.none(); // false, one bit is set + bool all_set = bitvec.all(); // false, only one bit is set + size_t onBits = bitvec.count(); // returns 1 + size_t sz = bitvec.size(); // returns 32 + + bitvec.flip(); // reverses the value of all the bits in bitvec + bitvec.reset(); // sets all the bits to 0 + bitvec.set(); // sets all the bits to 1 + + cout << "bitvec: " << bitvec << endl; + + sz = bitvec.size(); // returns 32 + + onBits = bitvec.count(); // returns 1, + // i.e., the number of bits that are on + + // assign 1 to even numbered bits + for (int index = 0; index != 32; index += 2) + bitvec[index] = 1; + + // equivalent loop using set operation + for (int index = 0; index != 32; index += 2) + bitvec.set(index); + + // bitvec is unchanged + auto b2 = ~bitvec; // b2 is a copy of bitvec with every bit flipped + + // assign value of last bit in bitvec to the first bit in b2 + b2[0] = bitvec[bitvec.size() - 1]; + bitvec[0] = 0; // turn off the bit at position 0 + bitvec[31] = bitvec[0]; // give last bit the same value as the first + bitvec[0].flip(); // flip the value of the bit at position 0 + ~bitvec[0]; // equivalent; flips the bit at position 0 + bool b = bitvec[0]; // convert the value of bitvec[0] to bool + b2[0] = ~bitvec[0]; // first bit in b2 has the opposite value + // of the first bit in bitvec + + unsigned i = 0; + if (bitvec.test(i)) + // bitvec[i] is on + ; + + //equivalent test using subscript + if (bitvec[i]) + // bitvec[i] is on + ; + + cout << "bitvec: positions turned on:\n\t"; + for (int index = 0; index != 32; ++index) + if (bitvec[index]) + cout << index << " "; + cout << endl; + + // equivalent; turn off first bit + bitvec.flip(0); // reverses the value of the first bit + bitvec.set(bitvec.size() - 1); // turns on the last bit + bitvec.set(0, 0); // turns off the first bit + bitvec.reset(i); // turns off the ith bit + bitvec.test(0); // returns false because the first bit is off + bitvec[0] = 0; + + bitvec.flip(0); // reverses value of first bit + bitvec[0].flip(); // also reverses the first bit + + cout << "new inits" < bits13(0xbeef); // bits are 1111011101111 + + // bits20 is larger than the initializer; + // high-order bits in bits20 are set to zero + bitset<20> bits20(0xbeef); // bits are 00001011111011101111 + + // on machines with 64-bit long long 0ULL is 64 bits of 0, + // so ~0ULL is 64 ones, so 0 ... 63 are one, and 64 ... 127 are zero + // if long long has 32 bits, 0 ... 32 are one, 33 ... 127 are zero + bitset<128> bits128(~0ULL); + cout << "bits13: " << bits13 << endl; + cout << "bits20: " << bits20 << endl; + cout << "bits128: " << bits128 << endl; + cout << "bits20[0] " << bits20[0] << endl; + cout << "bits20[19] " << bits20[19] << endl; + + // bitvec1 is smaller than the initializer + bitset<32> bitvec1(0xffff); // bits 0 ... 15 are set to 1; + // 16 ... 31 are 0 + + // bitvec2 same size as initializer + bitset<64> bitvec2(0xffff); // bits 0 ... 15 are set to 1; + // 16 ... 63 are 0 + + // assuming 64-bit long long, bits 0 to 63 initialized from 0xffff + bitset<128> bitvec3(0xffff); // bits 32 through 127 are zero + + cout << "bitvec1: " << bitvec1 << endl; + cout << "bitvec2: " << bitvec2 << endl; + cout << "bitvec2[0] " << bitvec2[0] << endl; + cout << "bitvec2[31] " << bitvec2[31] << endl; + + cout << "bitvec3: " << bitvec3 << endl; + + + bitset<32> bitvec4("1100"); // bits 2 and 3 are 1, all others are 0 + + cout << "strval: " << "1100" << endl; + cout << "bitvec4: " << bitvec4 << endl; + + string str("1111111000000011001101"); + bitset<32> bitvec5(str, 5, 4); // four bits starting at str[5], 1100 + bitset<32> bitvec6(str, str.size()-4); // use last four characters + + cout << "str: " << str << endl; + cout << "bitvec5: " << bitvec5 << endl; + + cout << "str: " << str << endl; + cout << "bitvec6: " << bitvec6 << endl; + + unsigned long ulong = bitvec3.to_ulong(); + cout << "ulong = " << ulong << endl; + + bitset<32> bitvec7 = bitvec1 & bitvec4; + + cout << "bitvec7: " << bitvec7 << endl; + + bitset<32> bitvec8 = bitvec1 | bitvec4; + + cout << "bitvec8: " << bitvec8 << endl; + + bitset<16> chk("111100110011001100000"); + cout << "chk: " << chk << endl; + + bitset<16> bits; + cin >> bits; // read up to 16 1 or 0 characters from cin + cout << "bits: " << bits << endl; // print what we just read + + return 0; +} diff --git a/17/ccpgms.cc b/17/ccpgms.cc new file mode 100644 index 0000000..4fced61 --- /dev/null +++ b/17/ccpgms.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; +using std::regex_error; + +int main() +{ + try { + // one or more alphanumeric characters followed by a '.' + // followed by "cpp" or "cxx" or "cc" + regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase); + + smatch results; // object to hold details about the match + string filename; + while (cin >> filename) + if (regex_search(filename, results, r)) + cout << results.str() << endl; // print the match + } catch (regex_error e) + { cout << e.what() << " " << e.code() << endl; } + + return 0; +} + +#else + +// do nothing +int main() { return 0; } +#endif diff --git a/17/data/badgetc b/17/data/badgetc new file mode 100644 index 0000000..e4dd415 --- /dev/null +++ b/17/data/badgetc @@ -0,0 +1,3 @@ +abc +ÿ +def diff --git a/17/data/bits b/17/data/bits new file mode 100644 index 0000000..9db0de3 --- /dev/null +++ b/17/data/bits @@ -0,0 +1 @@ +11110001 diff --git a/17/data/copyOut b/17/data/copyOut new file mode 100644 index 0000000..15a1117 --- /dev/null +++ b/17/data/copyOut @@ -0,0 +1,9 @@ +abcd +efg +hi +j +5 9 12 14 +5 8 10 11 20 +5 7 8 18 30 +5 6 8 15 26 37 +5 9 12 14 24 37 49 64 diff --git a/17/data/files b/17/data/files new file mode 100644 index 0000000..81410da --- /dev/null +++ b/17/data/files @@ -0,0 +1,35 @@ +badgetc.cpp +bits.cpp +ccpgms.cpp +findbook.cpp +findDups.cpp +findDupsV2.cpp +findgerunds.cpp +game.cpp +getc.cpp +getput.cpp +manips.cpp +manips11.cpp +normalInts.cpp +no-skipws.cpp +phones.cpp +phones2.cpp +phonesFinal.cpp +play.cpp +point.cpp +prec.cpp +rand1.cpp +rand2.cpp +rand4.cpp +rand6.cpp +REerrs.cpp +regex1.cpp +sci_dec.cpp +seed.cpp +seek.cpp +setw.cpp +skipws.cpp +subexpr.cpp +tuple.cpp +validatePhones.cpp +zips.cpp diff --git a/17/data/findDups b/17/data/findDups new file mode 100644 index 0000000..cd3e880 --- /dev/null +++ b/17/data/findDups @@ -0,0 +1 @@ +Some some repeated words words diff --git a/17/data/findDupsV2 b/17/data/findDupsV2 new file mode 100644 index 0000000..15827af --- /dev/null +++ b/17/data/findDupsV2 @@ -0,0 +1 @@ +some some input that has has some dups diff --git a/17/data/findgerunds b/17/data/findgerunds new file mode 100644 index 0000000..9e0510f --- /dev/null +++ b/17/data/findgerunds @@ -0,0 +1,4 @@ +when i was going to saint ives +when going to saint ives +i was going when i went +i was when going diff --git a/17/data/game b/17/data/game new file mode 100644 index 0000000..837293b --- /dev/null +++ b/17/data/game @@ -0,0 +1 @@ +y y y y y y y y y y y y y q diff --git a/17/data/no-skipws b/17/data/no-skipws new file mode 100644 index 0000000..8c9edd3 --- /dev/null +++ b/17/data/no-skipws @@ -0,0 +1,2 @@ +a b c +d diff --git a/17/data/phones b/17/data/phones new file mode 100644 index 0000000..9f124fb --- /dev/null +++ b/17/data/phones @@ -0,0 +1,3 @@ +morgan (201) 555-0168 862-555-0123 +drew (973)555.0130 +lee (609) 555-0132 2015550175 800.555-0110 diff --git a/17/data/phones2 b/17/data/phones2 new file mode 100644 index 0000000..1292977 --- /dev/null +++ b/17/data/phones2 @@ -0,0 +1,3 @@ +morgan (201) 555-0168 (862) 555-0123 +drew (973) 555-0130 +lee (609) 555-0132 (201) 555-0175 (800) 555-0110 diff --git a/17/data/phonesFinal b/17/data/phonesFinal new file mode 100644 index 0000000..cdb3da8 --- /dev/null +++ b/17/data/phonesFinal @@ -0,0 +1,11 @@ +(908)555.0136 +(908.555.0136 +908).555.0136 +908 555.0136 +908-555.0136 +908 555 0136 +908.555.0136 +908-555-0136 +morgan (201) 555-0168 862-555-0123 +drew (973)555.0130 +lee (609) 555-0132 2015550175 800.555-0110 diff --git a/17/data/skipws b/17/data/skipws new file mode 100644 index 0000000..8c9edd3 --- /dev/null +++ b/17/data/skipws @@ -0,0 +1,2 @@ +a b c +d diff --git a/17/data/store1 b/17/data/store1 new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/17/data/store1 @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/17/data/store2 b/17/data/store2 new file mode 100644 index 0000000..4224fa4 --- /dev/null +++ b/17/data/store2 @@ -0,0 +1,8 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-999-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-999-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/17/data/store3 b/17/data/store3 new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/17/data/store3 @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/17/data/store4 b/17/data/store4 new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/17/data/store4 @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/17/data/validatePhones b/17/data/validatePhones new file mode 100644 index 0000000..0545ae7 --- /dev/null +++ b/17/data/validatePhones @@ -0,0 +1,8 @@ +(908)555.0106 +(908.555.0106 +908).555.0106 +908 555.0106 +908-555.0106 +908 555 0106 +908.555.0106 +908-555-0106 diff --git a/17/findbook.cc b/17/findbook.cc new file mode 100644 index 0000000..84dd13c --- /dev/null +++ b/17/findbook.cc @@ -0,0 +1,135 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +using std::pair; + +#include +using std::string; + +#include +using std::tuple; using std::get; +using std::make_tuple; + +#include +using std::vector; + +#include +using std::accumulate; + +#include +using std::equal_range; + +#include +#include +using std::domain_error; + +#include +using std::ostream; using std::istream; +using std::cout; using std::endl; + +#include +using std::ifstream; + +#include "Sales_data.h" + +bool lt(const Sales_data &lhs, const Sales_data &rhs) +{ + return lhs.isbn() < rhs.isbn(); +} + +// need to leave this for as a traditional for loop because we +// use the iterator to compute an index +// matches has three members: an index of a store and iterators into that store's vector +typedef tuple::size_type, + vector::const_iterator, + vector::const_iterator> matches; + +// files holds the transactions for every store +// findBook returns a vector with an entry for each store that sold the given book +vector +findBook(const vector> &files, + const string &book) +{ + vector ret; // initially empty + // for each store find the range of matching books, if any + for (auto it = files.cbegin(); it != files.cend(); ++it) { + // find the range of Sales_data that have the same ISBN + auto found = equal_range(it->cbegin(), it->cend(), + book, compareIsbn); + if (found.first != found.second) // this store had sales + // remember the index of this store and the matching range + ret.push_back(make_tuple(it - files.cbegin(), + found.first, found.second)); + } + return ret; // empty if no matches found +} + +vector build_store(const string &s) +{ + Sales_data item; + vector ret; + ifstream is(s); + while (read(is, item)) + ret.push_back(item); + sort (ret.begin(), ret.end(), lt); // need sort for equal_range to work + return ret; +} + +void reportResults(istream &in, ostream &os, + const vector> &files) +{ + string s; // book to look for + while (in >> s) { + auto trans = findBook(files, s); // stores that sold this book + if (trans.empty()) { + cout << s << " not found in any stores" << endl; + continue; // get the next book to look for + } + for (const auto &store : trans) // for every store with a sale + // get returns the specified member from the tuple in store + os << "store " << get<0>(store) << " sales: " + << accumulate(get<1>(store), get<2>(store), + Sales_data(s)) + << endl; + } +} + +int main(int argc, char **argv) +{ + assert(argc > 1); + // each element in files holds the transactions for a particular store + vector> files; + for (int cnt = 1; cnt != argc; ++cnt) + files.push_back(build_store(argv[cnt])); + + ifstream in("../data/findbook.in"); // ISBNs to search for + reportResults(in, cout, files); +} diff --git a/17/game.cc b/17/game.cc new file mode 100644 index 0000000..1b8a156 --- /dev/null +++ b/17/game.cc @@ -0,0 +1,62 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::default_random_engine; +using std::bernoulli_distribution; + +#include +using std::cin; using std::cout; using std::endl; + +bool play(bool player) { return player; } +int main() +{ + string resp; + // distributions and engines have state, + // they must be defined outside the loop! + default_random_engine e; + + // 50/50 odds by default + bernoulli_distribution b(0.55); // give the house a slight edge + + do { + bool first = b(e); // if true, the program will go first + cout << (first ? "We go first" + : "You get to go first") << endl; + // play the game passing the indicator of who goes first + cout << ((play(first)) ? "sorry, you lost" + : "congrats, you won") << endl; + cout << "play again? Enter 'yes' or 'no'" << endl; + } while (cin >> resp && resp[0] == 'y'); + + return 0; +} diff --git a/17/getc.cc b/17/getc.cc new file mode 100644 index 0000000..a22108f --- /dev/null +++ b/17/getc.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include // for EOF + +int main() +{ + int ch; // use an int, not a char to hold the return from get() + + // loop to read and write all the data in the input + while ((ch = cin.get()) != EOF) + cout.put(ch); + cout << endl; + + return 0; +} diff --git a/17/getput.cc b/17/getput.cc new file mode 100644 index 0000000..c8a3de8 --- /dev/null +++ b/17/getput.cc @@ -0,0 +1,41 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + char ch; + while (cin.get(ch)) + cout.put(ch); + cout << endl; + + return 0; +} diff --git a/17/makefile b/17/makefile new file mode 100644 index 0000000..265168e --- /dev/null +++ b/17/makefile @@ -0,0 +1,24 @@ +# executable files for this directory +OBJECTS = badgetc.exe bits.exe findbook.exe \ + game.exe getc.exe getput.exe manips.exe \ + normalInts.exe no-skipws.exe point.exe \ + prec.exe rand1.exe rand2.exe rand4.exe \ + rand6.exe seed.exe seek.exe setw.exe \ + skipws.exe tuple.exe manips11.exe sci_dec.exe \ + phones.exe ccpgms.exe validatePhones.exe \ + phonesFinal.exe subexpr.exe \ + regex1.exe zips.exe REerrs.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../14 + +phones.o ccpgms.o validatePhones.o \ +phonesFinal.o subexpr.o regex1.o \ +REerrs.o sci_dec.o manips11.o zips.o: ../Version_test.h + +findbook.exe: findbook.o ../14/Sales_data.o + $(CC) $(CCFLAGS) -o findbook findbook.o ../14/Sales_data.o + diff --git a/17/manips.cc b/17/manips.cc new file mode 100644 index 0000000..33129c3 --- /dev/null +++ b/17/manips.cc @@ -0,0 +1,75 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; +using std::boolalpha; using std::noboolalpha; +using std::oct; using std::dec; using std::hex; +using std::showbase; using std::noshowbase; +using std::uppercase; using std::nouppercase; + +bool get_status() +{ + return false; +} + +int main() +{ + bool b; + cout << "default bool values: " << true << " " << false + << "\nalpha bool values: " << boolalpha + << true << " " << false << endl; + + bool bool_val = get_status(); + cout << boolalpha // sets the internal state of cout + << bool_val + << noboolalpha; // resets the internal state to default formatting + cout << endl; + + const int ival = 15, jval = 1024; // const, so values never change + + cout << "default: " << 20 << " " << 1024 << endl; + cout << "octal: " << oct << 20 << " " << 1024 << endl; + cout << "hex: " << hex << 20 << " " << 1024 << endl; + cout << "decimal: " << dec << 20 << " " << 1024 << endl; + + + cout << showbase; // show the base when printing integral values + cout << "default: " << 20 << " " << 1024 << endl; + cout << "in octal: " << oct << 20 << " " << 1024 << endl; + cout << "in hex: " << hex << 20 << " " << 1024 << endl; + cout << "in decimal: " << dec << 20 << " " << 1024 << endl; + cout << noshowbase; // reset the state of the stream + + cout << uppercase << showbase << hex + << "printed in hexadecimal: " << 20 << " " << 1024 + << nouppercase << noshowbase << dec << endl; + + return 0; +} diff --git a/17/manips11.cc b/17/manips11.cc new file mode 100644 index 0000000..e303270 --- /dev/null +++ b/17/manips11.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include "Version_test.h" +#ifdef HEX_MANIPS +using std::hexfloat; using std::defaultfloat; +#endif + +int main() +{ + double pi = 3.14; + cout << pi << " " +#ifdef HEX_MANIPS + << hexfloat << pi // no workaround for this missing manipulator +#endif + << defaultfloat << " " << pi << endl; + + return 0; +} diff --git a/17/no-skipws.cc b/17/no-skipws.cc new file mode 100644 index 0000000..bddd868 --- /dev/null +++ b/17/no-skipws.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; +using std::noskipws; using std::skipws; + +int main() +{ + char ch; + cin >> noskipws; // set cin so that it reads whitespace + while (cin >> ch) + cout << ch; + cin >> skipws; // reset cin to the default state + // so that it ignores whitespace + + return 0; +} diff --git a/17/normalInts.cc b/17/normalInts.cc new file mode 100644 index 0000000..fd44116 --- /dev/null +++ b/17/normalInts.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::time; + +#include +using std::rand; + +#include +using std::default_random_engine; +using std::normal_distribution; + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::max_element; + +#include +using std::accumulate; + +#include +using std::lround; + +#include +using std::size_t; + +int main() +{ + default_random_engine e; // generates random integers + normal_distribution<> n(4,1.5); // mean 4, standard deviation 1.5 + vector vals(9); // nine elements each 0 + + for (size_t i = 0; i != 200; ++i) { + unsigned v = lround(n(e)); // round to the nearest integer + if (v < vals.size()) // if this result is in range + ++vals[v]; // count how often each number appears + } + + int sum = accumulate(vals.begin(), vals.end(), 0); + + if (sum != 200) + cout << "discarded " << 200 - sum << " results" << endl; + + for (size_t j = 0; j != vals.size(); ++j) + cout << j << ": " << string(vals[j], '*') << endl; + + return 0; +} diff --git a/17/phones.cc b/17/phones.cc new file mode 100644 index 0000000..ab76ea8 --- /dev/null +++ b/17/phones.cc @@ -0,0 +1,89 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; + +void checkPattern(const regex &r, const string &s) +{ + smatch results; + if (regex_search(s, results, r)) + cout << results.str() << endl; + else + cout << "no match for " << s << endl; +} + +int main() +{ + // phone has 10 digits, optional parentheses around the area code + // components are separated by an optional space, ',' or '-' + string pattern = "\\(?\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}"; + regex r(pattern); // a regex to match our pattern + + // some numbers to try to match + string mtch1 = "(908) 555-0181"; + string mtch2 = "(908)555-0182"; + string mtch3 = "908 555-0183"; + string mtch4 = "908.555-0184"; + string mtch5 = "9085550185"; + + smatch results; + checkPattern(r, mtch1); + checkPattern(r, mtch2); + checkPattern(r, mtch3); + checkPattern(r, mtch4); + checkPattern(r, mtch5); + + string s; + while (getline(cin, s)) + { + checkPattern(r, s); + } + + return 0; +} + +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/17/phonesFinal.cc b/17/phonesFinal.cc new file mode 100644 index 0000000..7e79ca7 --- /dev/null +++ b/17/phonesFinal.cc @@ -0,0 +1,83 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; +using std::regex_error; +using std::regex_constants::format_no_copy; + +int main() +{ + // phone has 10 digits, optional parentheses around the area code + // components are separated by an optional space, ',' or '-' + string phone = "\\(?\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}"; + + // parentheses indicate subexpressions; + // our overall expression has 7 subexpressions: + // ( ddd ) space ddd - dddd + // the subexpressions 1, 3, 4, and 6 are optional + // the subexpressions 2, 5, and 7 hold the digits of the number + phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})"; + regex r(phone); // a regex to find our pattern + smatch m; // a match object for the results + string s; + + // generate just the phone numbers: use a new format string + string fmt = "$2.$5.$7"; // reformat numbers to ddd.ddd.dddd + string fmt2 = "$2.$5.$7 "; // adds space at the end as a separator + + // read each record from the input file + while (getline(cin, s)) + { + cout << regex_replace(s, r, fmt) << endl; + + // tell regex_replace to copy only the text that it replaces + cout << regex_replace(s, r, fmt2, format_no_copy) << endl; + } + + return 0; +} +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/17/point.cc b/17/point.cc new file mode 100644 index 0000000..d5bfece --- /dev/null +++ b/17/point.cc @@ -0,0 +1,43 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; +using std::showpoint; using std::noshowpoint; + +int main() +{ + cout << 10.0 << endl; // prints 10 + cout << showpoint << 10.0 // prints 10.0000 + << noshowpoint << endl; // revert to default format + + cout << 10.0 << endl; // prints 10 + + return 0; +} diff --git a/17/prec.cc b/17/prec.cc new file mode 100644 index 0000000..f18b652 --- /dev/null +++ b/17/prec.cc @@ -0,0 +1,56 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::setprecision; + +#include +using std::sqrt; + +int main() +{ + // cout.precision reports the current precision value + cout << "Precision: " << cout.precision() + << ", Value: " << sqrt(2.0) << endl; + + // cout.precision(12) asks that 12 digits of precision be printed + cout.precision(12); + cout << "Precision: " << cout.precision() + << ", Value: " << sqrt(2.0) << endl; + + // alternative way to set precision using the setprecision manipulator + cout << setprecision(3); + cout << "Precision: " << cout.precision() + << ", Value: " << sqrt(2.0) << endl; + + return 0; +} diff --git a/17/rand1.cc b/17/rand1.cc new file mode 100644 index 0000000..bc53196 --- /dev/null +++ b/17/rand1.cc @@ -0,0 +1,109 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::time; + +#include +using std::rand; + +#include +using std::default_random_engine; +using std::uniform_int_distribution; +using std::normal_distribution; + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::max_element; + +#include +using std::size_t; + +int main() +{ + default_random_engine e; // generates random unsigned integers + for (size_t i = 0; i < 10; ++i) + // e() "calls" the object to produce the next random number + cout << e() << " "; + cout << endl; + + // uniformly distributed from 0 to 9 inclusive + uniform_int_distribution u(0,9); + default_random_engine e2; + for (size_t i = 0; i < 10; ++i) + // u uses e as a source of numbers + // each call returns a uniformly distributed value + // in the specified range + cout << u(e2) << " "; + cout << endl; + + default_random_engine e3; + // generates normally distributed doubles + // with mean 100, standard deviation 15 + normal_distribution<> n(100,15); + for (size_t i = 0; i < 10; ++i) + cout << n(e3) << " "; + cout << endl; + + // bad, but common way to generate random numbers in a given range + for (size_t i = 0; i < 10; ++i) + // r will be a between 0 and 9, but randomness is compromised + cout << rand()%10 << " "; + cout << endl; + + default_random_engine e4; + // uniformly distributed from 0 to 9 inclusive + uniform_int_distribution u2(0,9); + for (size_t i = 0; i < 10; ++i) + cout << u2(e4) << " "; + cout << endl; + + // seeding the engine causes it to generate different numbers + // on different executions + default_random_engine e5(time(0)); + for (size_t i = 0; i < 10; ++i) + cout << u2(e5) << " "; + cout << endl; + + // assuming shorts have 16 bits + uniform_int_distribution us1; // values from 0 to 32767 + uniform_int_distribution us2(42); // values from 42 to 32767 + cout << "min: " << us1.min() << " max: " << us1.max() << endl; + cout << "min: " << us2.min() << " max: " << us2.max() << endl; + + default_random_engine e6; + cout << "min: " << e6.min() << " max: " << e6.max() << endl; + + return 0; +} diff --git a/17/rand2.cc b/17/rand2.cc new file mode 100644 index 0000000..59046ac --- /dev/null +++ b/17/rand2.cc @@ -0,0 +1,96 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::time; + +#include +using std::rand; + +#include +using std::default_random_engine; +using std::uniform_int_distribution; +using std::normal_distribution; + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::size_t; + +#include +using std::max_element; + +#include +using std::accumulate; + +#include +using std::lround; + +int main() +{ + vector vals(32); // preallocate so each element is 0 + default_random_engine e; // generates numbers + uniform_int_distribution u(0,31); // inclusive range + for (size_t i = 0; i != 100; ++i) + ++vals[u(e)]; // count how often each number appears + int m = *max_element(vals.begin(), vals.end()); + for (int i = m; i != 0; --i) { + for (size_t j = 0; j != vals.size(); ++j) + if (vals[j] > i-1) cout << "* "; + else cout << " "; + cout << endl; + } + + vector vals2(32); // preallocate so each element 0 value + default_random_engine e2; // restart the sequence + normal_distribution<> n(15,5); // mean 15, standard deviation 5 + for (size_t i = 0; i != 100; ++i) { + size_t v = lround(n(e)); + if (v < vals.size()) + ++vals[v]; // count how often each number appears + else + cout << "discarding: " << v << " "; + } + cout << endl; + + cout << std::accumulate(vals.begin(), vals.end(), 0) << endl; + m = *max_element(vals.begin(), vals.end()); + for (int i = m; i != 0; --i) { + for (size_t j = 0; j != vals.size(); ++j) + if (vals[j] > i-1) cout << "* "; + else cout << " "; + cout << endl; + } + + return 0; +} diff --git a/17/rand4.cc b/17/rand4.cc new file mode 100644 index 0000000..3070753 --- /dev/null +++ b/17/rand4.cc @@ -0,0 +1,77 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::time; + +#include +using std::rand; + +#include +using std::default_random_engine; +using std::uniform_int_distribution; +using std::uniform_real_distribution; + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::size_t; + +#include +using std::max_element; + +int main() +{ + default_random_engine e; // generates unsigned random integers + // uniformly distributed from 0 to 1 inclusive + uniform_real_distribution u(0,1); + for (size_t i = 0; i < 10; ++i) + cout << u(e) << " "; + cout << endl; + + // empty <> signify we want to use the default result type + uniform_real_distribution<> u2(0,1); // generates double by default + default_random_engine e2; + for (size_t i = 0; i < 10; ++i) + cout << u2(e2) << " "; // should generate the same sequence + cout << endl; + + default_random_engine e3; + // uniformly distributed from 0 to 9 inclusive + uniform_int_distribution u3(0,9); + for (size_t i = 0; i < 100; ++i) + cout << u3(e3) << ((i != 99)? " ": ""); + cout << endl; + + return 0; +} diff --git a/17/rand6.cc b/17/rand6.cc new file mode 100644 index 0000000..b351a79 --- /dev/null +++ b/17/rand6.cc @@ -0,0 +1,87 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::time; + +#include +using std::rand; + +#include +using std::default_random_engine; +using std::uniform_int_distribution; + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::vector; + +// almost surely the wrong way to generate a vector of random integers +// output from this function will be the same 100 numbers on every call! +vector bad_randVec() +{ + default_random_engine e; + uniform_int_distribution u(0,9); + vector ret; + for (size_t i = 0; i < 100; ++i) + ret.push_back(u(e)); + return ret; +} + +// returns a vector of 100 uniformly distributed random numbers +vector good_randVec() +{ + // because engines and distributions retain state, they usually should be + // defined as static so that new numbers are generated on each call + static default_random_engine e; + static uniform_int_distribution u(0,9); + vector ret; + for (size_t i = 0; i < 100; ++i) + ret.push_back(u(e)); + return ret; +} + +int main() +{ + vector v1(bad_randVec()); + vector v2(bad_randVec()); + + // will print equal + cout << ((v1 == v2) ? "equal" : "not equal") << endl; + + // very unlikely to print equal + if (good_randVec() == good_randVec()) + cout << "equal!" << endl; + else + cout << "not equal" << endl; +} diff --git a/17/regex1.cc b/17/regex1.cc new file mode 100644 index 0000000..ef14ef4 --- /dev/null +++ b/17/regex1.cc @@ -0,0 +1,85 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; + +int main() +{ + // find the characters ei that follow a character other than c + string pattern("[^c]ei"); + // we want the whole word in which our pattern appears + pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*"; + + regex r(pattern); // construct a regex to find pattern + smatch results; // define an object to hold the results of a search + + // define a string that has text that does and doesn't match pattern + string test_str = "receipt freind theif receive"; + + // use r to find a match to pattern in test_str + if (regex_search(test_str, results, r)) // if there is a match + cout << results.str() << endl; // print the matching word + + sregex_iterator it(test_str.begin(), test_str.end(), r); + sregex_iterator end_it; // end iterator + for ( ; it != end_it; ++it) + cout << it->str() << endl; // print current match + + // alternative way to obtain all the matches in a given string + auto it2 = test_str.cbegin(); + while (it2 != test_str.cend() && + regex_search(it2, test_str.cend(), results, r)) { + cout << results.str() << endl; + // reposition the iterator past any nonmatched part + // of the string plus the size of this match + it2 += results.prefix().length() + results.length(); + + } + + return 0; +} +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/17/runpgms b/17/runpgms new file mode 100644 index 0000000..e03172b --- /dev/null +++ b/17/runpgms @@ -0,0 +1,19 @@ +echo "badgetc: " && ./badgetc.exe < data/skipws +echo "bits: " && ./bits.exe < data/bits +echo "ccpgms: " && ./ccpgms.exe +using std::sqrt; + +#include +using std::cout; using std::endl; +using std::fixed; using std::scientific; +using std::uppercase; using std::nouppercase; + +#include "Version_test.h" +#ifdef HEX_MANIPS +using std::hexfloat; using std::defaultfloat; +#endif + +int main() +{ + cout << "default format: " << 100 * sqrt(2.0) << '\n' + << "scientific: " << scientific << 100 * sqrt(2.0) << '\n' + << "fixed decimal: " << fixed << 100 * sqrt(2.0) << '\n' +#ifdef HEX_MANIPS // no workaround for this missing manipulator + << "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << '\n' +#endif + << "use defaults: " << defaultfloat << 100 * sqrt(2.0) + << "\n\n"; + + cout << uppercase + << "scientific: " << scientific << sqrt(2.0) << '\n' + << "fixed decimal: " << fixed << sqrt(2.0) << '\n' +#ifdef HEX_MANIPS // no workaround for this missing manipulator + << "hexadecimal: " << hexfloat << sqrt(2.0) << "\n\n" +#endif + << nouppercase; + + return 0; +} diff --git a/17/seed.cc b/17/seed.cc new file mode 100644 index 0000000..95a0ecc --- /dev/null +++ b/17/seed.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::time; + +#include +using std::rand; + +#include +using std::default_random_engine; +using std::uniform_int_distribution; + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::vector; + +int main() +{ + default_random_engine e1; // uses the default seed + default_random_engine e2(2147483646); // use the given seed value + + // e3 and e4 will generate the same sequence + // because they use the same seed + default_random_engine e3; // uses the default seed value + e3.seed(32767); // call seed to set a new seed value + default_random_engine e4(32767); // set the seed value to 32767 + for (size_t i = 0; i != 100; ++i) { + if (e1() == e2()) + cout << "unseeded match at iteration: " << i << endl; + if (e3() != e4()) + cout << "seeded differs at iteration: " << i << endl; + } + + return 0; +} diff --git a/17/seek.cc b/17/seek.cc new file mode 100644 index 0000000..941e304 --- /dev/null +++ b/17/seek.cc @@ -0,0 +1,73 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cerr; using std::endl; + +#include +using std::fstream; + +#include +using std::string; + +#include // for EXIT_FAILURE + +int main() +{ + // open for input and output and preposition file pointers to end-of-file + // file mode argument + fstream inOut("data/copyOut", + fstream::ate | fstream::in | fstream::out); + if (!inOut) { + cerr << "Unable to open file!" << endl; + return EXIT_FAILURE; // EXIT_FAILURE + } + + // inOut is opened in ate mode, so it starts out positioned at the end + auto end_mark = inOut.tellg();// remember original end-of-file position + inOut.seekg(0, fstream::beg); // reposition to the start of the file + size_t cnt = 0; // accumulator for the byte count + string line; // hold each line of input + + // while we haven't hit an error and are still reading the original data + while (inOut && inOut.tellg() != end_mark + && getline(inOut, line)) { // and can get another line of input + cnt += line.size() + 1; // add 1 to account for the newline + auto mark = inOut.tellg(); // remember the read position + inOut.seekp(0, fstream::end); // set the write marker to the end + inOut << cnt; // write the accumulated length + // print a separator if this is not the last line + if (mark != end_mark) inOut << " "; + inOut.seekg(mark); // restore the read position + } + inOut.seekp(0, fstream::end); // seek to the end + inOut << "\n"; // write a newline at end-of-file + + return 0; +} diff --git a/17/setw.cc b/17/setw.cc new file mode 100644 index 0000000..e810e16 --- /dev/null +++ b/17/setw.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; +using std::left; using std::right; using std::internal; + +#include +using std::setw; using std::setfill; + +int main() +{ + int i = -16; + double d = 3.14159; + + // pad the first column to use a minimum of 12 positions in the output + cout << "i: " << setw(12) << i << "next col" << '\n' + << "d: " << setw(12) << d << "next col" << '\n'; + + // pad the first column and left-justify all columns + cout << left + << "i: " << setw(12) << i << "next col" << '\n' + << "d: " << setw(12) << d << "next col" << '\n' + << right; // restore normal justification + + // pad the first column and right-justify all columns + cout << right + << "i: " << setw(12) << i << "next col" << '\n' + << "d: " << setw(12) << d << "next col" << '\n'; + + // pad the first column but put the padding internal to the field + cout << internal + << "i: " << setw(12) << i << "next col" << '\n' + << "d: " << setw(12) << d << "next col" << '\n'; + + // pad the first column, using # as the pad character + cout << setfill('#') + << "i: " << setw(12) << i << "next col" << '\n' + << "d: " << setw(12) << d << "next col" << '\n' + << setfill(' '); // restore the normal pad character + + return 0; +} diff --git a/17/skipws.cc b/17/skipws.cc new file mode 100644 index 0000000..0d2b5dc --- /dev/null +++ b/17/skipws.cc @@ -0,0 +1,41 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + char ch; + while (cin >> ch) + cout << ch; + cout << endl; + + return 0; +} diff --git a/17/subexpr.cc b/17/subexpr.cc new file mode 100644 index 0000000..5efbdb4 --- /dev/null +++ b/17/subexpr.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::string; + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; +using std::regex_error; + +int main() +{ + try { + // r has two subexpressions: + // the first is the part of the file name before the period + // the second is the file extension + regex r("([[:alnum:]]+)\\.(cpp|cxx|cc)$", regex::icase); + smatch results; + string filename; + while (cin >> filename) + if (regex_search(filename, results, r)) + cout << results.str(1) << endl; + // print the first subexpression + } catch (regex_error e) + { cout << e.what() << " " << e.code() << endl; } + + return 0; +} +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/17/tuple.cc b/17/tuple.cc new file mode 100644 index 0000000..1ec6aab --- /dev/null +++ b/17/tuple.cc @@ -0,0 +1,77 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::list; + +#include +using std::vector; + +#include +using std::tuple; using std::get; +using std::tuple_size; using std::tuple_element; +using std::make_tuple; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + // tuple that represents a bookstore transaction: + // ISBN, count, price per book + auto item = make_tuple("0-999-78345-X", 3, 20.00); + auto book = get<0>(item); // returns the first member of item + auto cnt = get<1>(item); // returns the second member of item + auto price = get<2>(item)/cnt; // returns the last member of item + get<2>(item) *= 0.8; // apply 20% discount + + cout << book << " " << cnt << " " << price << endl; + + typedef decltype(item) trans; // trans is the type of item + + // returns the number of members in object's of type trans + size_t sz = tuple_size::value; // returns 3 + + // cnt has the same type as the second member in item + tuple_element<1, trans>::type cnt2 = get<1>(item); // cnt is an int + tuple_element<0, trans>::type book2 = get<0>(item); + tuple_element<2, trans>::type price2 = get<2>(item); + cout << tuple_size::value << endl; + + cout << book2 << " " << cnt2 << " " << price2 << endl; + + tuple threeD; // all three members set to 0 + tuple, int, list> + someVal("constants", {3.14, 2.718}, 42, {0,1,2,3,4,5}); + + return 0; +} diff --git a/17/validatePhones.cc b/17/validatePhones.cc new file mode 100644 index 0000000..92ed930 --- /dev/null +++ b/17/validatePhones.cc @@ -0,0 +1,98 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; +using std::regex_error; + +// the area code doesn't have a separator; +// the remaining separators must be equal +// OR the area code parens are correct and +// the next separator is blank or missing +bool valid(const smatch& m) +{ + // if there is an open parenthesis before the area code + if(m[1].matched) + // the area code must be followed by a close parenthesis + // and followed immediately by the rest of the number or a space + return m[3].matched + && (m[4].matched == 0 || m[4].str() == " "); + else + // then there can't be a close after the area code + // the delimiters between the other two components must match + return !m[3].matched + && m[4].str() == m[6].str(); +} +int main() +{ + // phone has 10 digits, optional parentheses around the area code + // components are separated by an optional space, ',' or '-' + string phone = "\\(?\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}"; + + // our overall expression has seven subexpressions: + // ( ddd ) separator ddd separator dddd + // subexpressions 1, 3, 4, and 6 are optional; + // subexpressions 2, 5, and 7 hold the number + phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ]?)(\\d{4})"; + regex r(phone); // a regex to find our pattern + smatch m; // a match object to hold the results + string s; // a string to search + + // read each record from the input file + while (getline(cin, s)) { + // for each matching phone number + for (sregex_iterator it(s.begin(), s.end(), r), end_it; + it != end_it; ++it) + // check whether the number's formatting is valid + if (valid(*it)) + cout << "valid: " << it->str() << endl; + else + cout << "not valid: " << it->str() << endl; + } + + return 0; +} +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/17/zips.cc b/17/zips.cc new file mode 100644 index 0000000..7173aa6 --- /dev/null +++ b/17/zips.cc @@ -0,0 +1,70 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Version_test.h" + +// if the regular expression library isn't support, do nothing +#ifdef REGEX + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::regex; using std::sregex_iterator; using std::smatch; + +int main() +{ + string zip = "\\d{5}-\\d{4}|\\d{5}"; + + string test_str = "908.647.4306 164 gates, 07933 07933-1257"; + regex r(zip); // a regex to find the parts in our pattern + smatch results; + + if (regex_search(test_str, results, r)) + cout << results.str() << endl; + + sregex_iterator it(test_str.begin(), test_str.end(), r); + sregex_iterator end_it; // end iterator + while (it != end_it) { + cout << it->str() << endl; // print current match + ++it; // advance iterator for next search + } + + return 0; +} +#else + +// do nothing +int main() { return 0; } + +#endif + diff --git a/18/Animal.cc b/18/Animal.cc new file mode 100644 index 0000000..3a6b1a6 --- /dev/null +++ b/18/Animal.cc @@ -0,0 +1,59 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Animal.h" +#include +using std::ostream; using std::cout; using std::endl; + +// operations that take references to base classes of type Panda +void print(const Bear&) +{ + cout << "print(const Bear&)" << std::endl; + +} +void highlight(const Endangered&) +{ + cout << "highlight(const Endangered&)" << std::endl; +} + +ostream& operator<<(ostream &os, const ZooAnimal&) +{ + return os << "ZooAnimal output operator" << endl; +} + +int main() { + Panda ying_yang("ying_yang"); + + print(ying_yang); // passes Panda to a reference to Bear + highlight(ying_yang); // passes Panda to a reference to Endangered + cout << ying_yang << endl; // passes Panda to a reference to ZooAnimal + Panda ling_ling = ying_yang; // uses the copy constructor + + return 0; +} diff --git a/18/Animal.h b/18/Animal.h new file mode 100644 index 0000000..0d57fda --- /dev/null +++ b/18/Animal.h @@ -0,0 +1,138 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include + +class Endangered { +public: + virtual ~Endangered() + { std::cout << "Endangered dtor" << std::endl; } + virtual std::ostream& print() const + { return std::cout << "Endangered::print" << std::endl; } + virtual void highlight() const + { std::cout << "Endangered::highlight" << std::endl; } + virtual double max_weight() const + { std::cout << "Endangered::max_weight" << std::endl; return 0; } + // . . . +}; + +class ZooAnimal; +extern std::ostream& +operator<<(std::ostream&, const ZooAnimal&); + +class ZooAnimal { +public: + ZooAnimal() = default; + ZooAnimal(std::string animal, bool exhibit, + std::string family): nm(animal), + exhibit_stat(exhibit), + fam_name(family) { } + virtual ~ZooAnimal() + { std::cout << "Animal dtor" << std::endl; } + + virtual std::ostream& print() const + { return std::cout << "Animal::print" << std::endl; } + virtual int population() const + { std::cout << "Animal::population" << std::endl; return 0;} + virtual double max_weight() const + { std::cout << "Animal::max_weight" << std::endl; return 0;} + + // accessors + std::string name() const { return nm; } + std::string family_name() const { return fam_name; } + bool onExhibit() const { return exhibit_stat; } + // . . . +protected: + std::string nm; + bool exhibit_stat = false; + std::string fam_name; + // . . . +private: +}; + +using DanceType = unsigned; +constexpr DanceType two_left_feet = 0; +constexpr DanceType Astaire = 1; +constexpr DanceType Rogers = 42; + +class Bear : public ZooAnimal { +public: + Bear() = default; + Bear(std::string name, bool onExhibit=true, + std::string family = "Bear"): + ZooAnimal(name, onExhibit, family), + dancetype(two_left_feet) { } + + virtual std::ostream &print() const + { return std::cout << "Bear::print" << std::endl; } + virtual int toes() const + { std::cout << "Bear::toes" << std::endl; return 0; } + int mumble(int) + { std::cout << "Bear::mumble" << std::endl; return 0; } + void dance(DanceType) const + { std::cout << "Bear::dance" << std::endl; } + + virtual ~Bear() + { std::cout << "Bear dtor" << std::endl; } +private: + DanceType dancetype = Rogers; +}; + +class Panda : public Bear, public Endangered { +public: + Panda() = default; + Panda(std::string name, bool onExhibit=true); + virtual ~Panda() + { std::cout << "Panda dtor" << std::endl; } + virtual std::ostream& print() const + { return std::cout << "Panda::print" << std::endl; } + void highlight() + { std::cout << "Panda::highlight" << std::endl; } + virtual int toes() + { std::cout << "Panda::toes" << std::endl; return 0; } + virtual void cuddle() + { std::cout << "Panda::cuddle" << std::endl; } + virtual double max_weight() const; +// . . . +}; + +inline +Panda::Panda(std::string name, bool onExhibit) + : Bear(name, onExhibit, "Panda") { } + +inline +double Panda::max_weight() const +{ + return std::max(ZooAnimal::max_weight(), + Endangered::max_weight()); +} + +class PolarBear : public Bear { /* . . . */ }; diff --git a/18/Animal_virtual_baseVers.h b/18/Animal_virtual_baseVers.h new file mode 100644 index 0000000..48b1900 --- /dev/null +++ b/18/Animal_virtual_baseVers.h @@ -0,0 +1,153 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include + +class Endangered { +public: + enum Status { critical, environment, improving }; + Endangered(Status stat = improving): animal_status(stat) { } + virtual ~Endangered() + {std::cout << "Endangered::~Endangered" << std::endl; } + virtual std::ostream& print(std::ostream&) const + { return std::cout << "Endangered::print" << std::endl; } + virtual void highlight() const + {std::cout << "Endangered::highlighted" << std::endl; } + // . . . +private: + Status animal_status; +}; + +class ZooAnimal; +extern std::ostream& +operator<<(std::ostream&, const ZooAnimal&); + +class ZooAnimal { +public: + ZooAnimal() = default; + ZooAnimal(std::string animal, bool exhibit, + std::string family): nm(animal), + exhibit_stat(exhibit), + fam_name(family) { } + virtual ~ZooAnimal() + {std::cout << "ZooAnimal::~ZooAnimal" << std::endl; } + + virtual std::ostream& print(std::ostream&) const + { return std::cout << "ZooAnimal::print" << std::endl; } + virtual int population() const + {std::cout << "ZooAnimal::population" << std::endl; return 0;} + + // accessors + std::string name() const { return nm; } + std::string family_name() const { return fam_name; } + bool onExhibit() const { return exhibit_stat; } + // . . . +protected: + std::string nm; + bool exhibit_stat = false; + std::string fam_name; + // . . . +private: +}; + +// the order of the keywords public and virtual is not significant +class Raccoon : public virtual ZooAnimal { +public: + Raccoon() = default; + Raccoon(std::string name, bool onExhibit=true); + + virtual std::ostream& print(std::ostream&) const + { return std::cout << "Raccoon::print" << std::endl; } + + bool pettable() const {return pettable_flag; } + void pettable(bool petval) {pettable_flag = petval;} + // . . . + +protected: + bool pettable_flag = false; + // . . . +}; + +class Bear : virtual public ZooAnimal { +public: + // when the most derived class + Bear(std::string name, bool onExhibit=true); +protected: + // when an intermediate derived class + Bear() : dance_flag(two_left_feet) { } + +public: + enum DanceType { two_left_feet, macarena, fandango }; + + virtual std::ostream &print(std::ostream&) const + { return std::cout << "Bear::print" << std::endl; } + virtual std::string isA() const + { std::cout << "Bear::isA" << std::endl; return "Bear"; } + int mumble(int) + { std::cout << "Bear::mumble" << std::endl; return 0; } + void dance(DanceType) const + { std::cout << "Bear::dance" << std::endl; } + + virtual ~Bear() { std::cout << "Bear::~Bear" << std::endl; } +private: + std::string name; + DanceType dance_flag; +}; + +class Panda : public Bear, + public Raccoon, public Endangered { +public: + Panda() = default; + Panda(std::string name, bool onExhibit=true); + virtual std::ostream& print(std::ostream&) const + { return std::cout << "Panda::print" << std::endl; } + + bool sleeping() const {return sleeping_flag;} + void sleeping(bool newval) {sleeping_flag = newval;} + // . . . + +protected: + bool sleeping_flag = false; + // . . . +}; + + +Bear::Bear(std::string name, bool onExhibit): + ZooAnimal(name, onExhibit, "Bear") { } +Raccoon::Raccoon(std::string name, bool onExhibit) + : ZooAnimal(name, onExhibit, "Raccoon") { } + +Panda::Panda(std::string name, bool onExhibit) + : ZooAnimal(name, onExhibit, "Panda"), + Bear(name, onExhibit), + Raccoon(name, onExhibit), + Endangered(Endangered::critical), + sleeping_flag(false) { } + diff --git a/18/README b/18/README new file mode 100644 index 0000000..42c3ace --- /dev/null +++ b/18/README @@ -0,0 +1,10 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + book_sales book + +Programs not listed above print output and do +not read any input + diff --git a/18/Sales_data.cc b/18/Sales_data.cc new file mode 100644 index 0000000..00821a3 --- /dev/null +++ b/18/Sales_data.cc @@ -0,0 +1,123 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::istream; using std::ostream; + +#include "Sales_data.h" +#include "bookexcept.h" + +// this version of the compound assignment operator +// throws an exception if the objects refer to different books +Sales_data& Sales_data::operator+=(const Sales_data &rhs) +{ + if (isbn() != rhs.isbn()) + throw isbn_mismatch("wrong isbns", isbn(), rhs.isbn()); + units_sold += rhs.units_sold; + revenue += rhs.revenue; + return *this; +} + +// operator+ code is unchanged, but because it uses += +// this version of the function also throws when called +// for books whose isbns differ +Sales_data +operator+(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum += rhs; // add rhs into sum + return sum; +} + +// remaning functions unchanged from chapter 16 +// define the hash interface for Sales_data +namespace std { +size_t +hash::operator()(const Sales_data& s) const +{ + return hash()(s.bookNo) ^ + hash()(s.units_sold) ^ + hash()(s.revenue); +} +} // close the std namespace; note: no semicolon after the close curly + +// remaining members unchanged from chapter 14 +Sales_data::Sales_data(istream &is) +{ + is >> *this; // read a transaction from is into this object +} + +double Sales_data::avg_price() const +{ + if (units_sold) + return revenue/units_sold; + else + return 0; +} + +istream &operator>>(istream &is, Sales_data &item) +{ + double price; // no need to initialize; we'll read into price before we use it + is >> item.bookNo >> item.units_sold >> price; + if (is) // check that the inputs succeeded + item.revenue = item.units_sold * price; + else + item = Sales_data(); // input failed: give the object the default state + return is; +} + +ostream &operator<<(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} + +// operators replace these original named functions +istream &read(istream &is, Sales_data &item) +{ + double price = 0; + is >> item.bookNo >> item.units_sold >> price; + item.revenue = price * item.units_sold; + return is; +} +ostream &print(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} + +Sales_data add(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum += rhs; // add rhs into sum + return sum; +} + diff --git a/18/book.cc b/18/book.cc new file mode 100644 index 0000000..61b6c98 --- /dev/null +++ b/18/book.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" +#include "bookexcept.h" + +#include +using std::cin; using std::cerr; using std::endl; + +int main() +{ + // use the hypothetical bookstore exceptions + Sales_data item1, item2, sum; + while (cin >> item1 >> item2) { // read two transactions + try { + sum = item1 + item2; // calculate their sum + // use sum + } catch (const isbn_mismatch &e) { + cerr << e.what() << ": left isbn(" << e.left + << ") right isbn(" << e.right << ")" << endl; + } + } + return 0; +} diff --git a/18/bookexcept.h b/18/bookexcept.h new file mode 100644 index 0000000..8a50270 --- /dev/null +++ b/18/bookexcept.h @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef BOOKEXCEPT_H +#define BOOKEXCEPT_H +#include +#include + +// hypothetical exception classes for a bookstore application +class out_of_stock: public std::runtime_error { +public: + explicit out_of_stock(const std::string &s): + std::runtime_error(s) { } +}; + +class isbn_mismatch: public std::logic_error { +public: + explicit isbn_mismatch(const std::string &s): + std::logic_error(s) { } + isbn_mismatch(const std::string &s, + const std::string &lhs, const std::string &rhs): + std::logic_error(s), left(lhs), right(rhs) { } + const std::string left, right; +}; + +#endif diff --git a/18/class.cc b/18/class.cc new file mode 100644 index 0000000..2c683cd --- /dev/null +++ b/18/class.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 + */ + +#include +#include + +int main() +{ + std::string s; + + // ok: calls std::getline(std::istream&, const std::string&) + getline(std::cin, s); + + std::cout << s << std::endl; + + return 0; +} + diff --git a/18/data/book_sales b/18/data/book_sales new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/18/data/book_sales @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/18/ex-over.cc b/18/ex-over.cc new file mode 100644 index 0000000..84a6350 --- /dev/null +++ b/18/ex-over.cc @@ -0,0 +1,59 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +namespace primerLib { + void compute() { cout << "primerLib::compute()" << endl; } + void compute(const void *) + { cout << "primerLib::compute(const void *)" << endl; } +} + +// brings comput() and compute(const void*) into scope +using primerLib::compute; + +void compute(int) { cout << "compute(int)" << endl; } +void compute(double, double = 3.4) + { cout << "compute(double, double)" << endl; } +void compute(char*, char* = 0) + { cout << "compute(char*, char*)" << endl; } + +int main() +{ + int i = 42; + char c = 'a'; + compute(i); // compute(int) + compute(c); // compute(int) + compute(&i); // primerLib::compute(const void*) + compute(&c); // compute(char*, char*) + compute(3.4);// compute(double, double) + + return 0; +} diff --git a/18/inherit.cc b/18/inherit.cc new file mode 100644 index 0000000..beb766d --- /dev/null +++ b/18/inherit.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +namespace NS { + class Quote { + public: + Quote() { std::cout << "Quote::Quote" << std::endl; } + }; + void display(const Quote&) + { std::cout << "display(const Quote&)" << std::endl; } +} + +// Bulk_item's base class is declared in namespace NS +class Bulk_item : public NS::Quote { +public: + Bulk_item() { std::cout << "Bulk_item::Bulk_item" << std::endl; } +}; + +int main() { + Bulk_item book1; + + display(book1); // calls Quote::display + + return 0; +} + diff --git a/18/makefile b/18/makefile new file mode 100644 index 0000000..80f04f1 --- /dev/null +++ b/18/makefile @@ -0,0 +1,18 @@ +# executable files for this directory +OBJECTS = Animal.exe book.exe class.exe \ + ex-over.exe inherit.exe mi-scope.exe\ + over.exe over2.exe spec.exe \ + usingDirective.exe virt-inherit.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../16 + +Animal.o: Animal.h +book.o: ../16/Sales_data.h bookexcept.h +virt-inherit.o: Animal_virtual_baseVers.h + +book.exe: book.o bookexcept.h Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) book.o Sales_data.o -o book.exe diff --git a/18/mi-scope.cc b/18/mi-scope.cc new file mode 100644 index 0000000..8204c28 --- /dev/null +++ b/18/mi-scope.cc @@ -0,0 +1,72 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include + +struct Base1 { + void print(int) const // public by default + { std::cout << "Base1::print(int) " << ival << std::endl; } +protected: + int ival = 1; +}; + +struct Base2 { + void print(double) const // public by default + { std::cout << "Base2::print(double) " << ival << std::endl; } +protected: + int ival = 2; +}; + +struct Derived : public Base1 { + void print(std::string) const // public by default + { std::cout << "Derived::print(string) " << ival << std::endl; } +protected: + int ival = 3; +}; + +struct MI : public Derived, public Base2 { + void print(std::vector) // public by default + { std::cout << "MI::print(int(vector) " + << ival << std::endl; } +protected: + int ival = 4; +}; + +int main() +{ + MI obj; + obj.Base1::print(0); + obj.Base2::print(3.14); + obj.Derived::print("hi"); + obj.print(std::vector()); + + return 0; +} diff --git a/18/over.cc b/18/over.cc new file mode 100644 index 0000000..22cb5cf --- /dev/null +++ b/18/over.cc @@ -0,0 +1,64 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include + +namespace libs_R_us { + extern void print(int) + { std::cout << "libs_R_us::print(int)" << std::endl; } + extern void print(double) + { std::cout << "libs_R_us::print(double)" << std::endl; } +} + +// ordinary declaration +void print(const std::string &) +{ + std::cout << "print(const std::string &)" << std::endl; +} + +// this using directive adds names to the candidate set for calls to print: +using namespace libs_R_us; + +// the candidates for calls to print at this point in the program are: +// print(int) from libs_R_us +// print(double) from libs_R_us +// print(const std::string &) declared explicitly + +int main() +{ + int ival = 42; + print("Value: "); // calls global print(const string &) + print(ival); // calls libs_R_us::print(int) + + return 0; +} + diff --git a/18/over2.cc b/18/over2.cc new file mode 100644 index 0000000..0216291 --- /dev/null +++ b/18/over2.cc @@ -0,0 +1,53 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +namespace AW { + int print(int i) + { std::cout << "AW::print(int)" << std::endl; return i; } +} +namespace Primer { + double print(double d) + { std::cout << "Primer::print(double)" << std::endl; return d; } +} + +// using directives create an overload set of functions from different namespaces +using namespace AW; +using namespace Primer; + +long double print(long double); + +int main() { + print(1); // calls AW::print(int) + print(3.1); // calls Primer::print(double) + + return 0; +} + diff --git a/18/runpgms b/18/runpgms new file mode 100644 index 0000000..0a92c00 --- /dev/null +++ b/18/runpgms @@ -0,0 +1,6 @@ +echo "book: " && ./book.exe < data/book_sales + +../noinput Animal.exe ex-over.exe inherit.exe mi-scope.exe\ + over.exe over2.exe spec.exe \ + usingDirective.exe virt-inherit.exe + diff --git a/18/spec.cc b/18/spec.cc new file mode 100644 index 0000000..bb53ff7 --- /dev/null +++ b/18/spec.cc @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::exception; + +// this function will compile, even though it clearly violates its exception specification +void f() noexcept // promises not to throw any exception +{ + throw exception(); // violates the exception specification +} + +void g() { } +void h() noexcept(noexcept(f())) { f(); } +void i() noexcept(noexcept(g())) { g(); } +int main() +{ + try { + cout << "f: " << std::boolalpha << noexcept(f()) << endl; + cout << "g: " << std::boolalpha << noexcept(g()) << endl; + cout << "h: " << std::boolalpha << noexcept(h()) << endl; + cout << "i: " << std::boolalpha << noexcept(i()) << endl; + f(); + } catch (exception &e) { + cout << "caught " << e.what() << endl; + } + + return 0; +} diff --git a/18/usingDirective.cc b/18/usingDirective.cc new file mode 100644 index 0000000..cd5f2c8 --- /dev/null +++ b/18/usingDirective.cc @@ -0,0 +1,73 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +// namespace A and function f are defined at global scope +namespace A { + int i = 0, j = 42; +} + +void f() +{ + using namespace A; // injects the names from A into the global scope + + // uses i and j from namespace A + cout << "i: " << i << " j: " << j << endl; +} + +namespace blip { + int i = 16, j = 15, k = 23; + void f() + { cout << "i: " << i << " j: " << j << " k: " << k << endl; } +} + +int j = 0; // ok: j inside blip is hidden inside a namespace + +int main() +{ + // using directive; + // the names in blip are ``added'' to the global scope + using namespace blip; // clash between ::j and blip::j + // detected only if j is used + + ++i; // sets blip::i to 17 + ++::j; // ok: sets global j to 1 + ++blip::j; // ok: sets blip::j to 16 + + int k = 97; // local k hides blip::k + ++k; // sets local k to 98 + + ::f(); + blip::f(); + cout << "j: " << ::j << " k: " << k << endl; + + return 0; +} diff --git a/18/virt-inherit.cc b/18/virt-inherit.cc new file mode 100644 index 0000000..0499b7b --- /dev/null +++ b/18/virt-inherit.cc @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Animal_virtual_baseVers.h" +#include +using std::cout; using std::endl; using std::ostream; + +void dance(const Bear&) + { cout << "dance(const Bear&)" << endl; } + +void rummage(const Raccoon&) + { cout << "rummage(const Raccoon&)" << endl; } + +ostream& operator<<(ostream&, const ZooAnimal&) + { return cout << "ZooAnimal output operator" << endl; } + +int main () +{ + Panda ying_yang; + dance(ying_yang); // ok: passes Panda object as a Bear + rummage(ying_yang); // ok: passes Panda object as a Raccoon + + cout << ying_yang; // ok: passes Panda object as a ZooAnimal + + return 0; +} diff --git a/19/.useScreen.cc.swp b/19/.useScreen.cc.swp new file mode 100644 index 0000000000000000000000000000000000000000..a22cc1f7e1a36601abf7903897490531b386baaf GIT binary patch literal 16384 zcmeHN&5s;M6)&5w4B$AJ8$wVSOV-S0c4l@L6K8gDlsL9X@OoEShxit1x~pf(?(XVT zchBsw3Kx5`_spFfA^t#*vqFG8zx%Q<-7fD3zNn2 zZc>bWffV+JTHokpMkUgmmAy!ryx27=EmS({P317qq3n&)@{)>W-dlU9SLSj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BzK@abcYl5<1acbp+}4LX(An|iCY}AvPONl-c9sG&Y$)? zw)6BAda{PIS(Q#SJ; + +class Screen { +public: + typedef std::string::size_type pos; + + // Action is a type that can point to a member function of Screen + // that returns a reference to a Screen and takes no arguments + using Action = Screen&(Screen::*)(); + + // constructor: build screen of given size containing all blanks + Screen(pos ht = 0, pos wd = 0): contents(ht * wd, ' '), cursor(0), + height(ht), width(wd) { } + friend int main(); + // data is a static member that returns a pointer to member + static const std::string Screen::*data() + { return &Screen::contents; } + + char get_cursor() const { return contents[cursor]; } + char get() const { return contents[cursor]; } + inline char get(pos ht, pos wd) const; +private: + std::string contents; + pos cursor; + pos height, width; + +public: + // cursor movement functions + // beware: these functions don't check whether the operation is valid + Screen& home() { cursor = 0; return *this; } + Screen& forward() { ++cursor; return *this; } + Screen& back() { --cursor; return *this; } + Screen& up() { cursor += height; return *this; } + Screen& down() {cursor -= height; return *this; } + + // specify which direction to move; enum see XREF(enums) + enum Directions { HOME, FORWARD, BACK, UP, DOWN }; + Screen& move(Directions); +private: + static Action Menu[]; // function table +}; + +char Screen::get(pos r, pos c) const // declared as inline in the class +{ + pos row = r * width; // compute row location + return contents[row + c]; // return character at the given column +} + +inline +Screen& Screen::move(Directions cm) +{ + // run the element indexed by cm on this object + return (this->*Menu[cm])(); // Menu[cm] points to a member function +} + +#endif + diff --git a/19/TextQuery.h b/19/TextQuery.h new file mode 100644 index 0000000..ed6250b --- /dev/null +++ b/19/TextQuery.h @@ -0,0 +1,99 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef TEXTQUERY_H +#define TEXTQUERY_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class TextQuery { +public: + class QueryResult; // nested class to be defined later + // other members as in XREF(TextQueryClass) + typedef std::vector::size_type line_no; + TextQuery(std::ifstream&); + QueryResult query(const std::string&) const; + void display_map(); // debugging aid: print the map +private: + std::shared_ptr> file; // input file + // map of each word to the set of the lines in which that word appears + std::map>> wm; + // characters that constitute whitespace + static std::string whitespace_chars; + // canonicalizes text: removes punctuation and makes everything lower case + static std::string cleanup_str(const std::string&); +}; + +//we're defining the QueryResult class that is a member of class TextQuery +class TextQuery::QueryResult { + // in class scope, we don't have to qualify the name of the QueryResult parameters + friend std::ostream& + print(std::ostream&, const QueryResult&); +public: + // no need to define QueryResult::line_no; a nested class can use a member + // of its enclosing class without needing to qualify the member's name + QueryResult(std::string, + std::shared_ptr>, + std::shared_ptr>); + // other members as in XREF(TextQueryClass) + typedef std::set::const_iterator line_it; + std::set::size_type size() const { return lines->size(); } + line_it begin() const { return lines->cbegin(); } + line_it end() const { return lines->cend(); } + std::shared_ptr> get_file() { return file; } +private: + std::string sought; // word this query represents + std::shared_ptr> lines; // lines it's on + std::shared_ptr> file; //input file + static int static_mem; +}; +std::ostream &print(std::ostream&, const TextQuery::QueryResult&); + +using std::shared_ptr; using std::string; using std::set; +using std::vector; + +inline +// defining the member named QueryResult for the class named QueryResult +// that is nested inside the class TextQuery +TextQuery::QueryResult::QueryResult(string s, + shared_ptr> p, + shared_ptr> f): + sought(s), lines(p), file(f) { } +#endif diff --git a/19/Token.h b/19/Token.h new file mode 100644 index 0000000..06fd0bf --- /dev/null +++ b/19/Token.h @@ -0,0 +1,140 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef TOKEN_H +#define TOKEN_H + +#include +using std::string; + +#include + +#include "Token.h" + +class Token { +friend std::ostream &operator<<(std::ostream&, const Token&); +public: + // copy control needed because our class has a union with a string member + // defining the move constructor and move-assignment operator is left as an exercise + Token(): tok(INT), ival{0} { } + Token(const Token &t): tok(t.tok) { copyUnion(t); } + Token &operator=(const Token&); + // if the union holds a string, we must destroy it; see XREF(expldtor) + ~Token() { if (tok == STR) sval.~string(); } + // assignment operators to set the differing members of the union + Token &operator=(const std::string&); + Token &operator=(char); + Token &operator=(int); + Token &operator=(double); +private: + enum {INT, CHAR, DBL, STR} tok; // discriminant + union { // anonymous union + char cval; + int ival; + double dval; + std::string sval; + }; // each Token object has an unnamed member of this unnamed union type + // check the discriminant and copy the union member as appropriate + void copyUnion(const Token&); +}; + +inline +void Token::copyUnion(const Token &t) +{ + switch (t.tok) { + case Token::INT: ival = t.ival; break; + case Token::CHAR: cval = t.cval; break; + case Token::DBL: dval = t.dval; break; + // to copy a string, construct it using placement new; see (XREF(placenew)) + case Token::STR: new(&sval) std::string(t.sval); break; + } +} + +inline +std::ostream &operator<<(std::ostream &os, const Token &t) +{ + switch (t.tok) { + case Token::INT: return os << t.ival; + case Token::CHAR: return os << t.cval; + case Token::DBL: return os << t.dval; + case Token::STR: return os << t.sval; + } +} + +inline +Token &Token::operator=(double d) +{ + if (tok == STR) sval.~string(); // if we have a string, free it + dval = d; + tok = DBL; + return *this; +} + +inline +Token &Token::operator=(char c) +{ + if (tok == STR) sval.~string(); // if we have a string, free it + cval = c; + tok = CHAR; + return *this; +} + +inline +Token &Token::operator=(int i) +{ + if (tok == STR) sval.~string(); // if we have a string, free it + ival = i; // assign to the appropriate member + tok = INT; // update the discriminant + return *this; +} + +inline +Token &Token::operator=(const std::string &s) +{ + if (tok == STR) // if we already hold a string, just do an assignment + sval = s; + else + new(&sval) std::string(s); // otherwise construct a string + tok = STR; // update the discriminant + return *this; +} + +inline +Token &Token::operator=(const Token &t) +{ + // if this object holds a string and t doesn't, we have to free the old string + if (tok == STR && t.tok != STR) sval.~string(); + if (tok == STR && t.tok == STR) + sval = t.sval; // no need to construct a new string + else + copyUnion(t); // will construct a string if t.tok is STR + tok = t.tok; + return *this; +} +#endif diff --git a/19/bitfields.cc b/19/bitfields.cc new file mode 100644 index 0000000..f3c96f4 --- /dev/null +++ b/19/bitfields.cc @@ -0,0 +1,90 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced. Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ +#include +using std::cout; using std::endl; + +typedef unsigned int Bit; + +class File { + Bit mode: 2; // mode has 2 bits + Bit modified: 1; // modified has 1 bit + Bit prot_owner: 3; // prot_owner has 3 bits + Bit prot_group: 3; // prot_group has 3 bits + Bit prot_world: 3; // prot_world has 3 bits + // operations and data members of File +public: + // file modes specified as octal literals; see XREF(intLiterals) + enum modes { READ = 01, WRITE = 02, EXECUTE = 03 }; + File &open(modes); + void close(); + void write(); + bool isRead() const; + void setWrite(); + void execute(); + bool isExecute() const; +}; + +void File::write() +{ + modified = 1; + // . . . +} + +void File::close() +{ + if (modified) + // . . . save contents + ; +} + +inline bool File::isRead() const { return mode & READ; } +inline void File::setWrite() { mode |= WRITE; } + + +File &File::open(File::modes m) +{ + mode |= READ; // set the READ bit by default + // other processing + if (m & WRITE) // if opening READ and WRITE + // processing to open the file in read/write mode + cout << "myFile.mode READ is set" << endl; + return *this; +} + +int main() +{ + File myFile; + + myFile.open(File::READ); + if (myFile.isRead()) + cout << "reading" << endl; + + return 0; +} + diff --git a/19/calc.cc b/19/calc.cc new file mode 100644 index 0000000..b687b2f --- /dev/null +++ b/19/calc.cc @@ -0,0 +1,105 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; +using std::ostream; + +#include +using std::map; + +#include +using std::string; + +#include +using std::bind; using std::function; +using namespace std::placeholders; + +// this version of the desk calculator adds a class that +// represents both the left and right shift operators. +// This calss illustrates using pointers to member +// in the desk calculator +struct ShiftOps { + ShiftOps(std::ostream &o) : os(o) { } + unsigned Lshift(unsigned op1, unsigned op2) + { os << "Lshift: " << op1 << " " << op2; return op1 << op2; } + unsigned Rshift(unsigned op1, unsigned op2) + { os << "Rshift: " << op1 << " " << op2; return op1 >> op2; } +private: + std::ostream &os; +}; + +// ordinary function +int add(int i, int j) { return i + j; } + +// lambda, which generates an unnamed function-object class +auto mod = [](int i, int j) { return i % j; }; + +// function-object class +// Note, in the first printing of The Primer this struct was named div +struct divide { + int operator()(int denominator, int divisor) { + return denominator / divisor; + } +}; + +int main() +{ + function f1 = add; // function pointer + function f2 = divide(); // callable class type + function f3 = [](int i, int j) // lambda + { return i * j; }; + cout << f1(4,2) << endl; // prints 6 + cout << f2(4,2) << endl; // prints 2 + cout << f3(4,2) << endl; // prints 8 + + // table of callable objects corresponding to each binary operator + // all the callables must take two ints and return an int + // an element can be a function pointer, function object, or lambda + map> binops = { + {"+", add}, // function pointer + {"-", std::minus()}, // library function object + {"/", divide()}, // user-defined function object + {"*", [](int i, int j) { return i * j; }}, // unnamed lambda + {"%", mod} }; // named lambda object + + cout << binops["+"](10, 5) << endl; // calls add(10, 5) + cout << binops["-"](10, 5) << endl; // uses the call operator of the minus object + cout << binops["/"](10, 5) << endl; // uses the call operator of the divide object + cout << binops["*"](10, 5) << endl; // calls the lambda function object + cout << binops["%"](10, 5) << endl; // calls the lambda function object + + // memp can point to either shift operation in ShiftOps + // memp points to the Lshift member + function memp = &ShiftOps::Lshift; + + ShiftOps shift(cout); // declare an object to which to bind the member pointer + binops.insert({"<<", bind(memp, &shift, _1, _2)}); + cout << binops["<<"](10, 5) << endl; // calls member function +} diff --git a/19/dyn_cast.cc b/19/dyn_cast.cc new file mode 100644 index 0000000..0727b6b --- /dev/null +++ b/19/dyn_cast.cc @@ -0,0 +1,98 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::bad_cast; + +#include +using std::cout; using std::endl; + +struct A { virtual ~A() { } }; +struct B : virtual public A { /* . . . */ }; +struct C : public B { /* . . . */ }; +struct D : public B, virtual public A { /* . . . */ }; + +void exercises() { + A *pa = new C; + if (B *pb = dynamic_cast< B* >(pa)) + cout << "cast from C to B* ok" << endl; + else + cout << "cast from C to B* not ok" << endl; + B *pb = new B; + if (C *pc = dynamic_cast< C* >(pb)) + cout << "cast from B to C* ok" << endl; + else + cout << "cast from B to C* not ok" << endl; + + A *pc = new C; + if (B *pb = dynamic_cast< B* >(pc)) + cout << "cast C to B* ok" << endl; + else + cout << "cast C to B* not ok" << endl; + + A *pd = new D; + if (B *pb = dynamic_cast< B* >(pd)) + cout << "cast D to B* ok" << endl; + else + cout << "cast D to B* not ok" << endl; +} + +struct Base { + virtual ~Base() {}; +}; + +struct Derived: public Base { }; + +void cast_to_ref(const Base &b) +{ + try { + const Derived &d = dynamic_cast(b); + // use the Derived object to which b referred + } catch (bad_cast) { + cout << "called f with an object that is not a Derived" << endl; + } +} + +int main() +{ + Base *bp; + bp = new Derived; // bp actually points to a Derived object + if (Derived *dp = dynamic_cast(bp)) + { + // use the Derived object to which dp points + } else { // bp points at a Base object + // use the Base object to which bp points + } + + exercises(); + + cast_to_ref(*bp); + cast_to_ref(Base()); +} + diff --git a/19/enum-match.cc b/19/enum-match.cc new file mode 100644 index 0000000..0c62a6e --- /dev/null +++ b/19/enum-match.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +// unscoped enumeration; the underlying type is machine dependent +enum Tokens {INLINE = 128, VIRTUAL = 129}; + +void ff(Tokens) +{ + cout << "ff(Tokens)" << endl; +} +void ff(int) +{ + cout << "ff(int)" << endl; +} +void newf(int) +{ + cout << "newf(int)" << endl; +} +void newf(unsigned char) +{ + cout << "newf(unsigned char)" << endl; +} + +int main() { + Tokens curTok = INLINE; + ff(128); // exactly matches ff(int) + ff(INLINE); // exactly matches ff(Tokens) + ff(curTok); // exactly matches ff(Tokens) + + void newf(unsigned char); + void newf(int); + unsigned char uc = VIRTUAL; + + newf(VIRTUAL); // calls newf(int) + newf(uc); // calls newf(unsigned char) + + newf(129); // calls newf(int) + + return 0; +} diff --git a/19/enum.cc b/19/enum.cc new file mode 100644 index 0000000..c2f1576 --- /dev/null +++ b/19/enum.cc @@ -0,0 +1,79 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::size_t; + +// forward declaration of unscoped enum named intValues +enum intValues : unsigned long long; // unscoped, must specify a type +enum class open_modes; // scoped enums can use int by default + +enum class open_modes {input, output, append}; + +// input is an enumerator of open_modes +open_modes om = open_modes::input; + +enum class intTypes { + charTyp = 8, shortTyp = 16, intTyp = 16, + longTyp = 32, long_longTyp = 64 +}; + +constexpr intTypes charbits = intTypes::charTyp; + +enum intValues : unsigned long long { + charTyp = 255, shortTyp = 65535, intTyp = 65535, + longTyp = 4294967295UL, + long_longTyp = 18446744073709551615ULL +}; + +int main() +{ + enum color {red, yellow, green}; // unscoped enumeration + enum class peppers {red, yellow, green}; // enumerators are hidden + + // unnamed, unscoped enum + enum {floatPrec = 6, doublePrec = 10, double_doublePrec = 10}; + + // enumerators are in scope for an unscoped enumeration + color eyes = green; + + color hair = color::red; // we can explicitly access the enumerators + peppers p2 = peppers::red; // using red from peppers + + int i = color::red; // unscoped enumerator implicitly converted to int + + // point2d is 2, point2w is 3, point3d is 3, point3w is 4 + enum class Points { point2d = 2, point2w, + point3d = 3, point3w }; + + return 0; +} diff --git a/19/makefile b/19/makefile new file mode 100644 index 0000000..89fd1e9 --- /dev/null +++ b/19/makefile @@ -0,0 +1,15 @@ +# executable files for this directory +OBJECTS = bitfields.exe calc.exe dyn_cast.exe enum.exe enum-match.exe \ + memFN-bind.exe tok2.exe typeid.exe useScreen.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../14 + +Screen.o useScreen.o: Screen.h + +useScreen.exe: useScreen.o Screen.o + $(CC) $(CCFLAGS) useScreen.o Screen.o -o useScreen.exe + diff --git a/19/memFN-bind.cc b/19/memFN-bind.cc new file mode 100644 index 0000000..5313215 --- /dev/null +++ b/19/memFN-bind.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +#include +using std::find_if; + +#include +using std::bind; +using std::placeholders::_1; + +int main() +{ + vector svec = {"hi", "bye"}; + // bind each string in the range to the implicit first argument to empty + auto it = find_if(svec.begin(), svec.end(), + bind(&string::empty, _1)); + if (it == svec.end()) + cout << "worked" << endl; + else + cout << "failed" << endl; + auto f = bind(&string::empty, _1); + f(*svec.begin()); // ok: argument is a string f will use .* to call empty + f(&svec[0]); // ok: argument is a pointer to string f will use .-> to call empty +} + + diff --git a/19/out2 b/19/out2 new file mode 100644 index 0000000..3ed2920 --- /dev/null +++ b/19/out2 @@ -0,0 +1,36 @@ +bitfields.exe +reading +calc.exe +6 +2 +8 +15 +5 +2 +50 +0 +Lshift: 10 5320 +dyn_cast.exe +cast from C to B* ok +cast from B to C* not ok +cast C to B* ok +cast D to B* ok +called f with an object that is not a Derived +enum.exe +enum-match.exe +ff(int) +ff(Tokens) +ff(Tokens) +newf(int) +newf(unsigned char) +newf(int) +memFN-bind.exe +worked +tok2.exe +0 0 +boo +good bye +42 +typeid.exe +i, A10_i, 10Sales_data, Ss, P4Base, 7Derived +useScreen.exe diff --git a/19/runpgms b/19/runpgms new file mode 100644 index 0000000..ab6330b --- /dev/null +++ b/19/runpgms @@ -0,0 +1,4 @@ +../noinput bitfields.exe calc.exe dyn_cast.exe enum.exe \ + enum-match.exe memFN-bind.exe tok2.exe typeid.exe \ + useScreen.exe + diff --git a/19/tok2.cc b/19/tok2.cc new file mode 100644 index 0000000..65f7f9a --- /dev/null +++ b/19/tok2.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include "Token.h" + +int main() +{ + Token token; + Token t2; + Token t3; + cout << t2 << " " << t3 << endl; + t2 = string("hi mom!"); + t3 = "good bye"; + token = t2; + token = "boo"; + cout << token << endl; + t2 = t3; + cout << t2 << endl; + + token = 42; + cout << token << endl; +} diff --git a/19/typeid.cc b/19/typeid.cc new file mode 100644 index 0000000..494bd38 --- /dev/null +++ b/19/typeid.cc @@ -0,0 +1,60 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" + +#include +using std::cout; using std::endl; + +#include + +#include +using std::string; + +struct Base { + virtual ~Base() { } +}; + +struct Derived : Base { }; + +int main() +{ + int arr[10]; + Derived d; + Base *p = &d; + + cout << typeid(42).name() << ", " + << typeid(arr).name() << ", " + << typeid(Sales_data).name() << ", " + << typeid(std::string).name() << ", " + << typeid(p).name() << ", " + << typeid(*p).name() << endl; + + return 0; +} diff --git a/19/useScreen.cc b/19/useScreen.cc new file mode 100644 index 0000000..badd83c --- /dev/null +++ b/19/useScreen.cc @@ -0,0 +1,115 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced. Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Screen.h" + +#include +using std::function; + +#include +using std::cout; using std::endl; + +#include +using std::string; + +struct X { + int foo(int i) { cout << "foo(" << i << ")" << endl; return i; } +}; + +void xfcn() +{ + function f; + f = &X::foo; // pointer to member + + X x; + int v = f(&x, 5); + cout << "v = " << v << endl; +} + +int main () +{ + // pdata can point to a string member of a const (or nonconst) Screen + const string Screen::*pdata; // uninitialized + pdata = &Screen::contents; // points to the contents member + auto pdata2 = &Screen::contents; // equivalent declaration + + // data() returns a pointer to the contents member of class Screen + const string Screen::*pdata3 = Screen::data(); + + // Screen objects + Screen myScreen, *pScreen = &myScreen; + const Screen cScreen, *pcScreen = &cScreen; + + // .* dereferences pdata to fetch the contents member + // from the object myScreen + auto str = myScreen.*pdata; // s is a string + auto cstr = cScreen.*pdata; // c is a const string + + // ->* dereferences pdata to fetch contents + // from the object to which pScreen points + str = pScreen->*pdata; + + // pmf is a pointer that can point to a Screen member function + // that takes no arguments, returns a char, and is const + // that returns a char and takes no arguments + auto pmf = &Screen::get_cursor; + char (Screen::*pmf2)() const = &Screen::get; // same type as pmf + + pmf = &Screen::get; // which version of get deduced from type of pmf + pmf2 = &Screen::get_cursor; + + Screen s; + char c1 = s.get_cursor(); // gets character at the cursor directly + char c2 = (s.*pmf2)(); // calls get_cursor indirectly through pmf2 + + // call the function to which pmf points + // on the object to which pScreen points + c1 = (pScreen->*pmf)(); + + // pmf3 points to the two-parameter version of get + char (Screen::*pmf3)(Screen::pos, Screen::pos) const = &Screen::get; + c1 = myScreen.get(0,0); // call two-parameter version of get + c2 = (myScreen.*pmf3)(0,0); // equivalent call to get + + // Op is a type that can point to a member function of Screen + // that returns a char and takes two pos arguments + using Op = char (Screen::*)(Screen::pos, Screen::pos) const; + // equivalent declaration of Op using a typedef + typedef char (Screen::*Op)(Screen::pos, Screen::pos) const; + + Op get = &Screen::get; // get points to the get member of Screen + + myScreen.move(Screen::HOME); // invokes myScreen.home + myScreen.move(Screen::DOWN); // invokes myScreen.down + + // bind an object of type function to a pointer to member + function f = &Screen::get_cursor; + + return 0; +} diff --git a/2/README b/2/README new file mode 100644 index 0000000..834d139 --- /dev/null +++ b/2/README @@ -0,0 +1,10 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + add_item Sales_data + +Programs not listed above print output and do +not read any input + diff --git a/2/Sales_data.cc b/2/Sales_data.cc new file mode 100644 index 0000000..fae73f2 --- /dev/null +++ b/2/Sales_data.cc @@ -0,0 +1,70 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include +#include "Sales_data.h" + +int main() +{ + Sales_data data1, data2; + + // code to read into data1 and data2 + double price = 0; // price per book, used to calculate total revenue + + // read the first transactions: ISBN, number of books sold, price per book + std::cin >> data1.bookNo >> data1.units_sold >> price; + // calculate total revenue from price and units_sold + data1.revenue = data1.units_sold * price; + + // read the second transaction + std::cin >> data2.bookNo >> data2.units_sold >> price; + data2.revenue = data2.units_sold * price; + + // code to check whether data1 and data2 have the same ISBN + // and if so print the sum of data1 and data2 + if (data1.bookNo == data2.bookNo) { + unsigned totalCnt = data1.units_sold + data2.units_sold; + double totalRevenue = data1.revenue + data2.revenue; + + // print: ISBN, total sold, total revenue, average price per book + std::cout << data1.bookNo << " " << totalCnt + << " " << totalRevenue << " "; + if (totalCnt != 0) + std::cout << totalRevenue/totalCnt << std::endl; + else + std::cout << "(no sales)" << std::endl; + + return 0; // indicate success + } else { // transactions weren't for the same ISBN + std::cerr << "Data must refer to the same ISBN" + << std::endl; + return -1; // indicate failure + } +} diff --git a/2/Sales_data.h b/2/Sales_data.h new file mode 100644 index 0000000..5968332 --- /dev/null +++ b/2/Sales_data.h @@ -0,0 +1,40 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef SALES_DATA_H +#define SALES_DATA_H + +#include + +struct Sales_data { + std::string bookNo; + unsigned units_sold = 0; + double revenue = 0.0; +}; +#endif diff --git a/2/compounddecls.cc b/2/compounddecls.cc new file mode 100644 index 0000000..e6026eb --- /dev/null +++ b/2/compounddecls.cc @@ -0,0 +1,56 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +int main() +{ + // i is an int; p is a pointer to int; r is a reference to int + int i = 1024, *p = &i, &r = i; + + // three ways to print the value of i + std::cout << i << " " << *p << " " << r << std::endl; + + int j = 42, *p2 = &j; + int *&pref = p2; // pref is a reference to the pointer p2 + + // prints the value of j, which is the int to which p2 points + std::cout << *pref << std::endl; + + // pref refers to a pointer; assigning &i to pref makes p point to i + pref = &i; + std::cout << *pref << std::endl; // prints the value of i + + // dereferencing pref yields i, the int to which p2 points; + *pref = 0; // changes i to 0 + + std::cout << i << " " << *pref << std::endl; + + return 0; +} + diff --git a/2/convs.cc b/2/convs.cc new file mode 100644 index 0000000..fbc0c35 --- /dev/null +++ b/2/convs.cc @@ -0,0 +1,57 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +int main() +{ + int i = 42; + std::cout << i << std::endl; // prints 42 + if (i) // condition will evaluate as true + i = 0; + std::cout << i << std::endl; // prints 0 + + bool b = 42; // b is true + std::cout << b << std::endl; // prints 1 + + int j = b; // j has value 1 + std::cout << j << std::endl; // prints 1 + + double pi = 3.14; // pi has value 3.14 + std::cout << pi << std::endl; // prints 3.14 + + j = pi; // j has value 3 + std::cout << j << std::endl; // prints 3 + + unsigned char c = -1; // assuming 8-bit chars, c has value 255 + i = c; // the character with value 255 is an unprintable character + // assigns value of c (i.e., 255) to an int + std::cout << i << std::endl; // prints 255 + + return 0; +} diff --git a/2/data/add_item b/2/data/add_item new file mode 100644 index 0000000..92fd61c --- /dev/null +++ b/2/data/add_item @@ -0,0 +1,2 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/2/data/data/add_item b/2/data/data/add_item new file mode 100644 index 0000000..92fd61c --- /dev/null +++ b/2/data/data/add_item @@ -0,0 +1,2 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/2/dbl_ptr.cc b/2/dbl_ptr.cc new file mode 100644 index 0000000..530ea91 --- /dev/null +++ b/2/dbl_ptr.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; +using std::endl; + +int main() +{ + int ival = 1024; + int *pi = &ival; // pi points to an int + int **ppi = π // ppi points to a pointer to an int + cout << "The value of ival\n" + << "direct value: " << ival << "\n" + << "indirect value: " << *pi << "\n" + << "doubly indirect value: " << **ppi + << endl; + + int i = 2; + int *p1 = &i; // p1 points to i + *p1 = *p1 * *p1; // equivalent to i = i * i + cout << "i = " << i << endl; + + *p1 *= *p1; // equivalent to i *= i + cout << "i = " << i << endl; + + return 0; +} diff --git a/2/decl-ex.cc b/2/decl-ex.cc new file mode 100644 index 0000000..2e2662f --- /dev/null +++ b/2/decl-ex.cc @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + int a = 0; + decltype(a) c = a; // c is an int + decltype((a)) d = a; // d is a reference to a + ++c; // increments c, a (and d) unchanged + cout << "a: " << a << " c: " << c << " d: " << d << endl; + ++d; // increments a through the reference d + cout << "a: " << a << " c: " << c << " d: " << d << endl; + + int A = 0, B = 0; + decltype((A)) C = A; // C is a reference to A + decltype(A = B) D = A; // D is also a reference to A + ++C; + cout << "A: " << A << " C: " << C << " D: " << D << endl; + ++D; + cout << "A: " << A << " C: " << C << " D: " << D << endl; + + return 0; +} diff --git a/2/escape.cc b/2/escape.cc new file mode 100644 index 0000000..f206e0b --- /dev/null +++ b/2/escape.cc @@ -0,0 +1,39 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() { + std::cout << '\n'; // prints a newline + std::cout << "\tHi!\n"; // prints a tab followd by "Hi!" and a newline + std::cout << "Hi \x4dO\115!\n"; // prints Hi MOM! followed by a newline + std::cout << '\115' << '\n'; // prints M followed by a newline + + return 0; +} diff --git a/2/inits.cc b/2/inits.cc new file mode 100644 index 0000000..86c1ddc --- /dev/null +++ b/2/inits.cc @@ -0,0 +1,49 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_item.h" +#include +#include + + +int main() { + + int v1(1024); // direct-initialization, functional form + int v2{1024}; // direct-initialization, list initializer form + int v3 = 1024; // copy-initialization + int v4 = {1024}; // copy-initialization, list initializer form + + + // alternative ways to initialize string from a character string literal + std::string titleA = "C++ Primer, 5th Ed."; + std::string titleB("C++ Primer, 5th Ed."); + std::string all_nines(10, '9'); // all_nines = "9999999999" + + return 0; +} diff --git a/2/literals.cc b/2/literals.cc new file mode 100644 index 0000000..ed35542 --- /dev/null +++ b/2/literals.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +int main() +{ + std::cout << "Hello World!"; // simple character string literal + std::cout << ""; // empty character string literal + // literal using newlines and tabs + std::cout << "\nCC\toptions\tfile.[cC]\n"; + + // multiline string literal + std::cout << "a really, really long string literal " + "that spans two lines" << std::endl; + + // three ways to print a capital M + std::cout << 'M' << " " << '\115' << " " << '\x4d' << std::endl; + + unsigned long long bigVal = -1ULL; + std::cout << bigVal << std::endl; + + return 0; +} + diff --git a/2/makefile b/2/makefile new file mode 100644 index 0000000..6801cc4 --- /dev/null +++ b/2/makefile @@ -0,0 +1,11 @@ +# executable files for this directory +OBJECTS = compounddecls.exe convs.exe dbl_ptr.exe \ + decl-ex.exe escape.exe inits.exe \ + literals.exe ref-ex.exe Sales_data.exe \ + scope_levels.exe unsigned.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 diff --git a/2/ref-ex.cc b/2/ref-ex.cc new file mode 100644 index 0000000..24c5d03 --- /dev/null +++ b/2/ref-ex.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +int main() +{ + int i = 0, &ri = i; // ri is a reference to i + // ri is just another name for i; + // this statement prints the value of i twice + std::cout << i << " " << ri << std::endl; + + i = 5; // changing i is reflected through ri as well + std::cout << i << " " << ri << std::endl; + + ri = 10; // assigning to ri actually assigns to i + std::cout << i << " " << ri << std::endl; + + return 0; +} diff --git a/2/runpgms b/2/runpgms new file mode 100644 index 0000000..66fd34b --- /dev/null +++ b/2/runpgms @@ -0,0 +1,6 @@ +echo "Sales_data:" && ./Sales_data < data/add_item + +../noinput compounddecls.exe convs.exe dbl_ptr.exe \ + decl-ex.exe escape.exe inits.exe \ + literals.exe ref-ex.exe \ + scope_levels.exe unsigned.exe diff --git a/2/runpgms.bat b/2/runpgms.bat new file mode 100644 index 0000000..3d93c39 --- /dev/null +++ b/2/runpgms.bat @@ -0,0 +1,7 @@ +echo on +Sales_data < data/add_item + +for %%i in (compounddecls.exe convs.exe dbl_ptr.exe + decl-ex.exe escape.exe inits.exe + literals.exe ref-ex.exe + scope_levels.exe unsigned.exe ) do %%i diff --git a/2/scope_levels.cc b/2/scope_levels.cc new file mode 100644 index 0000000..c622b04 --- /dev/null +++ b/2/scope_levels.cc @@ -0,0 +1,53 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +// Program for illustration purposes only: It is bad style for a function +// to use a global variable and also define a local variable with the same name + +int reused = 42; // reused has global scope + +int main() +{ + int unique = 0; // unique has block scope + + // output #1: uses global reused; prints 42 0 + std::cout << reused << " " << unique << std::endl; + + int reused = 0; // new, local object named reused hides global reused + + // output #2: uses local reused; prints 0 0 + std::cout << reused << " " << unique << std::endl; + + // output #3: explicitly requests the global reused; prints 42 0 + std::cout << ::reused << " " << unique << std::endl; + + return 0; +} diff --git a/2/unsigned.cc b/2/unsigned.cc new file mode 100644 index 0000000..86bde70 --- /dev/null +++ b/2/unsigned.cc @@ -0,0 +1,89 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +int main() +{ + unsigned u = 10, u2 = 42; + std::cout << u2 - u << std::endl; + std::cout << u - u2 << std::endl; + + int i = 10, i2 = 42; + std::cout << i2 - i << std::endl; + std::cout << i - i2 << std::endl; + + u = 42; + i = 10; + std::cout << i - u << std::endl; + std::cout << u - i << std::endl; + + u = 10; + i = -42; + std::cout << i + i << std::endl; // prints -84 + std::cout << u + i << std::endl; // if 32-bit ints, prints 4294967264 + + i = 10; + std::cout << "good" << std::endl; + while (i >= 0) { + std::cout << i << std::endl; + --i; + } + + for (int i = 10; i >= 0; --i) + std::cout << i << std::endl; + + for (unsigned u = 0; u <= 10; ++u) + std::cout << u << std::endl; // prints 0 . . . 10 + +/* NOTE: the condition in the following loop + will run indefinitely + // WRONG: u can never be less than 0; the condition will always succeed + for (unsigned u = 10; u >= 0; --u) + std::cout << u << std::endl; +*/ + u = 11; // start the loop one past the first element we want to print + while (u > 0) { + --u; // decrement first, so that the last iteration will print 0 + std::cout << u << std::endl; + } + + // be wary of comparing ints and unsigned + u = 10; + i = -42; + if (i < u) // false: i is converted to unsigned + std::cout << i << std::endl; + else + std::cout << u << std::endl; // prints 10 + + u = 42; u2 = 10; + std::cout << u - u2 << std::endl; // ok: result is 32 + std::cout << u2 - u << std::endl; // ok: but the result will wrap around +} + diff --git a/3/README b/3/README new file mode 100644 index 0000000..0c93687 --- /dev/null +++ b/3/README @@ -0,0 +1,21 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + add add_using + grades arrayScores + grades vecScores + hexify hexify + ptr_traversal2 ptr_traversal2 + string_io string_io + string_io2 string_io + vecStrings2 vecStrings2 + word_echo getline + word_echo string_size2 + word_echo string_size3 + word_echo word_echo + +Programs not listed above print output and do +not read any input + diff --git a/3/add_using.cc b/3/add_using.cc new file mode 100644 index 0000000..356c179 --- /dev/null +++ b/3/add_using.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include + +// using declarations for names from the standard library +using std::cin; +using std::cout; using std::endl; + +int main() +{ + cout << "Enter two numbers:" << endl; + + int v1, v2; + cin >> v1 >> v2; + + cout << "The sum of " << v1 << " and " << v2 + << " is " << v1 + v2 << endl; + + return 0; +} diff --git a/3/arrayScores.cc b/3/arrayScores.cc new file mode 100644 index 0000000..918d25f --- /dev/null +++ b/3/arrayScores.cc @@ -0,0 +1,62 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + + +int main () +{ + vector grades; + // count the number of grades by clusters of ten: + // 0--9, 10--19, . . . 90--99, 100 + unsigned scores[11] = {}; // 11 buckets, all value initialized to 0 + unsigned grade; + while (cin >> grade) { + if (grade <= 100) + // increment the counter for the current cluster + ++scores[grade/10]; + grades.push_back(grade); + } + cout << "grades.size = " << grades.size() << endl; + + for (auto g : grades) // for every element in grades + cout << g << " " ; + cout << endl; + + for (auto i : scores) // for each counter in scores + cout << i << " "; // print the value of that counter + cout << endl; +} diff --git a/3/cctype.cc b/3/cctype.cc new file mode 100644 index 0000000..88a808a --- /dev/null +++ b/3/cctype.cc @@ -0,0 +1,81 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::isupper; using std::toupper; +using std::islower; using std::tolower; +using std::isalpha; using std::isspace; + +#include +using std::cout; using std::endl; + +int main() +{ + string s("Hello World!!!"); + // punct_cnt has the same type that s.size returns + decltype(s.size()) punct_cnt = 0; + + // count the number of punctuation characters in s + for (auto c : s) // for every char in s + if (ispunct(c)) // if the character is punctuation + ++punct_cnt; // increment the punctuation counter + + cout << punct_cnt + << " punctuation characters in " << s << endl; + + // convert s to uppercase + string orig = s; + for (auto &c : s) // for every char in s (note: c is a reference) + // c is a reference, so this assignment changes the char in s + c = toupper(c); + cout << s << endl; + + // convert first word in s to uppercase + s = orig; // restore s to original case + decltype(s.size()) index = 0; + + // process characters in s until we run out of characters + // or we hit a whitespace + while (index != s.size() && !isspace(s[index])) { + + // s[index] returns a reference so we can change + // the underlying character + s[index] = toupper(s[index]); + + // increment the index to look at the next character + // on the next iteration + ++index; + } + cout << s << endl; + + return 0; +} diff --git a/3/char_repl.cc b/3/char_repl.cc new file mode 100644 index 0000000..aa97fa5 --- /dev/null +++ b/3/char_repl.cc @@ -0,0 +1,126 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + string str("some string"), orig = str; + if (!str.empty()) // make sure there's a character to print + cout << str[0] << endl;// print the first character in str + + if (!str.empty()) // make sure there's a character in str[0] + // assign a new value to the first character in str + str[0] = toupper(str[0]); + cout << str << endl; + + str = orig; // restore str to its original value + + // equivalent code using iterators instead of subscripts + if (str.begin() != str.end()) { // make sure str is not empty + auto it = str.begin(); // it denotes the first character in str + *it = toupper(*it); // make that character uppercase + } + cout << str << endl; + + str = orig; // restore str to its original value + + // four wasy to capitalize first word in str: + // 1. for loop with subscripts + for (decltype(str.size()) index = 0; + index != str.size() && !isspace(str[index]); ++index) + str[index] = toupper(str[index]); // capitalize the current character + cout << str << endl; + + str = orig; // restore str to its original value + + // 2. for loop with iterators instead of subscripts + for (auto it = str.begin(); it != str.end() && !isspace(*it); ++it) + *it = toupper(*it); // capitalize the current character + cout << str << endl; + + str = orig; // restore str to its original value + + // 3. while instead of a for with subscripts + decltype(str.size()) index = 0; // subscript to look at characters in str + while (index != str.size() && !isspace(str[index])) { + str[index] = toupper(str[index]); // capitalize the current character + ++index; // advance the index to get the next character + } + cout << str << endl; + + // 4. while loop with iterators + auto beg = str.begin(); + while (beg != str.end() && !isspace(*beg)) { + *beg = toupper(*beg); + ++beg; + } + cout << str << endl; + + str = orig; // restore str to its original value + + // range for loop to process every character + // first a loop to print the characters in str one character to a line + for (auto c : str) // for every char in str + cout << c << endl; // print the current character followed by a newline + + // next change every character in str + for (auto &c : str) // note: c is a reference + c = '*'; // assign a new value to the underlying char in str + cout << str << endl; + + str = orig; // restore str to its original value + + // equivalent code using traditional for loops + // first print the characters in str + for (decltype(str.size()) ix = 0; ix != str.size(); ++ix) + cout << str[ix] << endl; // print current character followd by a newline + + // next change every character in str + for (decltype(str.size()) ix = 0; ix != str.size(); ++ix) + str[ix] = '*'; // assigns a new value to the character in str + cout << str << endl; + + str = orig; // restore str to its original value + + // equivalent code using traditional for loops and iterators + // first print the characters in str + for (auto beg = str.begin(); beg != str.end(); ++beg) + cout << *beg << endl; // print current character followd by a newline + + // next change every character in str + for (auto beg = str.begin(); beg != str.end(); ++beg) + *beg = '*'; // assigns a new value to the character in str + cout << str << endl; + return 0; +} diff --git a/3/cstring_hdr.cc b/3/cstring_hdr.cc new file mode 100644 index 0000000..8d0bf87 --- /dev/null +++ b/3/cstring_hdr.cc @@ -0,0 +1,86 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include + +#include +using std::size_t; + +int main() { + string s1 = "A string example"; + string s2 = "A different string"; + + if (s1 < s2) // false: s2 is less than s1 + cout << s1 << endl; + else + cout << s2 << endl; + const char ca1[] = "A string example"; + const char ca2[] = "A different string"; + + if (strcmp(ca1, ca2) < 0) // same effect as string comparison s1 < s2 + cout << ca1 << endl; + else + cout << ca2 << endl; + + const char *cp1 = ca1, *cp2 = ca2; + cout << strcmp(cp1, cp2) << endl; // output is positive + cout << strcmp(cp2, cp1) << endl; // output is negative + cout << strcmp(cp1, cp1) << endl; // output is zero + + + cout << strlen(cp1) << endl; // prints 16; strlen ignores the null + + const unsigned sz = 16 + 18 + 2; + char largeStr[sz]; // will hold the result + // disastrous if we miscalculated the size of largeStr + strcpy(largeStr, ca1); // copies ca1 into largeStr + strcat(largeStr, " "); // adds a space at the end of largeStr + strcat(largeStr, ca2); // concatenates ca2 onto largeStr + // prints A string example A different string + cout << largeStr << endl; + + strncpy(largeStr, ca1, sz); // size to copy includes the null + if (strlen(ca1) > sz) + largeStr[sz-1] = '\0'; + strncat(largeStr, " ", 2); // pedantic, but a good habit + strncat(largeStr, ca2, sz - strlen(largeStr)); + cout << largeStr << endl; + + // initialize large_string as a concatenation of s1, a space, and s2 + string large_string = s1 + " " + s2; + cout << large_string << endl; + + return 0; +} diff --git a/3/data/add b/3/data/add new file mode 100644 index 0000000..a07a471 --- /dev/null +++ b/3/data/add @@ -0,0 +1 @@ +3 7 diff --git a/3/data/grades b/3/data/grades new file mode 100644 index 0000000..de39b73 --- /dev/null +++ b/3/data/grades @@ -0,0 +1,14 @@ +45 +65 +99 +100 +34 +67 +95 +76 +88 +75 +83 +92 +76 +93 diff --git a/3/data/hexify b/3/data/hexify new file mode 100644 index 0000000..616d361 --- /dev/null +++ b/3/data/hexify @@ -0,0 +1 @@ +12 0 5 15 8 15 diff --git a/3/data/iterarith b/3/data/iterarith new file mode 100644 index 0000000..fcaa91d --- /dev/null +++ b/3/data/iterarith @@ -0,0 +1 @@ +now is the time for all good boys to use their sense diff --git a/3/data/ptr_traversal2 b/3/data/ptr_traversal2 new file mode 100644 index 0000000..09b0ff3 --- /dev/null +++ b/3/data/ptr_traversal2 @@ -0,0 +1 @@ +345 42 5 34 8 -20 16 35 100 200 diff --git a/3/data/string_io b/3/data/string_io new file mode 100644 index 0000000..a882a97 --- /dev/null +++ b/3/data/string_io @@ -0,0 +1 @@ + Hello World! diff --git a/3/data/vecStrings2 b/3/data/vecStrings2 new file mode 100644 index 0000000..2be983a --- /dev/null +++ b/3/data/vecStrings2 @@ -0,0 +1,9 @@ +When we subscript a string, we get back a character. +When we subscript a vector, we get an object of whatever type the vector contains. +We can use the resulting (unnamed) object in exactly the same way we can use any other object of that type. +In particular, if the type is a class type, such as a library string, we can call member functions or use operators defined by the element type. + +As an example, let's write a program to capitalize the first word of every sentence in the first paragraph of some text. +We'll assume that our input contains a series of lines. +Each line is either a complete sentence or it contains only a newline. +Lines containing a only a newline separate paragraphs. diff --git a/3/data/word_echo b/3/data/word_echo new file mode 100644 index 0000000..e544f33 --- /dev/null +++ b/3/data/word_echo @@ -0,0 +1,2 @@ +Now is the time for all good boys +to come to the aid of the party diff --git a/3/getline.cc b/3/getline.cc new file mode 100644 index 0000000..95764b1 --- /dev/null +++ b/3/getline.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; using std::getline; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + string line; + + // read input a line at a time until end-of-file + while (getline(cin, line)) + cout << line << endl; + + return 0; +} diff --git a/3/hexify.cc b/3/hexify.cc new file mode 100644 index 0000000..fa43071 --- /dev/null +++ b/3/hexify.cc @@ -0,0 +1,56 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::size_t; + +int main() +{ + const string hexdigits = "0123456789ABCDEF"; // possible hex digits + + cout << "Enter a series of numbers between 0 and 15" + << " separated by spaces. Hit ENTER when finished: " + << endl; + string result; // will hold the resulting hexify'd string + + string::size_type n; // hold numbers from the input + while (cin >> n) + if (n < hexdigits.size()) // ignore invalid input + result += hexdigits[n]; // fetch the indicated hex digit + + cout << "Your hex number is: " << result << endl; + + return 0; +} diff --git a/3/makefile b/3/makefile new file mode 100644 index 0000000..7482f1b --- /dev/null +++ b/3/makefile @@ -0,0 +1,15 @@ +# executable files for this directory +OBJECTS = add_using.exe arrayScores.exe cctype.exe \ + char_repl.exe cstring_hdr.exe getline.exe \ + hexify.exe multidim.exe \ + ptr_traversal2.exe square.exe strcompare.exe \ + string_add.exe string_io.exe string_io2.exe \ + string_size.exe string_size2.exe string_size3.exe \ + vec_decls.exe vecStrings2.exe vecScores.exe \ + vecSubs.exe word_echo.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 diff --git a/3/multidim.cc b/3/multidim.cc new file mode 100644 index 0000000..579e833 --- /dev/null +++ b/3/multidim.cc @@ -0,0 +1,141 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::begin; using std::end; + +#include +using std::vector; +#include +using std::cout; using std::endl; + +#include +using std::size_t; + +int main() +{ + // array of size 3; each element is an array of 4 uninitailzed ints + int ia1[3][4]; + + // array of size 10; each element is a 20-element array + // whose elements are arrays of 30 ints + int arr[10][20][30] = {0}; // initialize all elements to 0 + + // assigns the first element of arr to the last element + // in the last row of ia + ia1[2][3] = arr[0][0][0]; + + // binds row to the second four-element array in ia + int (&row)[4] = ia1[1]; + + // three elements, each element is an array of size 4 + int ia2[3][4] = { + {0, 1, 2, 3}, // initializers for the row indexed by 0 + {4, 5, 6, 7}, // initializers for the row indexed by 1 + {8, 9, 10, 11} // initializers for the row indexed by 2 + }; + + // equivalent initialization without the optional + // nested braces for each row + int ia3[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; + + // explicitly initialize only element 0 in each row + int ia4[3][4] = {{ 0 }, { 4 }, { 8 }}; + + // explicitly initialize row 0; the remaining elements + // are value initialized + int ix[3][4] = {0, 3, 6, 9}; + + // prints 9 0 0 + cout << ix[0][3] << ' ' << ix[1][0] << ' ' << ix[2][0] << endl; + + constexpr size_t rowCnt = 3, colCnt = 4; + int ia[rowCnt][colCnt]; // 12 uninitialized elements + + // for each row + for (size_t i = 0; i != rowCnt; ++i) { + // for each column within the row + for (size_t j = 0; j != colCnt; ++j) { + // assign the element's positional index as its value + ia[i][j] = i * colCnt + j; + } + } + + // four ways to print the contents of ia + // 1. using nested range for loops + for (const auto &row : ia) // for every element in the outer array + for (auto col : row) // for every element in the inner array + cout << col << endl; // print the element's value + cout << ia[0][0] << ' ' << ia[2][3] << endl; // prints 0 11 + + + // 2. using pointers and a traditional for loop + // with pointer arithmetic to calculate the end pointers + for (auto p = ia; p != ia + rowCnt; ++p) { + // q points to the first element of an array of four ints; + // that is, q points to an int + for (auto q = *p; q != *p + colCnt; ++q) + cout << *q << ' '; + cout << endl; + } + + // 3. using pointers and a traditional for loop + // with the library begin and end functions to manage the pointers + for (auto p = begin(ia); p != end(ia); ++p) { + // q points to the first element in an inner array + for (auto q = begin(*p); q != end(*p); ++q) + cout << *q << ' '; // prints the int value to which q points + cout << endl; + } + + // 4. using a type alias to declare the loop control variable + using int_array = int[4]; // new style type alias declaration + + for (int_array *p = ia; p != ia + 3; ++p) { + for (int *q = *p; q != *p + 4; ++q) + cout << *q << ' '; + cout << endl; + } + + // alternative way to assign positional index to elements + // in a two-dimensional array + int alt_ia[rowCnt][colCnt]; // 12 uninitialized elements + size_t cnt = 0; + for (auto &row : alt_ia) // for every element in the outer array + for (auto &col : row) { // for every element in the inner array + col = cnt; // give this element the next value + ++cnt; // increment cnt + } + // now print the value of the array + for (const auto &row : alt_ia) // for every element in the outer array + for (auto col : row) // for every element in the inner array + cout << col << endl; + + return 0; +} diff --git a/3/ptr_traversal2.cc b/3/ptr_traversal2.cc new file mode 100644 index 0000000..3c8d419 --- /dev/null +++ b/3/ptr_traversal2.cc @@ -0,0 +1,75 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::begin; using std::end; + +#include +using std::size_t; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + int ia[] = {0,1,2,3,4,5,6,7,8,9}; + + int *p = ia; // p points to the first element in ia + ++p; // p points to ia[1] + + int *e = &ia[10]; // pointer just past the last element in ia + for (int *b = ia; b != e; ++b) + cout << *b << " "; // print the elements in ia + cout << endl; + + const size_t sz = 10; + int arr[sz]; // array of 10 ints + + for (auto &n : arr) // for each element in arr + cin >> n; // read values from the standard input + + for (auto i : arr) + cout << i << " "; + cout << endl; + + // pbeg points to the first and + // pend points just past the last element in arr + int *pbeg = begin(arr), *pend = end(arr); + + // find the first negative element, + // stopping if we've seen all the elements + while (pbeg != pend && *pbeg >= 0) + ++pbeg; + if (pbeg == pend) + cout << "no negative elements in arr" << endl; + else + cout << "first negative number was " << *pbeg << endl; + return 0; +} + diff --git a/3/runpgms b/3/runpgms new file mode 100644 index 0000000..516b108 --- /dev/null +++ b/3/runpgms @@ -0,0 +1,17 @@ +echo "add:" && ./add_using < data/add +echo "arrayScores:" && ./arrayScores < data/grades +echo "hexify:" && ./hexify < data/hexify +echo "ptr_traversal2:" && ./ptr_traversal2 < data/ptr_traversal2 +echo "string_io:" && ./string_io < data/string_io +echo "string_io2:" && ./string_io2 < data/string_io +echo "vecStrings2:" && ./vecStrings2 < data/vecStrings2 +echo "vecScores:" && ./vecScores < data/grades +echo "getline:" && ./getline < data/word_echo +echo "string_size2:" && ./string_size2 < data/word_echo +echo "string_size3:" && ./string_size3 < data/word_echo +echo "word_echo:" && ./word_echo < data/word_echo + +../noinput cctype.exe char_repl.exe cstring_hdr.exe \ + multidim.exe square.exe strcompare.exe \ + string_add.exe string_size.exe vec_decls.exe \ + vecSubs.exe diff --git a/3/square.cc b/3/square.cc new file mode 100644 index 0000000..0252115 --- /dev/null +++ b/3/square.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + vector v{1,2,3,4,5,6,7,8,9}; + for (auto &i : v) // for each element in v (note: i is a reference) + i *= i; // square the element value + for (auto i : v) // for each element in v + cout << i << " "; // print the element + cout << endl; + + return 0; +} + diff --git a/3/strcompare.cc b/3/strcompare.cc new file mode 100644 index 0000000..07690e3 --- /dev/null +++ b/3/strcompare.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + string str = "Hello"; + string phrase = "Hello World"; + string slang = "Hiya"; + + if (str < phrase) cout << "str is smaller" << endl; + if (slang > str) cout << "slang is greater" << endl; + if (slang > phrase) cout << "slang is greater" << endl; + + return 0; +} diff --git a/3/string_add.cc b/3/string_add.cc new file mode 100644 index 0000000..d78b1e9 --- /dev/null +++ b/3/string_add.cc @@ -0,0 +1,51 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +int main() +{ + + string s1 = "hello, ", s2 = "world\n"; + string s3 = s1 + s2; // s3 is hello, world\n + cout << s1 << s2 << s3 << endl; + + s1 += s2; // equivalent to s1 = s1 + s2 + cout << s1; + + string s4 = "hello", s5 = "world"; // no punctuation in s4 or s2 + string s6 = s4 + ", " + s5 + '\n'; + cout << s4 << s5 << "\n" << s6 << endl; + + return 0; +} diff --git a/3/string_io.cc b/3/string_io.cc new file mode 100644 index 0000000..6be2dce --- /dev/null +++ b/3/string_io.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cin; +using std::cout; +using std::endl; + +int main() +{ + string s; // empty string + cin >> s; // read a whitespace-separated string into s + cout << s << endl; // write s to the output + return 0; +} diff --git a/3/string_io2.cc b/3/string_io2.cc new file mode 100644 index 0000000..28a3ecc --- /dev/null +++ b/3/string_io2.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + string s1, s2; + + cin >> s1 >> s2; // read first input into s1, second into s2 + cout << s1 << s2 << endl; // write both strings + + return 0; +} diff --git a/3/string_size.cc b/3/string_size.cc new file mode 100644 index 0000000..1ece68c --- /dev/null +++ b/3/string_size.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +string st1; // empty string +string st2(st1); // st2 is a copy of st1 + +int main() +{ + string st("The expense of spirit\n"); + cout << "The size of " << st << "is " << st.size() + << " characters, including the newline" << endl; + return 0; +} diff --git a/3/string_size2.cc b/3/string_size2.cc new file mode 100644 index 0000000..1b2bd07 --- /dev/null +++ b/3/string_size2.cc @@ -0,0 +1,49 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::string; using std::getline; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + string line; + + // read input a line at a time and print lines that are longer than 80 characters + while (getline(cin, line)) + if (line.size() > 80) + cout << line << endl; + + return 0; +} diff --git a/3/string_size3.cc b/3/string_size3.cc new file mode 100644 index 0000000..005c13c --- /dev/null +++ b/3/string_size3.cc @@ -0,0 +1,46 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; using std::getline; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + string line; + + // read input a line at a time and discard blank lines + while (getline(cin, line)) + if (!line.empty()) + cout << line << endl; + + return 0; +} diff --git a/3/vecScores.cc b/3/vecScores.cc new file mode 100644 index 0000000..835d982 --- /dev/null +++ b/3/vecScores.cc @@ -0,0 +1,78 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + // hold the grades we read from the standard input + vector grades; + + // count the number of grades by clusters of ten: + // 0--9, 10--19, . .. 90--99, 100 + vector scores(11, 0); // 11 buckets, all initially 0 + unsigned grade; + while (cin >> grade) { // read the grades + if (grade <= 100) // handle only valid grades + grades.push_back(grade); + ++scores[grade/10]; // increment the counter for the current cluster + } + cout << "grades.size = " << grades.size() << endl; + for (auto it : grades) + cout << it << " " ; + cout << endl; + + cout << "scores.size = " << scores.size() << endl; + for (auto it : scores) + cout << it << " " ; + cout << endl; + + // equivalent program using iterators instead of subscripts + vector alt_scores(11, 0); // 11 buckets, all initially 0 + // for each grade in the input + for (auto it = grades.begin(); it != grades.end(); ++it) { + unsigned i = *it; + // increment the counter for the current cluster + ++(*(alt_scores.begin() + i/10)); + } + + cout << "alt_scores.size = " << alt_scores.size() << endl; + for (auto it = alt_scores.begin(); it != alt_scores.end(); ++it) + cout << *it << " " ; + cout << endl; + +} + diff --git a/3/vecStrings2.cc b/3/vecStrings2.cc new file mode 100644 index 0000000..48bc9de --- /dev/null +++ b/3/vecStrings2.cc @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::string; + +int main() +{ + vector text; // holds the input + + string s; + while (getline(cin, s)) // read the entire input file + text.push_back(s); // storing each line as an element in text + cout << "text.size: " << text.size() << endl; + + // print each line in text up to the first blank line + for (auto it = text.cbegin(); + it != text.cend() && !(*it).empty(); ++it) + cout << *it << endl; + + // equivalent loop using arrow to dereference it and call empty + for (auto it = text.cbegin(); + it != text.cend() && !it->empty(); ++it) + cout << *it << endl; +} + diff --git a/3/vecSubs.cc b/3/vecSubs.cc new file mode 100644 index 0000000..191ff2e --- /dev/null +++ b/3/vecSubs.cc @@ -0,0 +1,93 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + vector v = {0,1,2,3,4,5,6,7,8,9}; + auto sz = v.size(); + decltype(sz) i = 0; + // duplicate contents of v onto the back of v + while (i != sz) { + v.push_back(*v.begin() + i); + ++i; + } + // prints 0...9 0...9 + for (auto it : v) + cout << it << " "; + cout << endl; + + // alternative way to stop when we get to the original last element + vector alt_v = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0...9 + for (decltype(alt_v.size()) i = 0, sz = alt_v.size(); i != sz; ++i) + alt_v.push_back(alt_v[i]); + + // prints 0...9 0...9 + for (auto it : alt_v) + cout << it << " "; + cout << endl; + + vector v2 = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0 ... 9 + decltype(v2.size()) ix = 0; // we'll use ix to index the vector + + // set the elements with values less than 5 to 0 + while (ix != v2.size() && v2[ix] < 5) { + v2[ix] = 0; // changes the value of the element in v + ++ix; // increment the index so the next iteration fetches the next element + } + + // print the elements using subscripts + for (unsigned i = 0; i != v2.size(); ++i) + cout << v2[i] << " "; + cout << endl; + + // equivalent but using iterators + vector alt_v2 = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0...9 + // set the elements to 0 up to the first one that is 5 or greater + auto it = alt_v2.begin(); + while (it != alt_v2.end() && *it < 5) { + *it = 0; // changes the value of the element in alt_v2 + ++it; // advance the iterator to denote the next element + } + for (auto it = alt_v2.begin(); // it denotes first element in alt_v2 + it != alt_v2.end(); // so long as it denotes an element + ++it) // increment the iterator to next element + cout << *it << " "; // print element denoted by it from alt_v2 + cout << endl; + + return 0; +} diff --git a/3/vec_decls.cc b/3/vec_decls.cc new file mode 100644 index 0000000..6b8a7a5 --- /dev/null +++ b/3/vec_decls.cc @@ -0,0 +1,91 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::vector; + +#include "Sales_item.h" + +int main() +{ + // list initialization, articles has 3 elements + vector articles = {"a", "an", "the"}; + + vector svec; // default initialization; svec has no elements + vector ivec; // ivec holds objects of type int + vector Sales_vec; // holds Sales_items + + vector> file; // vector whose elements are vectors + vector> vecOfvec; // each element is itself a vector + + // all five vectors have size 0 + cout << svec.size() << " " << ivec.size() << " " + << Sales_vec.size() << " " + << file.size() << " " << vecOfvec.size() << endl; + + vector ivec2(10); // ten elements, each initialized to 0 + vector ivec3(10, -1); // ten int elements, each initialized to -1 + vector svec2(10); // ten elements, each an empty string + vector svec3(10, "hi!"); // ten strings; each element is "hi!" + cout << ivec2.size() << " " << ivec3.size() << " " + << svec2.size() << " " << svec3.size() << endl; + + // 10 is not a string, so cannot be list initialization + vector v1(10); // construct v1 with ten value-initialized elements + vector v2{10}; // ten elements value-initialized elements + vector v3(10, "hi"); // ten elements with value "hi" + // again list initialization is not viable, so ordinary construction + vector v4{10, "hi"}; // ten elements with values "hi" + + // all four vectors have size ten + cout << v1.size() << " " << v2.size() + << " " << v3.size() << " " << v4.size() << endl; + + vector vs1{"hi"}; // list initialization: vs1 has 1 element + vector vs2{10}; // ten default-initialized elements + vector vs3{10, "hi"}; // has ten elements with value "hi" + cout << vs1.size() << " " << vs2.size() << " " << vs3.size() << endl; + + vector v5(10, 1); // ten elements with value 1 + vector v6{10, 1}; // two elements with values 10 and 1 + cout << v5.size() << " " << v6.size() << endl; + + // intention is clearer + vector alt_v3 = {10}; // one element with value 10 + vector alt_v4 = {10, 1}; // two elements with values 10 and 1 + cout << alt_v3.size() << " " << alt_v4.size() << endl; + + return 0; +} diff --git a/3/word_echo.cc b/3/word_echo.cc new file mode 100644 index 0000000..bea4369 --- /dev/null +++ b/3/word_echo.cc @@ -0,0 +1,42 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +int main() +{ + string word; + while (cin >> word) // read until end-of-file + cout << word << endl; // write each word followed by a new line + return 0; +} diff --git a/4/README b/4/README new file mode 100644 index 0000000..9fe3dd8 --- /dev/null +++ b/4/README @@ -0,0 +1,10 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + vecSubs cond + +Programs not listed above print output and do +not read any input + diff --git a/4/arith-ex.cc b/4/arith-ex.cc new file mode 100644 index 0000000..41744e3 --- /dev/null +++ b/4/arith-ex.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + cout << -30 * 3 + 21 / 5 << endl; + + cout << -30 + 3 * 21 / 5 << endl; + + cout << 30 / 3 * 21 % 5 << endl; + + cout << 30 / 3 * 21 % 4 << endl; + + cout << -30 / 3 * 21 % 4 << endl; + + cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl; + + return 0; +} diff --git a/4/arraysizeof.cc b/4/arraysizeof.cc new file mode 100644 index 0000000..897c750 --- /dev/null +++ b/4/arraysizeof.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::size_t; + +int ia[] = {0,1,2,3,4,5,6,7,8,9}; + +int main() +{ + // sizeof(ia)/sizeof(*ia) returns the number of elements in ia + constexpr size_t sz = sizeof(ia)/sizeof(*ia); + + int arr2[sz]; // ok sizeof returns a constant expression + + cout << "ia size: " << sz << endl; + + return 0; +} diff --git a/4/cond.cc b/4/cond.cc new file mode 100644 index 0000000..154a8a9 --- /dev/null +++ b/4/cond.cc @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::vector; + +vector grades; + +int main() +{ + unsigned i; + while (cin >> i) + grades.push_back(i); + + for (auto grade : grades) { // for each grade in grades + // set the initial grade as pass or fail + string finalgrade = (grade < 60) ? "fail" : "pass"; + + finalgrade = (grade > 90) ? "high pass" + : (grade < 60) ? "fail" : "pass"; + + cout << grade << " " + finalgrade << endl; + } + + return 0; +} diff --git a/4/data/oofe b/4/data/oofe new file mode 100644 index 0000000..c4e1776 --- /dev/null +++ b/4/data/oofe @@ -0,0 +1 @@ +0 -100 1 -1 2 -3 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9 diff --git a/4/data/vecSubs b/4/data/vecSubs new file mode 100644 index 0000000..cf37e78 --- /dev/null +++ b/4/data/vecSubs @@ -0,0 +1,14 @@ +42 +65 +95 +100 +39 +67 +95 +76 +88 +76 +83 +92 +76 +93 diff --git a/4/incr.cc b/4/incr.cc new file mode 100644 index 0000000..79786ec --- /dev/null +++ b/4/incr.cc @@ -0,0 +1,43 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + int i = 0, j; + j = ++i; // j = 1, i = 1: prefix yields the incremented value + cout << i << " " << j << endl; + + j = i++; // j = 1, i = 2: postfix yields the unincremented value + cout << i << " " << j << endl; + + return 0; +} diff --git a/4/int-div.cc b/4/int-div.cc new file mode 100644 index 0000000..03f798d --- /dev/null +++ b/4/int-div.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + // ival1 is 3; result is truncated; remainder is discarded + int ival1 = 21/6; + + // ival2 is 3; no remainder; result is an integral value + int ival2 = 21/7; + + cout << ival1 << " " << ival2 << endl; + + return 0; +} diff --git a/4/makefile b/4/makefile new file mode 100644 index 0000000..893e4b4 --- /dev/null +++ b/4/makefile @@ -0,0 +1,11 @@ +# executable files for this directory +OBJECTS = arith-ex.exe arraysizeof.exe cond.exe \ + incr.exe int-div.exe oofe.exe overflow.exe \ + prec.exe shift-prec.exe sizeof_pgm.exe \ + unaryOps.exe vec_init.exe warn_dbltoint.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../2 diff --git a/4/oofe.cc b/4/oofe.cc new file mode 100644 index 0000000..c4cff96 --- /dev/null +++ b/4/oofe.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::toupper; + +// chapter 6 will explain functions +// tolower and toupper change the argument itself, not a local copy +string &tolower(string &s) +{ + for (auto &i : s) + i = tolower(i); + return s; +} + +string &toupper(string &s) +{ + for (auto &i : s) + i = toupper(i); + return s; +} + +int main() +{ + int i = 0; + cout << i << " " << ++i << endl; // undefined + + string s("a string"), orig = s; + cout << toupper(s) << endl; // changes s to uppercase + cout << tolower(s) << endl; // changes s to lowercase + + s = orig; + // the calls to toupper and tolower change the value of s + // << doesn't guarantee order of evaluation, + // so this expression is undefined + cout << toupper(s) << " " << tolower(s) << endl; + + string first = toupper(s); // we control the order of evaluation + string second = tolower(s); // by storing the results in the order in which we want + + cout << first << " " << second << endl; // one possible evaluation + cout << second << " " << first << endl; // equally legal evaluation! + cout << first << " " << first << endl; // another legal evaluation! + cout << second << " " << second << endl; // and a fourth! + + return 0; +} diff --git a/4/overflow.cc b/4/overflow.cc new file mode 100644 index 0000000..8b54dd1 --- /dev/null +++ b/4/overflow.cc @@ -0,0 +1,41 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + short short_value = 32767; // max value if shorts are 16 bits + + short_value += 1; // this calculation overflows + cout << "short_value: " << short_value << endl; + + return 0; +} diff --git a/4/prec.cc b/4/prec.cc new file mode 100644 index 0000000..712dc7b --- /dev/null +++ b/4/prec.cc @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + cout << 6 + 3 * 4 / 2 + 2 << endl; + + // parentheses in this expression match default precedence and associativity + cout << ((6 + ((3 * 4) / 2)) + 2) << endl; // prints 14 + + int temp = 3 * 4; // 12 + int temp2 = temp / 2; // 6 + int temp3 = temp2 + 6; // 12 + int result = temp3 + 2; // 14 + cout << result << endl; + + // parentheses result in alternative groupings + cout << (6 + 3) * (4 / 2 + 2) << endl; // prints 36 + cout << ((6 + 3) * 4) / 2 + 2 << endl; // prints 20 + cout << 6 + 3 * 4 / (2 + 2) << endl; // prints 9 + + return 0; +} diff --git a/4/runpgms b/4/runpgms new file mode 100644 index 0000000..b0b141f --- /dev/null +++ b/4/runpgms @@ -0,0 +1,6 @@ +echo "cond:" && ./cond < data/vecSubs + +../noinput arith-ex.exe arraysizeof.exe \ + incr.exe int-div.exe oofe.exe overflow.exe \ + prec.exe shift-prec.exe sizeof_pgm.exe \ + unaryOps.exe vec_init.exe warn_dbltoint.exe diff --git a/4/shift-prec.cc b/4/shift-prec.cc new file mode 100644 index 0000000..420ef3c --- /dev/null +++ b/4/shift-prec.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + int grade = 75; + cout << ((grade < 60) ? "fail" : "pass"); // prints pass or fail + cout << endl; + + cout << (grade < 60) ? "fail" : "pass"; // prints 1 or 0! + cout << endl; + + // previous expression is equivalent to the following + cout << (grade < 60); // prints 1 or 0 + cout ? "fail" : "pass"; // test cout and then yield one of the two literals + // depending on whether cout is true or false + cout << endl; + int i = 15, j = 20; + cout << (i < j ? i : j); // ok: prints smaller of i and j + cout << endl; + + cout << (i < j) ? i : j; // ok: prints 1 or 0! + cout << endl; + + // previous expression is equivalent to the following + cout << (i < j); // prints 1 or 0 + cout ? i : j; // test cout and then evaluate i or j + // depending on whether cout evaluates to true or false + cout << endl; + + return 0; +} diff --git a/4/sizeof_pgm.cc b/4/sizeof_pgm.cc new file mode 100644 index 0000000..ccaa273 --- /dev/null +++ b/4/sizeof_pgm.cc @@ -0,0 +1,77 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include "Sales_data.h" + +int main() +{ + Sales_data data, *p; + sizeof(Sales_data); // size required to hold an object of type Sales_data + sizeof data; // size of data's type, i.e., sizeof(Sales_data) + sizeof p; // size of a pointer + sizeof *p; // size of the type to which p points, i.e., sizeof(Sales_data) + sizeof data.revenue; // size of the type of Sales_data's revenue member + sizeof Sales_data::revenue; // alternative way to get the size of revenue + + cout << "short: " << sizeof(short) << "\n" + << "short[3]: " << sizeof(short[3]) << "\n" + << "short*: " << sizeof(short*) << "\n" + << "short&: " << sizeof(short&) << endl; + + cout << endl; + + cout << "int: " << sizeof(int) << "\n" + << "int[3]: " << sizeof(int[3]) << "\n" + << "int*: " << sizeof(int*) << "\n" + << "int&: " << sizeof(int&) << endl; + + cout << endl; + + cout << "Sales_data: " << sizeof(Sales_data) << "\n" + << "Sales_data[3]: " << sizeof(Sales_data[3]) << "\n" + << "Sales_data*: " << sizeof(Sales_data*) << "\n" + << "Sales_data&: " << sizeof(Sales_data&) << endl; + + cout << "Sales_data::revenue: " << sizeof Sales_data::revenue << "\n" + << "data.revenue: " << sizeof data.revenue << endl; + + int x[10]; + int *ip = x; + + // number of elements in x + cout << sizeof(x)/sizeof(*x) << endl; + + // divides sizeof a pointer by sizeof an int + cout << sizeof(ip)/sizeof(*ip) << endl; + + return 0; +} diff --git a/4/unaryOps.cc b/4/unaryOps.cc new file mode 100644 index 0000000..f257999 --- /dev/null +++ b/4/unaryOps.cc @@ -0,0 +1,44 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + int i = 1024; + int k = -i; // i is -1024 + + bool b = true; + bool b2 = -b; // b2 is true! + + cout << b << " " << b2 << " " << endl; + + return 0; +} diff --git a/4/vec_init.cc b/4/vec_init.cc new file mode 100644 index 0000000..c09edda --- /dev/null +++ b/4/vec_init.cc @@ -0,0 +1,66 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + + vector ivec; // empty vector + int cnt = 10; + // add elements 10 . . . 1 to ivec + while (cnt > 0) + ivec.push_back(cnt--); // int postfix decrement + + auto iter = ivec.begin(); + // prints 10 9 8 . . . 1 + while (iter != ivec.end()) + cout << *iter++ << endl; // iterator postfix increment + + vector vec2(10, 0); // ten elements initially all 0 + cnt = vec2.size(); + // assign values from size . . . 1 to the elements in vec2 + for(vector::size_type ix = 0; + ix != vec2.size(); ++ix, --cnt) + vec2[ix] = cnt; + + iter = vec2.begin(); + // prints 10 9 8 . . . 1 + while (iter != vec2.end()) + cout << *iter++ << endl; // iterator postfix increment + + return 0; +} diff --git a/4/warn_dbltoint.cc b/4/warn_dbltoint.cc new file mode 100644 index 0000000..89fb24f --- /dev/null +++ b/4/warn_dbltoint.cc @@ -0,0 +1,40 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +int main() +{ + // the compiler might warn about loss of precision + int ival = 3.541 + 3; // the compiler might warn about loss of precision + cout << ival << endl; // prints 6 + + return 0; +} diff --git a/5/README b/5/README new file mode 100644 index 0000000..32881b5 --- /dev/null +++ b/5/README @@ -0,0 +1,15 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + add_item_data add_item2 + doWhile doWhile + grades ifgrades + guesses guess + readme othercnt + readme vowels + +Programs not listed above print output and do +not read any input + diff --git a/5/add_item2.cc b/5/add_item2.cc new file mode 100644 index 0000000..e4266b6 --- /dev/null +++ b/5/add_item2.cc @@ -0,0 +1,65 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::runtime_error; + +#include +using std::cin; using std::cout; using std::endl; + +#include "Sales_item.h" + +int main() +{ + Sales_item item1, item2; + + while (cin >> item1 >> item2) { + try { + // execute code that will add the two Sales_items + // if the addition fails, the code throws a runtime_error exception + // first check that the data are for the same item + if (item1.isbn() != item2.isbn()) + throw runtime_error("Data must refer to same ISBN"); + + // if we're still here, the ISBNs are the same + cout << item1 + item2 << endl; + } catch (runtime_error err) { + // remind the user that the ISBNs must match + // and prompt for another pair + cout << err.what() + << "\nTry Again? Enter y or n" << endl; + char c; + cin >> c; + if (!cin || c == 'n') + break; // break out of the while loop + } // ends the catch clause + } // ends the while loop + + return 0; // indicate success +} diff --git a/5/data/add_item_data b/5/data/add_item_data new file mode 100644 index 0000000..f25d0a9 --- /dev/null +++ b/5/data/add_item_data @@ -0,0 +1,10 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-999-78345-X 3 20.00 +0-201-78345-X 2 25.00 +y +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 +0-999-78345-X 3 20.00 +0-201-78345-X 2 25.00 +n diff --git a/5/data/doWhile b/5/data/doWhile new file mode 100644 index 0000000..8eb0402 --- /dev/null +++ b/5/data/doWhile @@ -0,0 +1,4 @@ +10 42 +y +3 17 +n diff --git a/5/data/grades b/5/data/grades new file mode 100644 index 0000000..cf37e78 --- /dev/null +++ b/5/data/grades @@ -0,0 +1,14 @@ +42 +65 +95 +100 +39 +67 +95 +76 +88 +76 +83 +92 +76 +93 diff --git a/5/data/guesses b/5/data/guesses new file mode 100644 index 0000000..cabc403 --- /dev/null +++ b/5/data/guesses @@ -0,0 +1,3 @@ +5 +9 +7 diff --git a/5/data/readme b/5/data/readme new file mode 100644 index 0000000..05a439d --- /dev/null +++ b/5/data/readme @@ -0,0 +1,14 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + add_item_data add_item2 + vecSubs ifgrades + readme othercnt + readme vowels + doWhile doWhile + +Programs not listed above print output and do +not read any input + diff --git a/5/doWhile.cc b/5/doWhile.cc new file mode 100644 index 0000000..415d842 --- /dev/null +++ b/5/doWhile.cc @@ -0,0 +1,53 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +int main() +{ + // repeatedly ask the user for a pair of numbers to sum + string rsp; // used in the condition; can't be defined inside the do + do { + cout << "please enter two values: "; + int val1 = 0, val2 = 0; + cin >> val1 >> val2; + cout << "The sum of " << val1 << " and " << val2 + << " = " << val1 + val2 << "\n\n" + << "More? Enter yes or no: "; + cin >> rsp; + } while (!rsp.empty() && rsp[0] != 'n'); + + cout << endl; + + return 0; +} diff --git a/5/guess.cc b/5/guess.cc new file mode 100644 index 0000000..bcdce21 --- /dev/null +++ b/5/guess.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + do { + cout << "Guess a number between 0 and 9" << endl; + unsigned i, mynum = 7; + cin >> i; + + if (i == mynum) { + cout << "Congrats you guessed right!" << endl; + break; + } else + if (i < mynum) + cout << "too low" << endl; + else + cout << "too high" << endl; + + if (i != mynum) + if (i < mynum) + cout << "too low" << endl; + else + cout << "too high" << endl; + else { + cout << "Congrats you guessed right!" << endl; + break; + } + } while (true); + + return 0; +} diff --git a/5/ifgrades.cc b/5/ifgrades.cc new file mode 100644 index 0000000..b211031 --- /dev/null +++ b/5/ifgrades.cc @@ -0,0 +1,116 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::endl; using std::cin; using std::cout; + +#include +using std::vector; + +#include +using std::string; + +const vector scores = {"F", "D", "C", "B", "A", "A++"}; +vector grades; + +// these functions demonstrate alternative ways to handle the if tests +// function that takes an unsigned value and a string +// and returns a string +string goodVers(string lettergrade, unsigned grade) +{ + // add a plus for grades the end in 8 or 9 and a minus for those ending in 0, 1, or 2 + if (grade % 10 > 7) + lettergrade += '+'; // grades ending in 8 or 9 get a '+' + else + if (grade % 10 < 3) + lettergrade += '-'; // those ending in 0, 1, or 2 get a '-' + return lettergrade; +} + +// incorrect version of the function to add a plus or minus to a grade +string badVers(string lettergrade, unsigned grade) +{ + // add a plus for grades the end in 8 or 9 and a minus for those ending in 0, 1, or 2 + // WRONG: execution does NOT match indentation; the else goes with the inner if + if (grade % 10 >= 3) + if (grade % 10 > 7) + lettergrade += '+'; // grades ending in 8 or 9 get a + + else + lettergrade += '-'; // grades ending in 3, 4, 5, 6 will get a minus! + return lettergrade; +} + +// corrected version using the same logic path as badVers +string rightVers(string lettergrade, unsigned grade) +{ + // add a plus for grades that end in 8 or 9 and a minus for those ending in 0, 1, or 2 + if (grade % 10 >= 3) { + if (grade % 10 > 7) + lettergrade += '+'; // grades ending in 8 or 9 get a + + } else // curlies force the else to go with the outer if + lettergrade += '-'; // grades ending in 0, 1, or 2 will get a minus + return lettergrade; +} + +int main() +{ + // read a set of scores from the input + unsigned grade; + while (cin >> grade) + grades.push_back(grade); + + // now process those grades + for (auto it : grades) { // for each grade we read + cout << it << " " ; // print the grade + string lettergrade; // hold coresponding letter grade + // if failing grade, no need to check for a plus or minus + if (it < 60) + lettergrade = scores[0]; + else { + lettergrade = scores[(it - 50)/10]; // fetch the letter grade + if (it != 100) // add plus or minus only if not already an A++ + if (it % 10 > 7) + lettergrade += '+'; // grades ending in 8 or 9 get a + + else if (it % 10 < 3) + lettergrade += '-'; // grades ending in 0, 1, or 2 get a - + } + cout << lettergrade << endl; + if (it > 59 && it !=100) { + cout << "alternative versions: " << it << " "; + // start over with just the basic grade, no + or - + lettergrade = scores[(it - 50)/10]; + cout << goodVers(lettergrade, it) << " "; + cout << badVers(lettergrade, it) << " "; + cout << rightVers(lettergrade, it) << " "; + cout << endl; + } + } + + return 0; +} diff --git a/5/makefile b/5/makefile new file mode 100644 index 0000000..48a61f0 --- /dev/null +++ b/5/makefile @@ -0,0 +1,10 @@ +# executable files for this directory +OBJECTS = add_item2.exe doWhile.exe guess.exe ifgrades.exe \ + othercnt.exe rangefor.exe vowels.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 + diff --git a/5/othercnt.cc b/5/othercnt.cc new file mode 100644 index 0000000..a0d2aff --- /dev/null +++ b/5/othercnt.cc @@ -0,0 +1,56 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + char ch; + // initialize counters for each vowel + unsigned vowelCnt = 0; + unsigned otherCnt = 0; // count anything that isn't a vowel + + while (cin >> ch) { + // if ch is a vowel, increment the appropriate counter + switch (ch) { + case 'a': case 'e': case 'i': case 'o': case 'u': + ++vowelCnt; + break; + default: + ++otherCnt; + break; + } + } + // print results + cout << "Number of vowels: \t" << vowelCnt << '\n' + << "Total non-vowels : \t" << otherCnt << '\n'; + + return 0; +} diff --git a/5/rangefor.cc b/5/rangefor.cc new file mode 100644 index 0000000..1d4752c --- /dev/null +++ b/5/rangefor.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::vector; + +#include +using std::string; + +int main() +{ + vector ivec; + vector v = {0,1,2,3,4,5,6,7,8,9}; + + // range variable must be a reference so we can write to the elements + for (auto &r : v) // for each element in v + r *= 2; // double the value of each element in v + + // print every element in v + for (int r : v) + cout << r << " "; // print the elements in v + cout << endl; + + return 0; +} diff --git a/5/runpgms b/5/runpgms new file mode 100644 index 0000000..5f4a110 --- /dev/null +++ b/5/runpgms @@ -0,0 +1,8 @@ +echo "add_item2:" && ./add_item2 < data/add_item_data +echo "guess:" && ./guess < data/guesses +echo "ifgrades:" && ./ifgrades < data/grades +echo "othercnt:" && ./othercnt < data/readme +echo "vowels:" && ./vowels < data/readme +echo "doWhile:" && ./doWhile < data/doWhile + +../noinput rangefor.exe diff --git a/5/vowels.cc b/5/vowels.cc new file mode 100644 index 0000000..712b260 --- /dev/null +++ b/5/vowels.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + // initialize counters for each vowel + unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0; + + char ch; + while (cin >> ch) { + // if ch is a vowel, increment the appropriate counter + switch (ch) { + case 'a': + ++aCnt; + break; + case 'e': + ++eCnt; + break; + case 'i': + ++iCnt; + break; + case 'o': + ++oCnt; + break; + case 'u': + ++uCnt; + break; + } + } + // print results + cout << "Number of vowel a: \t" << aCnt << '\n' + << "Number of vowel e: \t" << eCnt << '\n' + << "Number of vowel i: \t" << iCnt << '\n' + << "Number of vowel o: \t" << oCnt << '\n' + << "Number of vowel u: \t" << uCnt << endl; + + return 0; +} diff --git a/6/LocalMath.cc b/6/LocalMath.cc new file mode 100644 index 0000000..6eeca29 --- /dev/null +++ b/6/LocalMath.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "LocalMath.h" + +// return the greatest common divisor +int gcd(int v1, int v2) +{ + while (v2) { + int temp = v2; + v2 = v1 % v2; + v1 = temp; + } + return v1; +} + + +// factorial of val is val * (val - 1) * (val - 2) . . . * ((val - (val - 1)) * 1) +int fact(int val) +{ + int ret = 1; // local variable to hold the result as we calculate it + while (val > 1) + ret *= val--; // assign ret * val to ret and decrement val + return ret; // return the result +} + +// recursive version of factorial: +// calculate val!, which is 1 * 2 * 3 . . . * val +int factorial(int val) +{ + if (val > 1) + return factorial(val-1) * val; + return 1; +} + diff --git a/6/LocalMath.h b/6/LocalMath.h new file mode 100644 index 0000000..cb678a4 --- /dev/null +++ b/6/LocalMath.h @@ -0,0 +1,38 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +// not included in book text, but used by programs in this directory +#ifndef LOCALMATH_H +#define LOCALMATH_H + +//definition in LocalMath.cc +int fact(int); // iterative definition of factorial +int factorial(int); // recrusive version of factorial +int gcd(int, int); // find greatest common divisor +#endif diff --git a/6/README b/6/README new file mode 100644 index 0000000..cf5db64 --- /dev/null +++ b/6/README @@ -0,0 +1,11 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + mainmath mainmath + refparms refparms + +Programs not listed above print output and do +not read any input + diff --git a/6/arrRet.cc b/6/arrRet.cc new file mode 100644 index 0000000..3a1ff6a --- /dev/null +++ b/6/arrRet.cc @@ -0,0 +1,93 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::cout; using std::endl; + +// code to illustrate declarations of array-related types +int arr[10]; // arr is an array of ten ints +int *p1[10]; // p1 is an array of ten pointers +int (*p2)[10] = &arr; // p2 points to an array of ten ints + +using arrT = int[10]; // arrT is a synonym for the type array of ten ints + +// three ways to declare function returning pointer to array of ten ints +arrT* func(int i); // use a type alias +auto func(int i) -> int(*)[10]; // use a trailing return type +int (*func(int i))[10]; // direct declaration + +auto func2(int i) -> int(&)[10]; // func2 returns a refernce to an array + +// two arrays +int odd[] = {1,3,5,7,9}; +int even[] = {0,2,4,6,8}; + +// function that returns a pointer to an int in one of these arrays +int *elemPtr(int i) +{ + // returns a pointer to the first element in one of these arrays + return (i % 2) ? odd : even; +} + +// returns a pointer to an array of five int elements +decltype(odd) *arrPtr(int i) +{ + return (i % 2) ? &odd : &even; // returns a pointer to the array +} + +// returns a reference to an array of five int elements +int (&arrRef(int i))[5] +{ + return (i % 2) ? odd : even; +} + +int main() +{ + int *p = elemPtr(6); // p points to an int + int (*arrP)[5] = arrPtr(5); // arrP points to an array of five ints + int (&arrR)[5] = arrRef(4); // arrR refers to an array of five ints + + for (size_t i = 0; i < 5; ++i) + // p points to an element in an array, which we subscript + cout << p[i] << endl; + + for (size_t i = 0; i < 5; ++i) + // arrP points to an array, + // we must dereference the pointer to get the array itself + cout << (*arrP)[i] << endl; + + for (size_t i = 0; i < 5; ++i) + // arrR refers to an array, which we can subscript + cout << arrR[i] << endl; + + return 0; +} diff --git a/6/count-calls.cc b/6/count-calls.cc new file mode 100644 index 0000000..3612938 --- /dev/null +++ b/6/count-calls.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::cout; using std::endl; + +size_t count_calls() +{ + static size_t ctr = 0; // value will persist across calls + return ++ctr; +} + +int main() +{ + for (size_t i = 0; i != 10; ++i) + cout << count_calls() << endl; + return 0; +} diff --git a/6/data/mainmath b/6/data/mainmath new file mode 100644 index 0000000..5281552 --- /dev/null +++ b/6/data/mainmath @@ -0,0 +1 @@ +5 diff --git a/6/data/refparms b/6/data/refparms new file mode 100644 index 0000000..cff9da2 --- /dev/null +++ b/6/data/refparms @@ -0,0 +1,15 @@ +Hello World! +42 +65 +95 +100 +39 +67 +95 +76 +88 +76 +83 +92 +76 +93 diff --git a/6/errMsg_initList.cc b/6/errMsg_initList.cc new file mode 100644 index 0000000..9bff2e8 --- /dev/null +++ b/6/errMsg_initList.cc @@ -0,0 +1,110 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::initializer_list; + +#include +using std::ostringstream; + +// chapter 7 will explain the code used in defining ErrCode +struct ErrCode { + ErrCode(int i) : num(i) { } // initializes objects of type ErrCode + string msg() // member function of ErrCode + { ostringstream s; s << "ErrCode " << num; return s.str(); } + int num; // data member, note uninitialized +}; + +// version that takes an ErrCode and a list of strings +void error_msg(ErrCode e, initializer_list il) +{ + cout << e.msg() << ": "; + for (const auto &elem : il) + cout << elem << " " ; + cout << endl; +} + +// overloaded version takes only a list of strings +void error_msg(initializer_list il) +{ + for (auto beg = il.begin(); beg != il.end(); ++beg) + cout << *beg << " " ; + cout << endl; +} + +// function to illustrate list initializing return value +vector functionX() +{ + string expected = "description", actual = "some other case"; + // . . . + if (expected.empty()) + return {}; // return an empty vector + else if (expected == actual) + return {"functionX", "okay"}; // return list-initialized vector + else + return {"functionX", expected, actual}; +} + +int main() +{ + string expected = "description", actual = "some other case"; + initializer_list li = {0,1,2,3}; + + // expected, actual are strings + if (expected != actual) + error_msg({"functionX", expected, actual}); + else + error_msg({"functionX", "okay"}); + + // expected, actual are strings + if (expected != actual) + error_msg(ErrCode(42), {"functionX", expected, actual}); + else + error_msg(ErrCode(0), {"functionX", "okay"}); + + // can pass an empty list, calls second version of error_msg + error_msg({}); // prints blank line + + // call function that list initializes its return value + // results is a vector + auto results = functionX(); + for (auto i : results) + cout << i << " "; + cout << endl; +} + diff --git a/6/fact.cc b/6/fact.cc new file mode 100644 index 0000000..43c49d5 --- /dev/null +++ b/6/fact.cc @@ -0,0 +1,46 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +// declarations of our factorial functions +// definitions are in LocalMath.cc +#include "LocalMath.h" + +int main() +{ + cout << factorial(5) << endl; + cout << fact(5) << endl; + cout << factorial(0) << endl; + cout << fact(0) << endl; + + return 0; +} + diff --git a/6/fcnptrRet.cc b/6/fcnptrRet.cc new file mode 100644 index 0000000..0f19409 --- /dev/null +++ b/6/fcnptrRet.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +// declarations (not strictly speaking necessary in this file) +string::size_type sumLength(const string&, const string&); +string::size_type largerLength(const string&, const string&); + +// definition of these functions +string::size_type sumLength(const string &s1, const string &s2) +{ + return s1.size() + s2.size(); +} + +string::size_type largerLength(const string &s1, const string &s2) +{ + return (s1.size() > s2.size()) ? s1.size() : s2.size(); +} + +// depending on the value of its string parameter, +// getFcn returns a pointer to sumLength or to largerLength + +// three ways to declare getFcn +// 1. use decltype for the return type, +// remembering to add a * to indicate that getFcn returns a pointer +decltype(sumLength) *getFcn(const string &); + +// use trailing return type +auto getFcn(const string&) -> string::size_type(*)(const string&, const string&); + +// direct definition +string::size_type (*getFcn(const string&))(const string&, const string&); + +// define getFcn +decltype(sumLength)* +getFcn(const string &fetch) +{ + if (fetch == "sum") + return sumLength; + return largerLength; +} + +int main() +{ + // "sum" is the argument to getFcn + // ("hello", "world!") are arguments to the function getFcn returns + cout << getFcn("sum")("hello", "world!") << endl; // prints 11 + cout << getFcn("larger")("hello", "world!") << endl; // prints 6 + + return 0; +} diff --git a/6/good_printmain.cc b/6/good_printmain.cc new file mode 100644 index 0000000..883625d --- /dev/null +++ b/6/good_printmain.cc @@ -0,0 +1,55 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::begin; using std::end; + +#include +using std::size_t; + +#include +using std::cout; using std::endl; + +// const int ia[] is equivalent to const int* ia +// size is passed explicitly and used to control access to elements of ia +void print(const int ia[], size_t size) +{ + for (size_t i = 0; i != size; ++i) { + cout << ia[i] << endl; + } +} + +int main() +{ + int j[] = { 0, 1 }; // int array of size 2 + + print(j, end(j) - begin(j)); + + return 0; +} diff --git a/6/inline_shorter.cc b/6/inline_shorter.cc new file mode 100644 index 0000000..0126676 --- /dev/null +++ b/6/inline_shorter.cc @@ -0,0 +1,55 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +//inline version: find the shorter of two strings +inline const string & +shorterString(const string &s1, const string &s2) +{ + return s1.size() <= s2.size() ? s1 : s2; +} + +int main() +{ + string s1("successes"), s2("failure"); + cout << shorterString(s1, s2) << endl; + + // call the size member of the string returned by shorterString + cout << shorterString(s1, s2).size() << endl; + + // equivalent code as generated by the call to inline version + // of shorterString + cout << (s1.size() < s2.size() ? s1 : s2) << endl; + return 0; +} diff --git a/6/mainmath.cc b/6/mainmath.cc new file mode 100644 index 0000000..b9645e7 --- /dev/null +++ b/6/mainmath.cc @@ -0,0 +1,52 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "LocalMath.h" +#include +using std::cout; using std::endl; + +int main() +{ + // pass a literal to fact + int f = fact(5); // f equals 120, i.e., the result of fact(5) + cout << "5! is " << f << endl; + + // call fact on i and print the result + int i = 5; + int j = fact(i); + cout << i << "! is " << j << endl; + + // call fact on a const int + const int ci = 3; + int k = fact(ci); + cout << ci << "! is " << k << endl; + + return 0; +} + diff --git a/6/mainret.cc b/6/mainret.cc new file mode 100644 index 0000000..da55420 --- /dev/null +++ b/6/mainret.cc @@ -0,0 +1,42 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +/* EXIT_FAILURE and EXIT_SUCCESS are preprocessor variables + * such variables are not in the std namespace, + * hence, no using declaration and no std:: when we use these names +*/ +int main() +{ + bool some_failure = false; + if (some_failure) + return EXIT_FAILURE; // defined in cstdlib + else + return EXIT_SUCCESS; // defined in cstdlib +} diff --git a/6/make_plural.h b/6/make_plural.h new file mode 100644 index 0000000..1dda54f --- /dev/null +++ b/6/make_plural.h @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#ifndef MAKE_PLURAL_H +#define MAKE_PLURAL_H + +// return the plural version of word if ctr is greater than 1 +inline +string make_plural(size_t ctr, const string &word, + const string &ending) +{ + return (ctr > 1) ? word + ending : word; +} + +#endif diff --git a/6/makefile b/6/makefile new file mode 100644 index 0000000..632a65b --- /dev/null +++ b/6/makefile @@ -0,0 +1,21 @@ +# executable files for this directory +OBJECTS = arrRet.exe count-calls.exe errMsg_initList.exe\ + fact.exe fcnptrRet.exe good_printmain.exe \ + inline_shorter.exe mainmath.exe mainret.exe \ + mk_plural.exe ref-fcn.exe refparms.exe refret-ex.exe \ + reset.exe stl-arr-fcns.exe usefcnptr.exe wdebug.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = + +fact.o mainmath.o LocalMath.o: LocalMath.h + +fact.exe: fact.o LocalMath.o + $(CC) $(CCFLAGS) fact.o LocalMath.o -o fact.exe + +mainmath.exe: mainmath.o LocalMath.o + $(CC) $(CCFLAGS) mainmath.o LocalMath.o -o mainmath.exe + diff --git a/6/mk_plural.cc b/6/mk_plural.cc new file mode 100644 index 0000000..a990a45 --- /dev/null +++ b/6/mk_plural.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include "make_plural.h" + +int main() +{ + size_t cnt = 1; + cout << make_plural(cnt, "success", "es") << endl; + + cnt = 2; + cout << make_plural(cnt, "failure", "s") << endl; + + return 0; +} diff --git a/6/printFcns.h b/6/printFcns.h new file mode 100644 index 0000000..f8b09fe --- /dev/null +++ b/6/printFcns.h @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef PRINTFCNS_H +#define PRINTFCNS_H +#include +void print(const char *cp); +void print(const int *beg, const int *end); +void print(std::vector::const_iterator beg, + std::vector::const_iterator end); +void print(const int ia[], size_t size); +void print(const std::vector&); + +inline foo() { +int j[2] = {0,1}; +print("Hello World"); // calls print(const char*) +print(j, end(j) - begin(j)); // calls print(const int*, size_t) +print(begin(j), end(j)); // calls print(const int*, const int*) +#endif diff --git a/6/ref-fcn.cc b/6/ref-fcn.cc new file mode 100644 index 0000000..74afd64 --- /dev/null +++ b/6/ref-fcn.cc @@ -0,0 +1,51 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +char &get_val(string &str, string::size_type ix) +{ + return str[ix]; // get_val assumes the given index is valid +} + +int main() +{ + string s("a value"); + cout << s << endl; // prints a value + + get_val(s, 0) = 'A'; // changes s[0] to A + cout << s << endl; // prints A value + + return 0; +} + diff --git a/6/refparms.cc b/6/refparms.cc new file mode 100644 index 0000000..855ddf0 --- /dev/null +++ b/6/refparms.cc @@ -0,0 +1,108 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::string; + +#include +using std::vector; + +#include +using std::size_t; + +// returns the index of the first occurrence of c in s +// the reference parameter occurs counts how often c occurs +string::size_type find_char(const string &s, char c, + string::size_type &occurs) +{ + auto ret = s.size(); // position of the first occurrence, if any + occurs = 0; // set the occurrence count parameter + + for (decltype(ret) i = 0; i != s.size(); ++i) { + if (s[i] == c) { + if (ret == s.size()) + ret = i; // remember the first occurrence of c + ++occurs; // increment the occurrence count + } + } + return ret; // count is returned implicitly in occurs +} + +// returns an iterator that refers to the first occurrence of value +// the reference parameter occurs contains a second return value +vector::const_iterator find_val( + vector::const_iterator beg, // first element + vector::const_iterator end, // one past last element + int value, // the value we want + vector::size_type &occurs) // number of times it occurs +{ + auto res_iter = end; // res_iter will hold first occurrence, if any + occurs = 0; // set occurrence count parameter + + for ( ; beg != end; ++beg) + if (*beg == value) { + // remember first occurrence of value + if (res_iter == end) + res_iter = beg; + ++occurs; // increment occurrence count + } + + return res_iter; // count returned implicitly in occurs +} + +int main() +{ + + string s; + getline(cin, s); + size_t ctr = 0; + auto index = find_char(s, 'o', ctr); + cout << index << " " << ctr << endl; + + vector ivec; + int i; + // read values into ivec + while (cin >> i) + ivec.push_back(i); + + // for each value in the list of ints + for (auto i : {42, 33, 92}) { + auto it = find_val(ivec.begin(), ivec.end(), i, ctr); + if (it == ivec.end()) + cout << i << " is not in the input data" << endl; + else + cout << i << " was at position " + << it - ivec.begin() << endl; + } + + return 0; +} diff --git a/6/refret-ex.cc b/6/refret-ex.cc new file mode 100644 index 0000000..8081b53 --- /dev/null +++ b/6/refret-ex.cc @@ -0,0 +1,47 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +// get returns a reference to an element in the given array +int &get(int *arry, int index) { return arry[index]; } + +int main() { + int ia[10]; // array of ten uninitialized ints + + for (int i = 0; i != 10; ++i) + get(ia, i) = i; // call get to assign values to the elements + + for (auto i : ia) // print the elements + cout << i << " "; + cout << endl; + + return 0; +} diff --git a/6/reset.cc b/6/reset.cc new file mode 100644 index 0000000..9e3614b --- /dev/null +++ b/6/reset.cc @@ -0,0 +1,68 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::vector; + +// function that takes a reference to an int +// and sets the given object to zero +// i is just another name for the object passed to reset +void reset(int &i) +{ + i = 0; // changes the value of the object to which i refers +} + +// function that takes a pointer +// and sets the pointed-to value to zero +void reset(int *ip) +{ + *ip = 0; // changes the value of the object to which ip points + ip = 0; // changes the local copy of ip; the argument is unchanged +} + +int main() +{ + int j = 42; + reset(j); // j is passed by reference; the value in j is changed + cout << "j = " << j << endl; // prints j = 0 + + j = 42; // restore the original value of j + reset(&j); // changes j but not the address of j + cout << "j = " << j << endl; // prints j = 0 + + j = 42; // restore the original value of j + int *p = &j; + reset(p); // changes object to which p points not the address in p + cout << "j = " << *p << endl; // prints j = 0 + + return 0; +} diff --git a/6/runpgms b/6/runpgms new file mode 100644 index 0000000..fe04ea7 --- /dev/null +++ b/6/runpgms @@ -0,0 +1,9 @@ +echo "mainmath:" && ./mainmath < data/mainmath +echo "refparms:" && ./refparms < data/refparms + +../noinput arrRet.exe count-calls.exe \ + errMsg_initList.exe fact.exe fcnptrRet.exe \ + good_printmain.exe inline_shorter.exe \ + mainret.exe mk_plural.exe \ + ref-fcn.exe refret-ex.exe reset.exe \ + stl-arr-fcns.exe usefcnptr.exe wdebug.exe diff --git a/6/stl-arr-fcns.cc b/6/stl-arr-fcns.cc new file mode 100644 index 0000000..e0761c3 --- /dev/null +++ b/6/stl-arr-fcns.cc @@ -0,0 +1,69 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::endl; using std::cout; + +#include +using std::begin; using std::end; + +// prints a null-terminated array of characters +void print(const char *cp) +{ + if (cp) // if cp is not a null pointer + while (*cp) // so long as the character it points to is not a null character + cout << *cp++; // print the character and advance the pointer +} + +// print ints in the given range +void print(const int *beg, const int *end) +{ + // print every element starting at beg up to but not including end + while (beg != end) + cout << *beg++ << " "; // print the current element + // and advance the pointer +} + +int main() +{ + print("hi world!"); // calls first version of print + cout << endl; + + // j is converted to a pointer to the first element in j + // the second argument is a pointer to one past the end of j + int j[2] = {0, 1}; + print(begin(j), end(j)); // library begin and end functions + cout << endl; + + // equivalent call, directly calculate the begin and end pointers + print(j, j + 2); + cout << endl; + + return 0; +} diff --git a/6/use_mk_plural.cc b/6/use_mk_plural.cc new file mode 100644 index 0000000..6f5ff2b --- /dev/null +++ b/6/use_mk_plural.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::string; + +#include +using std::cout; using std::endl; + +// return the plural version of word if ctr is greater than 1 +string make_plural(size_t ctr, const string &word, + const string &ending) +{ + return (ctr > 1) ? word + ending : word; +} + +int main() +{ + size_t cnt = 1; + cout << make_plural(cnt, "success", "es") << endl; + cnt = 2; + cout << make_plural(cnt, "failure", "s") << endl; + + return 0; +} diff --git a/6/usefcnptr.cc b/6/usefcnptr.cc new file mode 100644 index 0000000..d23daa9 --- /dev/null +++ b/6/usefcnptr.cc @@ -0,0 +1,71 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::vector; + +// function to return minimum element in an vector of ints +int min_element(vector::iterator, + vector::iterator); + +// pointer to function, initialized to point to min_element +int (*pf)(vector::iterator, vector::iterator) + = min_element; + +int main() +{ + vector ivec; + // give ivec some values + cout << "Direct call: " + << min_element(ivec.begin(), ivec.end()) << endl; + + cout << "Indirect call: " + << pf(ivec.begin(), ivec.end()) << endl; + + cout << "equivalent indirect call: " + << (*pf)(ivec.begin(), ivec.end()) << endl; + + return 0; +} + +// returns minimum element in an vector of ints +int min_element(vector::iterator beg, + vector::iterator end) { + int minVal = 0; + while (beg != end) { + if (minVal > *beg) + minVal = *beg; + ++beg; + } + return minVal; +} + diff --git a/6/wdebug.cc b/6/wdebug.cc new file mode 100644 index 0000000..912f237 --- /dev/null +++ b/6/wdebug.cc @@ -0,0 +1,72 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +// assert is a preprocessor macro and therefore not in std +// hence we need to include cassert header, +// but no using declaration for assert + +#include +using std::string; + +#include +using std::endl; using std::cerr; using std::cin; + +#include +using std::size_t; + +void print(const int ia[], size_t size) +{ +#ifndef NDEBUG +// __func__ is a local static defined by the compiler that holds the name of this function +cerr << __func__ << ": array size is " << size << endl; +#endif +// . . . +} + +int main() +{ + string word = "foo"; + const string::size_type threshold = 5; + if (word.size() < threshold) + cerr << "Error: " << __FILE__ + << " : in function " << __func__ + << " at line " << __LINE__ << endl + << " Compiled on " << __DATE__ + << " at " << __TIME__ << endl + << " Word read was \"" << word + << "\": Length too short" << endl; + word = "something longer than five chars"; + assert(word.size() > threshold); + + return 0; +} diff --git a/7/Account.cc b/7/Account.cc new file mode 100644 index 0000000..c6ceb34 --- /dev/null +++ b/7/Account.cc @@ -0,0 +1,42 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include "Account.h" + +// define static data and function members +const string Account::accountType("Savings Account"); +double Account::interestRate = initRate(); + +void Account::rate(double newRate) +{ + interestRate = newRate; +} diff --git a/7/Account.h b/7/Account.h new file mode 100644 index 0000000..bff1822 --- /dev/null +++ b/7/Account.h @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ +#ifndef ACCOUNT_H +#define ACCOUNT_H + +#include + +class Account { +public: + Account() = default; + Account(const std::string &s, double amt): + owner(s), amount(amt) { } + + void calculate() { amount += amount * interestRate; } + double balance() { return amount; } +public: + static double rate() { return interestRate; } + static void rate(double); +private: + std::string owner; + double amount = 0.0; + static double interestRate; + static double initRate() { return .0225; } + static const std::string accountType; + static constexpr int period = 30;// period is a constant expression + double daily_tbl[period]; +}; +#endif diff --git a/7/Debug.cc b/7/Debug.cc new file mode 100644 index 0000000..36cc1ba --- /dev/null +++ b/7/Debug.cc @@ -0,0 +1,34 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Debug.h" +// only implementation for the Debug classes are definitions +// for static members named enable +constexpr Debug HW_Subsystem::enable; +constexpr Debug IO_Subsystem::enable; diff --git a/7/Debug.h b/7/Debug.h new file mode 100644 index 0000000..8ee1195 --- /dev/null +++ b/7/Debug.h @@ -0,0 +1,71 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef DEBUG_H +#define DEBUG_H +class Debug { +public: + constexpr Debug(bool b = true): hw(b), io(b), other(b) { } + constexpr Debug(bool h, bool i, bool o): + hw(h), io(i), other(o) { } + constexpr bool any() { return hw || io || other; } + constexpr bool hardware() { return hw || io; } + constexpr bool app() { return other; } + + void set_io(bool b) { io = b; } + void set_hw(bool b) { hw = b; } + void set_other(bool b) { hw = b; } +private: + bool hw; // hardware errors other than IO errors + bool io; // IO errors + bool other; // other errors +}; + +class HW_Subsystem { +public: + HW_Subsystem(): debug(false) { } // by default no debugging + bool field_debug() { return debug.any(); } + bool default_debug() { return enable.any() && debug.any(); } + void set_debug(bool b) { debug.set_hw(b); } // turn on hardware debugging +private: + Debug debug; + constexpr static Debug enable{true, false, false}; +}; + +class IO_Subsystem { +public: + IO_Subsystem(): debug(false) { } // by default no debugging + bool field_debug() { return debug.any(); } + bool default_debug() { return enable.any() && debug.any(); } + void set_debug(bool b) { debug.set_io(b); } // turn on IO debugging +private: + Debug debug; + constexpr static Debug enable{true, false, true}; +}; +#endif diff --git a/7/README b/7/README new file mode 100644 index 0000000..c19cdd4 --- /dev/null +++ b/7/README @@ -0,0 +1,11 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + book_sales avg_price + add_item add_item + +Programs not listed above print output and do +not read any input + diff --git a/7/Sales_data.cc b/7/Sales_data.cc new file mode 100644 index 0000000..2ef3509 --- /dev/null +++ b/7/Sales_data.cc @@ -0,0 +1,82 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + + +#include +using std::istream; using std::ostream; + +#include "Sales_data.h" +Sales_data::Sales_data(std::istream &is) +{ + // read will read a transaction from is into this object + read(is, *this); +} + +double +Sales_data::avg_price() const { + if (units_sold) + return revenue/units_sold; + else + return 0; +} + +// add the value of the given Sales_data into this object +Sales_data& +Sales_data::combine(const Sales_data &rhs) +{ + units_sold += rhs.units_sold; // add the members of rhs into + revenue += rhs.revenue; // the members of ``this'' object + return *this; // return the object on which the function was called +} + +Sales_data +add(const Sales_data &lhs, const Sales_data &rhs) +{ + Sales_data sum = lhs; // copy data members from lhs into sum + sum.combine(rhs); // add data members from rhs into sum + return sum; +} + +// transactions contain ISBN, number of copies sold, and sales price +istream& +read(istream &is, Sales_data &item) +{ + double price = 0; + is >> item.bookNo >> item.units_sold >> price; + item.revenue = price * item.units_sold; + return is; +} + +ostream& +print(ostream &os, const Sales_data &item) +{ + os << item.isbn() << " " << item.units_sold << " " + << item.revenue << " " << item.avg_price(); + return os; +} diff --git a/7/Sales_data.h b/7/Sales_data.h new file mode 100644 index 0000000..d710edb --- /dev/null +++ b/7/Sales_data.h @@ -0,0 +1,70 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef SALES_DATA_H +#define SALES_DATA_H + +#include +#include + +class Sales_data { +friend Sales_data add(const Sales_data&, const Sales_data&); +friend std::ostream &print(std::ostream&, const Sales_data&); +friend std::istream &read(std::istream&, Sales_data&); +public: + // constructors + Sales_data() = default; + Sales_data(const std::string &s): bookNo(s) { } + Sales_data(const std::string &s, unsigned n, double p): + bookNo(s), units_sold(n), revenue(p*n) { } + Sales_data(std::istream &); + + // operations on Sales_data objects + std::string isbn() const { return bookNo; } + Sales_data& combine(const Sales_data&); + double avg_price() const; +private: + std::string bookNo; + unsigned units_sold = 0; + double revenue = 0.0; +}; + + +// nonmember Sales_data interface functions +Sales_data add(const Sales_data&, const Sales_data&); +std::ostream &print(std::ostream&, const Sales_data&); +std::istream &read(std::istream&, Sales_data&); + +// used in future chapters +inline +bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) +{ + return lhs.isbn() < rhs.isbn(); +} +#endif diff --git a/7/Screen.h b/7/Screen.h new file mode 100644 index 0000000..92c8bc1 --- /dev/null +++ b/7/Screen.h @@ -0,0 +1,96 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +#include + +class Screen { +public: + typedef std::string::size_type pos; + Screen() = default; // needed because Screen has another constructor + // cursor initialized to 0 by its in-class initializer + Screen(pos ht, pos wd, char c): height(ht), width(wd), + contents(ht * wd, c) { } + friend class Window_mgr; + Screen(pos ht = 0, pos wd = 0): + cursor(0), height(ht), width(wd), contents(ht * wd, ' ') { } + char get() const // get the character at the cursor + { return contents[cursor]; } // implicitly inline + inline char get(pos ht, pos wd) const; // explicitly inline + Screen &clear(char = bkground); +private: + static const char bkground = ' '; +public: + Screen &move(pos r, pos c); // can be made inline later + Screen &set(char); + Screen &set(pos, pos, char); + // display overloaded on whether the object is const or not + Screen &display(std::ostream &os) + { do_display(os); return *this; } + const Screen &display(std::ostream &os) const + { do_display(os); return *this; } +private: + // function to do the work of displaying a Screen + void do_display(std::ostream &os) const {os << contents;} + pos cursor = 0; + pos height = 0, width = 0; + std::string contents; +}; + +Screen &Screen::clear(char c) +{ + contents = std::string(height*width, c); + return *this; +} + +inline // we can specify inline on the definition +Screen &Screen::move(pos r, pos c) +{ + pos row = r * width; // compute the row location + cursor = row + c; // move cursor to the column within that row + return *this; // return this object as an lvalue +} + +char Screen::get(pos r, pos c) const // declared as inline in the class +{ + pos row = r * width; // compute row location + return contents[row + c]; // return character at the given column +} + +inline Screen &Screen::set(char c) +{ + contents[cursor] = c; // set the new value at the current cursor location + return *this; // return this object as an lvalue +} +inline Screen &Screen::set(pos r, pos col, char ch) +{ + contents[r*width + col] = ch; // set specified location to given value + return *this; // return this object as an lvalue +} + diff --git a/7/WindowMgr.h b/7/WindowMgr.h new file mode 100644 index 0000000..c4060ba --- /dev/null +++ b/7/WindowMgr.h @@ -0,0 +1,90 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef WINDOW_MGR +#define WINDOW_MGR + +#include +#include +#include +#include "newscreen.h" + +class BitMap; +// overloaded storeOn functions +extern std::ostream& storeOn(std::ostream &, Screen &); +extern BitMap& storeOn(BitMap &, Screen &); + +class Window_mgr { +public: + // location ID for each screen on the window + using ScreenIndex = std::vector::size_type; + + // add a Screen to the window and returns its index + ScreenIndex addScreen(const Screen&); + + // reset the Screen at the given position to all blanks + void clear(ScreenIndex); + + // change dimensions of a given Screen + void resize(Screen::pos r, Screen::pos c, ScreenIndex i); +private: + // Screens this Window_mgr is tracking + // by default, a Window_mgr has one standard sized blank Screen + std::vector screens{Screen(24, 80, ' ')}; +}; + +// return type is seen before we're in the scope of Window_mgr +inline +Window_mgr::ScreenIndex +Window_mgr::addScreen(const Screen &s) +{ + screens.push_back(s); + return screens.size() - 1; +} + +inline +void Window_mgr::clear(ScreenIndex i) +{ + // s is a reference to the Screen we want to clear + Screen &s = screens[i]; + // reset the contents of that Screen to all blanks + s.contents = std::string(s.height * s.width, ' '); +} + +inline +void +Window_mgr::resize(Screen::pos r, Screen::pos c, ScreenIndex i) +{ + screens[i].height = r; // Window_mgr is a friend of Screen + screens[i].width = c; // so it is ok to use Screen's private members + // resize and clear the contents member + screens[i].contents = std::string(r * c, ' '); +} + +#endif diff --git a/7/add_item.cc b/7/add_item.cc new file mode 100644 index 0000000..0b85f75 --- /dev/null +++ b/7/add_item.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cerr; using std::cin; using std::cout; using std::endl; + +#include "Sales_data.h" + +int main() +{ + Sales_data data1, data2; + if (read(cin, data1) && read(cin, data2)) { // read the transactions + if (data1.isbn() == data2.isbn()) { // check isbns + data1.combine(data2); // add the transactions + print(cout, data1); // print the results + cout << endl; // followed by a newline + } + } else + cerr << "Input failed!" << endl; + + return 0; +} diff --git a/7/avg_price.cc b/7/avg_price.cc new file mode 100644 index 0000000..bc8420d --- /dev/null +++ b/7/avg_price.cc @@ -0,0 +1,54 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cerr; using std::cin; using std::cout; using std::endl; + +#include "Sales_data.h" + +int main() +{ + Sales_data total; // variable to hold the running sum + if (read(cin, total)) { // read the first transaction + Sales_data trans; // variable to hold data for the next transaction + while(read(cin, trans)) { // read the remaining transactions + if (total.isbn() == trans.isbn()) // check the isbns + total.combine(trans); // update the running total + else { + print(cout, total) << endl; // print the results + total = trans; // process the next book + } + } + print(cout, total) << endl; // print the last transaction + } else { // there was no input + cerr << "No data?!" << endl; // notify the user + } + + return 0; +} diff --git a/7/data/add_item b/7/data/add_item new file mode 100644 index 0000000..92fd61c --- /dev/null +++ b/7/data/add_item @@ -0,0 +1,2 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/7/data/book_sales b/7/data/book_sales new file mode 100644 index 0000000..0d14afe --- /dev/null +++ b/7/data/book_sales @@ -0,0 +1,10 @@ +0-201-70353-X 4 24.99 +0-201-82470-1 4 45.39 +0-201-88954-4 2 15.00 +0-201-88954-4 5 12.00 +0-201-88954-4 7 12.00 +0-201-88954-4 2 12.00 +0-399-82477-1 2 45.39 +0-399-82477-1 3 45.39 +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/7/makefile b/7/makefile new file mode 100644 index 0000000..fb6ad3b --- /dev/null +++ b/7/makefile @@ -0,0 +1,30 @@ +# executable files for this directory +OBJECTS = add_item.exe avg_price.exe \ + useAccount.exe useDebug.exe useScreen.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../1 + +useScreen.exe: Screen.h + +Account.o useAccount.o: Account.h +Debug.o useDebug.o: Debug.h + +useAccount.exe: Account.o useAccount.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Account.o useAccount.o \ + -o useAccount.exe + +useDebug.exe: Debug.o useDebug.o + $(CC) $(CCFLAGS) $(LOCFLAGS) Debug.o useDebug.o -o useDebug.exe + +add_item.exe: add_item.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) add_item.o Sales_data.o \ + -o add_item.exe + +avg_price.exe: avg_price.o Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) avg_price.o Sales_data.o \ + -o avg_price.exe + diff --git a/7/runpgms b/7/runpgms new file mode 100644 index 0000000..3862f3d --- /dev/null +++ b/7/runpgms @@ -0,0 +1,4 @@ +echo "avg_price:" && ./avg_price < data/book_sales +echo "add_item:" && ./add_item < data/add_item + +../noinput useAccount.exe useDebug.exe useScreen.exe diff --git a/7/useAccount.cc b/7/useAccount.cc new file mode 100644 index 0000000..acea15d --- /dev/null +++ b/7/useAccount.cc @@ -0,0 +1,46 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include "Account.h" + +int main() +{ + Account a1("bem", 42); + cout << a1.balance() << endl; + a1.calculate(); + cout << a1.balance() << endl; + + return 0; +} diff --git a/7/useDebug.cc b/7/useDebug.cc new file mode 100644 index 0000000..980ef6f --- /dev/null +++ b/7/useDebug.cc @@ -0,0 +1,58 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cerr; using std::endl; + +#include "Debug.h" + +int main() +{ + constexpr Debug io_sub(false, true, false); // debugging IO + if (io_sub.any()) // equivalent to if(true) + cerr << "print appropriate error messages" << endl; + + constexpr Debug prod(false); // no debugging during production + if (prod.any()) // equivalent to if(false) + cerr << "print an error message" << endl; + + IO_Subsystem ioErrs; // by default, don't print any debugging + // no debugging here + if (ioErrs.default_debug()) // if (false || debug.any()) + cerr << "print message 3" << endl; + ioErrs.set_debug(true); // turn on debugging + if (ioErrs.default_debug()) // if (false || debug.any()) + cerr << "print message 4" << endl; + ioErrs.set_debug(false); // okay, debugging section complete + + HW_Subsystem hw; + hw.set_debug(true); + if (ioErrs.default_debug() || hw.default_debug()) // if (false || debug.any()) + cerr << "print message 5" << endl; +} diff --git a/7/useScreen.cc b/7/useScreen.cc new file mode 100644 index 0000000..15785ff --- /dev/null +++ b/7/useScreen.cc @@ -0,0 +1,67 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cout; using std::endl; + +#include +using std::string; + +#include "Screen.h" + +int main() +{ + Screen myScreen(5,3); + // move the cursor to a given position, and set that character + myScreen.move(4,0).set('#'); + + Screen nextScreen(5, 5, 'X'); + nextScreen.move(4,0).set('#').display(cout); + cout << "\n"; + nextScreen.display(cout); + cout << endl; + + const Screen blank(5, 3); + myScreen.set('#').display(cout); // calls nonconst version + cout << endl; + blank.display(cout); // calls const version + cout << endl; + + myScreen.clear('Z').display(cout); cout << endl; + myScreen.move(4,0); + myScreen.set('#'); + myScreen.display(cout); cout << endl; + myScreen.clear('Z').display(cout); cout << endl; + + // if move returns Screen not Screen& + Screen temp = myScreen.move(4,0); // the return value would be copied + temp.set('#'); // the contents inside myScreen would be unchanged + myScreen.display(cout); + cout << endl; +} diff --git a/8/README b/8/README new file mode 100644 index 0000000..9d8aab5 --- /dev/null +++ b/8/README @@ -0,0 +1,14 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + add_item add_item + add_item add_itemV2 + add_item fileio + sstream sstream + sstream fileio (Note, fileio reads both add_item and sstream) + +Programs not listed above print output and do +not read any input + diff --git a/8/add_item.cc b/8/add_item.cc new file mode 100644 index 0000000..015be26 --- /dev/null +++ b/8/add_item.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" +#include + +using std::cerr; using std::cin; using std::cout; using std::endl; + +int main() +{ + Sales_data data1, data2; + read(cin, data1); // read the transactions + read(cin, data2); + // code to add into data1 and data2 unchanged from chapter 7 + if (data1.isbn() == data2.isbn()) { // check isbns + Sales_data sum = add(data1, data2); // add the transactions + print(cout, sum); // print the results + cout << endl; // followed by a newline + } + + return 0; +} diff --git a/8/add_itemV2.cc b/8/add_itemV2.cc new file mode 100644 index 0000000..2c3234d --- /dev/null +++ b/8/add_itemV2.cc @@ -0,0 +1,48 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include "Sales_data.h" +#include + +using std::cerr; using std::cin; using std::cout; using std::endl; + +int main() +{ + Sales_data data1, data2; + if (read(cin, data1) && read(cin, data2)) { // read the transactions + if (data1.isbn() == data2.isbn()) { // check isbns + Sales_data sum = add(data1, data2); // add the transactions + print(cout, sum); // print the results + cout << endl; // followed by a newline + } + } else + cerr << "Input failed!" << endl; + + return 0; +} diff --git a/8/buf.cc b/8/buf.cc new file mode 100644 index 0000000..3746694 --- /dev/null +++ b/8/buf.cc @@ -0,0 +1,53 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::endl; using std::flush; using std::ends; +using std::unitbuf; using std::nounitbuf; using std::cout; + +int main() +{ + // writes hi and a newline, then flushes the buffer + cout << "hi!" << endl; + + // writes hi, then flushes the buffer; adds no data + cout << "hi!" << flush; + + // writes hi and a null, then flushes the buffer + cout << "hi!" << ends; + + cout << unitbuf; // all writes will be flushed immediately + + // any output is flushed immediately, no buffering + cout << "first" << " second" << endl; + + cout << nounitbuf; // returns to normal buffering + + return 0; +} diff --git a/8/clearIO.cc b/8/clearIO.cc new file mode 100644 index 0000000..f85657d --- /dev/null +++ b/8/clearIO.cc @@ -0,0 +1,74 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::endl; + +#include +using std::istringstream; + +#include +using std::string; + +void read() +{ + // turns on both fail and bad bits + cin.setstate(cin.badbit | cin.eofbit | cin.failbit); +} + +void off() +{ + // turns off failbit and badbit but all other bits unchanged + cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit); +} + + +int main() +{ + cout << "before read" << endl; + if (cin.good()) cout << "cin's good" << endl; + if (cin.bad()) cout << "cin's bad" << endl; + if (cin.fail()) cout << "cin's fail" << endl; + if (cin.eof()) cout << "cin's eof" << endl; + + read(); + cout << "after read" << endl; + if (cin.good()) cout << "cin's good" << endl; + if (cin.bad()) cout << "cin's bad" << endl; + if (cin.fail()) cout << "cin's fail" << endl; + if (cin.eof()) cout << "cin's eof" << endl; + + off(); + cout << "after off" << endl; + if (cin.good()) cout << "cin's good" << endl; + if (cin.bad()) cout << "cin's bad" << endl; + if (cin.fail()) cout << "cin's fail" << endl; + if (cin.eof()) cout << "cin's eof" << endl; + return 0; +} diff --git a/8/data/add_item b/8/data/add_item new file mode 100644 index 0000000..92fd61c --- /dev/null +++ b/8/data/add_item @@ -0,0 +1,2 @@ +0-201-78345-X 3 20.00 +0-201-78345-X 2 25.00 diff --git a/8/data/sstream b/8/data/sstream new file mode 100644 index 0000000..284eaef --- /dev/null +++ b/8/data/sstream @@ -0,0 +1,3 @@ +morgan 2015552368 8625550123 +drew 9735550130 +lee 6095550132 2015550175 8005550000 diff --git a/8/fileIO.cc b/8/fileIO.cc new file mode 100644 index 0000000..7162d3c --- /dev/null +++ b/8/fileIO.cc @@ -0,0 +1,72 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cerr; using std::cout; using std::endl; + +#include +using std::ifstream; + +#include +using std::string; + +#include +using std::runtime_error; + +void process(ifstream &is) +{ + string s; + while (is >> s) + cout << s << endl; +} + +int main(int argc, char* argv[]) +{ + // for each file passed to the program + for (auto p = argv + 1; p != argv + argc; ++p) { + ifstream input(*p); // create input and open the file + if (input) { // if the file is ok, ``process'' this file + process(input); + } else + cerr << "couldn't open: " + string(*p); + } // input goes out of scope and is destroyed on each iteration + + auto p = argv + 1, end = argv + argc; + + ifstream input; + while (p != end) { // for each file passed to the program + input.open(*p); // open the file, automatically clears the stream + if (input) { // if the file is ok, read and ``process'' the input + process(input); + } else + cerr << "couldn't open: " + string(*p); + input.close(); // close file when we're done with it + ++p; // increment pointer to get next file + } +} diff --git a/8/makefile b/8/makefile new file mode 100644 index 0000000..74d1ddc --- /dev/null +++ b/8/makefile @@ -0,0 +1,18 @@ +# executable files for this directory +OBJECTS = buf.exe clearIO.exe fileIO.exe add_item.exe \ + add_itemV2.exe sstream.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I../7 + +add_itemV2.exe: add_itemV2.o ../7/Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) add_itemV2.o ../7/Sales_data.o \ + -o add_itemV2.exe + +add_item.exe: add_item.o ../7/Sales_data.o + $(CC) $(CCFLAGS) $(LOCFLAGS) add_item.o ../7/Sales_data.o \ + -o add_item.exe + diff --git a/8/runpgms b/8/runpgms new file mode 100644 index 0000000..4975537 --- /dev/null +++ b/8/runpgms @@ -0,0 +1,6 @@ +echo "add_item:" && ./add_item < data/add_item +echo "add_itemV2:" && ./add_itemV2 < data/add_item +echo "fileIO:" && ./fileIO data/add_item data/sstream +echo "sstream:" && ./sstream < data/sstream + +../noinput buf.exe clearIO.exe diff --git a/8/sstream.cc b/8/sstream.cc new file mode 100644 index 0000000..70901f3 --- /dev/null +++ b/8/sstream.cc @@ -0,0 +1,109 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::cin; using std::cout; using std::cerr; +using std::istream; using std::ostream; using std::endl; + +#include +using std::ostringstream; using std::istringstream; + +#include +using std::vector; + +#include +using std::string; + +// members are public by default +struct PersonInfo { + string name; + vector phones; +}; + +// we'll see how to reformat phone numbers in chapter 17 +// for now just return the string we're given +string format(const string &s) { return s; } + +bool valid(const string &s) +{ + // we'll see how to validate phone numbers + // in chapter 17, for now just return true + return true; +} + +vector +getData(istream &is) +{ + // will hold a line and word from input, respectively + string line, word; + + // will hold all the records from the input + vector people; + + // read the input a line at a time until end-of-file (or other error) + while (getline(is, line)) { + PersonInfo info; // object to hold this record's data + istringstream record(line); // bind record to the line we just read + record >> info.name; // read the name + while (record >> word) // read the phone numbers + info.phones.push_back(word); // and store them + people.push_back(info); // append this record to people + } + + return people; +} + +ostream& process(ostream &os, vector people) +{ + for (const auto &entry : people) { // for each entry in people + ostringstream formatted, badNums; // objects created on each loop + for (const auto &nums : entry.phones) { // for each number + if (!valid(nums)) { + badNums << " " << nums; // string in badNums + } else + // ``writes'' to formatted's string + formatted << " " << format(nums); + } + if (badNums.str().empty()) // there were no bad numbers + os << entry.name << " " // print the name + << formatted.str() << endl; // and reformatted numbers + else // otherwise, print the name and bad numbers + cerr << "input error: " << entry.name + << " invalid number(s) " << badNums.str() << endl; + } + + return os; +} + +int main() +{ + process(cout, getData(cin)); + + return 0; +} diff --git a/9/README b/9/README new file mode 100644 index 0000000..236afea --- /dev/null +++ b/9/README @@ -0,0 +1,10 @@ +Some programs read cin for their input. +Sample data files are in the data directory: + + File Programs that use that input file + ---- -------- + erase erase + +Programs not listed above print output and do +not read any input + diff --git a/9/capacity.cc b/9/capacity.cc new file mode 100644 index 0000000..fcdfe43 --- /dev/null +++ b/9/capacity.cc @@ -0,0 +1,77 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + vector ivec; + + // size should be zero; capacity is implementation defined + cout << "ivec: size: " << ivec.size() + << " capacity: " << ivec.capacity() << endl; + + // give ivec 24 elements + for (vector::size_type ix = 0; ix != 24; ++ix) + ivec.push_back(ix); + + // size should be 24; capacity will be >= 24 and is implementation defined + cout << "ivec: size: " << ivec.size() + << " capacity: " << ivec.capacity() << endl; + ivec.reserve(50); // sets capacity to at least 50; might be more + // size should be 24; capacity will be >= 50 and is implementation defined + cout << "ivec: size: " << ivec.size() + << " capacity: " << ivec.capacity() << endl; + + // add elements to use up the excess capacity + while (ivec.size() != ivec.capacity()) + ivec.push_back(0); + + // capacity should be unchanged and size and capacity are now equal + cout << "ivec: size: " << ivec.size() + << " capacity: " << ivec.capacity() << endl; + ivec.push_back(42); // add one more element + + // size should be 51; capacity will be >= 51 and is implementation defined + cout << "ivec: size: " << ivec.size() + << " capacity: " << ivec.capacity() << endl; + + ivec.shrink_to_fit(); // ask for the memory to be returned + + // size should be unchanged; capacity is implementation defined + cout << "ivec: size: " << ivec.size() + << " capacity: " << ivec.capacity() << endl; + +return 0; +} + diff --git a/9/data/erase b/9/data/erase new file mode 100644 index 0000000..82e87be --- /dev/null +++ b/9/data/erase @@ -0,0 +1,7 @@ +Esmeralda +Frollo +Hunckback +Pierre +Quasimodo +Torterue +Tristan diff --git a/9/erase1.cc b/9/erase1.cc new file mode 100644 index 0000000..7993fd3 --- /dev/null +++ b/9/erase1.cc @@ -0,0 +1,83 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::find; + +#include +using std::string; + +#include +using std::list; + +#include +using std::vector; + +#include +using std::cin; using std::cout; using std::endl; + +int main() +{ + list slist; + string s; + while (cin >> s) + slist.push_back(s); // read the contents into slist + + /* we'll explain find in chapter 10 + * find looks in the sequence denoted by its first two + * iterator arguments for the value of its third argument + * returns an iterator to the first element with that value + * if that element exists in the input sequence + * otherwise returns the end iterator + */ + auto iter = find(slist.begin(), slist.end(), "Quasimodo"); + if (iter != slist.end()) + slist.erase(iter); + + auto orig = slist; // keep a copy before we destroy the contents + slist.clear(); // delete all the elements within the container + cout << "after clear, size is: " << slist.size() << endl; + + slist = orig; // restore the data + slist.erase(slist.begin(), slist.end()); // equivalent + cout << "after erase begin to end, size is: " << slist.size() << endl; + + slist = orig; // restore the data + auto elem1 = slist.begin(), elem2 = slist.end(); + // delete the range of elements between two iterators + // returns an iterator to the element just after the last removed element + elem1 = slist.erase(elem1, elem2); // after the call elem1 == elem2 + cout << "after erase elem1 to elem2 size is: " << slist.size() << endl; + + if (elem1 != elem2) + cout << "somethings wrong" << endl; + else + cout << "okay, they're equal " << endl; + +} diff --git a/9/erase2.cc b/9/erase2.cc new file mode 100644 index 0000000..1eb95ee --- /dev/null +++ b/9/erase2.cc @@ -0,0 +1,101 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::find; + +#include +using std::string; + +#include +using std::list; + +#include +using std::forward_list; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +int main() +{ + // lst has ten elements 0 ... 9 in value + list lst = {0,1,2,3,4,5,6,7,8,9}; + + // print the initial values in lst + cout << "initial list: "; + for (auto it : lst) + cout << it << " "; + cout << endl; + + // erase the odd elements in lst + auto it = lst.begin(); + while (it != lst.end()) + if (*it % 2) // if the element is odd + it = lst.erase(it); // erase this element + else + ++it; + + // print the current contents of lst + cout << "after erasing odd elements from lst: "; + for (auto it : lst) + cout << it << " "; + cout << endl; + + // repeat the same actions but on a forward_list + forward_list flst = {0,1,2,3,4,5,6,7,8,9}; + + // print the initial values in flst + cout << "initial list: "; + for (auto it : flst) + cout << it << " "; + cout << endl; + + // erase the odd elements in flst + auto prev = flst.before_begin(); // element "off the start" of flst + auto curr = flst.begin(); // denotes the first element in flst + while (curr != flst.end()) { // while there are still elements + if (*curr % 2) // if the element is odd + curr = flst.erase_after(prev); // erase it and move curr + else { + prev = curr; // move the iterators to denote the next + ++curr; // element and one before the next element + } + } + + // print the current contents of lst + cout << "after erasing elements from flst: "; + for (auto it : flst) + cout << it << " "; + cout << endl; + + return 0; +} diff --git a/9/find-str.cc b/9/find-str.cc new file mode 100644 index 0000000..fb2e647 --- /dev/null +++ b/9/find-str.cc @@ -0,0 +1,45 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + string name("AnnaBelle"); + auto pos1 = name.find("Anna"); // pos1 == 0 + cout << pos1 ; + string lowercase("annabelle"); + pos1 = lowercase.find("Anna"); // pos1 == npos + cout << " " << pos1 << endl; + return 0; +} diff --git a/9/find_ops.cc b/9/find_ops.cc new file mode 100644 index 0000000..34a369a --- /dev/null +++ b/9/find_ops.cc @@ -0,0 +1,70 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +int main() +{ + string numbers("0123456789"), name("r2d2"); + // returns 1, i.e., the index of the first digit in name + auto pos = name.find_first_of(numbers); + if (pos != string::npos) + cout << "found number at index: " << pos + << " element is " << name[pos] << endl; + else + cout << "no number in: " << name << endl; + + pos = 0; + // each iteration finds the next number in name + while ((pos = name.find_first_of(numbers, pos)) + != string::npos) { + cout << "found number at index: " << pos + << " element is " << name[pos] << endl; + + ++pos; // move to the next character + } + + string river("Mississippi"); + + auto first_pos = river.find("is"); // returns 1 + auto last_pos = river.rfind("is"); // returns 4 + cout << "find returned: " << first_pos + << " rfind returned: " << last_pos << endl; + + string dept("03714p3"); + // returns 5, which is the index to the character 'p' + pos = dept.find_first_not_of(numbers); + cout << "first_not returned: " << pos << endl; + + return 0; +} diff --git a/9/makefile b/9/makefile new file mode 100644 index 0000000..b52891b --- /dev/null +++ b/9/makefile @@ -0,0 +1,11 @@ +# executable files for this directory +OBJECTS = capacity.exe erase1.exe erase2.exe find_ops.exe \ + find-str.exe refreshIter.exe stack.exe \ + str_assignOps.exe substr.exe useConvs.exe + +# tells make to use the file "../GNU_makefile_template", which +# defines general rules for making .o and .exe files +include ../GNU_makefile_template + +LOCFLAGS = -I..\7 + diff --git a/9/refreshIter.cc b/9/refreshIter.cc new file mode 100644 index 0000000..589fe20 --- /dev/null +++ b/9/refreshIter.cc @@ -0,0 +1,66 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::vector; + +#include +using std::cout; using std::endl; + +void printVec(const vector &vi) +{ + // print the vector's elements + auto iter = vi.begin(); + while (iter != vi.end()) + cout << *iter++ << endl; +} + +int main() +{ + // silly loop to remove even-valued elements + // and insert a duplicate of odd-valued elements + vector vi = {0,1,2,3,4,5,6,7,8,9}; + printVec(vi); + + // we call begin, not cbegin because we're changing vi + auto iter = vi.begin(); + while (iter != vi.end()) { + if (*iter % 2) { // if the element is odd + iter = vi.insert(iter, *iter); // duplicate it + iter += 2; // advance past this element and the new one + } else + iter = vi.erase(iter); // remove even elements + // don't advance the iterator; + // iter denotes the element after the one we erased + } + printVec(vi); + + return 0; +} + diff --git a/9/runpgms b/9/runpgms new file mode 100644 index 0000000..c747211 --- /dev/null +++ b/9/runpgms @@ -0,0 +1,5 @@ +echo "erase1: " && ./erase1 < data/erase + +../noinput capacity.exe erase2.exe find_ops.exe \ + find-str.exe refreshIter.exe stack.exe \ + str_assignOps.exe substr.exe useConvs.exe diff --git a/9/stack.cc b/9/stack.cc new file mode 100644 index 0000000..60ab145 --- /dev/null +++ b/9/stack.cc @@ -0,0 +1,61 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::size_t; + +#include +using std::deque; + +#include +using std::stack; + +#include +using std::cout; using std::cerr; using std::endl; + +bool process(int); + +int main() +{ + stack intStack; // empty stack + + // fill up the stack + for (size_t ix = 0; ix != 10; ++ix) + intStack.push(ix); // intStack holds 0 . . . 9 inclusive + + // while there are still values in intStack + while (!intStack.empty()) { + int value = intStack.top(); + // code that uses value + cout << value << endl; + intStack.pop(); // pop the top element, and repeat + } + + return 0; +} diff --git a/9/str_assignOps.cc b/9/str_assignOps.cc new file mode 100644 index 0000000..8ffa561 --- /dev/null +++ b/9/str_assignOps.cc @@ -0,0 +1,106 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::vector; + +#include +using std::cout; using std::endl; + + +int main() +{ + string s = "some string", s2 = "some other string"; + // equivalent ways to insert all the characters from s2 at beginning of s + // insert iterator range before s.begin() + s.insert(s.begin(), s2.begin(), s2.end()); + cout << "insert iterators version: " << s << endl; + + s = "some string"; + s.insert(0, s2); // insert a copy of s2 before position 0 in s + cout << "insert string at given position: " << s << endl; + + s = "some string"; + // insert s2.size() characters from s2 starting at s2[0] before s[0] + s.insert(0, s2, 0, s2.size()); + cout << "insert positional version: " << s << endl; + + + s = ""; // s is now empty + vector c_vec(1, 'a'); + // insert characters from c_vec into s + s.insert(s.begin(), c_vec.begin(), c_vec.end()); + s.insert(s.size(), 5, '!'); // add five exclamation points at the end of s + cout << s << endl; + + s.erase(s.size() - 5, 5); // erase the last five characters from s + cout << s << endl; + + s = ""; // s is now empty + const char *cp = "Stately, plump Buck"; + s.assign(cp, 7); // s == "Stately" + cout << s << endl; + s.insert(s.size(), cp + 7); // s == "Stately, plump Buck" + cout << s << endl; + + s = "C++ Primer"; // reset s and s2 + s2 = s; // to "C++ Primer" + s.insert(s.size(), " 4th Ed."); // s == "C++ Primer 4th Ed." + s2.append(" 4th Ed."); // equivalent: appends " 4th Ed." to s2; + cout << s << " " << s2 << endl; + + // two ways to replace "4th" by "5th" + // 1. insert and erase + s.erase(11, 3); // s == "C++ Primer Ed." + s.insert(11, "5th"); // s == "C++ Primer 5th Ed." + + // 2. use replace + // erase three characters starting at position 11 + // and then insert "5th" + s2.replace(11, 3, "5th"); // equivalent: s == s2 + + cout << s << " " << s2 << endl; + + // two ways to replace "5th" by "Fifth" + // 1. use replace if we know where the string we want to replace is + s.replace(11, 3, "Fifth"); // s == "C++ Primer Fifth Ed." + + // 2. call find first to get position from which to replace + auto pos = s2.find("5th"); + if (pos != string::npos) + s2.replace(pos, 3, "Fifth"); + else + cout << "something's wrong, s2 is: " << s2 << endl; + cout << s << " " << s2 << endl; + + return 0; +} diff --git a/9/substr.cc b/9/substr.cc new file mode 100644 index 0000000..c3ca04c --- /dev/null +++ b/9/substr.cc @@ -0,0 +1,50 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#include +using std::string; + +#include +using std::cout; using std::endl; + +#include +using std::out_of_range; + +int main() +{ + try { + string s("hello world"); + cout << s.substr(0, 5) << endl; // prints hello + cout << s.substr(6) << endl; // prints world + cout << s.substr(6, 11) << endl; // prints world + cout << s.substr(12) << endl; // throws out_of_range + } catch(out_of_range) {cout << "caught out_of_range" << endl; } + + return 0; +} diff --git a/9/useConvs.cc b/9/useConvs.cc new file mode 100644 index 0000000..a3c1411 --- /dev/null +++ b/9/useConvs.cc @@ -0,0 +1,60 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +// Version_test.h contains definitions for to_string and stod +// if the compiler does not yet define these functions, +// this code will use the definitions we provide +#include "Version_test.h" + +#include +using std::string; +#ifdef STRING_NUMERIC_CONVS +using std::to_string; using std::stod; +#endif + +#include +using std::cout; using std::endl; + +int main() +{ + int i = 42; + // converts the int i to its character representation + string s = to_string(i); + + double d = stod(s); // converts the string s to floating-point + cout << "i = " << i << " s = " << s << " d is: " << d << endl; + + // convert the first substring in s that starts with a digit, d = 3.14 + string s2 = "pi = 3.14"; + d = stod(s2.substr(s2.find_first_of("+-.0123456789"))); + + cout << "d = " << d << " s = " << s << " s2 is: " << s2 << endl; + + return 0; +} diff --git a/CompilerNotes.pdf b/CompilerNotes.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d1b406fe4801460d810be0bfac3872363c91e289 GIT binary patch literal 26405 zcmagGb9iOVwl5spwmM12ww-irYsI#0+a0T8+qP}nHot!N+2^_EK6l^muK#M*oTJA0 z)tEIn7n!_>7%d|mD?HiW{`5RN3?nNM1CgzP1w0QAy|lTFF~Gsx!`O(3kzSgJiH(JU zm0pgBk)2+Yh>4Mvm62Zd?}YN-2?kC!dI=&eA{KU*ztb!_e0=c6Hb(!DA^Jal@Gwkl z|7#5qV?$dbV|jpswd3Cvg>9{zt!*5MSm@;)jE&3)Ar2NnRv~s)PF5CXRwfovVNOOi1|}w9W{;#(%hR`IngN^oq8&PDG6VNT5is^cNc<7G`>7cROQxbwy(ndNnOZA|@inzW_Ko zIT!=1;bF|S3=E8r^z{w&%Yaa#Dg^=nByswWz$lp^0s-aVA}=7&vws8v3~A%^-(g5y zIG7k1nI=F__4NxgU#8vmPzC&ef%c+c(BWbJ0{U;7|AyAl3E<%5=3s0B55oWtLq;Yl zCk7Am-*CgjurU2EltqcyIsYSuM2w98Y%M42e+$dOLa#)`%<;eRudlyjq^}=g3KSqf z2BF*oH9!N02R~wvDL>J}5bvMruW;Sb@0I8Y3>>Sal>b9NJ!Lx3q6>OJ8V-(nEbl{> z$i!HG1sQRdnF$083n;%F9`!#6^DjjIt#>lUP5>i-6M%^MUnt80tc@LsnAra{kh3$k z5j1o%x3$q?{I4rw=1w~F@&Hp~X@G&TmE&KD{&nL&S7iRG7QKq2v8=6)@xN~QJNsWW z|FvVnf7h_GHT_@8`Y($AW_unV~du2sqM_XqH!+#|DFG`BGPJfk}h~dAq=pVNK z2Sfj?{%?lBFazg*?;ax)E5|=CCF_3?lQXbTav3 zoio9|7%O*Cs73&A7$G)1Q@N0?JG;QL`d|o>FW~df2Q4$^QbQ0PqAq&%EYRGur!A{8 zQUg3mjJ@Edr4kT%HK~}Y#Q2k9xw}$qO?Do0?;BrlyG+zfIjL37S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)x?Jb{SyaHffGYRg6;Uv1f3>#99863v;gQRn}w zB^&hpYdwJ8*$oZX+RKJM@Z&M!;rVR1kN z3WlZ2g%N=A^g0CMi39|eNQBDSLF9kPU03#(Pl|8o(I46LUkmWs&&9^N{MJVQ{r^85 z{`Y0CN&HXA8Wg{C@gH2jgVI`_|KM7K;&(3ogX?!tTFdhvTx(GL&c%Ol{SHcNd42^K z$Il5TflQxedeNtoo6Tr{eU{*FK^Wxfs_{RDlVEb@-(oP$;3k>FknY)L1L-(9IW`Vf z>760EX1qfAdrXg=$=h=#<1F~6K$tSiR~ZwYboT5l@f2d%e#yCYG}9K4AtC#h&cW*wPy1J1 zuvDdS0l?hz+JEFfJ9K@5;lkN;jz4CGP(79^@jPM~6P=ZJTNQfZ6oxYPSJPMX3-lEq zZHB zBt#8{gg}(7!MbE31pr^Q$@E9qN*+O9rl!yU@NbLXW+Yz%0FL|?{je}45Wz<+q4YyB zQ0t!|5QjlKI|fFnhb6XL23)b+s_L$6k^D^JuoHGxT!3y8uN#vlGXr}#2ypi#`~5Ty z@syzK&>IZ8LA_j!E5XbXOtB0x2}Y2>u;LWJ=oaRk3|fg?=9^$gA97P^-t_4^HwP3d O$IT%rsb#Fq&GA2Um=R9^ literal 0 HcmV?d00001 diff --git a/GNU_makefile_template b/GNU_makefile_template new file mode 100644 index 0000000..a92dc64 --- /dev/null +++ b/GNU_makefile_template @@ -0,0 +1,56 @@ +#g++ compiler: options + +# -std=c++0x enables ISO C++ 11 standard +# -I.. pulls in the Version_test.h file for conditional compilation +# of code the uses features that might not yet be implemented +CC = g++ +CCFLAGS = -std=c++0x -I.. + +# Some programs include headers defined in earlier chapters +# LOCFLAGS used to set tell the compiler where to find a +# header that is not in the same directory as the source file itself +# LOCFLAGS will be set in directory level makefiles as needed +LOCFLAGS = + +### + +####### To compile without using a makefile +# To compile an object file from a source file you could execute +# g++ -std=c++0x -c filename.cc # produces filename.obj +# To compile an executable file from an object file, you would execute +# g++ -std=c++0x filename.o # produces filename.exe +# To compile an executable file from a source file, you would execute +# g++ -std=c++0x filename.cc # produces filename.exe +####### + +# each subdirectory contains a Makefile that lists the executable +# files that can be made in that directory. That list is assigned +# to the make macro named $OBJECTS +# This rule says that the make target named "all" depends on those +# files. Executing "make all" in a subdirectory will cause make +# to build each .exe file listed in that subdirectory's makefile +all: $(OBJECTS) + +# rule that says how to make a .o object file from a .cc source file +# for a given source file in a given directory you could compile it +# into an object file by executing "make filename.o" + +# $< and $@ are macros defined by make +# $< refers to the file being processed (i.e., compiled or linked) +# $@ refers to the generated file +%.o: %.cc + $(CC) $(CCFLAGS) $(LOCFLAGS) -c $< -o $@ + +# rule that says how to make a .exe executable file from a .o object file +%.exe: %.o + $(CC) $(CCFLAGS) $(LOCFLAGS) $< -o $@ + +# target to clean up the object files and any core files +# executing "make clean" in a subdirectory will remove all +# files named core or any file ending in .obj, or .stackdump +clean: + rm -rf *.o core *.stackdump + +# target to remove executable files as well as object and core files +clobber: clean + rm -rf *.exe diff --git a/Version_test.h b/Version_test.h new file mode 100644 index 0000000..188b49d --- /dev/null +++ b/Version_test.h @@ -0,0 +1,105 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced. Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +#ifndef VERSION_TEST_H +#define VERSION_TEST_H + + +/* As of the first printing of C++ Primer, 5th Edition (July 2012), + * the gcc Version 4.7.0 did not yet support some C++ 11 features. + * + * The code we distribute contains both normal C++ code and + * workarounds for missing features. We use preprocessor variables to + * determine whether a given features is implemented in a given release + * of the compiler. + * The base version we used to test the code in the book + * is gcc version 4.7.0 (GCC) + * + * When new releases are available we will update this file which will + * #define the features implmented in that release. +*/ + +#if __cplusplus == 201103L +// base version, future releases of this file will +// #define these variables as features as they are implemented + +/* Code in this delivery use the following variables to control compilation + + Variable tests C++ 11 Feature +HEX_MANIPS hexfloat and defaultfloat manipulators +REFMEMS reference qualified member functions +REGEX regular expressions library +STRING_NUMERIC_CONVS conversions to and from string to numeric +*/ +#endif // ends compiler version check + +#ifndef STRING_NUMERIC_CONVS +// if the library doesn't define to_string +// or the numeric conversion functions +// as a workaround we define to_string and stod in this file + +// Readers can ignore the implemnetations of to_string and stod +// but can use these functions in their code. + +#include +#include +#include +#include +// we use sprintf from stdio to implement to_string +#include + +inline +std::string to_string(int i) +{ + char buf[100]; + std::sprintf(buf, "%d", i); + return buf; +} + +inline +double stod(const std::string &s, std::size_t * = 0) +{ + char **buf = 0; + return std::strtod(s.c_str(), buf); +} +#endif // STRING_NUMERIC_CONVS + +#include + +#ifndef HEX_MANIPS +inline +std::ostream &defaultfloat(std::ostream &os) +{ + os.unsetf(std::ios_base::floatfield); + return os; +} +#endif // HEX_MANIPS + +#endif // ends header guard + diff --git a/makefile b/makefile new file mode 100644 index 0000000..bec114f --- /dev/null +++ b/makefile @@ -0,0 +1,65 @@ +DIRS = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + +all: + cd 1; make + cd 2; make + cd 3; make + cd 4; make + cd 5; make + cd 6; make + cd 7; make + cd 8; make + cd 9; make + cd 10; make + cd 11; make + cd 12; make + cd 13; make + cd 14; make + cd 15; make + cd 16; make + cd 17; make + cd 18; make + cd 19; make + +clean: + cd 1; make clean + cd 2; make clean + cd 3; make clean + cd 4; make clean + cd 5; make clean + cd 6; make clean + cd 7; make clean + cd 8; make clean + cd 9; make clean + cd 10; make clean + cd 11; make clean + cd 12; make clean + cd 13; make clean + cd 14; make clean + cd 15; make clean + cd 16; make clean + cd 17; make clean + cd 18; make clean + cd 19; make clean + +clobber: + cd 1; make clobber + cd 2; make clobber + cd 3; make clobber + cd 4; make clobber + cd 5; make clobber + cd 6; make clobber + cd 7; make clobber + cd 8; make clobber + cd 9; make clobber + cd 10; make clobber + cd 11; make clobber + cd 12; make clobber + cd 13; make clobber + cd 14; make clobber + cd 15; make clobber + cd 16; make clobber + cd 17; make clobber + cd 18; make clobber + cd 19; make clobber + diff --git a/noinput b/noinput new file mode 100644 index 0000000..e247914 --- /dev/null +++ b/noinput @@ -0,0 +1,5 @@ +for i +do + echo $i + ./$i +done diff --git a/runpgms b/runpgms new file mode 100644 index 0000000..c8ccd61 --- /dev/null +++ b/runpgms @@ -0,0 +1,7 @@ +for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 +do + echo $i + cd $i + runpgms + cd .. +done -- 2.44.0