├── Chapter17 ├── strings.dat ├── file2 ├── file1 ├── file3 ├── Gratzer-Chapter17.zip ├── mat.dat ├── pat.dat ├── Review Questions Chapter 17.pdf ├── Programming Exercises Chapter 17.pdf ├── Review Questions Chapter 17.aux ├── Programming Exercises Chapter 17.aux ├── employeeList.dat ├── pe17-1.cpp ├── pe17-2.cpp ├── pe17-3.cpp ├── pe17-4.cpp ├── pe17-5.cpp └── pe17-7.cpp ├── Chapter3 ├── Prata-Chapter3.zip ├── Programming Exercises Chapter 3.pdf ├── Programming Exercises Chapter 3.aux ├── pe3-5.cpp ├── pe3-1.cpp ├── pe3-6.cpp ├── pe3-2.cpp ├── pe3-3.cpp └── pe3-4.cpp ├── Chapter4 ├── Prata-Chapter4.zip ├── Review Questions Chapter 4.pdf ├── Programming Exercises Chapter 4.pdf ├── Review Questions Chapter 4.aux ├── Programming Exercises Chapter 4.aux ├── pe4-4.cpp ├── pe4-5.cpp ├── pe4-3.cpp ├── pe4-2.cpp ├── pe4-7.cpp ├── pe4-8.cpp ├── pe4-1.cpp ├── pe4-6.cpp └── pe4-9.cpp ├── Chapter5 ├── Prata-Chapter5.zip ├── Review Questions Chapter 5.pdf ├── Programming Exercises Chapter 5.pdf ├── Review Questions Chapter 5.aux ├── Programming Exercises Chapter 5.aux ├── pe5-9.cpp ├── pe5-2.cpp ├── pe5-8.cpp ├── pe5-7.cpp ├── pe5-4.cpp ├── pe5-1.cpp ├── pe5-5.cpp ├── pe5-6.cpp └── pe5-3.cpp ├── Chapter6 ├── Prata-Chapter6.zip ├── Review Questions Chapter 6.pdf ├── Programming Exercises Chapter 6.pdf ├── contributions2.txt ├── Review Questions Chapter 6.aux ├── Programming Exercises Chapter 6.aux ├── file.txt ├── pe6-1.cpp ├── pe6-3.cpp ├── pe6-7.cpp ├── pe6-5.cpp ├── pe6-8.cpp ├── pe6-2.cpp └── pe6-6.cpp ├── Chapter7 ├── Prata-Chapter7.zip ├── Review Questions Chapter 7.pdf ├── Programming Exercises Chapter 7.pdf ├── Review Questions Chapter 7.aux ├── Programming Exercises Chapter 7.aux ├── pe7-1.cpp ├── pe7-5.cpp ├── pe7-4.cpp ├── pe7-9.cpp ├── pe7-7.cpp ├── pe7-2.cpp ├── pe7-6.cpp └── pe7-3.cpp ├── Chapter8 ├── Prata-Chapter8.zip ├── Review Questions Chapter 8.pdf ├── Programming Exercises Chapter 8.pdf ├── Review Questions Chapter 8.aux ├── Programming Exercises Chapter 8.aux ├── pe8-3.cpp ├── pe8-5.cpp ├── pe8-1.cpp ├── pe8-6.cpp └── pe8-4.cpp ├── Chapter9 ├── Prata-Chapter9.zip ├── Review Questions Chapter 9.pdf ├── Programming Exercises Chapter 9.pdf ├── Review Questions Chapter 9.aux ├── Programming Exercises Chapter 9.aux ├── sales.h ├── golf.h ├── golf.cpp ├── pe9-2.cpp └── pe9-1.cpp ├── Chapter10 ├── Prata-Chapter10.zip ├── Review Questions Chapter 10.pdf ├── Programming Exercises Chapter 10.pdf ├── Review Questions Chapter 10.aux ├── Programming Exercises Chapter 10.aux ├── plorg.h ├── list.h ├── BankAccount.h ├── pe10-2.cpp ├── move.h ├── plorg.cpp ├── Person.h ├── customerStack.h ├── Person.cpp ├── move.cpp ├── pe10-6.cpp ├── golf2.h ├── customerStack.cpp ├── pe10-1.cpp ├── list.cpp ├── pe10-7.cpp ├── BankAccount.cpp ├── pe10-5.cpp ├── pe10-8.cpp └── golf2.cpp ├── Chapter11 ├── Prata-Chapter11.zip ├── Review Questions Chapter 11.pdf ├── Programming Exercises Chapter 11.pdf ├── Review Questions Chapter 11.aux ├── Programming Exercises Chapter 11.aux ├── usetime4.cpp ├── pe11-5.cpp ├── complex0.h ├── pe11-7.cpp ├── vect2.h ├── vect.h ├── pe11-6.cpp ├── mytime4.h ├── complex0.cpp ├── stonewt3.h ├── stonewt2.h ├── stonewt3.cpp └── stonewt2.cpp ├── Chapter12 ├── Prata-Chapter12.zip ├── Review Questions Chapter 12.pdf ├── Programming Exercises Chapter 12.pdf ├── Review Questions Chapter 12.aux ├── Programming Exercises Chapter 12.aux ├── Cow.h ├── stock3.h ├── pe12-4.cpp ├── stack2.h ├── pe12-1.cpp ├── pe12-3.cpp ├── pe12-2.cpp ├── queue.h ├── string2.h ├── stack2.cpp ├── Cow.cpp └── queue.cpp ├── Chapter13 ├── Prata-Chapter13.zip ├── Review Questions Chapter 13.pdf ├── Programming Exercises Chapter 13.pdf ├── Review Questions Chapter 13.aux ├── Programming Exercises Chapter 13.aux ├── classic2.h ├── classic.h ├── cd2.h ├── cd.h ├── vintageport.h ├── port.h ├── pe13-2.cpp ├── pe13-1.cpp ├── classic.cpp ├── vintageport.cpp ├── cd.cpp ├── pe13-3.cpp ├── classic2.cpp ├── pe13-4.cpp ├── dma2.h └── port.cpp ├── Chapter14 ├── Prata-Chapter14.zip ├── Review Questions Chapter 14.pdf ├── Programming Exercises Chapter 14.pdf ├── Review Questions Chapter 14.aux ├── Programming Exercises Chapter 14.aux ├── winec2.h ├── winec.h ├── pair.h ├── pe14-5.cpp ├── pe14-1.cpp ├── pe14-2.cpp ├── pe14-4.cpp ├── pe14-3.cpp ├── winec.cpp ├── winec2.cpp └── person.h ├── Chapter15 ├── Prata-Chapter15.zip ├── Review Questions Chapter 15.pdf ├── Programming Exercises Chapter 15.pdf ├── Programming Exercises Chapter 15.pdf.u1conflict ├── Review Questions Chapter 15.aux ├── Programming Exercises Chapter 15.aux ├── pe15-1.cpp ├── sales.h ├── sales.cpp ├── tv.cpp ├── tv.h ├── pe15-2.cpp └── pe15-4.cpp ├── Chapter16 ├── Prata-Chapter16.zip ├── Review Questions Chapter 16.pdf ├── Programming Exercises Chapter 16.pdf ├── Programming Exercises Chapter 16.pdf.u1conflict ├── Review Questions Chapter 16.aux ├── Programming Exercises Chapter 16.aux ├── pe16-1.cpp ├── pe16-2.cpp ├── pe16-4.cpp ├── pe16-7.cpp └── pe16-8.cpp └── README /Chapter17/strings.dat: -------------------------------------------------------------------------------- 1 | her e is a line -------------------------------------------------------------------------------- /Chapter17/file2: -------------------------------------------------------------------------------- 1 | zero lassitude 2 | finance drama 3 | -------------------------------------------------------------------------------- /Chapter17/file1: -------------------------------------------------------------------------------- 1 | eggs kites donuts 2 | balloons hammers 3 | stones 4 | -------------------------------------------------------------------------------- /Chapter17/file3: -------------------------------------------------------------------------------- 1 | eggs kites donuts zero lassitude 2 | balloons hammers finance drama 3 | stones 4 | -------------------------------------------------------------------------------- /Chapter3/Prata-Chapter3.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter3/Prata-Chapter3.zip -------------------------------------------------------------------------------- /Chapter4/Prata-Chapter4.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter4/Prata-Chapter4.zip -------------------------------------------------------------------------------- /Chapter5/Prata-Chapter5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter5/Prata-Chapter5.zip -------------------------------------------------------------------------------- /Chapter6/Prata-Chapter6.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter6/Prata-Chapter6.zip -------------------------------------------------------------------------------- /Chapter7/Prata-Chapter7.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter7/Prata-Chapter7.zip -------------------------------------------------------------------------------- /Chapter8/Prata-Chapter8.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter8/Prata-Chapter8.zip -------------------------------------------------------------------------------- /Chapter9/Prata-Chapter9.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter9/Prata-Chapter9.zip -------------------------------------------------------------------------------- /Chapter10/Prata-Chapter10.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter10/Prata-Chapter10.zip -------------------------------------------------------------------------------- /Chapter11/Prata-Chapter11.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter11/Prata-Chapter11.zip -------------------------------------------------------------------------------- /Chapter12/Prata-Chapter12.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter12/Prata-Chapter12.zip -------------------------------------------------------------------------------- /Chapter13/Prata-Chapter13.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter13/Prata-Chapter13.zip -------------------------------------------------------------------------------- /Chapter14/Prata-Chapter14.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter14/Prata-Chapter14.zip -------------------------------------------------------------------------------- /Chapter15/Prata-Chapter15.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter15/Prata-Chapter15.zip -------------------------------------------------------------------------------- /Chapter16/Prata-Chapter16.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter16/Prata-Chapter16.zip -------------------------------------------------------------------------------- /Chapter17/Gratzer-Chapter17.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter17/Gratzer-Chapter17.zip -------------------------------------------------------------------------------- /Chapter17/mat.dat: -------------------------------------------------------------------------------- 1 | Noah Ruderman 2 | Michelle Bachmann 3 | darth Vader 4 | Barney 5 | Virginia Woolf 6 | Prof Roberts 7 | -------------------------------------------------------------------------------- /Chapter17/pat.dat: -------------------------------------------------------------------------------- 1 | prof roberts 2 | noah ruderman 3 | esther jean 4 | murray lamb 5 | yuri levin 6 | prof gessel 7 | michelle bachmann 8 | -------------------------------------------------------------------------------- /Chapter4/Review Questions Chapter 4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter4/Review Questions Chapter 4.pdf -------------------------------------------------------------------------------- /Chapter5/Review Questions Chapter 5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter5/Review Questions Chapter 5.pdf -------------------------------------------------------------------------------- /Chapter6/Review Questions Chapter 6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter6/Review Questions Chapter 6.pdf -------------------------------------------------------------------------------- /Chapter7/Review Questions Chapter 7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter7/Review Questions Chapter 7.pdf -------------------------------------------------------------------------------- /Chapter8/Review Questions Chapter 8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter8/Review Questions Chapter 8.pdf -------------------------------------------------------------------------------- /Chapter9/Review Questions Chapter 9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter9/Review Questions Chapter 9.pdf -------------------------------------------------------------------------------- /Chapter10/Review Questions Chapter 10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter10/Review Questions Chapter 10.pdf -------------------------------------------------------------------------------- /Chapter11/Review Questions Chapter 11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter11/Review Questions Chapter 11.pdf -------------------------------------------------------------------------------- /Chapter12/Review Questions Chapter 12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter12/Review Questions Chapter 12.pdf -------------------------------------------------------------------------------- /Chapter13/Review Questions Chapter 13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter13/Review Questions Chapter 13.pdf -------------------------------------------------------------------------------- /Chapter14/Review Questions Chapter 14.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter14/Review Questions Chapter 14.pdf -------------------------------------------------------------------------------- /Chapter15/Review Questions Chapter 15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter15/Review Questions Chapter 15.pdf -------------------------------------------------------------------------------- /Chapter16/Review Questions Chapter 16.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter16/Review Questions Chapter 16.pdf -------------------------------------------------------------------------------- /Chapter17/Review Questions Chapter 17.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter17/Review Questions Chapter 17.pdf -------------------------------------------------------------------------------- /Chapter3/Programming Exercises Chapter 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter3/Programming Exercises Chapter 3.pdf -------------------------------------------------------------------------------- /Chapter4/Programming Exercises Chapter 4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter4/Programming Exercises Chapter 4.pdf -------------------------------------------------------------------------------- /Chapter5/Programming Exercises Chapter 5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter5/Programming Exercises Chapter 5.pdf -------------------------------------------------------------------------------- /Chapter6/Programming Exercises Chapter 6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter6/Programming Exercises Chapter 6.pdf -------------------------------------------------------------------------------- /Chapter6/contributions2.txt: -------------------------------------------------------------------------------- 1 | 4 2 | Sam Stone 3 | 2000 4 | Freida Flass 5 | 100500 6 | Tammy Tubbs 7 | 5000 8 | Rich Raptor 9 | 55000 10 | -------------------------------------------------------------------------------- /Chapter7/Programming Exercises Chapter 7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter7/Programming Exercises Chapter 7.pdf -------------------------------------------------------------------------------- /Chapter8/Programming Exercises Chapter 8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter8/Programming Exercises Chapter 8.pdf -------------------------------------------------------------------------------- /Chapter9/Programming Exercises Chapter 9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter9/Programming Exercises Chapter 9.pdf -------------------------------------------------------------------------------- /Chapter10/Programming Exercises Chapter 10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter10/Programming Exercises Chapter 10.pdf -------------------------------------------------------------------------------- /Chapter11/Programming Exercises Chapter 11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter11/Programming Exercises Chapter 11.pdf -------------------------------------------------------------------------------- /Chapter12/Programming Exercises Chapter 12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter12/Programming Exercises Chapter 12.pdf -------------------------------------------------------------------------------- /Chapter13/Programming Exercises Chapter 13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter13/Programming Exercises Chapter 13.pdf -------------------------------------------------------------------------------- /Chapter14/Programming Exercises Chapter 14.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter14/Programming Exercises Chapter 14.pdf -------------------------------------------------------------------------------- /Chapter15/Programming Exercises Chapter 15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter15/Programming Exercises Chapter 15.pdf -------------------------------------------------------------------------------- /Chapter16/Programming Exercises Chapter 16.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter16/Programming Exercises Chapter 16.pdf -------------------------------------------------------------------------------- /Chapter17/Programming Exercises Chapter 17.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter17/Programming Exercises Chapter 17.pdf -------------------------------------------------------------------------------- /Chapter15/Programming Exercises Chapter 15.pdf.u1conflict: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter15/Programming Exercises Chapter 15.pdf.u1conflict -------------------------------------------------------------------------------- /Chapter16/Programming Exercises Chapter 16.pdf.u1conflict: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noahc66260/C-PrimerPlus/HEAD/Chapter16/Programming Exercises Chapter 16.pdf.u1conflict -------------------------------------------------------------------------------- /Chapter4/Review Questions Chapter 4.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter5/Review Questions Chapter 5.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter6/Review Questions Chapter 6.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter7/Review Questions Chapter 7.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter8/Review Questions Chapter 8.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter9/Review Questions Chapter 9.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter10/Review Questions Chapter 10.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter11/Review Questions Chapter 11.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter12/Review Questions Chapter 12.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter13/Review Questions Chapter 13.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter14/Review Questions Chapter 14.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter15/Review Questions Chapter 15.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter16/Review Questions Chapter 16.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter17/Review Questions Chapter 17.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter3/Programming Exercises Chapter 3.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter4/Programming Exercises Chapter 4.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter5/Programming Exercises Chapter 5.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter6/Programming Exercises Chapter 6.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter7/Programming Exercises Chapter 7.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter8/Programming Exercises Chapter 8.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter9/Programming Exercises Chapter 9.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter10/Programming Exercises Chapter 10.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter11/Programming Exercises Chapter 11.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter12/Programming Exercises Chapter 12.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter13/Programming Exercises Chapter 13.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter14/Programming Exercises Chapter 14.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter15/Programming Exercises Chapter 15.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter16/Programming Exercises Chapter 16.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter17/Programming Exercises Chapter 17.aux: -------------------------------------------------------------------------------- 1 | \relax 2 | \newlabel{tocindent-1}{0pt} 3 | \newlabel{tocindent0}{0pt} 4 | \newlabel{tocindent1}{0pt} 5 | \newlabel{tocindent2}{0pt} 6 | \newlabel{tocindent3}{0pt} 7 | -------------------------------------------------------------------------------- /Chapter6/file.txt: -------------------------------------------------------------------------------- 1 | This is a document that contains letters 2 | 3 | as well as numbers and newline characters as well. The 4 | intention is for this to complement the file 5 | read_file.cpp 6 | 7 | We'll see how it goes 12345. 8 | 9 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Solutions to all exercises of C++ Primer Plus, 5th edition, by Stephen Prata. 2 | This was done back in 2011 to gain familiarity with C++. 3 | Most of the programs are on the smaller side and are used to 4 | gain familiarity with various concepts convered in the chapters. 5 | 6 | There are slightly more than 100 programs among the various folders. 7 | There are also my answers to the review questions, written in LaTeX. 8 | -------------------------------------------------------------------------------- /Chapter12/Cow.h: -------------------------------------------------------------------------------- 1 | // Cow.h -- interface of the Cow class (code given by book) 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // this code is accompanied by Cow.cpp and questionOne.cpp 4 | 5 | class Cow 6 | { 7 | char name[20]; 8 | char * hobby; 9 | double weight; 10 | public: 11 | Cow(); 12 | Cow(const char * nm, const char * ho, double wt); 13 | Cow(const Cow & c); 14 | ~Cow(); 15 | Cow & operator=(const Cow & c); 16 | void ShowCow() const; // display all cow data 17 | }; 18 | -------------------------------------------------------------------------------- /Chapter3/pe3-5.cpp: -------------------------------------------------------------------------------- 1 | // pe3-5.cpp -- computes miles per gallons driven by a vehicle 2 | // This is exercise 5 of chapter 3 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | cout << "How many miles have you driven? "; 10 | double miles; 11 | cin >> miles; 12 | cout << "How many gallons have you used? "; 13 | double gallons; 14 | cin >> gallons; 15 | cout << "Your car has gotten " << miles/gallons << " miles per gallon" << endl; 16 | } 17 | -------------------------------------------------------------------------------- /Chapter17/employeeList.dat: -------------------------------------------------------------------------------- 1 | 0 2 | First name: Noah 3 | Last name: Ruderman 4 | Job: Dishwasher 5 | 1 6 | First name: Jeff 7 | Last name: Abraham 8 | Job: Huge Swinging Dick 9 | Number of people managed: 2 10 | 2 11 | First name: Roman 12 | Last name: Manziyenko 13 | Job: Diplomattress 14 | Reports to: Jeff 15 | 3 16 | First name: Ming 17 | Last name: Chow 18 | Job: Omnom 19 | Number of people managed: 25 20 | Reports to: Department head 21 | 2 22 | First name: I'ma 23 | Last name: Fink 24 | Job: Hit man 25 | Reports to: Da Boss 26 | -------------------------------------------------------------------------------- /Chapter3/pe3-1.cpp: -------------------------------------------------------------------------------- 1 | // pe3-1.cpp -- Converts height of user from inches to feet and inches. 2 | // This is exercise 1 of chapter 3 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const int inches_per_foot = 12; 10 | cout << "Enter your height in inches: "; 11 | int height_inches; 12 | cin >> height_inches; 13 | cout << "You are " << height_inches / inches_per_foot << " feet "; 14 | cout << "and " << height_inches % inches_per_foot << " inches tall." << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Chapter15/pe15-1.cpp: -------------------------------------------------------------------------------- 1 | // pe15-1.cpp -- uses the modified tv.h class 2 | // This is exercise 1 of chapter 15 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include"tv.h" 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | Tv tv; 11 | Remote remote; 12 | 13 | tv.onoff(); 14 | cout << "Here are our original settings: " << endl; 15 | tv.settings(); 16 | remote.settings(); 17 | 18 | tv.toggleRemoteMode(remote); 19 | cout << "These are our new settings:" << endl; 20 | tv.settings(); 21 | remote.settings(); 22 | 23 | cout << "Bye!" << endl; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Chapter10/plorg.h: -------------------------------------------------------------------------------- 1 | // plorg.h - definition of the Plorg class 2 | // This is exercise 7 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // plorg.h is accompanied by plorg.cpp and questionSeven.cpp 4 | 5 | #ifndef PLORG_H_ 6 | #define PLORG_H_ 7 | 8 | class Plorg 9 | { 10 | /* 11 | Class invariants: 12 | std::strlen(name) <= 19 13 | */ 14 | 15 | 16 | private: 17 | enum {MAX = 20}; 18 | char name[MAX]; 19 | int CI; // contentment index 20 | public: 21 | Plorg(const char * name = "Plorga"); 22 | void changeCI(int CI); 23 | void show(void) const; // prints name and CI to standard output 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Chapter13/classic2.h: -------------------------------------------------------------------------------- 1 | // classic2.h -- interface of the Classic class with dynamic memory 2 | // This is exercise 2 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #ifndef CLASSIC_H_ 5 | #define CLASSIC_H_ 6 | 7 | #include "cd2.h" 8 | 9 | class Classic : public Cd 10 | { 11 | private: 12 | char * primary_work; 13 | public: 14 | Classic(); 15 | Classic(const char * primary, Cd & d); 16 | Classic(const char * primary, const char * s1, 17 | const char * s2, int n, double x); 18 | Classic(const Classic & c); 19 | ~Classic(); 20 | virtual void Report() const; 21 | Classic & operator=(const Classic & c); 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Chapter3/pe3-6.cpp: -------------------------------------------------------------------------------- 1 | // pe3-6.cpp -- liters per 100 km gasoline consumption to miles per gallon 2 | // This is exercise 6 of chapter 3 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const double gallons_per_liter = 1/3.875; 10 | const double km_per_mile = 100/62.14; 11 | 12 | cout << "Enter the gas consumption in liters per 100 km: "; 13 | double lp100km; 14 | cin >> lp100km; 15 | double mpg = 1/(lp100km / 100 * gallons_per_liter * km_per_mile); 16 | cout << lp100km << " liters per 100 km is equivalent to " << mpg << " miles per gallon" << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Chapter5/pe5-9.cpp: -------------------------------------------------------------------------------- 1 | // pe5-9.cpp -- asks the user for the number of characters to be displayed in a row and prints a pattern 2 | // This is exercise 9 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | int rows; 10 | 11 | cout << "Enter the number of rows: "; 12 | cin >> rows; 13 | 14 | for (int i = 1; i <= rows; i++) 15 | { 16 | for (int k = 1; k <= rows - i; k++) 17 | { 18 | cout << "."; 19 | } 20 | for (int k = rows - i; k < rows; k++) 21 | { 22 | cout << "*"; 23 | } 24 | cout << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter10/list.h: -------------------------------------------------------------------------------- 1 | // list.h - definition of the List class which is an ADT 2 | // This is exercise 8 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // list.h is accompanied by list.cpp and questionEight.cpp 4 | 5 | #ifndef LIST_H_ 6 | #define LIST_H_ 7 | 8 | typedef double Item; 9 | 10 | class List 11 | { 12 | private: 13 | enum {MAX = 10}; 14 | Item items[MAX]; 15 | int index; 16 | public: 17 | List(); 18 | void add(const Item & item); 19 | bool isEmpty(void) const; 20 | bool isFull(void) const; 21 | void visit(void (*pf)(Item & item)); 22 | int numberOfItems(void) const {return index;} 23 | void printList(void) const; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /Chapter13/classic.h: -------------------------------------------------------------------------------- 1 | // classic.h -- interface of the Classic class 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #ifndef CLASSIC_H_ 5 | #define CLASSIC_H_ 6 | 7 | #include "cd.h" 8 | 9 | class Classic : public Cd 10 | { 11 | private: 12 | enum {len_primary = 40}; 13 | char primary_work[len_primary]; 14 | public: 15 | Classic(); 16 | Classic(const char * primary, Cd & d); 17 | Classic(const char * primary, const char * s1, 18 | const char * s2, int n, double x); 19 | Classic(const Classic & c); 20 | ~Classic(); 21 | virtual void Report() const; 22 | Classic & operator=(const Classic & c); 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /Chapter4/pe4-4.cpp: -------------------------------------------------------------------------------- 1 | // pe4-4.cpp -- asks for the user's name and displays it using the string class 2 | // This is exercise 4 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | cout << "Enter your first name: "; 11 | string first_name; 12 | getline(cin, first_name); 13 | cout << "Enter your last name: "; 14 | string last_name; 15 | getline(cin, last_name); 16 | string full_name = (last_name + ", ") + first_name; 17 | cout << "Here's the information on a single string: " 18 | << full_name 19 | << endl; 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter13/cd2.h: -------------------------------------------------------------------------------- 1 | // cd2.h -- interface of the Cd class with dynamic memory 2 | // This is exercise 2 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #ifndef Cd_H_ 5 | #define Cd_H_ 6 | 7 | #include 8 | 9 | // base class 10 | class Cd 11 | { 12 | private: 13 | char * performers; 14 | char * label; 15 | int selections; // number of selections 16 | double playtime; // playing time in minutes 17 | public: 18 | Cd(const char * s1, const char * s2, int n, double x); 19 | Cd(const Cd & d); 20 | Cd(); 21 | virtual ~Cd(); 22 | virtual void Report() const; // reports all CD data 23 | Cd & operator=(const Cd & d); 24 | }; 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /Chapter10/BankAccount.h: -------------------------------------------------------------------------------- 1 | // BankAccount.h - This is the header file which contains the BankAccount class 2 | // This is exercise 1 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // This file is accompanied by BankAccount.cpp and questionOne.cpp 4 | 5 | #ifndef BANKACCOUNT_H_ 6 | #define BANKACCOUNT_H_ 7 | 8 | #include 9 | 10 | class BankAccount 11 | { 12 | private: 13 | std::string name; 14 | std::string account; 15 | double balance; 16 | public: 17 | BankAccount(std::string name, std::string account, 18 | double balance= 0.0); 19 | void printAccount(void) const; 20 | void deposit(double sum); 21 | void withdraw(double sum); 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Chapter10/pe10-2.cpp: -------------------------------------------------------------------------------- 1 | // pe10-2.cpp - uses the Person class 2 | // This is exercise 2 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // This file is accompanied by Person.h and Person.cpp 4 | 5 | #include 6 | #include "Person.h" 7 | 8 | int main() 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | Person one; 14 | Person two("Smythecraft"); 15 | Person three("Dimwiddy", "Sam"); 16 | one.Show(); 17 | cout << endl; 18 | one.FormalShow(); 19 | cout << endl; 20 | two.Show(); 21 | cout << endl; 22 | two.FormalShow(); 23 | cout << endl; 24 | three.Show(); 25 | cout << endl; 26 | three.FormalShow(); 27 | cout << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Chapter11/usetime4.cpp: -------------------------------------------------------------------------------- 1 | // usetime4.cpp -- using the fourth draft of the Time class 2 | // compile pe11-4.cpp and mytime4.cpp together 3 | // This is listing 11.12 in Stephen Prata's C++ Primer Plus 4 | #include 5 | #include "mytime4.h" 6 | 7 | int main() 8 | { 9 | using std::cout; 10 | using std::endl; 11 | Time aida(3,35); 12 | Time tosca(2, 48); 13 | Time temp; 14 | 15 | cout << "Aida and Tosca:\n"; 16 | cout << aida << "; " << tosca << endl; 17 | temp = aida + tosca; 18 | cout << "Aida + Tosca: " << temp << endl; 19 | temp = aida * 1.17; 20 | cout << "Aida * 1.17: " << temp << endl; 21 | cout << "10 * Tosca: " << 10 * tosca << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter17/pe17-1.cpp: -------------------------------------------------------------------------------- 1 | // pe17-1.cpp -- description here 2 | // This is exercise 1 of chapter 17 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | cout << "Enter whatever you'd like." << endl; 10 | cout << "Input will be read up until the first '$' character:" << endl; 11 | char ch; 12 | cin.get(ch); 13 | int count = 0; 14 | 15 | while (ch != '$') 16 | { 17 | count++; 18 | cin.get(ch); 19 | } 20 | cin.putback(ch); 21 | cout << count << " characters read." << endl; 22 | cout << "The next character in the input buffer is " 23 | << (char) cin.get() << endl; 24 | cin.putback(ch); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter5/pe5-2.cpp: -------------------------------------------------------------------------------- 1 | // pe5-2.cpp -- program asks the user to enter numbers and sums the cumulative entries. Program terminates when the user enters 0 2 | // This is exercise 2 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | cout << "Enter as many numbers as you'd like. Enter 0 to terminate the program. " << endl; 10 | int num, sum = 0; 11 | cin >> num; 12 | 13 | while (num != 0) 14 | { 15 | sum += num; 16 | if (cin.get() == '\n') 17 | cout << "The sum is currently: " << sum << endl; 18 | cin >> num; 19 | } 20 | 21 | cout << "The grand total is " << sum << endl; 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter16/pe16-1.cpp: -------------------------------------------------------------------------------- 1 | // pe16-1.cpp -- description here 2 | // This is exercise 1 of chapter 16 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | 6 | bool isPalindrome(std::string & s); 7 | int main(void) 8 | { 9 | using namespace std; 10 | cout << "Enter a string: "; 11 | string s; 12 | getline(cin, s); 13 | 14 | if (isPalindrome(s)) 15 | cout << s << " is a palindrome" << endl; 16 | else 17 | cout << s << " is not a palindrome" << endl; 18 | 19 | return 0; 20 | } 21 | 22 | bool isPalindrome(std::string & s) 23 | { 24 | using namespace std; 25 | 26 | for (int i = 0, j = s.size()-1; i < j; i++, j--) 27 | if (s[i] != s[j]) 28 | return false; 29 | 30 | return true; 31 | } 32 | -------------------------------------------------------------------------------- /Chapter4/pe4-5.cpp: -------------------------------------------------------------------------------- 1 | // pe4-5.cpp -- we create a structure whose parameters are the brand, weight, and calories of a candy bar; initialize it; and display its contents 2 | // This is exercise 5 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | struct CandyBar 10 | { 11 | char brand[20]; 12 | float weight; 13 | int calories; 14 | } snack = 15 | { 16 | "Mocha Munch", 17 | 2.3, 18 | 350 19 | }; 20 | 21 | cout << "Our snack brand is " << snack.brand << endl; 22 | cout << "The weight is " << snack.weight << endl; 23 | cout << "And it contains " << snack.calories << " calories" << endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /Chapter10/move.h: -------------------------------------------------------------------------------- 1 | // move.h - definition of the Move class 2 | // This is exercise 6 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // move.h is accompanied by move.cpp and questionSix.cpp 4 | 5 | #ifndef MOVE_H_ 6 | #define MOVE_H_ 7 | 8 | class Move 9 | { 10 | private: 11 | double x; 12 | double y; 13 | public: 14 | Move(double a = 0, double b = 0); //sets x, y to a, b 15 | void showmove() const; 16 | Move add(const Move & m) const; 17 | // this function adds x of m to x of invoking object to get new x, 18 | // adds y of m to y of invoking object to get new y, creates a new 19 | // move object initialized to new x, y values and returns it 20 | void reset(double a = 0, double b = 0); // resets x, y to a, b 21 | }; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Chapter11/pe11-5.cpp: -------------------------------------------------------------------------------- 1 | // pe11-5.cpp -- uses the Stonewt class 2 | // This is exercise 5 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // compile with the stonewt2.cpp 4 | 5 | #include 6 | #include "stonewt2.h" 7 | 8 | int main(void) 9 | { 10 | using namespace std; 11 | 12 | Stonewt jimmy(150.0); 13 | Stonewt bobby(200.0, '2'); 14 | Stonewt danny(125.0, '3'); 15 | 16 | cout << "Jimmy's weight: " << jimmy << endl; 17 | cout << "Bobby's weight: " << bobby << endl; 18 | cout << "Danny's weight: " << danny << endl; 19 | 20 | cout << "Jimmy + Bobby's weight: " << jimmy + bobby << endl; 21 | cout << "Bobby - Danny's weight: " << bobby - danny << endl; 22 | cout << "10 * Danny's weight: " << 10 * danny << endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter14/winec2.h: -------------------------------------------------------------------------------- 1 | // winec2.h -- interface for the Wine class 2 | // This is exercise 1 of chapter 14 in C++ Primer Plus by Stephen Prata 3 | 4 | #ifndef WINEC2_H_ 5 | #define WINEC2_H_ 6 | 7 | #include 8 | #include 9 | #include"pair.h" 10 | 11 | typedef std::valarray ArrayInt; 12 | typedef Pair PairArray; 13 | 14 | class Wine : private PairArray, private std::string 15 | { 16 | private: 17 | int years; // the numbers of years we have data from 18 | public: 19 | Wine(const char * l, int y, const int yr[], const int bot[]); 20 | Wine(const char * l, int y); 21 | ~Wine(); 22 | void Show() const; 23 | void GetBottles(); 24 | const std::string & Label() const; 25 | int sum() const; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Chapter5/pe5-8.cpp: -------------------------------------------------------------------------------- 1 | // pe5-8.cpp -- this is version 2 of count_words which instead uses the string class instead of a char array. 2 | // This is exercise 8 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | string delimiter = "done"; 11 | string word; 12 | int word_count = 0; 13 | 14 | cout << "Enter words (to stop, type the word done): "; 15 | cin >> word; 16 | 17 | // while loop to read each word. test condition checks if "done" is entered 18 | while (word != delimiter) 19 | { 20 | word_count++; 21 | cin >> word; 22 | } 23 | 24 | cout << "You entered a total of " << word_count << " words" << endl; 25 | 26 | return 0; 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /Chapter10/plorg.cpp: -------------------------------------------------------------------------------- 1 | // plorg.cpp - implementation of the Plorg class 2 | // This is exercise 7 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // plorg.cpp is accompanied by plorg.h and questionSeven.cpp 4 | 5 | #include 6 | #include 7 | #include "plorg.h" 8 | 9 | Plorg::Plorg(const char * name) 10 | { 11 | std::strncpy(this->name, name, MAX); 12 | (this->name)[MAX - 1] = '\0'; 13 | CI = 50; 14 | } 15 | 16 | 17 | void Plorg::changeCI(int CI) 18 | { 19 | this->CI = CI; 20 | return; 21 | } 22 | 23 | 24 | void Plorg::show(void) const // prints name and CI to standard output 25 | { 26 | using std::cout; 27 | using std::endl; 28 | 29 | cout << "Name: " << name << endl; 30 | cout << "Contentment Index: " << CI << endl; 31 | return; 32 | } 33 | -------------------------------------------------------------------------------- /Chapter14/winec.h: -------------------------------------------------------------------------------- 1 | // winec.h -- interface for the Wine class 2 | // This is exercise 1 of chapter 14 in C++ Primer Plus by Stephen Prata 3 | 4 | #ifndef WINEC_H_ 5 | #define WINEC_H_ 6 | 7 | #include 8 | #include 9 | 10 | class Wine 11 | { 12 | private: 13 | typedef std::valarray ArrayInt; 14 | typedef std::pair PairArray; 15 | std::string label; 16 | PairArray data; // contains data on vintage years and bottles 17 | int years; // the numbers of years we have data from 18 | public: 19 | Wine(const char * l, int y, const int yr[], const int bot[]); 20 | Wine(const char * l, int y); 21 | ~Wine(); 22 | void Show() const; 23 | void GetBottles(); 24 | const std::string & Label() const; 25 | int sum() const; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Chapter4/pe4-3.cpp: -------------------------------------------------------------------------------- 1 | // pe4-3.cpp -- asks for the user's name and displays it using char arrays and the cstring header file 2 | // This is exercise 3 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | cout << "Enter your first name: "; 11 | char first_name[20]; 12 | cin.getline(first_name,20); 13 | first_name[19] = '\0'; 14 | cout << "Enter your last name: "; 15 | char last_name[20]; 16 | cin.getline(last_name,20); 17 | last_name[19] = '\0'; 18 | char full_name[40]; 19 | strcpy(full_name, strcat(strcat(last_name, ", "), first_name)); 20 | cout << "Here's the information on a single string: " 21 | << full_name 22 | << endl; 23 | return 0; 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter7/pe7-1.cpp: -------------------------------------------------------------------------------- 1 | // pe7-1.cpp -- calculates the harmonic mean of two numbers 2 | // This is exercise 1 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | // function prototype 7 | double harmonic(double x, double y); 8 | 9 | int main(void) 10 | { 11 | using namespace std; 12 | cout << "Enter two numbers (0 to exit): "; 13 | double x, y; 14 | 15 | while (cin >> x >> y && x != 0 && y != 0) 16 | { 17 | cout << "The harmonic mean of " << x << " and " << y 18 | << " is " << harmonic(x,y) << endl; 19 | cout << "Enter two numbers (0 to exit): "; 20 | } 21 | cout << "Bye!" << endl; 22 | 23 | return 0; 24 | } 25 | 26 | // function definition 27 | double harmonic(double x, double y) 28 | { 29 | return 2.0 * x * y / (x + y); 30 | } 31 | -------------------------------------------------------------------------------- /Chapter10/Person.h: -------------------------------------------------------------------------------- 1 | // Person.h - abstraction of the Person class 2 | // This is exercise 2 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // This file is accompanied by Person.cpp and questionTwo.cpp 4 | 5 | #ifndef PERSON_H_ 6 | #define PERSON_H_ 7 | 8 | #include 9 | using std::string; 10 | 11 | class Person 12 | { 13 | private: 14 | static const int LIMIT = 25; 15 | string lname; // Person's last name 16 | char fname[LIMIT]; // Person's first name 17 | public: 18 | Person() {lname = ""; fname[0] = '\0'; } // #1 19 | Person(const string & ln, const char * fn = "Heyyou"); // #2 20 | // the following methods display lname and fname 21 | void Show() const; // firstname, lastname format 22 | void FormalShow() const;// lastname, firstname format 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /Chapter8/pe8-3.cpp: -------------------------------------------------------------------------------- 1 | // pe8-3.cpp -- echos a string in upper case characters 2 | // This is exercise 3 of chapter 8 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | // function takes string reference and converts the contents to uppercase 10 | void stringToUpper(string & str); 11 | 12 | int main(void) 13 | { 14 | cout << "Enter a string (q to quit): "; 15 | string str; 16 | getline(cin, str); 17 | 18 | while (str != "q") 19 | { 20 | stringToUpper(str); 21 | cout << str << endl; 22 | cout << "Enter a string (q to quit): "; 23 | getline(cin, str); 24 | } 25 | return 0; 26 | } 27 | 28 | void stringToUpper(string & str) 29 | { 30 | for (int i = 0; i < str.size(); i++) 31 | str[i] = toupper(str[i]); 32 | } 33 | -------------------------------------------------------------------------------- /Chapter13/cd.h: -------------------------------------------------------------------------------- 1 | // cd.h -- interface of the Cd class 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // Note: This code was given by the book, the question is really 4 | // to write classic.h, classic.cpp, and cd.cpp 5 | 6 | #ifndef Cd_H_ 7 | #define Cd_H_ 8 | 9 | // base class 10 | class Cd 11 | { 12 | private: 13 | enum {len_performer = 50, len_label = 20}; 14 | char performers[len_performer]; 15 | char label[len_label]; 16 | int selections; // number of selections 17 | double playtime; // playing time in minutes 18 | public: 19 | Cd(const char * s1, const char * s2, int n, double x); 20 | Cd(const Cd & d); 21 | Cd(); 22 | virtual ~Cd(); 23 | virtual void Report() const; // reports all CD data 24 | Cd & operator=(const Cd & d); 25 | }; 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /Chapter10/customerStack.h: -------------------------------------------------------------------------------- 1 | // customerStack.h - customerStack class header file 2 | // This is exercise 5 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // customerStack.h is accompanied by customerStack.cpp and questionFive.cpp 4 | 5 | #ifndef CUSTOMERSTACK_H_ 6 | #define CUSTOMERSTACK_H_ 7 | 8 | struct customer 9 | { 10 | char fullname[35]; 11 | double payment; 12 | }; 13 | 14 | typedef customer Item; 15 | 16 | class CustomerStack 17 | { 18 | /* 19 | Class invariants: 20 | (top == 0 || top > 0) && top <= MAX 21 | */ 22 | 23 | private: 24 | enum {MAX = 10}; 25 | Item items[MAX]; 26 | int top; // index for top stack item 27 | public: 28 | CustomerStack(); 29 | bool isempty() const; 30 | bool isfull() const; 31 | bool push(const Item & item); 32 | bool pop(Item & item); 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /Chapter6/pe6-1.cpp: -------------------------------------------------------------------------------- 1 | // pe6-1.cpp -- the program echos keyboard input except for digits, converting each upper case character to lower case, and vica versa, up to the @ symbol. 2 | // This is exercise 1 of chapter 6 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | 11 | cout << "You may begin typing, enter @ to quit" << endl; 12 | char ch; 13 | cin.get(ch); 14 | 15 | while (cin.good() && ch != '@') 16 | { 17 | if (isdigit(ch)) 18 | { 19 | cin.get(ch); 20 | continue; 21 | } 22 | else if (isupper(ch)) 23 | ch = tolower(ch); 24 | else if (islower(ch)) 25 | ch = toupper(ch); 26 | 27 | cout << ch; 28 | cin.get(ch); 29 | } 30 | 31 | cout << endl << "All done!" << endl; 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter4/pe4-2.cpp: -------------------------------------------------------------------------------- 1 | // pe4-2.cpp -- this program requests and displays information but uses the string class instead of char arrays 2 | // This is exercise 2 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | cout << "What is your first name? "; 11 | string first_name; 12 | getline(cin, first_name); 13 | cout << "What is your last name? "; 14 | string last_name; 15 | getline(cin, last_name); 16 | cout << "What letter grade do you deserve? "; 17 | char grade; 18 | cin >> grade; 19 | cout << "What is your age? "; 20 | int age; 21 | cin >> age; 22 | 23 | cout << "Name: " << last_name << ", " << first_name << endl 24 | << "Grade: " << char (grade + 1) << endl 25 | << "Age: " << age << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter5/pe5-7.cpp: -------------------------------------------------------------------------------- 1 | // pe5-7.cpp -- the program counts words in the input until the word done is entered. The word count does not include the delimiter "done" 2 | // This is exercise 7 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | char delimiter[] = "done"; 11 | const int max_length = 50; 12 | char word[max_length]; 13 | int word_count = 0; 14 | 15 | cout << "Enter words (to stop, type the word done): "; 16 | cin >> word; 17 | 18 | // while loop to read each word. test condition checks if "done" is entered 19 | while (strcmp(word, delimiter)) 20 | { 21 | word_count++; 22 | cin >> word; 23 | } 24 | 25 | cout << "You entered a total of " << word_count << " words" << endl; 26 | 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter13/vintageport.h: -------------------------------------------------------------------------------- 1 | // vintageport.h -- the VintagePort class interface 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // Note: this code was given as part of the problem 4 | 5 | #ifndef VINTAGEPORT_H_ 6 | #define VINTAGEPORT_H_ 7 | 8 | #include "port.h" 9 | class VintagePort : public Port // style necessarily = "vintage" 10 | { 11 | private: 12 | char * nickname; // i.e., "The Noble" or "Old Velvet", etc. 13 | int year; // vintage year 14 | public: 15 | VintagePort(); 16 | VintagePort(const char * br, int b, const char * nn, int y); 17 | VintagePort(const VintagePort & vp); 18 | ~VintagePort() { delete [] nickname; } 19 | VintagePort & operator=(const VintagePort & vp); 20 | virtual void Show() const; 21 | friend ostream & operator<<(ostream & os, const VintagePort & vp); 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Chapter14/pair.h: -------------------------------------------------------------------------------- 1 | // pair.h -- interface and implementation of the pair class 2 | // This is exercise 2 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: This code is the first part of listing 14.19 4 | 5 | #ifndef PAIR_H_ 6 | #define PAIR_H_ 7 | 8 | #include 9 | #include 10 | 11 | template 12 | class Pair 13 | { 14 | private: 15 | T1 a; 16 | T2 b; 17 | public: 18 | T1 & first(); 19 | T2 & second(); 20 | T1 first() const { return a; } 21 | T2 second() const { return b; } 22 | Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) { } 23 | Pair() {} 24 | }; 25 | 26 | template 27 | T1 & Pair::first() 28 | { 29 | return a; 30 | } 31 | 32 | template 33 | T2 & Pair::second() 34 | { 35 | return b; 36 | } 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /Chapter10/Person.cpp: -------------------------------------------------------------------------------- 1 | // Person.cpp - implementation of the Person class 2 | // This is exercise 2 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // This file is accompanied by Person.h and questionTwo.cpp 4 | 5 | #include 6 | #include 7 | #include "Person.h" 8 | 9 | // Constructor 10 | Person::Person(const std::string & ln, const char * fn) 11 | { 12 | using std::strncpy; 13 | 14 | lname = ln; 15 | strncpy(fname, fn, LIMIT); 16 | fname[LIMIT-1] = '\0'; 17 | } 18 | 19 | // firstname, lastname format 20 | void Person::Show() const 21 | { 22 | using std::cout; 23 | using std::endl; 24 | 25 | cout << fname << ", " << lname << endl; 26 | } 27 | 28 | // lastname, firstname format 29 | void Person::FormalShow() const 30 | { 31 | using std::cout; 32 | using std::endl; 33 | 34 | cout << lname << ", " << fname << endl; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter4/pe4-7.cpp: -------------------------------------------------------------------------------- 1 | // pe4-7.cpp -- the user is asked for information regarding a pizza and the program displays that information. 2 | // This is exercise 7 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | struct pizza 6 | { 7 | char name[20]; 8 | float diameter; 9 | float weight; 10 | }; 11 | 12 | int main(void) 13 | { 14 | using namespace std; 15 | pizza pie; 16 | 17 | cout << "What is the name of the pizza company? "; 18 | cin.getline(pie.name, 20); 19 | 20 | cout << "What is the diameter of the pizza? "; 21 | cin >> pie.diameter; 22 | 23 | cout << "What is the weight of the pizza? "; 24 | cin >> pie.weight; 25 | 26 | cout << "The name of the pizza company is " << pie.name << endl 27 | << "The diameter is " << pie.diameter << endl 28 | << "The weight is " << pie.weight << endl; 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter3/pe3-2.cpp: -------------------------------------------------------------------------------- 1 | // pe3-2.cpp -- calculates user's bmi 2 | // This is exercise 2 of chapter 3 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const int Feet_to_inches = 12; 10 | const double Inches_to_meter = 0.0254; 11 | const double Pounds_to_kg = 1/2.2; 12 | 13 | cout << "Please enter your height in feet and inches." << endl; 14 | cout << "Feet: "; 15 | int feet_tall; 16 | cin >> feet_tall; 17 | cout << "Inches: "; 18 | int inches_tall; 19 | cin >> inches_tall; 20 | cout << "What is your weight in pounds? "; 21 | int pounds; 22 | cin >> pounds; 23 | 24 | double height_meters = (feet_tall*Feet_to_inches + inches_tall)*Inches_to_meter; 25 | double weight_kg = pounds*Pounds_to_kg; 26 | 27 | cout << "Your BMI is " << weight_kg / (height_meters * height_meters) << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter5/pe5-4.cpp: -------------------------------------------------------------------------------- 1 | // pe5-4.cpp -- the program asks the user for monthly sales of books and then sums the total number of books sold using arrays 2 | // This is exercise 4 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const char * months[] = 10 | { 11 | "January", 12 | "February", 13 | "March", 14 | "April", 15 | "May", 16 | "June", 17 | "July", 18 | "August", 19 | "September", 20 | "October", 21 | "November", 22 | "December" 23 | }; 24 | int sales[12]; 25 | int sum = 0; 26 | 27 | for (int i = 0; i < 12; i++) 28 | { 29 | cout << "Enter books sales for the month of " << months[i] << ": "; 30 | cin >> sales[i]; 31 | sum += sales[i]; 32 | } 33 | 34 | cout << "The total sales for the year is " << sum << endl; 35 | return 0; 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /Chapter16/pe16-2.cpp: -------------------------------------------------------------------------------- 1 | // pe16-2.cpp -- description here 2 | // This is exercise 2 of chapter 16 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | 6 | bool isPalindrome(std::string & s); 7 | int main(void) 8 | { 9 | using namespace std; 10 | cout << "Enter a string: "; 11 | string s; 12 | getline(cin, s); 13 | 14 | if (isPalindrome(s)) 15 | cout << s << " is a palindrome" << endl; 16 | else 17 | cout << s << " is not a palindrome" << endl; 18 | 19 | return 0; 20 | } 21 | 22 | bool isPalindrome(std::string & s) 23 | { 24 | using namespace std; 25 | 26 | string palindrome; 27 | for (unsigned int i = 0; i < s.size(); i++) 28 | if (isalpha(s[i])) 29 | palindrome.push_back(tolower(s[i])); 30 | 31 | for (int i = 0, j = palindrome.size()-1; i < j; i++, j--) 32 | if (palindrome[i] != palindrome[j]) 33 | return false; 34 | 35 | return true; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter5/pe5-1.cpp: -------------------------------------------------------------------------------- 1 | // pe5-1.cpp -- asks the user for two numbers and sums all the integers between them, inclusive. The program assumes that the first integer the user enters is smaller than the second 2 | // This is exercise 1 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | cout << "Enter a number: "; 10 | int first; 11 | cin >> first; 12 | cout << "Enter another number larger than the first: "; 13 | int second; 14 | cin >> second; 15 | 16 | if (second < first) 17 | { 18 | cout << "Error. Program Abort" << endl; 19 | return 1; 20 | } 21 | 22 | int sum = 0; 23 | int temp = first; 24 | while (temp <= second) 25 | { 26 | sum += temp; 27 | temp += 1; 28 | } 29 | 30 | cout << "The sum of all integers " << first << " through " << second 31 | << " is " << sum << endl; 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Chapter13/port.h: -------------------------------------------------------------------------------- 1 | // port.h -- the Port class interface 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // Note: this code was given as part of the problem 4 | 5 | #ifndef PORT_H_ 6 | #define PORT_H_ 7 | 8 | #include 9 | using namespace std; 10 | class Port 11 | { 12 | private: 13 | char * brand; 14 | char style[20]; // i.e., tawny, ruby, vintage 15 | int bottles; 16 | public: 17 | Port(const char * br = "none", const char * st = "none", int b = 0); 18 | Port(const Port & p); // copy constructor 19 | virtual ~Port() { delete [] brand; } 20 | Port & operator=(const Port & p); 21 | Port & operator+=(int b); // adds b to bottles 22 | Port & operator-=(int b); // subtracts b from bottles, if available 23 | int BottleCount() const { return bottles; } 24 | virtual void Show() const; 25 | friend ostream & operator<<(ostream & os, const Port & p); 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Chapter3/pe3-3.cpp: -------------------------------------------------------------------------------- 1 | // pe3-3.cpp -- converts the latitude in degrees, minutes, and seconds to degrees in decimal format 2 | // This is exercise 3 of chapter 3 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const double Minutes_to_degree = 1/60.0; 10 | const double Seconds_to_degree = 1/3600.0; 11 | 12 | cout << "Enter a latitude in degrees, minutes, and seconds:" << endl; 13 | cout << "First, enter the degrees: "; 14 | int degrees; 15 | cin >> degrees; 16 | cout << "Next, enter the minutes of the arc: "; 17 | int minutes; 18 | cin >> minutes; 19 | cout << "Finally, enter the seconds of the arc: "; 20 | int seconds; 21 | cin >> seconds; 22 | cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = "; 23 | cout << degrees + minutes*Minutes_to_degree + seconds*Seconds_to_degree << " degrees" << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Chapter9/sales.h: -------------------------------------------------------------------------------- 1 | // sales.h -- contains everything in the SALES namespace 2 | // This is exercise 4 of chapter 9 in C++ Primer Plus by Stephen Prata 3 | 4 | namespace SALES 5 | { 6 | const int QUARTERS = 4; 7 | struct Sales 8 | { 9 | double sales[QUARTERS]; 10 | double average; 11 | double max; 12 | double min; 13 | }; 14 | 15 | // copies the lesser of 4 or n items from the array ar 16 | // to the sales member of s and computes and stores the 17 | // average, maximum, and minimum values of the entered items; 18 | // remaining elements of sales, if any, set to 0 19 | void setSales(Sales & s, const double ar[], int n); 20 | 21 | // gathers sales for 4 quarters interactively, stores them 22 | // in the sales member of s and computes and stores the 23 | // average, maximum, and minimum values 24 | void setSales(Sales & s); 25 | 26 | // display all information in structure s 27 | void showSales(const Sales & s); 28 | } 29 | -------------------------------------------------------------------------------- /Chapter3/pe3-4.cpp: -------------------------------------------------------------------------------- 1 | // pe3-4.cpp -- converts seconds to equivalent time in days, hours, minutes, and seconds. 2 | // This is exercise 4 of chapter 3 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const int Seconds_per_minute = 60; 10 | const int Seconds_per_hour = 60*60; 11 | const int Seconds_per_day = 60*60*24; 12 | 13 | cout << "Please enter the number of seconds: "; 14 | long int total_seconds; 15 | cin >> total_seconds; 16 | 17 | int days, hours, minutes, seconds; 18 | days = total_seconds / Seconds_per_day; 19 | hours = (total_seconds % Seconds_per_day) / Seconds_per_hour; 20 | minutes = (total_seconds % Seconds_per_hour) / Seconds_per_minute; 21 | seconds = (total_seconds % Seconds_per_minute); 22 | 23 | cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, and " << seconds << " seconds." << endl; 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter10/move.cpp: -------------------------------------------------------------------------------- 1 | // move.cpp - implementation of the Move class 2 | // This is exercise 6 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // move.cpp is accompanied by move.h and questionSix.cpp 4 | 5 | #include 6 | #include "move.h" 7 | 8 | Move::Move(double a, double b) 9 | { 10 | x = a; 11 | y = b; 12 | } 13 | 14 | 15 | void Move::showmove() const 16 | { 17 | using std::cout; 18 | using std::endl; 19 | 20 | cout << "x = " << this->x << endl; 21 | cout << "y = " << this->y << endl; 22 | return; 23 | } 24 | 25 | 26 | Move Move::add(const Move & m) const 27 | { 28 | Move s(m.x + this->x, m.y + this->y); 29 | return s; 30 | } 31 | // this function adds x of m to x of invoking object to get new x, 32 | // adds y of m to y of invoking object to get new y, creates a new 33 | // move object initialized to new x, y values and returns it 34 | 35 | 36 | void Move::reset(double a, double b) // resets x, y to a, b 37 | { 38 | x = a; 39 | y = b; 40 | return; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter7/pe7-5.cpp: -------------------------------------------------------------------------------- 1 | // pe7-5.cpp -- calculates the factorial of a number using a recursive function 2 | // This is exercise 5 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | unsigned long int factorial(unsigned int n); 7 | 8 | int main(void) 9 | { 10 | using namespace std; 11 | cout << "Enter a number: "; 12 | unsigned int number; 13 | // I've chosen to make number unsigned since our data should be positive. However, I am unsure if this is best since now I cannot error check by checking to see if number is negative. On my computer, the terminal displays "Segmentation fault" and ends the program. 14 | 15 | while (cin >> number) 16 | { 17 | cout << number << "! = " << factorial(number) << endl; 18 | cout << "Another number (q to quit): "; 19 | } 20 | 21 | return 0; 22 | } 23 | 24 | // a recursive function 25 | unsigned long int factorial(unsigned int n) 26 | { 27 | if (0 == n) 28 | return 1; 29 | else 30 | return n * factorial(n - 1); 31 | } 32 | -------------------------------------------------------------------------------- /Chapter11/complex0.h: -------------------------------------------------------------------------------- 1 | // complex0.h -- a user defined class for complex number operatons 2 | // This is exercise 7 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // this code is supplemented by complex0.cpp and questionSeven.cpp 4 | 5 | #include 6 | 7 | #ifndef COMPLEX0_H_ 8 | #define COMPLEX0_H_ 9 | 10 | class Complex 11 | { 12 | private: 13 | double a; 14 | double b; 15 | public: 16 | Complex(double a, double b = 0); 17 | Complex(); 18 | void set(double a, double b); 19 | friend Complex operator+(const Complex & c1, const Complex & c2); 20 | friend Complex operator-(const Complex & c1, const Complex & c2); 21 | friend Complex operator*(const Complex & c1, const Complex & c2); 22 | friend Complex operator*(double d, const Complex & c2); 23 | friend Complex operator~(const Complex & c); 24 | friend std::ostream & operator<<(std::ostream & os, 25 | const Complex & c); 26 | friend std::istream & operator>>(std::istream & os, 27 | Complex & c); 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Chapter4/pe4-8.cpp: -------------------------------------------------------------------------------- 1 | // pe4-8.cpp -- the user is asked for information regarding a pizza and the program displays that information. The program uses a dynamic structure to store the information 2 | // This is exercise 8 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | struct pizza 6 | { 7 | char name[20]; 8 | float diameter; 9 | float weight; 10 | }; 11 | 12 | int main(void) 13 | { 14 | using namespace std; 15 | //pizza pie; 16 | pizza * pie = new pizza; 17 | 18 | cout << "What is the diameter of the pizza? "; 19 | cin >> pie->diameter; 20 | cin.get(); 21 | 22 | cout << "What is the name of the pizza company? "; 23 | cin.getline(pie->name, 20); 24 | 25 | cout << "What is the weight of the pizza? "; 26 | cin >> pie->weight; 27 | 28 | cout << "The name of the pizza company is " << pie->name << endl 29 | << "The diameter is " << pie->diameter << endl 30 | << "The weight is " << pie->weight << endl; 31 | 32 | delete pie; 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Chapter12/stock3.h: -------------------------------------------------------------------------------- 1 | // stock3.h - stock2.h upgraded 2 | // Note: this is a modified version of listing 10.7 3 | // This is exercise 3 of chapter 12 in C++ Primer Plus by Stephen Prata 4 | 5 | #ifndef STOCK3_H_ 6 | #define STOCK3_H_ 7 | 8 | #include 9 | using std::ostream; 10 | 11 | class Stock 12 | { 13 | private: 14 | char * company; 15 | int shares; 16 | double share_val; 17 | double total_val; 18 | void set_tot() { total_val = shares * share_val; } 19 | public: 20 | Stock(); // default constructor 21 | Stock(const char * co, int n = 0, double pr = 0.0); 22 | Stock(const Stock & s); // copy constructor 23 | ~Stock(); // destructor 24 | void buy(int num, double price); 25 | void sell(int num, double price); 26 | void update(double price); 27 | friend ostream & operator<<(ostream & os, const Stock & s); 28 | const Stock & topval(const Stock & s) const; 29 | Stock & operator=(const Stock & s); 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /Chapter14/pe14-5.cpp: -------------------------------------------------------------------------------- 1 | // pe14-5.cpp 2 | // This is exercise 5 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: This code was given by Prata 4 | // useemp1.cpp -- using abstr_emp classes 5 | 6 | #include 7 | using namespace std; 8 | #include "emp.h" 9 | 10 | int main(void) 11 | { 12 | employee em("Trip", "Harris", "Thumper"); 13 | cout << em << endl; 14 | em.ShowAll(); 15 | 16 | manager ma("Amorphia", "Spindragon", "Nuancer", 5); 17 | cout << ma << endl; 18 | ma.ShowAll(); 19 | 20 | 21 | fink fi("Matt", "Oggs", "Oiler", "Juno Barr"); 22 | cout << fi << endl; 23 | fi.ShowAll(); 24 | highfink hf(ma, "Curly Kew"); // recruitment? 25 | hf.ShowAll(); 26 | cout << "Press a key for next phase:\n"; 27 | cin.get(); 28 | highfink hf2; 29 | hf2.SetAll(); 30 | 31 | cout << "Using an abstr_emp * pointer:\n"; 32 | abstr_emp * tri[4] = {&em, &fi, &hf, &hf2}; 33 | for (int i = 0; i < 4; i++) 34 | tri[i]->ShowAll(); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter4/pe4-1.cpp: -------------------------------------------------------------------------------- 1 | // pe4-1.cpp -- this program requests and displays information 2 | // This is exercise 1 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | cout << "What is your first name? "; 10 | char first_name[20]; 11 | cin.getline(first_name, 20); // we can see that cin does not leave a new line character after reading text 12 | first_name[19] = '\0'; 13 | //cin.get(); // This is unnecessary because the member function getline() does not leave a newline character in the input 14 | cout << "What is your last name? "; 15 | char last_name[20]; 16 | cin.getline(last_name, 20); 17 | last_name[19] = '\0'; 18 | cout << "What letter grade do you deserve? "; 19 | char grade; 20 | cin >> grade; 21 | cout << "What is your age? "; 22 | int age; 23 | cin >> age; 24 | 25 | cout << "Name: " << last_name << ", " << first_name << endl 26 | << "Grade: " << char (grade + 1) << endl 27 | << "Age: " << age << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter11/pe11-7.cpp: -------------------------------------------------------------------------------- 1 | // questionSeven.cpp -- uses the Complex class 2 | // This is exercise 7 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // this code is supplemented by complex0.cpp and questionSeven.cpp 4 | // Note: this section of the code was given by the question to 5 | // test complex0.h and complex0.cpp 6 | 7 | #include 8 | #include "complex0.h" // to avoid confusion with complex.h 9 | int main() 10 | { 11 | using namespace std; 12 | Complex a(3.0, 4.0); // initialize to (3,4i) 13 | Complex c; 14 | cout << "Enter a complex number (q to quit):\n"; 15 | while (cin >> c) 16 | { 17 | cout << "c is " << c << '\n'; 18 | cout << "complex conjugate is " << ~c << '\n'; 19 | cout << "a is " << a << '\n'; 20 | cout << "a + c is " << a + c << '\n'; 21 | cout << "a - c is " << a - c << '\n'; 22 | cout << "a * c is " << a * c << '\n'; 23 | cout << "2 * c is " << 2 * c << '\n'; 24 | cin.ignore(256, '\n'); 25 | cout << "Enter a complex number (q to quit):\n"; 26 | } 27 | cout << "Done!\n"; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Chapter10/pe10-6.cpp: -------------------------------------------------------------------------------- 1 | // pe10-6.cpp - uses the Move class 2 | // This is exercise 6 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // questionSix.cpp is accompanied by move.h and move.cpp 4 | 5 | #include 6 | #include "move.h" 7 | 8 | int main(void) 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | // initialize Move object 14 | cout << "Here is the default Move object" << endl; 15 | Move m; 16 | m.showmove(); 17 | 18 | // initialize second Move object 19 | cout << "Here is a second object with coordinates "; 20 | cout << "(5, -3.2)" << endl; 21 | Move n(5, -3.2); 22 | n.showmove(); 23 | 24 | cout << "Let's reset the values of the first object to "; 25 | cout << "coordinates (10, 10)" << endl; 26 | m.reset(10,10); 27 | m.showmove(); 28 | 29 | // Let's add the two objects and assign its values to the 30 | // first object 31 | cout << "Let's add the coordinates of the two objects "; 32 | cout << "together " << endl; 33 | m = m.add(n); 34 | m.showmove(); 35 | 36 | cout << "Bye!" << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Chapter11/vect2.h: -------------------------------------------------------------------------------- 1 | // vect2.h -- modified version of vect.h 2 | // This is exercise 2 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // see vect.h or listing 11.13 in C++ Primer Plus for the original file 4 | 5 | #ifndef VECTOR_H_ 6 | #define VECTOR_H_ 7 | #include 8 | namespace VECTOR 9 | { 10 | class Vector 11 | { 12 | private: 13 | double x; 14 | double y; 15 | char mode; 16 | public: 17 | Vector(); 18 | Vector(double n1, double n2, char form = 'r'); 19 | void set(double n1, double n2, char form = 'r'); 20 | ~Vector(); 21 | double xval() const {return x;} 22 | double yval() const {return y;} 23 | double magval() const; 24 | double angval() const; 25 | void polar_mode(); 26 | void rect_mode(); 27 | Vector operator+(const Vector & b) const; 28 | Vector operator-(const Vector & b) const; 29 | Vector operator-() const; 30 | Vector operator*(double n) const; 31 | friend Vector operator*(double n, const Vector & a); 32 | friend std::ostream & operator<<(std::ostream & os, const Vector & v); 33 | }; 34 | } 35 | #endif 36 | -------------------------------------------------------------------------------- /Chapter5/pe5-5.cpp: -------------------------------------------------------------------------------- 1 | // pe5-5.cpp -- the program asks the user for monthly sales of books and then sums the total number of books sold using two dimensional arrays that store three years worth of sales 2 | // This is exercise 5 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | const char * months[] = 10 | { 11 | "January", 12 | "February", 13 | "March", 14 | "April", 15 | "May", 16 | "June", 17 | "July", 18 | "August", 19 | "September", 20 | "October", 21 | "November", 22 | "December" 23 | }; 24 | const int year[3] = 25 | { 26 | 1995, 27 | 1996, 28 | 1997 29 | }; 30 | int sales[3][12]; 31 | int sum = 0; 32 | 33 | for (int j = 0; j < 3; j++) 34 | { 35 | for (int i = 0; i < 12; i++) 36 | { 37 | cout << "Enter books sales for the month of " << months[i] << " in " << year[j] << ": "; 38 | cin >> sales[j][i]; 39 | sum += sales[j][i]; 40 | } 41 | } 42 | 43 | cout << "The total sales for the year is " << sum << endl; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Chapter10/golf2.h: -------------------------------------------------------------------------------- 1 | // golf2.h -- the golf class header file 2 | // This is exercise 3 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // golf2.h is accompanied by golf2.cpp and questionThree.cpp 4 | 5 | #ifndef GOLF_H_ 6 | #define GOLF_H_ 7 | 8 | const int Len = 40; 9 | class Golf 10 | { 11 | /* 12 | Class invariants: 13 | every player must have a name 14 | */ 15 | 16 | 17 | private: 18 | char fullname[Len]; 19 | int handicap; 20 | public: 21 | // default constructor 22 | Golf(); 23 | 24 | // explicit constructor: 25 | // function sets golf structure to provided name, handicap 26 | // using values passed as arguments to the function 27 | Golf(const char * fullname, int handicap); 28 | 29 | // interactive version: 30 | // function solicits name and handicap from user 31 | // and sets the members of g to the values entered 32 | // returns 1 if name is entered, 0 if name is empty string 33 | int setgolf(); 34 | 35 | // function resets handicap to new value 36 | void setHandicap(int handicap); 37 | 38 | // function displays contents of golf structure 39 | void showgolf() const; 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Chapter11/vect.h: -------------------------------------------------------------------------------- 1 | // vect.h -- Vector class with <<, mode state 2 | // Note: this is listing 11.13 in Stephen Prata's C++ Primer Plus 3 | 4 | #ifndef VECTOR_H_ 5 | #define VECTOR_H_ 6 | #include 7 | namespace VECTOR 8 | { 9 | class Vector 10 | { 11 | private: 12 | double x; 13 | double y; 14 | double mag; 15 | double ang; 16 | char mode; 17 | void set_mag(); 18 | void set_ang(); 19 | void set_x(); 20 | void set_y(); 21 | public: 22 | Vector(); 23 | Vector(double n1, double n2, char form = 'r'); 24 | void set(double n1, double n2, char form = 'r'); 25 | ~Vector(); 26 | double xval() const {return x;} 27 | double yval() const {return y;} 28 | double magval() const {return mag;} 29 | double angval() const {return ang;} 30 | void polar_mode(); 31 | void rect_mode(); 32 | Vector operator+(const Vector & b) const; 33 | Vector operator-(const Vector & b) const; 34 | Vector operator-() const; 35 | Vector operator*(double n) const; 36 | friend Vector operator*(double n, const Vector & a); 37 | friend std::ostream & operator<<(std::ostream & os, const Vector & v); 38 | }; 39 | } 40 | #endif 41 | -------------------------------------------------------------------------------- /Chapter5/pe5-6.cpp: -------------------------------------------------------------------------------- 1 | // pe5-6.cpp -- the program asks the user to input data regarding the make and year of various cars and redisplays the information 2 | // This is exercise 6 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | struct car 11 | { 12 | string make; 13 | int year; 14 | }; 15 | 16 | cout << "How many cars do you with to catalog? "; 17 | int number_cars; 18 | cin >> number_cars; 19 | cin.get(); 20 | 21 | car * collection = new car[number_cars]; 22 | int count = 0; 23 | while (count < number_cars) 24 | { 25 | cout << "Car #" << count + 1 << ": " << endl 26 | << "Please enter the make: "; 27 | getline(cin, collection[count].make); 28 | cout << "Please enter the year made: "; 29 | cin >> collection[count].year; 30 | cin.get(); 31 | count += 1; 32 | } 33 | 34 | cout << "Here is your collection: " << endl; 35 | for (count = 0; count < number_cars; count++) 36 | { 37 | cout << collection[count].year << " " 38 | << collection[count].make << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Chapter12/pe12-4.cpp: -------------------------------------------------------------------------------- 1 | // pe12-4.cpp -- tests the Stack ADT class 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // compile with stack2.cpp 4 | 5 | #include 6 | #include "stack2.h" 7 | 8 | inline void printStack(Stack s) {std::cout << s;} 9 | 10 | int main() 11 | { 12 | using namespace std; 13 | 14 | cout << "setting s1..." << endl; 15 | Stack s1; 16 | cout << "setting s2..." << endl; 17 | Stack s2(5); 18 | cout << "setting s3..." << endl; 19 | Stack s3(-1); 20 | cout << "setting s4..." << endl; 21 | Stack s4 = Stack(10); 22 | cout << "setting s5..." << endl; 23 | Stack s5(s4); 24 | cout << "setting s6..." << endl; 25 | Stack s6 = Stack(s3); 26 | 27 | cout << "s1.isempty() == " << s1.isempty() << endl; 28 | s1.push(10); 29 | s1.push(20); 30 | s1.push(30); 31 | 32 | s2 = s1; 33 | unsigned long int x; 34 | s2.pop(x); 35 | cout << "We pushed 10, 20, and 30 into s1" << endl; 36 | cout << "We assigned s2 to s1" << endl; 37 | cout << "s2.pop(x), x = " << x << endl; 38 | 39 | cout << "s1: " << endl; 40 | printStack(s1); 41 | cout << "s2: " << endl << s2; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Chapter5/pe5-3.cpp: -------------------------------------------------------------------------------- 1 | // pe5-3.cpp -- we compare simple interest at 10% vs compound interest at 5% with a $100 principle balance 2 | // This is exercise 3 of chapter 5 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | double balance_simple, balance_compound, interest_simple, interest_compound; 11 | const int principle = 100; 12 | int years = 0; 13 | balance_simple = balance_compound = principle; 14 | 15 | // while loop in which interest accures and we count years as well as sums 16 | while (balance_compound <= balance_simple) 17 | { 18 | interest_simple = .10 * 100; 19 | balance_simple += interest_simple; 20 | interest_compound = .05 * balance_compound; 21 | balance_compound += interest_compound; 22 | years += 1; 23 | } 24 | 25 | cout << "After " << years << " years, the balance with compounding interest exceeds that of the simple interest balance" << endl; 26 | cout << "The compounding interest balance is $" << balance_compound << endl; 27 | cout << "The simple interest balance is $" << balance_simple << endl; 28 | 29 | return 0; 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter14/pe14-1.cpp: -------------------------------------------------------------------------------- 1 | // pe14-1.cpp -- using Wine class with containment 2 | // This is exercise 1 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: The following code was given by the book to test the Wine class 4 | // compile with winec.cpp 5 | 6 | #include 7 | #include"winec.h" 8 | 9 | int main(void) 10 | { 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | 15 | cout << "Enter name of wine: "; 16 | char lab[50]; 17 | cin.getline(lab, 50); 18 | cout << "Enter number of years: "; 19 | int yrs; 20 | cin >> yrs; 21 | 22 | Wine holding(lab, yrs); // store label, years, give arrays yrs elements 23 | holding.GetBottles(); // solicit input for year, bottle count 24 | holding.Show(); // display object contents 25 | 26 | const int YRS = 3; 27 | int y[YRS] = {1993, 1995, 1998}; 28 | int b[YRS] = { 48, 60, 72}; 29 | // create new object, initialize using data in arrays y and b 30 | Wine more("Gushing Grape Red", YRS, y, b); 31 | more.Show(); 32 | cout << "Total bottles for " << more.Label() // use Label() method 33 | << ": " << more.sum() << endl; 34 | cout << "Bye\n"; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter14/pe14-2.cpp: -------------------------------------------------------------------------------- 1 | // pe14-2.cpp -- using Wine class with containment 2 | // This is exercise 2 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: The following code was given by the book to test the Wine class 4 | // compile with winec2.cpp 5 | 6 | #include 7 | #include"winec2.h" 8 | 9 | int main(void) 10 | { 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | 15 | cout << "Enter name of wine: "; 16 | char lab[50]; 17 | cin.getline(lab, 50); 18 | cout << "Enter number of years: "; 19 | int yrs; 20 | cin >> yrs; 21 | 22 | Wine holding(lab, yrs); // store label, years, give arrays yrs elements 23 | holding.GetBottles(); // solicit input for year, bottle count 24 | holding.Show(); // display object contents 25 | 26 | const int YRS = 3; 27 | int y[YRS] = {1993, 1995, 1998}; 28 | int b[YRS] = { 48, 60, 72}; 29 | // create new object, initialize using data in arrays y and b 30 | Wine more("Gushing Grape Red", YRS, y, b); 31 | more.Show(); 32 | cout << "Total bottles for " << more.Label() // use Label() method 33 | << ": " << more.sum() << endl; 34 | cout << "Bye\n"; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Chapter6/pe6-3.cpp: -------------------------------------------------------------------------------- 1 | // pe6-3.cpp -- displays a menu and reprompts the user if incorrect input is given 2 | // This is exercise 3 of chapter 6 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | using namespace std; 9 | 10 | // display menu 11 | cout << "Please enter one of the following choices: " << endl 12 | << "c) carnivore \t\t p) pianist" << endl 13 | << "t) tree \t\t g) game" << endl; 14 | 15 | char ch; 16 | cin >> ch; 17 | 18 | 19 | // ensure that input is appropriate 20 | while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g' ) 21 | { 22 | cout << "Please enter a c, p, t, or g: "; 23 | cin.ignore(100,'\n'); 24 | cin >> ch; 25 | } 26 | 27 | // select an option from the menu 28 | switch (ch) 29 | { 30 | case 'c': 31 | cout << "A wolf is a carnivore." << endl; 32 | break; 33 | case 'p': 34 | cout << "Wolfgang Amadeus Mozart was a pianist." << endl; 35 | break; 36 | case 't': 37 | cout << "A maple is a tree." << endl; 38 | break; 39 | case 'g': 40 | cout << "Monopoly is a game." << endl; 41 | break; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Chapter10/customerStack.cpp: -------------------------------------------------------------------------------- 1 | // customerStack.cpp - customerStack class implementation 2 | // This is exercise 5 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // customerStack.cpp is accompanied by customerStack.h and questionFive.cpp 4 | 5 | 6 | // do I need to include libraries? --> yes. 7 | #include"customerStack.h" 8 | 9 | // I believe I don't need a typedef command for Item since 10 | // we are importing the customerStack.h library --> you don't. 11 | 12 | CustomerStack::CustomerStack() 13 | { 14 | top = 0; 15 | } 16 | 17 | bool CustomerStack::isempty() const 18 | { 19 | if (0 == top) 20 | return true; 21 | else 22 | return false; 23 | } 24 | 25 | bool CustomerStack::isfull() const 26 | { 27 | if (MAX == top) 28 | return true; 29 | else 30 | return false; 31 | } 32 | 33 | bool CustomerStack::push(const Item & item) 34 | { 35 | if (0 == isfull()) 36 | { 37 | items[top++] = item; 38 | return true; 39 | } 40 | else 41 | return false; 42 | } 43 | 44 | bool CustomerStack::pop(Item & item) 45 | { 46 | if (0 == isempty()) 47 | { 48 | item = items[--top]; 49 | return true; 50 | } 51 | else 52 | return false; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Chapter9/golf.h: -------------------------------------------------------------------------------- 1 | // golf.h -- constants, and structures, and functions for golf header file 2 | // This is exercise 1 of chapter 9 in C++ Primer Plus by Stephen Prata 3 | // golf.h is accompanied by golf.cpp and questionOne.cpp 4 | 5 | // golf.h - for pe9-1.cpp 6 | 7 | // Don't I need to include the #ifndef GOLF ... thing, since 8 | // not doing so would cause problems if two files include golf.h? 9 | // Hm... I guess I'll find out. 10 | 11 | const int Len = 40; 12 | struct golf 13 | { 14 | char fullname[Len]; 15 | int handicap; 16 | }; 17 | 18 | // non-interactive version: 19 | // function sets golf structure to provided name, handicap 20 | // using values passed as arguments to the function 21 | void setgolf(golf & g, const char * name, int hc); 22 | 23 | // interactive version: 24 | // function solicits name and handicap from user 25 | // and sets the members of g to the values entered 26 | // returns 1 if name is entered, 0 if name is empty string 27 | int setgolf(golf & g); 28 | 29 | // function resets handicap to new value 30 | void handicap(golf & g, int hc); 31 | 32 | // function displays contents of golf structure 33 | void showgolf(const golf & g); 34 | -------------------------------------------------------------------------------- /Chapter17/pe17-2.cpp: -------------------------------------------------------------------------------- 1 | // pe17-2.cpp -- description here 2 | // This is exercise 2 of chapter 17 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char* argv[]) 9 | { 10 | using namespace std; 11 | 12 | // check for input on command line 13 | if (argc == 1) 14 | { 15 | cerr << "Error, a filename must be an argument on the command line." 16 | << endl; 17 | exit(EXIT_FAILURE); 18 | } 19 | 20 | // tell user that only one file is used if multiple arguments 21 | if (argc > 2) 22 | cout << "Arguments on the command line beyond the first are ignored" 23 | << endl; 24 | 25 | ofstream outfile; 26 | outfile.open(argv[1]); // How do I check if I'm rewriting an extant file? 27 | if (!outfile.is_open()) 28 | { 29 | cerr << "Error, could not open file." << endl; 30 | exit(EXIT_FAILURE); 31 | } 32 | 33 | cout << "Enter input (ctrl-D when done): "; 34 | char ch; 35 | 36 | while (cin.get(ch)) 37 | outfile.put(ch); 38 | if (!cin.eof()) 39 | cerr << "Warning, input quit due to something other than " 40 | << "simulated EOF" << endl; 41 | 42 | outfile.close(); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Chapter12/stack2.h: -------------------------------------------------------------------------------- 1 | // stack2.h -- Stack ADT (a modified version of listing 10.10) 2 | // Note: this code was given in the problem 3 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 4 | #ifndef STACK_H_ 5 | #define STACK_H_ 6 | 7 | #include 8 | 9 | typedef unsigned long Item; 10 | 11 | class Stack 12 | { 13 | private: 14 | enum {MAX = 10}; // constant specific to class 15 | Item * pitems; // holds stack items 16 | int size; // number of elements in the stack 17 | int top; // index for top stack item 18 | public: 19 | Stack(int n = 10); // creates stack with n elements 20 | Stack(const Stack & st); 21 | ~Stack(); 22 | bool isempty() const; 23 | bool isfull() const; 24 | // push() returns false if stack already is full, true otherwise 25 | bool push(const Item & item); // add item to stack 26 | // pop() returns false if stack already is empty, true otherwise 27 | bool pop(Item & item); // pop top into item 28 | Stack & operator=(const Stack & st); 29 | friend std::ostream & operator<<(std::ostream & os, const Stack & s); 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /Chapter6/pe6-7.cpp: -------------------------------------------------------------------------------- 1 | // pe6-7.cpp -- reads input until 'q' is entered and then displays words that begin with vowels, consonants, or neither. 2 | // This is exercise 7 of chapter 6 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | 11 | // Prompt user to enter input 12 | cout << "Enter words (q to quit): " << endl; 13 | 14 | // get data 15 | string word; 16 | cin >> word; 17 | 18 | // while loop, test expression evaluates cin.good() and string[0] != 'q' 19 | int vowels, consonants, neither; 20 | vowels = consonants = neither = 0; 21 | while (cin.good() && word != "q") 22 | { 23 | if (isalpha(word[0])) 24 | { 25 | if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u' || word[0] == 'y') 26 | vowels++; 27 | else 28 | consonants++; 29 | } 30 | else 31 | neither++; 32 | 33 | cin >> word; 34 | } 35 | 36 | // display words beginning with vowels, consonants, and neither 37 | cout << vowels << " words beginning with vowels" << endl; 38 | cout << consonants << " words beginning with consonants" << endl; 39 | cout << neither << " others" << endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Chapter12/pe12-1.cpp: -------------------------------------------------------------------------------- 1 | // pe12-1.cpp -- tests the Cow class 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // compile with questionOne.cpp 4 | 5 | #include 6 | #include "Cow.h" 7 | 8 | inline void ShowCow2(Cow c) {c.ShowCow();} 9 | 10 | int main() 11 | { 12 | using namespace std; 13 | cout << "Let's create object 1" << endl; 14 | Cow c1; 15 | cout << "Let's create object 2" << endl; 16 | Cow c2 = Cow("Melinda", "Tennis", 130.0); 17 | cout << "Let's create object 3" << endl; 18 | Cow c3 = c2; 19 | cout << "Let's create object 4" << endl; 20 | Cow c4("Karina", "Badminton", 125.0); 21 | cout << "Let's create object 5" << endl; 22 | Cow c5 = Cow(c3); 23 | cout << "Let's create object 6" << endl; 24 | Cow c6(c4); 25 | cout << "Let's create object 7" << endl; 26 | Cow * pc = new Cow; 27 | *pc = c6; 28 | 29 | cout << "Let's see our cows: " << endl; 30 | c1.ShowCow(); 31 | c2.ShowCow(); 32 | c3.ShowCow(); 33 | c4.ShowCow(); 34 | c5.ShowCow(); 35 | c6.ShowCow(); 36 | pc->ShowCow(); 37 | 38 | cout << "Let's see our cows AGAIN!" << endl; 39 | ShowCow2(c1); 40 | ShowCow2(c2); 41 | ShowCow2(c3); 42 | ShowCow2(c4); 43 | ShowCow2(c5); 44 | ShowCow2(c6); 45 | ShowCow2(*pc); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Chapter13/pe13-2.cpp: -------------------------------------------------------------------------------- 1 | // pe13-2.cpp -- description here 2 | // This is exercise 2 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // Note: This code was given by the book, the question is really 4 | // to write classic2.h, classic2.cpp, and cd2.cpp 5 | // compile with classic2.cpp and cd2.cpp 6 | 7 | #include 8 | using namespace std; 9 | #include "classic2.h" // which will contain #include cd.h 10 | void Bravo(const Cd & disk); 11 | int main(void) 12 | { 13 | Cd c1("Beatles", "Capitol", 14, 35.5); 14 | Classic c2 = Classic("Piano sonata in b flat, Fantasia in C", "Alfred Brendel", "Phillips", 2, 57.17); 15 | Cd * pcd = &c1; 16 | 17 | cout << "Using object directly:\n"; 18 | c1.Report(); // use Cd method 19 | c2.Report(); // use Classic method 20 | 21 | cout << "Using type cd * pointer to objects:\n"; 22 | pcd->Report(); // use Cd method for cd object 23 | pcd = &c2; 24 | pcd->Report(); // use Classic method for classic object 25 | 26 | cout << "Calling a function with a Cd reference argument:\n"; 27 | Bravo(c1); 28 | Bravo(c2); 29 | 30 | cout << "Testing assignment: \n"; 31 | Classic copy; 32 | copy = c2; 33 | copy.Report(); 34 | 35 | return 0; 36 | } 37 | 38 | void Bravo(const Cd & disk) 39 | { 40 | disk.Report(); 41 | } 42 | -------------------------------------------------------------------------------- /Chapter6/pe6-5.cpp: -------------------------------------------------------------------------------- 1 | // pe6-5.cpp -- calculates taxes based on income of tvarps 2 | // This is exercise 5 of chapter 6 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | const double first5000 = 0.0; 6 | const double next10000 = .10; 7 | const double next20000 = .15; 8 | const double after35000 = .20; 9 | 10 | int main(void) 11 | { 12 | using namespace std; 13 | 14 | // prompt user 15 | cout << "Please enter the number of tvarps you earned this year." << endl 16 | << "Enter a negative number to quit: "; 17 | double tvarps; 18 | cin >> tvarps; 19 | 20 | // calculate taxes 21 | double taxes = 0; 22 | while (tvarps >= 0) 23 | { 24 | if (tvarps >= 35000) 25 | taxes = 5000 * first5000 + 10000 * next10000 + 20000 * next20000 + (tvarps - 35000) * after35000; 26 | else if (tvarps >= 15000) 27 | taxes = 5000 * first5000 + 10000 * next10000 + (tvarps - 15000) * next20000; 28 | else if (tvarps >= 5000) 29 | taxes = 5000 * first5000 + (tvarps - 5000) * next10000; 30 | else 31 | taxes = (5000 - tvarps) * first5000; 32 | 33 | // reprompt user 34 | cout << "You owe " << taxes << " tvarps in taxes" << endl 35 | << "Enter a new income or a negative number to quit: "; 36 | cin >> tvarps; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Chapter13/pe13-1.cpp: -------------------------------------------------------------------------------- 1 | // pe13-1.cpp -- tests the Cd and Classic classes 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // Note: This code was given by the book, the question is really 4 | // to write classic.h, classic.cpp, and cd.cpp 5 | // compile with classic.cpp and cd.cpp 6 | 7 | #include 8 | using namespace std; 9 | #include "classic.h" // which will contain #include cd.h 10 | void Bravo(const Cd & disk); 11 | int main(void) 12 | { 13 | Cd c1("Beatles", "Capitol", 14, 35.5); 14 | Classic c2 = Classic("Piano sonata in b flat, Fantasia in C", "Alfred Brendel", "Phillips", 2, 57.17); 15 | Cd * pcd = &c1; 16 | 17 | cout << "Using object directly:\n"; 18 | c1.Report(); // use Cd method 19 | c2.Report(); // use Classic method 20 | 21 | cout << "Using type cd * pointer to objects:\n"; 22 | pcd->Report(); // use Cd method for cd object 23 | pcd = &c2; 24 | pcd->Report(); // use Classic method for classic object 25 | 26 | cout << "Calling a function with a Cd reference argument:\n"; 27 | Bravo(c1); 28 | Bravo(c2); 29 | 30 | cout << "Testing assignment: \n"; 31 | Classic copy; 32 | copy = c2; 33 | copy.Report(); 34 | 35 | return 0; 36 | } 37 | 38 | void Bravo(const Cd & disk) 39 | { 40 | disk.Report(); 41 | } 42 | -------------------------------------------------------------------------------- /Chapter8/pe8-5.cpp: -------------------------------------------------------------------------------- 1 | // pe8-5.cpp -- uses a template function to find the largest 2 | // value in an array of size 5 and returns that value. 3 | // This is exercise 5 of chapter 8 in C++ Primer Plus by Stephen Prata 4 | 5 | #include 6 | 7 | template 8 | T max5(T arr[]); 9 | 10 | int main(void) 11 | { 12 | using namespace std; 13 | 14 | cout << "Enter five int values (s to skip): "; 15 | int i1, i2, i3, i4, i5; 16 | if (cin >> i1 >> i2 >> i3 >> i4 >> i5) 17 | { 18 | int arri[5] = {i1, i2, i3, i4, i5}; 19 | int maxi = max5(arri); 20 | cout << "The maximum value is " << maxi << endl; 21 | } 22 | 23 | cout << "Enter five double values (s to skip): "; 24 | double d1, d2, d3, d4, d5; 25 | cin.ignore(100, '\n'); // discard any input in buffer 26 | if (cin >> d1 >> d2 >> d3 >> d4 >> d5) 27 | { 28 | double arrd[5] = {d1, d2, d3, d4, d5}; 29 | double maxd = max5(arrd); 30 | cout << "The maximum value is " << maxd << endl; 31 | } 32 | 33 | cout << "Bye!" << endl; 34 | 35 | return 0; 36 | } 37 | 38 | // returns the maximum value in the array 39 | template 40 | T max5(T arr[]) 41 | { 42 | T max = arr[0]; 43 | for (int i = 1; i < 5; i++) 44 | { 45 | if (arr[i] > max) 46 | max = arr[i]; 47 | } 48 | return max; 49 | } 50 | -------------------------------------------------------------------------------- /Chapter12/pe12-3.cpp: -------------------------------------------------------------------------------- 1 | // pe12-3.cpp -- uses the Stock class 2 | // Note: this is listing 10.9 with minor changes 3 | // This is exercise 3 of chapter 12 in C++ Primer Plus by Stephen Prata 4 | // compile with stock3.cpp 5 | 6 | #include 7 | #include "stock3.h" 8 | 9 | const int STKS = 4; 10 | int main() 11 | { 12 | using std::cout; 13 | using std::ios_base; 14 | 15 | // create an array of initialized objects 16 | Stock stocks[STKS] = { 17 | Stock("NanoSmart", 12, 20.0), 18 | Stock("Boffo Objects", 200, 2.0), 19 | Stock("Monolithic Obelisks", 130, 3.25), 20 | Stock("Fleep Enterprises", 60, 6.5) 21 | }; 22 | 23 | cout.precision(2); // #.## format 24 | cout.setf(ios_base::fixed, ios_base::floatfield);// #.## format 25 | cout.setf(ios_base::showpoint); // #.## format 26 | 27 | cout << "Stock holdings:\n"; 28 | int st; 29 | for (st = 0; st < STKS; st++) 30 | cout << stocks[st]; 31 | 32 | Stock top = stocks[0]; 33 | 34 | for (st = 1; st < STKS; st++) 35 | top = top.topval(stocks[st]); 36 | 37 | cout << "\nMost valuable holding:\n"; 38 | cout << top; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Chapter11/pe11-6.cpp: -------------------------------------------------------------------------------- 1 | // pe11-6.cpp -- uses the Stonewt class 2 | // This is exercise 6 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // compile with the stonewt3.cpp 4 | 5 | #include 6 | #include "stonewt3.h" 7 | 8 | const int ArrSize = 6; 9 | Stonewt reference(11,0); 10 | 11 | int main(void) 12 | { 13 | using namespace std; 14 | 15 | Stonewt stArr[6] = 16 | { 17 | Stonewt(11,4), 18 | Stonewt(200), 19 | Stonewt(150) 20 | }; 21 | 22 | double pounds; 23 | for (int i = 3; i < ArrSize; i++) 24 | { 25 | cout << "Enter the number of pounds for the Stonewt object"; 26 | cout << " at position " << i + 1 << ": "; 27 | cin >> pounds; 28 | stArr[i] = Stonewt(pounds); 29 | } 30 | 31 | Stonewt min = stArr[0]; 32 | Stonewt max = stArr[0]; 33 | int count = 0; 34 | for (int i = 0; i < ArrSize; i++) 35 | { 36 | if (stArr[i] >= reference) 37 | count++; 38 | 39 | if (stArr[i] > max) 40 | max = stArr[i]; 41 | else if (stArr[i] < min) 42 | min = stArr[i]; 43 | } 44 | 45 | cout << "The heaviest object weighed: " << max << " pounds." << endl; 46 | cout << "The lightest object weighed: " << min << " pounds." << endl; 47 | cout << "The number of objects that weighed above " << reference; 48 | cout << " pounds was " << count << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Chapter13/classic.cpp: -------------------------------------------------------------------------------- 1 | // classic.cpp -- implementation of the Classic class 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #include "classic.h" 5 | #include 6 | #include 7 | 8 | Classic::Classic() : Cd() 9 | { 10 | primary_work[0] = '\0'; 11 | } 12 | 13 | Classic::Classic(const char * primary, Cd & d) : Cd(d) 14 | { 15 | strncpy(primary_work, primary, len_primary-1); 16 | primary_work[len_primary-1] = '\0'; 17 | } 18 | 19 | Classic::Classic(const char * primary, const char * s1, 20 | const char * s2, int n, double x) : Cd(s1, s2, n, x) 21 | { 22 | strncpy(primary_work, primary, len_primary-1); 23 | primary_work[len_primary-1] = '\0'; 24 | } 25 | 26 | Classic::Classic(const Classic & c) : Cd(c) 27 | { 28 | strncpy(primary_work, c.primary_work, len_primary-1); 29 | primary_work[len_primary-1] = '\0'; 30 | } 31 | 32 | Classic::~Classic() 33 | { 34 | } 35 | 36 | void Classic::Report() const 37 | { 38 | using std::cout; 39 | using std::endl; 40 | 41 | Cd::Report(); 42 | cout << "Primary Work: " << primary_work << endl; 43 | } 44 | 45 | Classic & Classic::operator=(const Classic & c) 46 | { 47 | Cd::operator=(c); 48 | strncpy(primary_work, c.primary_work, len_primary-1); 49 | primary_work[len_primary-1] = '\0'; 50 | return *this; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Chapter10/pe10-1.cpp: -------------------------------------------------------------------------------- 1 | // pe10-1.cpp - uses the BankAccount class 2 | // This is exercise 1 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // This file is accompanied by BankAccount.h and BankAccount.cpp 4 | 5 | #include 6 | #include "BankAccount.h" 7 | 8 | int main(void) 9 | { 10 | using std::cout; 11 | using std::cin; 12 | using std::endl; 13 | 14 | BankAccount checking("Jerry", "2104032405", 2000.00); 15 | cout << "Here is our account: " << endl; 16 | checking.printAccount(); 17 | cout << "Now we deposit $100" << endl; 18 | checking.deposit(100); 19 | checking.printAccount(); 20 | cout << "And now we withdraw $1500" << endl; 21 | checking.withdraw(1500); 22 | checking.printAccount(); 23 | cout << "Now let's try some illegal operations" << endl; 24 | cout << "Let's deposit $-200" << endl; 25 | checking.deposit(-200); 26 | checking.printAccount(); 27 | cout << "Let's withdraw $-500" << endl; 28 | checking.withdraw(-500); 29 | checking.printAccount(); 30 | cout << "Finally let's withdraw $10,000" << endl; 31 | checking.withdraw(10000); 32 | checking.printAccount(); 33 | 34 | cout << "Let's try to initialize a saving's account " 35 | << "with a negative balance" << endl; 36 | BankAccount savings("Jerry", "1423540433", -1000); 37 | savings.printAccount(); 38 | 39 | cout << "Bye!" << endl; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Chapter4/pe4-6.cpp: -------------------------------------------------------------------------------- 1 | // pe4-6.cpp -- we create an array of a structure whose parameters are the brand, weight, and calories of a candy bar; initialize it; and display its contents 2 | // This is exercise 6 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | struct CandyBar 6 | { 7 | char brand[20]; 8 | float weight; 9 | int calories; 10 | }; 11 | 12 | int main(void) 13 | { 14 | using namespace std; 15 | 16 | CandyBar snacks[3] = {{"Twix", 1.1, 200}, {"Snickers", 2.5, 300}, {"Three Musketeers", 2.1, 150}}; 17 | 18 | cout << "For our first snack: " << endl; 19 | cout << "Our snack brand is " << snacks[0].brand << endl; 20 | cout << "The weight is " << snacks[0].weight << endl; 21 | cout << "And it contains " << snacks[0].calories << " calories" << endl << endl; 22 | 23 | cout << "For our second snack: " << endl; 24 | cout << "Our snack brand is " << snacks[1].brand << endl; 25 | cout << "The weight is " << snacks[1].weight << endl; 26 | cout << "And it contains " << snacks[1].calories << " calories" << endl << endl; 27 | 28 | cout << "For our third snack: " << endl; 29 | cout << "Our snack brand is " << snacks[2].brand << endl; 30 | cout << "The weight is " << snacks[2].weight << endl; 31 | cout << "And it contains " << snacks[2].calories << " calories" << endl; 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter6/pe6-8.cpp: -------------------------------------------------------------------------------- 1 | // pe6-8.cpp -- opens a text file, reads it character-by-character to the end of the file, and reports the number of characters in the file. 2 | // This is exercise 8 of chapter 9 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include // for ifstream object 6 | #include // for exit() and EXIT_FAILURE values 7 | const int SIZE = 100; 8 | 9 | int main(void) 10 | { 11 | using namespace std; 12 | 13 | // create ifstream object 14 | ifstream inFile; // inFile an ifstream object 15 | char file[SIZE]; 16 | 17 | // Prompt user to enter file name 18 | cout << "Enter the file name you wish to open, including the extension: "; 19 | cin.getline(file, SIZE); 20 | 21 | // Open file and check that file opened correctly 22 | inFile.open(file); 23 | if (!inFile.is_open()) 24 | { 25 | cout << "cout not open the file " << file << endl; 26 | cout << "Program terminating" << endl; 27 | exit(EXIT_FAILURE); 28 | } 29 | 30 | // read characters until EOF is reached 31 | char ch; 32 | inFile >> ch; 33 | int characters = 0; 34 | while (!inFile.eof()) 35 | { 36 | characters++; 37 | inFile >> ch; 38 | } 39 | 40 | // display the number of characters 41 | cout << characters << " characters in " << file << endl; 42 | 43 | // close file 44 | inFile.close(); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Chapter17/pe17-3.cpp: -------------------------------------------------------------------------------- 1 | // pe17-3.cpp -- description here 2 | // This is exercise 3 of chapter 17 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char* argv[]) 9 | { 10 | using namespace std; 11 | 12 | // check to see that appropriate files exist 13 | if (argc < 3) 14 | { 15 | cerr << "Error, two arguments representing filenames are missing" 16 | << endl; 17 | exit(EXIT_FAILURE); 18 | } 19 | else if (argc > 3) 20 | cerr << "More arguments than necessary are detected" << endl 21 | << "Only the first two arguments will be used" << endl; 22 | 23 | cout << "Warning: this will erase the contents of " << argv[2] 24 | << ". Proceed? (y/n) "; 25 | 26 | char ch; 27 | cin >> ch; 28 | if (ch != 'y' && ch !='Y') 29 | return 0; 30 | else 31 | { 32 | ifstream infile; 33 | infile.open(argv[1]); 34 | if (!infile.is_open()) 35 | { 36 | cerr << "Error, could not open " << argv[1] << endl; 37 | exit(EXIT_FAILURE); 38 | } 39 | 40 | ofstream outfile; 41 | outfile.open(argv[2]); 42 | if (!outfile.is_open()) 43 | { 44 | cerr << "Error, could not open " << argv[2] << endl; 45 | exit(EXIT_FAILURE); 46 | } 47 | 48 | while (infile.get(ch)) 49 | outfile.put(ch); 50 | 51 | infile.close(); 52 | outfile.close(); 53 | return 0; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Chapter16/pe16-4.cpp: -------------------------------------------------------------------------------- 1 | // pe16-4.cpp -- description here 2 | // This is exercise 4 of chapter 16 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | const int SIZE = 15; 9 | int reduce(long ar[], int n); 10 | 11 | int main(void) 12 | { 13 | using namespace std; 14 | 15 | cout << "Enter up to " << SIZE << " numbers (quit to quit):"; 16 | long num; 17 | int i = 0; 18 | long ar[SIZE]; 19 | while (cin >> num && i < SIZE) 20 | { 21 | ar[i++] = num; 22 | } 23 | 24 | if (0 == i) 25 | { 26 | cout << "No values were entered" << endl; 27 | return 0; 28 | } 29 | 30 | cout << "You entered "; 31 | for (int j = 0; j < i; j++) 32 | cout << ar[j] << " "; 33 | cout << endl; 34 | 35 | cout << "Here's your sorted array, without duplicates: \n"; 36 | int j = reduce(ar, i); 37 | for (int k = 0; k < j; k++) 38 | cout << ar[k] << " "; 39 | cout << endl; 40 | 41 | return 0; 42 | } 43 | 44 | int reduce(long ar[], int n) 45 | { 46 | using namespace std; 47 | 48 | vector var; 49 | for (int i = 0; i < n; i++) 50 | var.push_back(ar[i]); 51 | 52 | sort(var.begin(), var.end()); 53 | vector::iterator end = unique(var.begin(), var.end()); 54 | var.erase(end, var.end()); 55 | vector::iterator iv = var.begin(); 56 | for (int i = 0; iv != var.end(); iv++, i++) 57 | ar[i] = *iv; 58 | 59 | return var.size(); 60 | } 61 | -------------------------------------------------------------------------------- /Chapter6/pe6-2.cpp: -------------------------------------------------------------------------------- 1 | // pe6-2.cpp -- this program asks the user for donation values which are stored in an array. When the user enters non-numeric input or enters the maximum amount of donations, the program calculates the average donation and the number of donations above average. 2 | // This is exercise 2 of chapter 6 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | const int Max = 10; 6 | 7 | int main(void) 8 | { 9 | using namespace std; 10 | 11 | double donation[Max]; // array to hold donation values 12 | int index = 0; // index for donation array 13 | double value, total = 0; 14 | 15 | cout << "Enter value of donation number " << index + 1 << ": "; 16 | cin >> value; 17 | 18 | // prompt for and store donation values 19 | while (cin.good() && index < Max) 20 | { 21 | donation[index] = value; 22 | total += value; 23 | if (Max > ++index) 24 | { 25 | cout << "Enter value of donation number " << index + 1 << ": "; 26 | cin >> value; 27 | } 28 | } 29 | 30 | cout << "The average donation was $" << total/index << endl; 31 | 32 | // calculate the number of donations above average 33 | int above_average = 0; 34 | for (int i = 0; i < index; i++) 35 | { 36 | if (donation[i] > total/index) 37 | { 38 | above_average++; 39 | } 40 | } 41 | 42 | cout << "The number of donations above average was " << above_average << endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Chapter8/pe8-1.cpp: -------------------------------------------------------------------------------- 1 | // pe8-1.cpp -- main() uses a function with two arguments that displays a string either once or the number of times that the function was called. 2 | // This is exercise 1 of chapter 8 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | const int MAXLEN = 100; // the size of our char array for holding input 7 | 8 | void repeatString(const char * str, int echo = 0); 9 | 10 | int main(void) 11 | { 12 | using namespace std; 13 | 14 | cout << "Enter some characters: "; 15 | char str[MAXLEN]; 16 | cin.getline(str, MAXLEN); 17 | 18 | cout << "repeatString(str) = "; 19 | repeatString(str); 20 | cout << "repeatString(str, 1) = "; 21 | repeatString(str, 1); 22 | cout << "repeatString(str) = "; 23 | repeatString(str); 24 | cout << "repeatString(str, 1) = "; 25 | repeatString(str, 1); 26 | 27 | return 0; 28 | } 29 | 30 | // prints the string pointed to by str the number of times the function has been called if echo is non-zero, prints once otherwise 31 | void repeatString(const char * str, int echo) 32 | { 33 | using namespace std; 34 | static int count = 0; // we use count to keep track of the number of function calls 35 | count++; 36 | 37 | if (echo == 0) 38 | { 39 | cout << str << endl; 40 | return; 41 | } 42 | 43 | else 44 | { 45 | for (int i = 0; i < count; i++) 46 | cout << str << ' '; 47 | } 48 | 49 | cout << endl; 50 | return; 51 | } 52 | -------------------------------------------------------------------------------- /Chapter10/list.cpp: -------------------------------------------------------------------------------- 1 | // list.cpp - implementation of the List class ADT 2 | // This is exercise 8 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // list.cpp is accompanied by list.h and questionEight.cpp 4 | 5 | #include 6 | #include "list.h" 7 | 8 | List::List() 9 | { 10 | index = 0; 11 | } 12 | 13 | void List::add(const Item & item) 14 | { 15 | using std::cout; 16 | using std::endl; 17 | if (isFull() == 0) 18 | items[index++] = item; 19 | else 20 | cout << "List is full. Cannot add more items." << endl; 21 | } 22 | 23 | bool List::isEmpty(void) const 24 | { 25 | if (0 == index) 26 | return true; 27 | else 28 | return false; 29 | } 30 | 31 | bool List::isFull(void) const 32 | { 33 | if (MAX == index) 34 | return true; 35 | else 36 | return false; 37 | } 38 | 39 | void List::visit(void (*pf)(Item & item)) 40 | { 41 | for (int i = 0; i < index; i++) 42 | (*pf)(items[i]); 43 | } 44 | 45 | //int List::numberOfItems(void) const 46 | //{ 47 | // return index; 48 | //} 49 | 50 | void List::printList(void) const 51 | { 52 | using std::cout; 53 | using std::endl; 54 | 55 | if (isEmpty()) 56 | { 57 | cout << "No list to print" 58 | << endl; 59 | return; 60 | } 61 | else 62 | { 63 | 64 | for (int i = 0; i < index; i++) 65 | cout << "Item " << i + 1 << ": " 66 | << items[i] << endl; 67 | } 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /Chapter10/pe10-7.cpp: -------------------------------------------------------------------------------- 1 | // pe10-7.cpp - uses all features of the Plorg class 2 | // This is exercise 7 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // questionSeven.cpp is accompanied by plorg.h and plorg.cpp 4 | 5 | #include 6 | #include "plorg.h" 7 | 8 | int main(void) 9 | { 10 | // declare using directives 11 | using std::cout; 12 | using std::endl; 13 | using std::cin; 14 | 15 | // initialize a Plorg object with default constructor 16 | cout << "Here is our default Plorg object" << endl; 17 | Plorg p1; 18 | p1.show(); 19 | 20 | cout << "Enter a name for our next Plorg object: "; 21 | char name[30]; 22 | cin.get(name, 30); 23 | cin.clear(); // clear error flag if nothing is entered 24 | while (cin.get() != '\n' && cin.eof() == 0) 25 | continue; 26 | cin.clear(); // if we reach EOF to reset error flags 27 | Plorg p2(name); 28 | cout << "Here is our new Plorg object" << endl; 29 | p2.show(); 30 | 31 | // intiailze Plorg object with values given by user 32 | cout << "Enter a new CI value: "; 33 | int CI; 34 | while (!(cin >> CI)) 35 | { 36 | cin.clear(); 37 | while (cin.get() != '\n' && cin.eof() == 0) 38 | continue; 39 | cout << "Error. Not an integer." << endl; 40 | cout << "Enter a new CI value: "; 41 | } 42 | 43 | // change the CI value 44 | p2.changeCI(CI); 45 | 46 | // print object with new CI value 47 | cout << "Here is our new Plorg: " << endl; 48 | p2.show(); 49 | 50 | cout << "Bye!" << endl; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Chapter7/pe7-4.cpp: -------------------------------------------------------------------------------- 1 | // pe7-4.cpp -- calculates the chances of winning a lottery 2 | // This is exercise 4 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | long double probability(unsigned numbers, unsigned picks); 7 | 8 | int main(void) 9 | { 10 | using namespace std; // You will want to phase this out eventually as it is not good practice 11 | 12 | double total, total_powerball, choices, choices_powerball; 13 | cout << "Enter the total number of choices on the game card and\n" 14 | "the number of picks allowed:\n"; 15 | while ((cin >> total >> choices) && choices <= total) 16 | { 17 | cout << "Enter the total number of choices on the game card and\n" 18 | "the number of picks allowed for the power ball:\n"; 19 | if ((cin >> total_powerball >> choices_powerball) && choices_powerball <= total_powerball) 20 | { 21 | cout << "you have one chance in "; 22 | cout << probability(total, choices) * probability(total_powerball, choices_powerball); // compute the odds 23 | cout << " of winning.\n"; 24 | cout << "Next two numbers (q to quit): "; 25 | } 26 | else 27 | break; 28 | } 29 | cout << "bye\n"; 30 | return 0; 31 | } 32 | 33 | 34 | long double probability(unsigned numbers, unsigned picks) 35 | { 36 | long double result = 1.0; // here come some local variables 37 | long double n; 38 | unsigned p; 39 | 40 | for (n = numbers, p = picks; p > 0; n--, p--) 41 | result = result * n / p; 42 | return result; 43 | } 44 | -------------------------------------------------------------------------------- /Chapter12/pe12-2.cpp: -------------------------------------------------------------------------------- 1 | // pe12-2.cpp - code to test String class from string2.h and string2.cpp 2 | // Note: the code in this program was given by Prata 3 | // This is exercise 2 of chapter 12 in C++ Primer Plus by Stephen Prata 4 | // compile with string2.cpp 5 | 6 | #include 7 | using namespace std; 8 | #include "string2.h" 9 | int main() 10 | { 11 | String s1(" and I am a C++ student."); 12 | String s2 = "Please enter your name: "; 13 | String s3; 14 | cout << s2; // overloaded << operator 15 | cin >> s3; // overloaded >> operator 16 | s2 = "My name is " + s3; // overloaded =, + operators 17 | cout << s2 << ".\n"; 18 | s2 = s2 + s1; 19 | s2.stringup(); // converts string to uppercase 20 | cout << "The string\n" << s2 << "\ncontains " << s2.has('A'); 21 | cout << " 'A' characters in it.\n"; 22 | s1 = "red"; // String(const char *), 23 | // then String & operator=(const String &) 24 | String rgb[3] = { String(s1), String("green"), String("blue")}; 25 | cout << "Enter the name of a primary color for mixing light: "; 26 | 27 | String ans; 28 | bool success = false; 29 | while (cin >> ans) 30 | { 31 | ans.stringlow(); // converts string to lowercase 32 | for (int i = 0; i < 3; i++) 33 | { 34 | if (ans == rgb[i]) // overloaded == operator 35 | { 36 | cout << "That's right!\n"; 37 | success = true; 38 | break; 39 | } 40 | } 41 | if (success) 42 | break; 43 | else 44 | cout << "Try again!\n"; 45 | } 46 | cout << "Bye\n"; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Chapter16/pe16-7.cpp: -------------------------------------------------------------------------------- 1 | // pe16-7.cpp -- description here 2 | // This is exercise 7 of chapter 16 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | vector Lotto(int spots, int selected); 11 | 12 | int main(void) 13 | { 14 | using namespace std; 15 | 16 | cout << "Enter the number of spots on the lottery card: "; 17 | int spots; 18 | cin >> spots; 19 | cout << "Enter the number of spots to be selected: "; 20 | int selected; 21 | cin >> selected; 22 | 23 | vector result = Lotto(spots, selected); 24 | cout << "Your lucky numbers are: "; 25 | for (int i = 0; (unsigned) i < result.size(); i++) 26 | cout << result[i] << " "; 27 | cout << endl; 28 | 29 | return 0; 30 | } 31 | 32 | vector Lotto(int spots, int selected) 33 | { 34 | if (spots < selected) 35 | { 36 | cout << "The second argument of Lotto() must be less than "; 37 | cout << "the first argument." << endl; 38 | cout << "Program abort" << endl; 39 | exit(EXIT_FAILURE); 40 | } 41 | 42 | if (spots < 0 || selected < 0) 43 | { 44 | cout << "Both arguments of Lotto() must be positive integers"; 45 | cout << endl; 46 | cout << "Program abort" << endl; 47 | exit(EXIT_FAILURE); 48 | } 49 | 50 | vector v; 51 | for (int i = 1; i < spots + 1; i++) 52 | v.push_back(i); 53 | 54 | random_shuffle(v.begin(), v.end()); 55 | vector picks; 56 | for (int i = 0; i < selected; i++) 57 | picks.push_back(v[i]); 58 | 59 | return picks; 60 | } 61 | -------------------------------------------------------------------------------- /Chapter14/pe14-4.cpp: -------------------------------------------------------------------------------- 1 | // pe14-4.cpp -- tests BadDude class 2 | // This is exercise 4 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | // compile with person.cpp 4 | 5 | #include 6 | #include // for strchr() 7 | #include 8 | #include 9 | #include"person.h" 10 | const int SIZE = 4; 11 | 12 | int main(void) 13 | { 14 | using std::strchr; 15 | using std::cout; 16 | using std::cin; 17 | using std::endl; 18 | using std::strchr; 19 | 20 | srand(time(NULL)); 21 | Person * pp[SIZE]; 22 | 23 | int ct; 24 | for (ct = 0; ct < SIZE; ct++) 25 | { 26 | char choice; 27 | cout << "Enter the person category:\n" 28 | << "p: person g: gunslinger " 29 | << "k: poker player b: bad dude \n" 30 | << "q: quit\n"; 31 | cin >> choice; 32 | while (strchr("pgkbq", choice) == NULL) 33 | { 34 | cout << "Please enter a p, g, k, b, or q: "; 35 | cin >> choice; 36 | } 37 | cin.get(); // get newline character 38 | if (choice == 'q') 39 | break; 40 | switch(choice) 41 | { 42 | case 'p': pp[ct] = new Person; 43 | break; 44 | case 'g': pp[ct] = new Gunslinger; 45 | break; 46 | case 'k': pp[ct] = new PokerPlayer; 47 | break; 48 | case 'b': pp[ct] = new BadDude; 49 | break; 50 | } 51 | pp[ct]->Set(); 52 | pp[ct]->Show(); 53 | cout << "ct = " << ct << endl; 54 | } 55 | 56 | cout << "Your selection:" << endl << endl; 57 | for (int i = 0; i < ct; i++) 58 | { 59 | pp[i]->Show(); 60 | cout << endl; 61 | delete *(pp + i); 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Chapter11/mytime4.h: -------------------------------------------------------------------------------- 1 | // mytime4.h -- modified version of mytime3.h 2 | // This is exercise 4 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // see mytime3.h or listing 11.10 in C++ Primer Plus for the original file 4 | 5 | #ifndef MYTIME4_H_ 6 | #define MYTIME4_H_ 7 | #include 8 | 9 | class Time 10 | { 11 | private: 12 | int hours; 13 | int minutes; 14 | public: 15 | Time(); 16 | Time(int h, int m = 0); 17 | void AddMin(int m); 18 | void AddHr(int h); 19 | void Reset(int h = 0, int m = 0); 20 | friend Time operator+(const Time & t1, const Time & t2); 21 | friend Time operator-(const Time & t1, const Time & t2); 22 | friend Time operator*(const Time & t, double mult); 23 | friend Time operator*(double mult, const Time & t) 24 | {return t * mult;} 25 | friend std::ostream & operator<<(std::ostream & os, const Time & t); 26 | }; 27 | #endif 28 | 29 | 30 | // This is listing 11.10 31 | // mytime3.h -- Time class with friends 32 | //#ifndef MYTIME3_H_ 33 | //#define MYTIME3_H_ 34 | //#include 35 | // 36 | //class Time 37 | //{ 38 | //private: 39 | // int hours; 40 | // int minutes; 41 | //public: 42 | // Time(); 43 | // Time(int h, int m = 0); 44 | // void AddMin(int m); 45 | // void AddHr(int h); 46 | // void Reset(int h = 0, int m = 0); 47 | // Time operator+(const Time & t) const; 48 | // Time operator-(const Time & t) const; 49 | // Time operator*(double n) const; 50 | // friend Time operator*(double m, const Time & t) 51 | // { return t * m; } 52 | // friend std::ostream & operator<<(std::ostream & os, const Time & t); 53 | //}; 54 | //#endif 55 | -------------------------------------------------------------------------------- /Chapter13/vintageport.cpp: -------------------------------------------------------------------------------- 1 | // vintageport.cpp -- the VintagePort class interface 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include "vintageport.h" 7 | using std::ostream; 8 | 9 | VintagePort::VintagePort() : Port("The Noble", "Vintage") 10 | { 11 | nickname = NULL; 12 | year = 1900; 13 | } 14 | 15 | VintagePort::VintagePort(const char * br, int b, const char * nn, int y) 16 | : Port(br, "Vintage", b) 17 | { 18 | nickname = new char[strlen(nn) + 1]; 19 | strcpy(nickname, nn); 20 | year = y; 21 | } 22 | 23 | VintagePort::VintagePort(const VintagePort & vp) : Port(vp) 24 | { 25 | nickname = new char[strlen(vp.nickname) + 1]; 26 | strcpy(nickname, vp.nickname); 27 | year = vp.year; 28 | } 29 | 30 | VintagePort & VintagePort::operator=(const VintagePort & vp) 31 | { 32 | if (this == &vp) 33 | return *this; 34 | else 35 | { 36 | Port::operator=(vp); 37 | delete [] nickname; 38 | nickname = new char[strlen(vp.nickname) + 1]; 39 | strcpy(nickname, vp.nickname); 40 | year = vp.year; 41 | } 42 | return *this; 43 | } 44 | 45 | void VintagePort::Show() const 46 | { 47 | using std::cout; 48 | using std::endl; 49 | 50 | Port::Show(); 51 | cout << "Nickname: " << nickname << endl; 52 | cout << "Year: " << year << endl; 53 | } 54 | 55 | ostream & operator<<(ostream & os, const VintagePort & vp) 56 | { 57 | os << (const Port &) vp; 58 | if (NULL == vp.nickname) 59 | os << ", " << vp.year; 60 | else 61 | os << ", " << vp.nickname << ", " << vp.year; 62 | return os; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Chapter8/pe8-6.cpp: -------------------------------------------------------------------------------- 1 | // pe8-6.cpp -- uses maxn() to find the largest value in an array. 2 | // Also uses a specialization to find the largest string in an array 3 | // of char *. 4 | // This is exercise 6 of chapter 8 in C++ Primer Plus by Stephen Prata 5 | 6 | #include 7 | #include 8 | 9 | template 10 | T maxn(T arr[], int size); 11 | 12 | template <> 13 | const char * maxn(const char * arr[], int size); 14 | 15 | int main(void) 16 | { 17 | using namespace std; 18 | char str1[] = "Hello"; 19 | char str2[] = "Hi"; 20 | char str3[] = "Hey"; 21 | char str4[] = "Greetings!"; // 10 characters 22 | char str5[] = "Goodbye!!!"; // 10 characters 23 | 24 | char * arrs[5] = 25 | { 26 | str1, 27 | str2, 28 | str3, 29 | str4, 30 | str5 31 | }; 32 | 33 | int arri[5] = {1, 2, 3, 100, -100}; 34 | 35 | // use specialization 36 | cout << "The largest string is " 37 | << maxn(arrs, 5) << endl; 38 | 39 | // use template 40 | cout << "The largest number is " 41 | << maxn(arri, 5) << endl; 42 | 43 | return 0; 44 | } 45 | 46 | template 47 | T maxn(T arr[], int size) 48 | { 49 | T max = arr[0]; 50 | for(int i = 1; i < size; i++) 51 | { 52 | if (arr[i] > max) 53 | max = arr[i]; 54 | } 55 | return max; 56 | } 57 | 58 | template <> 59 | const char * maxn(const char * arr[], int size) 60 | { 61 | const char * longest = arr[0]; 62 | for (int i = 0; i < size; i++) 63 | { 64 | if (strlen(arr[i]) > strlen(longest)) 65 | longest = arr[i]; 66 | } 67 | 68 | return longest; 69 | } 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /Chapter14/pe14-3.cpp: -------------------------------------------------------------------------------- 1 | // pe14-3.cpp -- tests the QueueTp class 2 | // This is exercise 3 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: This program is a modified version of listing 14.12 4 | // compile with workermi.cpp 5 | #include 6 | #include 7 | #include "workermi.h" 8 | #include "QueueTp.h" 9 | const int SIZE = 5; 10 | 11 | int main() 12 | { 13 | using std::cin; 14 | using std::cout; 15 | using std::endl; 16 | using std::strchr; 17 | 18 | QueueTp q; 19 | Worker * pw; 20 | 21 | int ct; 22 | for (ct = 0; ct < SIZE; ct++) 23 | { 24 | char choice; 25 | cout << "Enter the employee category:\n" 26 | << "w: waiter s: singer " 27 | << "t: singing waiter q: quit\n"; 28 | cin >> choice; 29 | while (strchr("wstq", choice) == NULL) 30 | { 31 | cout << "Please enter a w, s, t, or q: "; 32 | cin >> choice; 33 | } 34 | if (choice == 'q') 35 | break; 36 | switch(choice) 37 | { 38 | case 'w': pw = new Waiter; 39 | break; 40 | case 's': pw = new Singer; 41 | break; 42 | case 't': pw = new SingingWaiter; 43 | break; 44 | } 45 | cin.get(); 46 | pw->Set(); 47 | q.enqueue(pw); 48 | } 49 | 50 | cout << "\nHere is your staff:\n"; 51 | int i; 52 | for (i = 0; i < ct; i++) 53 | { 54 | cout << endl; 55 | q.dequeue(pw); 56 | pw->Show(); 57 | delete pw; 58 | } 59 | cout << "Bye.\n"; 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Chapter7/pe7-9.cpp: -------------------------------------------------------------------------------- 1 | // pe7-9.cpp -- calculates arithmetic through a function that accepts two numbers and a pointer to a function 2 | // This is exercise 9 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | const int FUNCTIONS = 3; 7 | 8 | // function prototypes 9 | double add(double x, double y); 10 | double multiply(double x, double y); 11 | double subtract(double x, double y); 12 | void calculate(double x, double y, double (*pf[])(double a, double b), int length); 13 | 14 | int main(void) 15 | { 16 | using namespace std; 17 | 18 | // declare and initialize an array of pointers to functions 19 | double (*pf[FUNCTIONS])(double, double) = {add, multiply, subtract}; 20 | 21 | cout << "Enter two numbers (q to quit): "; 22 | double x, y; 23 | 24 | while (cin >> x >> y) 25 | { 26 | cout << "The sum, product, and difference are as follows: "; 27 | calculate(x, y, pf, FUNCTIONS); 28 | cout << "Enter another two numbers (q to quit): "; 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | // adds two numbers 35 | double add(double x, double y) 36 | { 37 | return x + y; 38 | } 39 | 40 | // multiplies two numbers 41 | double multiply(double x, double y) 42 | { 43 | return x * y; 44 | } 45 | 46 | // subtracts two numbers 47 | double subtract(double x, double y) 48 | { 49 | return x - y; 50 | } 51 | 52 | // makes calculations between two numbers through several designated functions 53 | void calculate(double x, double y, double (*pf[])(double a, double b), int length) 54 | { 55 | using namespace std; 56 | for (int i = 0; i < length; i++) 57 | { 58 | cout << (*pf[i])(x, y) << " "; 59 | } 60 | cout << endl; 61 | return; 62 | } 63 | -------------------------------------------------------------------------------- /Chapter4/pe4-9.cpp: -------------------------------------------------------------------------------- 1 | // pe4-9.cpp -- we create a dynamic array of a structure whose parameters are the brand, weight, and calories of a candy bar; initialize it; and display its contents 2 | // This is exercise 9 of chapter 4 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | struct CandyBar 7 | { 8 | char brand[20]; 9 | float weight; 10 | int calories; 11 | }; 12 | 13 | int main(void) 14 | { 15 | using namespace std; 16 | 17 | CandyBar * snacks = new CandyBar [3]; 18 | 19 | strcpy(snacks->brand, "Twix"); 20 | snacks->weight = 1.1; 21 | snacks->calories = 200; 22 | 23 | strcpy((snacks+1)->brand, "Snickers"); 24 | (snacks+1)->weight = 2.5; 25 | (snacks+1)->calories = 300; 26 | 27 | strcpy((snacks+2)->brand, "Three Musketeers"); 28 | (snacks+2)->weight = 2.1; 29 | (snacks+2)->calories = 150; 30 | 31 | cout << "For our first snack: " << endl; 32 | cout << "Our snack brand is " << snacks[0].brand << endl; 33 | cout << "The weight is " << snacks[0].weight << endl; 34 | cout << "And it contains " << snacks[0].calories << " calories" << endl << endl; 35 | 36 | cout << "For our second snack: " << endl; 37 | cout << "Our snack brand is " << snacks[1].brand << endl; 38 | cout << "The weight is " << snacks[1].weight << endl; 39 | cout << "And it contains " << snacks[1].calories << " calories" << endl << endl; 40 | 41 | cout << "For our third snack: " << endl; 42 | cout << "Our snack brand is " << snacks[2].brand << endl; 43 | cout << "The weight is " << snacks[2].weight << endl; 44 | cout << "And it contains " << snacks[2].calories << " calories" << endl; 45 | 46 | delete [] snacks; 47 | 48 | return 0; 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter11/complex0.cpp: -------------------------------------------------------------------------------- 1 | // complex0.cpp -- implementation of the complex0.h file, Complex class 2 | // This is exercise 7 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // this code is supplemented by complex0.h and questionSeven.cpp 4 | 5 | #include 6 | using std::cout; 7 | #include "complex0.h" 8 | 9 | Complex::Complex(double a, double b) 10 | { 11 | this->a = a; 12 | this->b = b; 13 | } 14 | 15 | Complex::Complex() 16 | { 17 | a = 0; 18 | b = 0; 19 | } 20 | 21 | void Complex::set(double a, double b) 22 | { 23 | this->a = a; 24 | this->b = b; 25 | } 26 | 27 | Complex operator+(const Complex & c1, const Complex & c2) 28 | { 29 | Complex sum; 30 | sum.a = c1.a + c2.a; 31 | sum.b = c1.b + c2.b; 32 | return sum; 33 | } 34 | 35 | Complex operator-(const Complex & c1, const Complex & c2) 36 | { 37 | Complex diff; 38 | diff.a = c1.a - c2.a; 39 | diff.b = c1.b - c2.b; 40 | return diff; 41 | } 42 | 43 | Complex operator*(const Complex & c1, const Complex & c2) 44 | { 45 | Complex prod; 46 | prod.a = c1.a*c2.a - c1.b*c2.b; 47 | prod.b = c1.a*c2.b + c1.b*c2.a; 48 | return prod; 49 | } 50 | 51 | Complex operator*(double d, const Complex & c) 52 | { 53 | Complex prod; 54 | prod.a = c.a * d; 55 | prod.b = c.b * d; 56 | return prod; 57 | } 58 | 59 | Complex operator~(const Complex & c) 60 | { 61 | Complex conj; 62 | conj.a = c.a; 63 | conj.b = -c.b; 64 | return conj; 65 | } 66 | 67 | std::ostream & operator<<(std::ostream & os, const Complex & c) 68 | { 69 | os << "(" << c.a << ", " << c.b << "i)"; 70 | return os; 71 | } 72 | 73 | std::istream & operator>>(std::istream & is, Complex & c) 74 | { 75 | is >> c.a; 76 | is >> c.b; 77 | return is; 78 | } 79 | -------------------------------------------------------------------------------- /Chapter12/queue.h: -------------------------------------------------------------------------------- 1 | // queue.h -- interface for a queue 2 | // Note: this code is listing 12.10 and was given for the problem 3 | // This is exercise 5 of chapter 12 in C++ Primer Plus by Stephen Prata 4 | 5 | #ifndef QUEUE_H_ 6 | #define QUEUE_H_ 7 | // This queue will contain Customer items 8 | class Customer 9 | { 10 | private: 11 | long arrive; // arrival time for customer 12 | int processtime; // processing time for customer 13 | public: 14 | Customer() { arrive = processtime = 0; } 15 | void set(long when); 16 | long when() const { return arrive; } 17 | int ptime() const { return processtime; } 18 | }; 19 | 20 | typedef Customer Item; 21 | 22 | class Queue 23 | { 24 | private: 25 | // class scope definitions 26 | // Node is a nested structure definition local to this class 27 | struct Node { Item item; struct Node * next;}; 28 | enum {Q_SIZE = 10}; 29 | // private class members 30 | Node * front; // pointer to front of Queue 31 | Node * rear; // pointer to rear of Queue 32 | int items; // current number of items in Queue 33 | const int qsize; // maximum number of items in Queue 34 | // preemptive definitions to prevent public copying 35 | Queue(const Queue & q) : qsize(0) { } 36 | Queue & operator=(const Queue & q) { return *this;} 37 | public: 38 | Queue(int qs = Q_SIZE); // create queue with a qs limit 39 | ~Queue(); 40 | bool isempty() const; 41 | bool isfull() const; 42 | int queuecount() const; 43 | bool enqueue(const Item &item); // add item to end 44 | bool dequeue(Item &item); // remove item from front 45 | }; 46 | #endif 47 | -------------------------------------------------------------------------------- /Chapter16/pe16-8.cpp: -------------------------------------------------------------------------------- 1 | // pe16-8.cpp -- description here 2 | // This is exercise 8 of chapter 16 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | bool string_compare(string s1, string s2); 13 | 14 | int main(void) 15 | { 16 | cout << "Enter Mat's list of friends "; 17 | cout << "(enter after each name, quit to quit):\n"; 18 | vector mat_list; 19 | string name; 20 | getline(cin, name); 21 | while (name != "quit") 22 | { 23 | if (name != "") 24 | mat_list.push_back(name); 25 | getline(cin, name); 26 | } 27 | 28 | cout << "Enter Pat's list of friends "; 29 | cout << "(enter after each name, quit to quit):\n"; 30 | vector pat_list; 31 | getline(cin, name); 32 | while (name != "quit") 33 | { 34 | if (name != "") 35 | pat_list.push_back(name); 36 | getline(cin, name); 37 | } 38 | 39 | sort(mat_list.begin(), mat_list.end(), string_compare); 40 | sort(pat_list.begin(), pat_list.end(), string_compare); 41 | vector merged_list; 42 | insert_iterator< vector > 43 | insert(merged_list, merged_list.begin() ); 44 | set_union(mat_list.begin(), 45 | mat_list.end(), 46 | pat_list.begin(), 47 | pat_list.end(), 48 | insert, 49 | string_compare); 50 | 51 | cout << "Here is our guestlist: " << endl; 52 | for (int i = 0; (unsigned) i < merged_list.size(); i++) 53 | cout << merged_list[i] << endl; 54 | 55 | return 0; 56 | } 57 | 58 | bool string_compare(string s1, string s2) 59 | { 60 | transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 61 | transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 62 | return s1 < s2; 63 | } 64 | -------------------------------------------------------------------------------- /Chapter10/BankAccount.cpp: -------------------------------------------------------------------------------- 1 | // BankAccount.cpp - The implementation of the BankAccount class 2 | // This is exercise 1 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // This file is accompanied by BankAccount.h and questionOne.cpp 4 | 5 | #include 6 | #include "BankAccount.h" 7 | 8 | BankAccount::BankAccount(std::string name, std::string account, 9 | double balance) 10 | { 11 | using std::cout; 12 | using std::endl; 13 | 14 | this->name = name; 15 | this->account = account; 16 | 17 | if (balance >= 0) 18 | this->balance = balance; 19 | else 20 | { 21 | cout << "You can't have a negative balance."; 22 | cout << endl << "Setting balance to 0."; 23 | cout << endl; 24 | this->balance = 0.0; 25 | } 26 | } 27 | 28 | void BankAccount::printAccount(void) const 29 | { 30 | using std::cout; 31 | using std::endl; 32 | 33 | cout << "Name: " << name << endl; 34 | cout << "Account #: " << account << endl; 35 | cout << "Balance: " << balance << endl; 36 | } 37 | 38 | void BankAccount::deposit(double sum) 39 | { 40 | using std::cout; 41 | using std::endl; 42 | 43 | if (sum < 0) 44 | { 45 | cout << "You can't deposit negative money" << endl; 46 | cout << "Deposit aborted" << endl; 47 | } 48 | else 49 | balance += sum; 50 | } 51 | 52 | void BankAccount::withdraw(double sum) 53 | { 54 | using std::cout; 55 | using std::endl; 56 | 57 | if (sum <= balance && sum >= 0) 58 | balance -= sum; 59 | else if (sum < 0) 60 | { 61 | cout << "You can't withdraw negative money" << endl; 62 | cout << "Withdrawal aborted" << endl; 63 | } 64 | else if (sum > balance) 65 | { 66 | cout << "You can't withdraw more than you have"; 67 | cout << endl << "Withdrawal aborted" << endl; 68 | } 69 | } 70 | 71 | 72 | -------------------------------------------------------------------------------- /Chapter14/winec.cpp: -------------------------------------------------------------------------------- 1 | // winec.cpp -- implementation of the wine class 2 | // This is exercise 1 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include"winec.h" 5 | #include 6 | using std::string; 7 | 8 | Wine::Wine(const char * l, int y, const int yr[], const int bot[]) 9 | { 10 | label = l; 11 | years = y; 12 | data.first.resize(years); 13 | data.second.resize(years); 14 | for (int i = 0; i < years; i++) 15 | { 16 | data.first[i] = yr[i]; 17 | data.second[i] = bot[i]; 18 | } 19 | } 20 | 21 | Wine::Wine(const char * l, int y) 22 | { 23 | label = l; 24 | years = y; 25 | data.first.resize(years); 26 | data.second.resize(years); 27 | } 28 | 29 | Wine::~Wine() 30 | { 31 | } 32 | 33 | void Wine::Show() const 34 | { 35 | using std::cout; 36 | using std::endl; 37 | 38 | cout << "Wine: " << label << endl; 39 | cout << "\tYear\tBottles" << endl; 40 | for (int i = 0; i < years; i++) 41 | { 42 | cout << "\t" << data.first[i] << "\t"; 43 | cout << data.second[i] << endl; 44 | } 45 | } 46 | 47 | void Wine::GetBottles() 48 | { 49 | using std::cout; 50 | using std::endl; 51 | using std::cin; 52 | 53 | cout << "Enter " << label << " data for " << years << " year(s):\n"; 54 | int input; 55 | for (int i = 0; i < years; i++) 56 | { 57 | cout << "Enter year: "; 58 | cin >> input; 59 | data.first[i] = input; 60 | cout << "Enter bottles for that year: "; 61 | cin >> input; 62 | data.second[i] = input; 63 | } 64 | } 65 | 66 | const string & Wine::Label() const 67 | { 68 | return label; 69 | } 70 | 71 | int Wine::sum() const 72 | { 73 | int sum = 0; 74 | for (int i = 0; i < years; i++) 75 | sum += data.second[i]; 76 | return sum; 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /Chapter15/sales.h: -------------------------------------------------------------------------------- 1 | // sales.h -- exceptions and inheritance 2 | // this code was written by Stephen Prata and used in pe15-4.cpp 3 | 4 | #include 5 | #include 6 | 7 | class Sales 8 | { 9 | public: 10 | enum {MONTHS = 12}; // could be a static const 11 | class bad_index : public std::logic_error 12 | { 13 | private: 14 | int bi; // bad index value 15 | public: 16 | explicit bad_index(int ix, 17 | const char * s = "Index error in Sales object\n"); 18 | int bi_val() const {return bi;} 19 | }; 20 | explicit Sales(int yy = 0); 21 | Sales(int yy, const double * gr, int n); 22 | virtual ~Sales() { } 23 | int Year() const { return year; } 24 | virtual double operator[](int i) const throw(std::logic_error); 25 | virtual double & operator[](int i) throw(std::logic_error); 26 | private: 27 | double gross[MONTHS]; 28 | int year; 29 | }; 30 | 31 | class LabeledSales : public Sales 32 | { 33 | public: 34 | static const int STRLEN = 50; // could be an enum 35 | class nbad_index : public Sales::bad_index 36 | { 37 | private: 38 | char lbl[STRLEN]; 39 | public: 40 | nbad_index(const char * lb, int ix, 41 | const char * s = "Index error in LabeledSales object\n"); 42 | const char * label_val() {return lbl;} 43 | }; 44 | explicit LabeledSales(const char * lb = "none", int yy = 0); 45 | LabeledSales(const char * lb, int yy, const double * gr, int n); 46 | virtual ~LabeledSales() { } 47 | const char * Label() const {return label;} 48 | virtual double operator[](int i) const throw(std::logic_error); 49 | virtual double & operator[](int i) throw(std::logic_error); 50 | private: 51 | char label[STRLEN]; 52 | }; 53 | -------------------------------------------------------------------------------- /Chapter14/winec2.cpp: -------------------------------------------------------------------------------- 1 | // winec2.cpp -- implementation of the wine class 2 | // This is exercise 1 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include"winec2.h" 5 | #include 6 | using std::string; 7 | 8 | Wine::Wine(const char * l, int y, const int yr[], const int bot[]) : 9 | PairArray(ArrayInt(y), ArrayInt(y)), string(l) 10 | { 11 | years = y; 12 | for (int i = 0; i < years; i++) 13 | { 14 | PairArray::first()[i] = yr[i]; 15 | PairArray::second()[i] = bot[i]; 16 | } 17 | } 18 | 19 | Wine::Wine(const char * l, int y) : 20 | PairArray(ArrayInt(y), ArrayInt(y)), string(l) 21 | { 22 | years = y; 23 | } 24 | 25 | Wine::~Wine() 26 | { 27 | } 28 | 29 | void Wine::Show() const 30 | { 31 | using std::cout; 32 | using std::endl; 33 | 34 | cout << "Wine: " << Label() << endl; 35 | cout << "\tYear\tBottles" << endl; 36 | for (int i = 0; i < years; i++) 37 | { 38 | cout << "\t" << PairArray::first()[i] << "\t"; 39 | cout << PairArray::second()[i] << endl; 40 | } 41 | } 42 | 43 | void Wine::GetBottles() 44 | { 45 | using std::cout; 46 | using std::endl; 47 | using std::cin; 48 | 49 | cout << "Enter " << Label() << " data for " << years << " year(s):\n"; 50 | int input; 51 | for (int i = 0; i < years; i++) 52 | { 53 | cout << "Enter year: "; 54 | cin >> input; 55 | PairArray::first()[i] = input; 56 | cout << "Enter bottles for that year: "; 57 | cin >> input; 58 | PairArray::second()[i] = input; 59 | } 60 | } 61 | 62 | const string & Wine::Label() const 63 | { 64 | return (const string &) (*this); 65 | } 66 | 67 | int Wine::sum() const 68 | { 69 | int sum = 0; 70 | for (int i = 0; i < years; i++) 71 | sum += PairArray::second()[i]; 72 | return sum; 73 | } 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Chapter7/pe7-7.cpp: -------------------------------------------------------------------------------- 1 | // pe7-7.cpp -- a modified version of arrfun3 (listing 7.7) 2 | // This is exercise 7 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | const int Max = 5; 6 | 7 | // function prototypes 8 | double * fill_array(double * ar, int limit); 9 | void show_array(const double * ar, double * end); // don't change data 10 | void revalue(double r, double * ar, double * end); 11 | 12 | int main(void) 13 | { 14 | using namespace std; 15 | double properties[Max]; 16 | double * end = fill_array(properties, Max); 17 | show_array(properties, end); 18 | cout << "Enter revaluation factor: "; 19 | double factor; 20 | cin >> factor; 21 | revalue(factor, properties, end); 22 | show_array(properties, end); 23 | cout << "Done.\n"; 24 | return 0; 25 | } 26 | 27 | double * fill_array(double * ar, int limit) 28 | { 29 | using namespace std; 30 | double temp; 31 | int i; 32 | for (i = 0; i < limit; i++) 33 | { 34 | cout << "Enter value #" << (i + 1) << ": "; 35 | cin >> temp; 36 | if (!cin) // bad input 37 | { 38 | cin.clear(); 39 | while (cin.get() != '\n') 40 | continue; 41 | cout << "Bad input; input process terminated.\n"; 42 | break; 43 | } 44 | else if (temp < 0) // signal to terminate 45 | break; 46 | *(ar + i) = temp; 47 | } 48 | return (ar + i); 49 | } 50 | 51 | void show_array(const double * ar, double * end) 52 | { 53 | using namespace std; 54 | const double * temp = ar; 55 | for (int i = 0; temp < end; i++, temp++) 56 | { 57 | cout << "Property #" << (i + 1) << ": $"; 58 | cout << *temp << endl; 59 | } 60 | } 61 | 62 | // multiplies each element of ar[] by r 63 | void revalue(double r, double * ar, double * end) 64 | { 65 | double * temp = ar; 66 | for (int i = 0; temp < end; i++, temp++) 67 | *temp *= r; 68 | } 69 | -------------------------------------------------------------------------------- /Chapter13/cd.cpp: -------------------------------------------------------------------------------- 1 | // cd.cpp -- implementation of the Cd class 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #include // for cout, endl 5 | #include // for strncpy 6 | #include "cd.h" 7 | 8 | Cd::Cd(const char * s1, const char * s2, int n, double x) 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | strncpy(performers, s1, len_performer - 1); 14 | performers[len_performer - 1] = '\0'; 15 | strncpy(label, s2, len_label - 1); 16 | label[len_label - 1] = '\0'; 17 | 18 | if (n < 0) 19 | { 20 | cout << "Selections must be a positive integer. "; 21 | cout << "Setting selections to 0." << endl; 22 | selections = 0; 23 | } 24 | else 25 | selections = n; 26 | 27 | if (x < 0) 28 | { 29 | cout << "Play time must be a positive real number. "; 30 | cout << "Setting play time to 0." << endl; 31 | playtime = 0; 32 | } 33 | else 34 | playtime = x; 35 | } 36 | 37 | Cd::Cd(const Cd & d) 38 | { 39 | strncpy(performers, d.performers, len_performer); 40 | strncpy(label, d.label, len_label); 41 | selections = d.selections; 42 | playtime = d.playtime; 43 | } 44 | 45 | Cd::Cd() 46 | { 47 | performers[0] = '\0'; 48 | label[0] = '\0'; 49 | selections = 0; 50 | playtime = 0; 51 | } 52 | 53 | Cd::~Cd() 54 | { 55 | } 56 | 57 | void Cd::Report() const 58 | { 59 | using std::cout; 60 | using std::endl; 61 | 62 | cout << "Performers: " << performers << endl; 63 | cout << "label: " << label << endl; 64 | cout << "Selections: " << selections << endl; 65 | cout << "Play time: " << playtime << endl; 66 | } 67 | 68 | Cd & Cd::operator=(const Cd & d) 69 | { 70 | strncpy(performers, d.performers, len_performer); 71 | strncpy(label, d.label, len_label); 72 | selections = d.selections; 73 | playtime = d.playtime; 74 | return *this; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /Chapter13/pe13-3.cpp: -------------------------------------------------------------------------------- 1 | // pe13-3.cpp -- uses baseDMA, lacksDMA, and hasDMA objects 2 | // This is exercise 3 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // compile with dma2.cpp 4 | 5 | #include 6 | #include "dma2.h" 7 | 8 | const int ArrLength = 3; 9 | const int LEN = 40; 10 | 11 | int main(void) 12 | { 13 | using namespace std; 14 | 15 | abcDMA * arr[ArrLength]; 16 | 17 | char label[LEN]; 18 | char style[LEN]; 19 | char color[LEN]; 20 | int rating; 21 | int choice; 22 | for (int i = 0; i < ArrLength; i++) 23 | { 24 | cout << "For element " << i + 1 << ", enter the label: "; 25 | cin.getline(label, LEN); 26 | cout << "Enter the rating: "; 27 | cin >> rating; 28 | cout << "Enter 1 if this is to be a baseDMA object" << endl; 29 | cout << "Enter 2 if this is to be a hasDMA object" << endl; 30 | cout << "Enter 3 if this is to be a lacksDMA object" << endl; 31 | cin >> choice; 32 | cin.get(); // discard newline character 33 | switch (choice) 34 | { 35 | case 1: 36 | arr[i] = new baseDMA(label, rating); 37 | break; 38 | case 2: 39 | cout << "Enter the style: "; 40 | cin.getline(style, LEN); 41 | arr[i] = new hasDMA(style, label, rating); 42 | break; 43 | case 3: 44 | cout << "Enter color: "; 45 | cin.getline(color, LEN); 46 | arr[i] = new lacksDMA(color, label, rating); 47 | break; 48 | default: 49 | cout << "Unrecognized input, setting object "; 50 | cout << "to baseDMA"; 51 | arr[i] = new baseDMA(label, rating); 52 | break; 53 | } 54 | 55 | } 56 | // when loop terminates, display values 57 | cout << "Here's what you entered:" << endl; 58 | for (int i = 0; i < ArrLength; i++) 59 | { 60 | cout << "Element " << i + 1 << ":" << endl; 61 | arr[i]->View(); 62 | } 63 | 64 | for (int i = 0; i < ArrLength; i++) 65 | delete arr[i]; 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Chapter7/pe7-2.cpp: -------------------------------------------------------------------------------- 1 | // pe7-2.cpp -- reports the average of up to 10 golf scores 2 | // This is exercise 2 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | const int MAX = 10; // the maximum number of entries allowed 7 | 8 | // input 9 | int inputScores(int scores[], int max); 10 | 11 | // display 12 | void displayScores(const int scores[], int numScores); 13 | 14 | // calculate average 15 | double averageScore(const int scores[], int numScores); 16 | 17 | using namespace std; 18 | 19 | int main(void) 20 | { 21 | int arr[MAX]; 22 | int entries = inputScores(arr, MAX); 23 | 24 | if (entries) 25 | { 26 | displayScores(arr, entries); 27 | cout << "The average score is " << averageScore(arr, entries) << endl; 28 | } 29 | else 30 | cout << "There are no scores to display or calculate the average for." 31 | << endl << "Bye!" << endl; 32 | 33 | return 0; 34 | } 35 | 36 | // input 37 | int inputScores(int scores[], int max) 38 | { 39 | cout << "You may enter up to " << max << " scores" << endl; 40 | cout << "Enter score #1 (enter \"f\" when finished): "; 41 | 42 | int score; 43 | int count = 0; 44 | while (count < max && cin >> score) 45 | { 46 | scores[count++] = score; 47 | if (count < max) 48 | cout << "Enter score #" << count + 1 << " (enter \"f\" when finished): "; 49 | } 50 | return count; 51 | } 52 | 53 | // display 54 | void displayScores(const int scores[], int numScores) 55 | { 56 | cout << "The scores are as follows: "; 57 | int i = 0; 58 | while (i < numScores) 59 | cout << scores[i++] << " "; 60 | cout << endl; 61 | 62 | return; 63 | } 64 | 65 | // calculate average 66 | double averageScore(const int scores[], int numScores) 67 | { 68 | int sum = 0; 69 | int i = 0; 70 | while (i < numScores) 71 | { 72 | sum += scores[i++]; 73 | } 74 | return (double) sum / numScores; 75 | } 76 | -------------------------------------------------------------------------------- /Chapter12/string2.h: -------------------------------------------------------------------------------- 1 | // string2.h -- an upgraded version of string1.h from C++ Primer Plus 2 | // This is exercise 2 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | using std::ostream; 6 | using std::istream; 7 | 8 | #ifndef STRING2_H_ 9 | #define STRING2_H_ 10 | class String 11 | { 12 | private: 13 | char * str; // pointer to string 14 | int len; // length of string 15 | static int num_strings; // number of objects 16 | static const int CINLIM = 80; // cin input limit 17 | public: 18 | // constructors and other methods 19 | String(const char * s); // constructor 20 | String(); // default constructor 21 | String(const String &); // copy constructor 22 | ~String(); // destructor 23 | int length () const { return len; } 24 | void stringlow(); 25 | void stringup(); 26 | int has(const char c) const; 27 | // returns number of occurences of 'c' in String 28 | // overloaded operator methods 29 | String & operator=(const String &); 30 | String & operator=(const char *); 31 | char & operator[](int i); 32 | const char & operator[](int i) const; 33 | // overloaded operator friends 34 | friend bool operator<(const String &st1, const String &st2); 35 | friend bool operator>(const String &st1, const String &st2); 36 | friend bool operator==(const String &st1, const String &st2); 37 | friend ostream & operator<<(ostream & os, const String & st); 38 | friend istream & operator>>(istream & is, String & st); 39 | friend String operator+(const String &st1, const String &st2); 40 | friend String operator+(const char * st1, const String &st2); 41 | friend String operator+(const String &st1, const char * st2); 42 | // static function 43 | static int HowMany(); 44 | }; 45 | #endif 46 | -------------------------------------------------------------------------------- /Chapter9/golf.cpp: -------------------------------------------------------------------------------- 1 | // golf.cpp -- function definitions for golf header file 2 | // This is exercise 1 of chapter 9 in C++ Primer Plus by Stephen Prata 3 | // golf.cpp is accompanied by golf.h and questionOne.cpp 4 | 5 | #include "golf.h" 6 | #include 7 | #include 8 | 9 | // non-interactive version: 10 | // function sets golf structure to provided name, handicap 11 | // using values passed as arguments to the function 12 | void setgolf(golf & g, const char * name, int hc) 13 | { 14 | std::strcpy(g.fullname, name); 15 | g.handicap = hc; 16 | return; 17 | } 18 | 19 | // interactive version: 20 | // function solicits name and handicap from user 21 | // and sets the members of g to the values entered 22 | // returns 1 if name is entered, 0 if name is empty string 23 | int setgolf(golf & g) 24 | { 25 | using std::cout; 26 | using std::cin; 27 | using std::endl; 28 | 29 | cout << "Enter the player's full name (enter nothing to quit): "; 30 | cin.get(g.fullname, Len); 31 | if ((g.fullname)[0] == '\0') 32 | { 33 | cin.clear(); // clear error flags 34 | cin.get(); // discard newline character 35 | return 0; 36 | } 37 | else 38 | { 39 | while (cin.get() != '\n') 40 | continue; 41 | cout << "Enter the player's handicap: "; 42 | cin >> g.handicap; 43 | cin.get(); // discard newline character from buffer 44 | return 1; 45 | } 46 | } 47 | 48 | // function resets handicap to new value 49 | void handicap(golf & g, int hc) 50 | { 51 | g.handicap = hc; 52 | return; 53 | } 54 | 55 | // function displays contents of golf structure 56 | void showgolf(const golf & g) 57 | { 58 | using std::cout; 59 | using std::endl; 60 | 61 | cout << "Player name: " << g.fullname << endl; 62 | cout << "Player handicap: " << g.handicap << endl; 63 | return; 64 | } 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Chapter14/person.h: -------------------------------------------------------------------------------- 1 | // person.h -- Interface for Person class and derived classes 2 | // This is exercise 4 of chapter 14 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #ifndef PERSON_H_ 5 | #define PERSON_H_ 6 | 7 | #include 8 | using std::string; 9 | 10 | class Person 11 | { 12 | private: 13 | string firstname; 14 | string lastname; 15 | protected: 16 | virtual void Data() const; 17 | public: 18 | Person(const string & first = "", const string & last = ""); 19 | virtual ~Person() {} 20 | void setFirstName(const string & s) {firstname = s;} 21 | void setLastName(const string & s) {lastname = s;} 22 | virtual void Show() const; 23 | virtual void Set(); 24 | }; 25 | 26 | class Gunslinger : virtual public Person 27 | { 28 | private: 29 | double drawtime; 30 | int notches; 31 | protected: 32 | void Data() const; 33 | virtual void setData(); 34 | public: 35 | Gunslinger(string first = "", string last = "", double d = 0, 36 | int n = 0); 37 | ~Gunslinger() {} 38 | void setDrawTime(double d); 39 | void setNotches(int n); 40 | double Draw() const {return drawtime;} 41 | int Notches() const {return notches;} 42 | virtual void Show() const; 43 | virtual void Set() {Person::Set(); Gunslinger::setData();} 44 | }; 45 | 46 | class PokerPlayer : virtual public Person 47 | { 48 | public: 49 | PokerPlayer(string first = "", string last = ""); 50 | ~PokerPlayer() {} 51 | int Draw() const {return rand()%52;} 52 | virtual void Show() const; 53 | virtual void Set() {Person::Set();} 54 | }; 55 | 56 | class BadDude : public Gunslinger, public PokerPlayer 57 | { 58 | public: 59 | BadDude(string first = "", string last = "", double d = 0, 60 | int n = 0); 61 | ~BadDude() {} 62 | double Gdraw() const {return Gunslinger::Draw();} // returns draw time 63 | int Cdraw() const {return PokerPlayer::Draw();} // returns next card drawn 64 | virtual void Show() const; 65 | void Set(); 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /Chapter15/sales.cpp: -------------------------------------------------------------------------------- 1 | // sales.cpp -- Sales implementation 2 | // this code was written by Stephen Prata and used in pe15-4.cpp 3 | #include "sales.h" 4 | 5 | Sales::bad_index::bad_index(int ix, const char * s ) 6 | : std::logic_error(s), bi(ix) 7 | { 8 | } 9 | 10 | Sales::Sales(int yy) 11 | { 12 | year = yy; 13 | for (int i = 0; i < MONTHS; ++i) 14 | gross[i] = 0; 15 | } 16 | 17 | Sales::Sales(int yy, const double * gr, int n) 18 | { 19 | year = yy; 20 | int lim = (n < MONTHS)? n : MONTHS; 21 | int i; 22 | for (i = 0; i < lim; ++i) 23 | gross[i] = gr[i]; 24 | // for i > n and i < MONTHS 25 | for ( ; i < MONTHS; ++i) 26 | gross[i] = 0; 27 | } 28 | 29 | double Sales::operator[](int i) const throw(std::logic_error) 30 | { 31 | if(i < 0 || i >= MONTHS) 32 | throw bad_index(i); 33 | return gross[i]; 34 | } 35 | 36 | double & Sales::operator[](int i) throw(std::logic_error) 37 | { 38 | if(i < 0 || i >= MONTHS) 39 | throw bad_index(i); 40 | return gross[i]; 41 | } 42 | 43 | LabeledSales::nbad_index::nbad_index(const char * lb, int ix, 44 | const char * s ) : Sales::bad_index(ix, s) 45 | { 46 | std::strcpy(lbl, lb); 47 | } 48 | 49 | LabeledSales::LabeledSales(const char * lb, int yy) 50 | : Sales(yy) 51 | { 52 | std::strcpy(label, lb); 53 | } 54 | 55 | LabeledSales::LabeledSales(const char * lb, int yy, const double * gr, int n) 56 | : Sales(yy, gr, n) 57 | { 58 | std::strcpy(label, lb); 59 | } 60 | 61 | double LabeledSales::operator[](int i) const throw(std::logic_error) 62 | { 63 | if(i < 0 || i >= MONTHS) 64 | throw nbad_index(Label(), i); 65 | return Sales::operator[](i); 66 | } 67 | 68 | double & LabeledSales::operator[](int i) throw(std::logic_error) 69 | { 70 | if(i < 0 || i >= MONTHS) 71 | throw nbad_index(Label(), i); 72 | return Sales::operator[](i); 73 | } 74 | -------------------------------------------------------------------------------- /Chapter11/stonewt3.h: -------------------------------------------------------------------------------- 1 | // stonewt3.h -- modified version of stonewt.h 2 | // This is exercise 6 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // see stonewt3.h or listing 11.16 in C++ Primer Plus for the original 4 | 5 | #ifndef STONEWT3_H_ 6 | #define STONEWT3_H_ 7 | class Stonewt 8 | { 9 | private: 10 | enum {Lbs_per_stn = 14}; // pounds per stone 11 | int stone; // whole stones 12 | double pds_left; // fractional pounds 13 | double pounds; // entire weight in pounds 14 | public: 15 | Stonewt(double lbs); // constructor for double pounds 16 | Stonewt(int stn, double lbs); // constructor for stone, lbs 17 | Stonewt(); // default constructor 18 | ~Stonewt(); 19 | void show_lbs() const; 20 | void show_stn() const; 21 | friend bool operator==(const Stonewt & s1, const Stonewt & s2); 22 | friend bool operator!=(const Stonewt & s1, const Stonewt & s2); 23 | friend bool operator>=(const Stonewt & s1, const Stonewt & s2); 24 | friend bool operator>(const Stonewt & s1, const Stonewt & s2); 25 | friend bool operator<=(const Stonewt & s1, const Stonewt & s2); 26 | friend bool operator<(const Stonewt & s1, const Stonewt & s2); 27 | friend std::ostream & operator<<(std::ostream & os, 28 | const Stonewt & s); 29 | }; 30 | #endif 31 | 32 | // the equality operators are == >= <= > < and != 33 | 34 | // stonewt.h -- definition for the Stonewt class 35 | // 36 | //#ifndef STONEWT_H_ 37 | //#define STONEWT_H_ 38 | //class Stonewt 39 | //{ 40 | //private: 41 | // enum {Lbs_per_stn = 14}; // pounds per stone 42 | // int stone; // whole stones 43 | // double pds_left; // fractional pounds 44 | // double pounds; // entire weight in pounds 45 | //public: 46 | // Stonewt(double lbs); // constructor for double pounds 47 | // Stonewt(int stn, double lbs); // constructor for stone, lbs 48 | // Stonewt(); // default constructor 49 | // ~Stonewt(); 50 | // void show_lbs() const; 51 | // void show_stn() const; 52 | //}; 53 | //#endif 54 | -------------------------------------------------------------------------------- /Chapter13/classic2.cpp: -------------------------------------------------------------------------------- 1 | // classic2.cpp -- implementation of the Classic class with dynamic memory 2 | // This is exercise 2 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #include "classic2.h" 5 | #include 6 | #include 7 | 8 | Classic::Classic() : Cd() 9 | { 10 | primary_work = NULL; 11 | } 12 | 13 | Classic::Classic(const char * primary, Cd & d) : Cd(d) 14 | { 15 | if (NULL == primary) 16 | { 17 | primary_work = NULL; 18 | } 19 | else 20 | { 21 | int len = strlen(primary); 22 | primary_work = new char[len + 1]; 23 | strcpy(primary_work, primary); 24 | } 25 | 26 | } 27 | 28 | Classic::Classic(const char * primary, const char * s1, 29 | const char * s2, int n, double x) : Cd(s1, s2, n, x) 30 | { 31 | if (NULL == primary) 32 | { 33 | primary_work = NULL; 34 | } 35 | else 36 | { 37 | int len = strlen(primary); 38 | primary_work = new char[len + 1]; 39 | strcpy(primary_work, primary); 40 | } 41 | } 42 | 43 | Classic::Classic(const Classic & c) : Cd(c) 44 | { 45 | if (NULL == c.primary_work) 46 | { 47 | primary_work = NULL; 48 | } 49 | else 50 | { 51 | int len = strlen(c.primary_work); 52 | primary_work = new char[len + 1]; 53 | strcpy(primary_work, c.primary_work); 54 | } 55 | } 56 | 57 | Classic::~Classic() 58 | { 59 | delete [] primary_work; 60 | } 61 | 62 | void Classic::Report() const 63 | { 64 | using std::cout; 65 | using std::endl; 66 | 67 | Cd::Report(); 68 | cout << "Primary Work: " << primary_work << endl; 69 | } 70 | 71 | Classic & Classic::operator=(const Classic & c) 72 | { 73 | if (this == &c) 74 | return *this; 75 | else 76 | { 77 | Cd::operator=(c); 78 | delete [] primary_work; 79 | if (NULL == c.primary_work) 80 | { 81 | primary_work = NULL; 82 | } 83 | else 84 | { 85 | int len = strlen(c.primary_work); 86 | primary_work = new char[len + 1]; 87 | strcpy(primary_work, c.primary_work); 88 | } 89 | return *this; 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /Chapter8/pe8-4.cpp: -------------------------------------------------------------------------------- 1 | // pe8-4.cpp -- uses a function to dynamically allocate memory to 2 | // copy a string and gives pointer to said string to a structure. 3 | // This is exercise 4 of chapter 10 in C++ Primer Plus by Stephen Prata 4 | 5 | #include 6 | using namespace std; 7 | #include // for strlen(), strcpy() 8 | struct stringy 9 | { 10 | char * str; 11 | int ct; 12 | }; 13 | 14 | // prototypes for set(), show(), and show() 15 | void set(stringy & x, char str[]); 16 | void show(const stringy & x, int repeat = 1); 17 | void show(const char str[], int repeat = 1); 18 | 19 | int main(void) 20 | { 21 | stringy beany; 22 | char testing[] = "Reality isn't what it used to be."; 23 | 24 | set(beany, testing); // first argument is a reference, 25 | // allocates space to hold copy of testing, 26 | // sets str member of beany to point to the 27 | // new block, copies testing to new block, 28 | // and sets ct member of beany 29 | show(beany); // prints member string once 30 | show(beany, 2); // prints member string twice 31 | testing[0] = 'D'; 32 | testing[1] = 'u'; 33 | show(testing); // prints string once 34 | show(testing, 3); // prints testing string thirce 35 | show("Done!"); 36 | 37 | delete [] beany.str; 38 | return 0; 39 | } 40 | 41 | 42 | // create dynamic array of char, copy string to location and assign 43 | // location to structure 44 | void set(stringy & x, char str[]) 45 | { 46 | int length = strlen(str); 47 | char * pstr = new char[length + 1]; 48 | strcpy(pstr, str); 49 | 50 | x.str = pstr; 51 | x.ct = length; 52 | return; 53 | } 54 | 55 | // prints member string a variable number of times 56 | void show(const stringy & x, int repeat) 57 | { 58 | for (int i = 0; i < repeat; i++) 59 | cout << x.str << endl; 60 | return; 61 | } 62 | 63 | // prints string a variable number of times 64 | void show(const char str[], int repeat) 65 | { 66 | for (int i = 0; i < repeat; i++) 67 | cout << str << endl; 68 | return; 69 | } 70 | -------------------------------------------------------------------------------- /Chapter11/stonewt2.h: -------------------------------------------------------------------------------- 1 | // stonewt2.h -- modified version of stonewt.h 2 | // This is exercise 5 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // see stonewt2.h or listing 11.16 in C++ Primer Plus for the original 4 | 5 | #ifndef STONEWT2_H_ 6 | #define STONEWT2_H_ 7 | class Stonewt 8 | { 9 | /* 10 | Class invariants: 11 | state == '1' || state == '2' || state == '3' 12 | */ 13 | 14 | private: 15 | enum {Lbs_per_stn = 14}; // pounds per stone 16 | int stone; // whole stones 17 | double pds_left; // fractional pounds 18 | double pounds; // entire weight in pounds 19 | char state; 20 | // '1' corresponds to stone form 21 | // '2' corresponds to int pound form 22 | // '3' corresponds to floating pound form 23 | public: 24 | Stonewt(double lbs, char st = '1'); // constructor for double pounds 25 | Stonewt(int stn, double lbs, char st = '1'); 26 | Stonewt(); // default constructor 27 | ~Stonewt(); 28 | void set_state(char st); 29 | friend Stonewt operator+(const Stonewt & s1, const Stonewt & s2); 30 | friend Stonewt operator-(const Stonewt & s1, const Stonewt & s2); 31 | friend Stonewt operator*(const Stonewt & s, double d); 32 | friend Stonewt operator*(double d, const Stonewt & s); 33 | friend std::ostream & operator<<(std::ostream & os, 34 | const Stonewt & s); 35 | }; 36 | #endif 37 | 38 | 39 | 40 | // stonewt.h -- definition for the Stonewt class 41 | // 42 | //#ifndef STONEWT_H_ 43 | //#define STONEWT_H_ 44 | //class Stonewt 45 | //{ 46 | //private: 47 | // enum {Lbs_per_stn = 14}; // pounds per stone 48 | // int stone; // whole stones 49 | // double pds_left; // fractional pounds 50 | // double pounds; // entire weight in pounds 51 | //public: 52 | // Stonewt(double lbs); // constructor for double pounds 53 | // Stonewt(int stn, double lbs); // constructor for stone, lbs 54 | // Stonewt(); // default constructor 55 | // ~Stonewt(); 56 | // void show_lbs() const; 57 | // void show_stn() const; 58 | //}; 59 | //#endif 60 | -------------------------------------------------------------------------------- /Chapter15/tv.cpp: -------------------------------------------------------------------------------- 1 | // tv.cpp -- methods for the Tv class (Remote methods are inline) 2 | // This is exercise 1 of chapter 15 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: this program is a modification of the tv.cpp file that is listing 15.2 4 | 5 | #include 6 | #include "tv.h" 7 | 8 | bool Tv::volup() 9 | { 10 | if (volume < MaxVal) 11 | { 12 | volume++; 13 | return true; 14 | } 15 | else 16 | return false; 17 | } 18 | bool Tv::voldown() 19 | { 20 | if (volume > MinVal) 21 | { 22 | volume--; 23 | return true; 24 | } 25 | else 26 | return false; 27 | } 28 | 29 | void Tv::chanup() 30 | { 31 | if (channel < maxchannel) 32 | channel++; 33 | else 34 | channel = 1; 35 | } 36 | 37 | void Tv::chandown() 38 | { 39 | if (channel > 1) 40 | channel--; 41 | else 42 | channel = maxchannel; 43 | } 44 | 45 | void Tv::settings() const 46 | { 47 | using std::cout; 48 | using std::endl; 49 | cout << "TV is " << (state == Off? "Off" : "On") << endl; 50 | if (state == On) 51 | { 52 | cout << "Volume setting = " << volume << endl; 53 | cout << "Channel setting = " << channel << endl; 54 | cout << "Mode = " 55 | << (mode == Antenna? "antenna" : "cable") << endl; 56 | cout << "Input = " 57 | << (input == TV? "TV" : "VCR") << endl; 58 | } 59 | } 60 | 61 | void Tv::toggleRemoteMode(Remote & r) const 62 | { 63 | if (On == state && r.mode_i == Remote::Interactive) 64 | r.mode_i = Remote::Normal; // is this OK? 65 | else if (On == state && r.mode_i == Remote::Normal) 66 | r.mode_i = Remote::Interactive; 67 | else 68 | return; 69 | } 70 | 71 | void Remote::settings() const 72 | { 73 | using std::cout; 74 | using std::endl; 75 | cout << "Remote is set to the " << 76 | (mode_d == Tv::TV ? "TV" : "VCR"); 77 | cout << " and is in " << 78 | (mode_i == Normal ? "Normal" : "Interactive") << " mode" << endl; 79 | } 80 | -------------------------------------------------------------------------------- /Chapter13/pe13-4.cpp: -------------------------------------------------------------------------------- 1 | // pe13-4.cpp -- tests Port and VintagePort classes 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // this is not part of the question but used to make sure that 4 | // the Port and VintagePort classes work 5 | // compile with port.cpp and vintageport.cpp 6 | 7 | #include 8 | #include "port.h" 9 | #include "vintageport.h" 10 | const int LEN = 50; 11 | const int arrSize = 2; 12 | 13 | int main(void) 14 | { 15 | using namespace std; 16 | 17 | char brand[LEN]; 18 | char style[LEN]; 19 | int bottles; 20 | char nickname[LEN]; 21 | int year; 22 | int choice; 23 | Port * arr[arrSize]; 24 | 25 | for (int i = 0; i < arrSize; i++) 26 | { 27 | cout << "Object " << i + 1 << endl; 28 | cout << "Enter the brand for the object: "; 29 | cin.getline(brand, LEN); 30 | cout << "Enter the number of bottles: "; 31 | cin >> bottles; 32 | cin.get(); // discard newline character 33 | cout << "Enter 1 if you would like this to be a Port object" << endl; 34 | cout << "Enter 2 if you would like this to be a Vintage Port object"; 35 | cout << endl; 36 | cin >> choice; 37 | cin.get(); // discard newline character 38 | 39 | switch (choice) 40 | { 41 | case 1: 42 | cout << "Enter the style: "; 43 | cin.getline(style, LEN); 44 | arr[i] = new Port(brand, style, bottles); 45 | break; 46 | case 2: 47 | cout << "Enter the nickname: "; 48 | cin.getline(nickname, LEN); 49 | cout << "Enter the year: "; 50 | cin >> year; 51 | cin.get(); // discard newline character 52 | arr[i] = new VintagePort(brand, bottles, nickname, year); 53 | break; 54 | default: 55 | cout << "Input not recognized. Making a default Port object"; 56 | cout << endl; 57 | arr[i] = new Port(); 58 | break; 59 | } 60 | } 61 | 62 | cout << "Here's our array:" << endl; 63 | for (int i = 0; i < arrSize; i++) 64 | { 65 | cout << "Element 1: " << endl; 66 | arr[i]->Show(); 67 | } 68 | cout << "Bye!" << endl; 69 | 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Chapter10/pe10-5.cpp: -------------------------------------------------------------------------------- 1 | // pe10-5.cpp - uses the customerStack class 2 | // This is exercise 5 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // questionFive.cpp is accompanied by customerStack.h and customerStack.cpp 4 | 5 | #include 6 | #include "customerStack.h" 7 | 8 | // to 9 | 10 | int main(void) 11 | { 12 | // place using directives here 13 | using std::cin; 14 | using std::cout; 15 | using std::endl; 16 | 17 | // initialize our custumers 18 | customer Jimmy = {"Jimmy", 100}; 19 | customer Timmy = {"Timmy", 39.50}; 20 | customer Minny = {"Minny", 47.36}; 21 | customer Vinny = {"Vinny", 18.01}; 22 | 23 | cout << "We have four customers: " << endl; 24 | cout << Jimmy.fullname << ", paid $" 25 | << Jimmy.payment << endl; 26 | cout << Timmy.fullname << ", paid $" 27 | << Timmy.payment << endl; 28 | cout << Minny.fullname << ", paid $" 29 | << Minny.payment << endl; 30 | cout << Vinny.fullname << ", paid $" 31 | << Vinny.payment << endl; 32 | 33 | cout << "Let's push Jimmy" << endl; 34 | 35 | // push customers 36 | CustomerStack s; 37 | if (s.push(Jimmy)) 38 | cout << Jimmy.fullname << " pushed successfully!" 39 | << endl; 40 | else 41 | cout << Jimmy.fullname << " not pushed successfully" 42 | << endl; 43 | 44 | double total = 0.0; 45 | cout << "Here is our running total: $" << total << endl; 46 | 47 | cout << "Let's pop Jimmy" << endl; 48 | 49 | // pop customers 50 | customer c; 51 | if (s.pop(c)) 52 | { 53 | cout << "Customer " << c.fullname << " popped." 54 | << endl; 55 | total += c.payment; 56 | } 57 | else 58 | cout << "No customers to pop" << endl; 59 | 60 | cout << "Let's pop another customer" << endl; 61 | 62 | // let's pop one last customer 63 | if (s.pop(c)) 64 | { 65 | cout << "Customer " << c.fullname << " popped." 66 | << endl; 67 | total += c.payment; 68 | } 69 | else 70 | cout << "No customers to pop" << endl; 71 | 72 | cout << "Here is our new running total: $" << total 73 | << endl; 74 | 75 | return 0; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /Chapter15/tv.h: -------------------------------------------------------------------------------- 1 | // tv.h -- Tv and Remote classes 2 | // This is exercise 1 of chapter 15 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: this program is a modification of the tv.h file that is listing 15.1 4 | 5 | #ifndef TV_H_ 6 | #define TV_H_ 7 | 8 | class Remote; 9 | class Tv 10 | { 11 | public: 12 | friend class Remote; // Remote can access Tv private parts 13 | enum {Off, On}; 14 | enum {MinVal,MaxVal = 20}; 15 | enum {Antenna, Cable}; 16 | enum {TV, VCR}; 17 | 18 | Tv(int s = Off, int mc = 100) : state(s), volume(5), 19 | maxchannel(mc), channel(2), mode(Cable), input(TV) {} 20 | void onoff() {state = (state == On)? Off : On;} 21 | bool ison() const {return state == On;} 22 | bool volup(); 23 | bool voldown(); 24 | void chanup(); 25 | void chandown(); 26 | void set_mode() {mode = (mode == Antenna)? Cable : Antenna;} 27 | void set_input() {input = (input == TV)? VCR : TV;} 28 | void settings() const; // display all settings 29 | void toggleRemoteMode(Remote & r) const; 30 | private: 31 | int state; // on or off 32 | int volume; // assumed to be digitized 33 | int maxchannel; // maximum number of channels 34 | int channel; // current channel setting 35 | int mode; // broadcast or cable 36 | int input; // TV or VCR 37 | }; 38 | 39 | class Remote 40 | { 41 | private: 42 | int mode_d; // controls TV or VCR 43 | int mode_i; // interactive or normal mode 44 | public: 45 | friend class Tv; 46 | enum {Normal, Interactive}; 47 | void settings() const; 48 | Remote(int md = Tv::TV, int mi = Normal) : mode_d(md), mode_i(mi) {} 49 | bool volup(Tv & t) { return t.volup();} 50 | bool voldown(Tv & t) { return t.voldown();} 51 | void onoff(Tv & t) { t.onoff(); } 52 | void chanup(Tv & t) {t.chanup();} 53 | void chandown(Tv & t) {t.chandown();} 54 | void set_chan(Tv & t, int c) {t.channel = c;} 55 | void set_mode(Tv & t) {t.set_mode();} 56 | void set_input(Tv & t) {t.set_input();} 57 | }; 58 | #endif 59 | -------------------------------------------------------------------------------- /Chapter12/stack2.cpp: -------------------------------------------------------------------------------- 1 | // stack2.cpp -- implementation of Stack ADT class (stack2.h) 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // compile with pe12_4.cpp 4 | 5 | #include "stack2.h" 6 | #include // for std::cerr 7 | 8 | Stack::Stack(int n) // creates stack with n elements 9 | { 10 | if (n < 1) 11 | { 12 | std::cerr << "Error. Argument must be a positive integer. " 13 | << "Setting n to 10." << std::endl; 14 | n = 10; 15 | } 16 | pitems = new Item[n]; 17 | size = top = 0; 18 | } 19 | 20 | Stack::Stack(const Stack & st) 21 | { 22 | pitems = new Item[st.size]; 23 | for (int i = 0; i < st.size; i++) 24 | pitems[i] = (st.pitems)[i]; 25 | size = st.size; 26 | top = st.top; 27 | } 28 | 29 | Stack::~Stack() 30 | { 31 | delete [] pitems; 32 | } 33 | 34 | bool Stack::isempty() const 35 | { 36 | return top == 0; 37 | } 38 | 39 | bool Stack::isfull() const 40 | { 41 | return top == MAX; 42 | } 43 | 44 | bool Stack::push(const Item & item) 45 | { 46 | if (top < MAX) 47 | { 48 | pitems[top++] = item; 49 | size++; 50 | return true; 51 | } 52 | else 53 | return false; 54 | } 55 | 56 | bool Stack::pop(Item & item) 57 | { 58 | if (top > 0) 59 | { 60 | item = pitems[--top]; 61 | size--; 62 | return true; 63 | } 64 | else 65 | return false; 66 | } 67 | 68 | Stack & Stack::operator=(const Stack & st) 69 | { 70 | if (this == &st) 71 | return *this; 72 | delete [] pitems; 73 | size = st.size; 74 | top = st.top; 75 | pitems = new Item[size]; 76 | for (int i = 0; i < size; i++) 77 | pitems[i] = (st.pitems)[i]; 78 | return *this; 79 | } 80 | 81 | std::ostream & operator<<(std::ostream & os, const Stack & s) 82 | { 83 | using std::endl; 84 | for (int i = 0; i < s.top; i++) 85 | { 86 | os << "item " << i + 1 << ": " << (s.pitems)[i] << endl; 87 | } 88 | return os; 89 | } 90 | -------------------------------------------------------------------------------- /Chapter13/dma2.h: -------------------------------------------------------------------------------- 1 | // dma2.h -- uses an ABC for baseDMA, hasDMA, lacksDMA class methods 2 | // This is exercise 3 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | 5 | #ifndef DMA2_H_ 6 | #define DMA2_H_ 7 | #include 8 | 9 | // Abstract base class 10 | class abcDMA 11 | { 12 | private: 13 | char * label; 14 | int rating; 15 | protected: 16 | const char * showLabel() const {return label;} 17 | int showRating() const {return rating;} 18 | public: 19 | abcDMA(const char * l = "NULL", int r = 0); 20 | abcDMA(const abcDMA & a); 21 | virtual void View() const = 0; // pure virtual function 22 | virtual ~abcDMA(); 23 | abcDMA & operator=(const abcDMA & a); 24 | friend std::ostream & operator<<(std::ostream & os, 25 | const abcDMA & rs); 26 | }; 27 | 28 | // Base Class Using DMA 29 | class baseDMA : public abcDMA 30 | { 31 | private: 32 | public: 33 | baseDMA(const char * l = "null", int r = 0); 34 | baseDMA(const baseDMA & rs); 35 | virtual ~baseDMA(); 36 | baseDMA & operator=(const baseDMA & rs); 37 | virtual void View() const; 38 | friend std::ostream & operator<<(std::ostream & os, 39 | const baseDMA & rs); 40 | }; 41 | 42 | // derived class without DMA 43 | // no destructor needed 44 | // uses implicit copy constructor 45 | // uses implicit assignment operator 46 | class lacksDMA :public abcDMA 47 | { 48 | private: 49 | enum { COL_LEN = 40}; 50 | char color[COL_LEN]; 51 | public: 52 | lacksDMA(const char * c = "blank", const char * l = "null", 53 | int r = 0); 54 | friend std::ostream & operator<<(std::ostream & os, 55 | const lacksDMA & rs); 56 | virtual void View() const; 57 | }; 58 | 59 | // derived class with DMA 60 | class hasDMA : public abcDMA 61 | { 62 | private: 63 | char * style; 64 | public: 65 | hasDMA(const char * s = "none", const char * l = "null", 66 | int r = 0); 67 | hasDMA(const hasDMA & hs); 68 | ~hasDMA(); 69 | hasDMA & operator=(const hasDMA & rs); 70 | friend std::ostream & operator<<(std::ostream & os, 71 | const hasDMA & rs); 72 | virtual void View() const; 73 | }; 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /Chapter13/port.cpp: -------------------------------------------------------------------------------- 1 | // port.cpp -- the Port class implementation 2 | // This is exercise 4 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include "port.h" 7 | using std::ostream; 8 | 9 | 10 | Port::Port(const char * br, const char * st, int b) 11 | { 12 | using std::cout; 13 | using std::endl; 14 | 15 | brand = new char[strlen(br) + 1]; 16 | strcpy(brand, br); 17 | brand[strlen(br)] = '\0'; 18 | 19 | strncpy(style, st, 19); 20 | style[20] = '\0'; 21 | 22 | if (b < 0) 23 | { 24 | cout << "Number of bottles must be a positive integer." << endl; 25 | cout << "Setting bottles to 0." << endl; 26 | bottles = 0; 27 | } 28 | else 29 | bottles = b; 30 | } 31 | 32 | Port::Port(const Port & p) // copy constructor 33 | { 34 | brand = new char[strlen(p.brand) + 1]; 35 | strcpy(brand, p.brand); 36 | strcpy(style, p.style); 37 | bottles = p.bottles; 38 | } 39 | 40 | Port & Port::operator=(const Port & p) 41 | { 42 | if (this == &p) 43 | return *this; 44 | else 45 | { 46 | delete [] brand; 47 | brand = new char[strlen(p.brand) + 1]; 48 | strcpy(brand, p.brand); 49 | strcpy(style, p.style); 50 | bottles = p.bottles; 51 | } 52 | return *this; 53 | } 54 | 55 | Port & Port::operator+=(int b) // adds b to bottles 56 | { 57 | bottles += b; 58 | return *this; 59 | } 60 | 61 | Port & Port::operator-=(int b) // subtracts b from bottles, if available 62 | { 63 | using std::cout; 64 | using std::endl; 65 | 66 | if (b > bottles) 67 | { 68 | cout << "You cannot subtract more bottles than you have!" << endl; 69 | cout << "Subtracting " << bottles << " bottles." << endl; 70 | bottles = 0; 71 | } 72 | else 73 | bottles -= b; 74 | 75 | return *this; 76 | } 77 | 78 | void Port::Show() const 79 | { 80 | using std::cout; 81 | using std::endl; 82 | 83 | cout << "Brand: " << brand << endl; 84 | cout << "Kind: " << style << endl; 85 | cout << "Bottles: " << bottles << endl; 86 | } 87 | 88 | ostream & operator<<(ostream & os, const Port & p) 89 | { 90 | os << p.brand << ", " << p.style << ", " << p.bottles; 91 | return os; 92 | } 93 | 94 | -------------------------------------------------------------------------------- /Chapter9/pe9-2.cpp: -------------------------------------------------------------------------------- 1 | // pe9-2.cpp -- revision of Listing 9.8 (static.cpp) so that it 2 | // uses string objects and not char arrays. 3 | // This is exercise 2 of chapter 9 in C++ Primer Plus by Stephen Prata 4 | 5 | #include 6 | #include // this library is no longer necessary 7 | using std::string; 8 | 9 | // function prototype 10 | void strcount(const string str); 11 | 12 | int main() 13 | { 14 | using namespace std; 15 | string input; 16 | 17 | cout << "Enter a line:\n"; 18 | getline(cin, input); 19 | while (input != "") 20 | { 21 | strcount(input); 22 | cout << "Enter next line (empty line to quit):\n"; 23 | getline(cin, input); 24 | } 25 | cout << "Bye\n"; 26 | return 0; 27 | } 28 | 29 | void strcount(const string str) 30 | { 31 | using std::cout; 32 | static int total = 0; 33 | 34 | cout << "\"" << str << "\" contains "; 35 | total += str.size(); 36 | cout << str.size() << " characters\n"; 37 | cout << total << " characters total\n"; 38 | return; 39 | } 40 | 41 | // Below is the original listing 9.9 42 | // static.cpp -- using a static local variable 43 | //#include 44 | // constants 45 | //const int ArSize = 10; 46 | // 47 | // function prototype 48 | //void strcount(const char * str); 49 | // 50 | //int main() 51 | //{ 52 | // using namespace std; 53 | // char input[ArSize]; 54 | // char next; 55 | // 56 | // cout << "Enter a line:\n"; 57 | // cin.get(input, ArSize); 58 | // while (cin) 59 | // { 60 | // cin.get(next); 61 | // while (next != '\n') // string didn't fit! 62 | // cin.get(next); 63 | // strcount(input); 64 | // cout << "Enter next line (empty line to quit):\n"; 65 | // cin.get(input, ArSize); 66 | // } 67 | // cout << "Bye\n"; 68 | // return 0; 69 | //} 70 | // 71 | //void strcount(const char * str) 72 | //{ 73 | // using namespace std; 74 | // static int total = 0; // static local variable 75 | // int count = 0; // automatic local variable 76 | // 77 | // cout << "\"" << str << "\" contains "; 78 | // while (*str++) // go to end of string 79 | // count++; 80 | // total += count; 81 | // cout << count << " characters\n"; 82 | // cout << total << " characters total\n"; 83 | //} 84 | -------------------------------------------------------------------------------- /Chapter15/pe15-2.cpp: -------------------------------------------------------------------------------- 1 | // pe15-2.cpp -- Uses exceptions to throw logic_error derived objects 2 | // This is exercise 2 of chapter 15 in C++ Primer Plus 5e by Stephen Prata 3 | // Note: This is a modified version of error4.cpp or listing 15.11 4 | #include 5 | #include // or math.h, unix users may need -lm flag 6 | #include 7 | double hmean(double a, double b) throw(std::logic_error); 8 | double gmean(double a, double b) throw(std::logic_error); 9 | int main() 10 | { 11 | using std::cout; 12 | using std::cin; 13 | using std::endl; 14 | using std::logic_error; 15 | using std::invalid_argument; 16 | using std::domain_error; 17 | 18 | double x, y, z; 19 | 20 | cout << "Enter two numbers: "; 21 | while (cin >> x >> y) 22 | { 23 | try { // start of try block 24 | z = hmean(x,y); 25 | cout << "Harmonic mean of " << x << " and " << y 26 | << " is " << z << endl; 27 | cout << "Geometric mean of " << x << " and " << y 28 | << " is " << gmean(x,y) << endl; 29 | cout << "Enter next set of numbers : "; 30 | }// end of try block 31 | catch (invalid_argument & ia) // start of catch block 32 | { 33 | cout << ia.what() << endl; 34 | cout << "Try again.\n"; 35 | continue; 36 | } 37 | catch (domain_error & de) 38 | { 39 | cout << de.what() << endl; 40 | cout << "Sorry, you don't get to play any more.\n"; 41 | break; 42 | } // end of catch block 43 | } 44 | cout << "Bye!\n"; 45 | return 0; 46 | } 47 | 48 | double hmean(double a, double b) throw(std::logic_error) 49 | { 50 | if (a == -b) 51 | throw std::invalid_argument 52 | ("Error in hmean(): values are equal"); 53 | return 2.0 * a * b / (a + b); 54 | } 55 | 56 | double gmean(double a, double b) throw(std::logic_error) 57 | { 58 | if (a < 0 || b < 0) 59 | throw std::domain_error 60 | ("Error in gmean(): both values must be positive"); 61 | return std::sqrt(a * b); 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Chapter11/stonewt3.cpp: -------------------------------------------------------------------------------- 1 | // stonewt3.cpp -- modified version of stonewt.cpp 2 | // This is exercise 6 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // see stonewt3.cpp or listing 11.17 in C++ Primer Plus for the original 4 | 5 | #include 6 | using std::cout; 7 | #include "stonewt3.h" 8 | 9 | // construct Stonewt object from double value 10 | Stonewt::Stonewt(double lbs) 11 | { 12 | stone = int (lbs) / Lbs_per_stn; // integer division 13 | pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs); 14 | pounds = lbs; 15 | } 16 | 17 | // construct Stonewt object from stone, double values 18 | Stonewt::Stonewt(int stn, double lbs) 19 | { 20 | stone = stn; 21 | pds_left = lbs; 22 | pounds = stn * Lbs_per_stn + lbs; 23 | } 24 | 25 | Stonewt::Stonewt() // default constructor, wt = 0 26 | { 27 | stone = pounds = pds_left = 0; 28 | } 29 | 30 | Stonewt::~Stonewt() // destructor 31 | { 32 | } 33 | 34 | // show weight in stones 35 | void Stonewt::show_stn() const 36 | { 37 | cout << stone << " stone, " << pds_left << " pounds\n"; 38 | } 39 | 40 | // show weight in pounds 41 | void Stonewt::show_lbs() const 42 | { 43 | cout << pounds << " pounds\n"; 44 | } 45 | 46 | bool operator==(const Stonewt & s1, const Stonewt & s2) 47 | { 48 | if (s1.pounds == s2.pounds) 49 | return true; 50 | else 51 | return false; 52 | } 53 | 54 | bool operator!=(const Stonewt & s1, const Stonewt & s2) 55 | { 56 | return !(s1 == s2); 57 | } 58 | 59 | bool operator>=(const Stonewt & s1, const Stonewt & s2) 60 | { 61 | if (s1.pounds >= s2.pounds) 62 | return true; 63 | else 64 | return false; 65 | } 66 | 67 | bool operator>(const Stonewt & s1, const Stonewt & s2) 68 | { 69 | if (s1.pounds > s2.pounds) 70 | return true; 71 | else 72 | return false; 73 | } 74 | 75 | bool operator<=(const Stonewt & s1, const Stonewt & s2) 76 | { 77 | if (s1.pounds <= s2.pounds) 78 | return true; 79 | else 80 | return false; 81 | } 82 | 83 | bool operator<(const Stonewt & s1, const Stonewt & s2) 84 | { 85 | if (s1.pounds < s2.pounds) 86 | return true; 87 | else 88 | return false; 89 | } 90 | 91 | std::ostream & operator<<(std::ostream & os, 92 | const Stonewt & s) 93 | { 94 | os << s.pounds; 95 | return os; 96 | } 97 | 98 | -------------------------------------------------------------------------------- /Chapter7/pe7-6.cpp: -------------------------------------------------------------------------------- 1 | // pe7-6.cpp -- uses functions to fill an array, display the contents of an array, and reverse the elements of an array 2 | // This is exercise 6 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | int Fill_array(double arr[], int size); 9 | void Show_array(const double arr[], int size); 10 | void Reverse_array(double arr[], int size); 11 | 12 | const int ARR_SIZE = 10; 13 | 14 | int main(void) 15 | { 16 | double arr[ARR_SIZE]; 17 | int entries = Fill_array(arr, ARR_SIZE); 18 | 19 | // show, reverse, show, reverse all but first/last elements, show 20 | cout << endl << "Here is the array:" << endl; 21 | Show_array(arr, entries); 22 | cout << "Let's reverse the array." << endl; 23 | Reverse_array(arr, entries); 24 | cout << "Here is our new array:" << endl; 25 | Show_array(arr, entries); 26 | cout << "Let's reverse all except the first and last elements." << endl; 27 | Reverse_array(arr + 1, entries - 2); 28 | cout << "Here is our final array:" << endl; 29 | Show_array(arr, entries); 30 | 31 | return 0; 32 | } 33 | 34 | // Don't forget to define any functions that you created 35 | 36 | int Fill_array(double arr[], int size) 37 | { 38 | // Every array item is initially set to 0 so that you don't get any strange numbers from unfilled array values. 39 | for (size_t j = 0; j < size; j++) 40 | { 41 | arr[j] = 0; 42 | } 43 | cout << "You may enter up to " << size << " values." << endl; 44 | cout << "Enter value number 1 (or type \"q\" when finished): "; 45 | int count = 0; 46 | while (count < size && cin >> arr[count]) 47 | { 48 | if (++count < size) 49 | cout << "Enter value number " << count + 1 << " (or type \"q\" when finished): "; 50 | } 51 | return count; 52 | } 53 | 54 | void Show_array(const double arr[], int size) 55 | { 56 | cout << endl; 57 | for (int i = 0; i < size; i++) 58 | cout << "Element number " << i + 1 << " = " << arr[i] << endl; 59 | cout << endl; 60 | return; 61 | } 62 | 63 | void Reverse_array(double arr[], int size) 64 | { 65 | int temp; 66 | for (int i = 0; i < size / 2; i++) 67 | { 68 | temp = arr[i]; 69 | arr[i] = arr[(size - 1) - i]; 70 | arr[(size - 1) - i] = temp; 71 | } 72 | return; 73 | } 74 | -------------------------------------------------------------------------------- /Chapter6/pe6-6.cpp: -------------------------------------------------------------------------------- 1 | // pe6-6.cpp -- tracks the contributions by storing data in an array of structures. Program then displays donors. 2 | // This is exercise 6 of chapter 6 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int Contribution = 10000; 9 | struct donors 10 | { 11 | string name; 12 | double amount; 13 | }; 14 | 15 | int main(void) 16 | { 17 | // ask the user for number of donors 18 | cout << "How many donors are there? "; 19 | int numberDonors; 20 | cin >> numberDonors; 21 | cin.get(); 22 | 23 | // dynamically create array of that size of structures 24 | 25 | donors * list = new donors[numberDonors]; 26 | int donationSizes[numberDonors]; 27 | 28 | // for loop to initialize structure 29 | 30 | for (int i = 0; i < numberDonors; i++) 31 | { 32 | cout << "Donor number " << i + 1 << endl 33 | << "Name: "; 34 | getline(cin, (list + i)->name, '\n'); 35 | cout << "Contribution: "; 36 | cin >> (list + i)->amount; 37 | cin.get(); 38 | 39 | // Store information on if donation surpasses Contribution 40 | if ((list + i)->amount >= Contribution) 41 | donationSizes[i] = 1; 42 | else 43 | donationSizes[i] = 0; 44 | } 45 | 46 | // for loop to list highest donors and contributions (Grand Patrons), print none if there are 0 members 47 | cout << "List of Grand Patrons: " << endl; 48 | int counter = 0; // to determine if there are any patrons in either group 49 | for (int i = 0; i < numberDonors; i++) 50 | { 51 | if (donationSizes[i] == 1) 52 | { 53 | cout << (list + i)->name << ", $" << (list + i)->amount << endl; 54 | counter = 1; 55 | } 56 | } 57 | 58 | // print "None" if no patrons are in list of Grand Patrons 59 | if (!counter) 60 | cout << "None" << endl; 61 | 62 | // for loop to list remaining donors (Patrons), print none there are 0 members 63 | counter = 0; 64 | cout << "List of remaining patrons:" << endl; 65 | for (int i = 0; i < numberDonors; i++) 66 | { 67 | if (donationSizes[i] == 0) 68 | { 69 | cout << (list + i)->name << endl; 70 | counter = 1; 71 | } 72 | } 73 | 74 | // print "None" if no people are in list of remaining patrons 75 | if (!counter) 76 | cout << "None" << endl; 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Chapter7/pe7-3.cpp: -------------------------------------------------------------------------------- 1 | // pe7-3.cpp -- uses two functions that pass a structure by value and by address 2 | // This is exercise 3 of chapter 7 in C++ Primer Plus by Stephen Prata 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct box 9 | { 10 | char maker[40]; 11 | float height; 12 | float width; 13 | float length; 14 | float volume; 15 | }; 16 | 17 | void displayStructure(box x); 18 | void setVolume(box * y); 19 | 20 | 21 | int main(void) 22 | { 23 | cout << "Enter the maker: "; 24 | box myBox; 25 | cin.get(myBox.maker, 40); 26 | 27 | // if I use the getline function the program doesn't work. The only difference is that get does not extract the delimiter. I don't understand why this makes a difference if the input is longer than 40 characters. This is the only way I can get the program to account for input longer than 40 characters. 28 | 29 | while(cin.get() != '\n') 30 | continue; 31 | 32 | cout << "Enter the height: "; 33 | cin >> myBox.height; 34 | cout << "Enter the width: "; 35 | cin >> myBox.width; 36 | cout << "Enter the length: "; 37 | cin >> myBox.length; 38 | cout << "Enter the volume: "; 39 | cin >> myBox.volume; 40 | 41 | // display contents of myBox 42 | cout << endl << "This is what you entered: " << endl; 43 | displayStructure(myBox); 44 | 45 | // reset volume if volume does not match other dimensions 46 | if (myBox.volume != myBox.height * myBox.width * myBox.length) 47 | { 48 | cout << endl << "It looks like the volume does not correspond to the dimensions appropriately. " 49 | << "Let me fix that for you." << endl; 50 | setVolume(&myBox); 51 | cout << "Here are the new corrected dimensions: " << endl; 52 | displayStructure(myBox); 53 | } 54 | 55 | cout << endl; 56 | return 0; 57 | } 58 | 59 | // display the contents of the structure 60 | void displayStructure(box x) 61 | { 62 | cout << "Maker: " << x.maker << endl; 63 | cout << "Height: " << x.height << endl; 64 | cout << "Width: " << x.width << endl; 65 | cout << "Length: " << x.length << endl; 66 | cout << "Volume: " << x.volume << endl; 67 | return; 68 | } 69 | 70 | // set the volume member to height*width*length 71 | void setVolume(box * y) 72 | { 73 | y->volume = y->height * y->width * y->length; 74 | return; 75 | } 76 | -------------------------------------------------------------------------------- /Chapter15/pe15-4.cpp: -------------------------------------------------------------------------------- 1 | // pe15-4.cpp -- a modified version of listing 15.16 or use_sales.cpp 2 | // This is exercise 4 of chapter 15 in C++ Primer Plus 5e by Stephen Prata 3 | // compile with sales.cpp 4 | 5 | #include 6 | #include "sales.h" 7 | #include // for typeid 8 | 9 | int main() 10 | { 11 | using std::cout; 12 | using std::cin; 13 | using std::endl; 14 | 15 | double vals1[12] = 16 | { 17 | 1220, 1100, 1122, 2212, 1232, 2334, 18 | 2884, 2393, 3302, 2922, 3002, 3544 19 | }; 20 | 21 | double vals2[12] = 22 | { 23 | 12, 11, 22, 21, 32, 34, 24 | 28, 29, 33, 29, 32, 35 25 | }; 26 | 27 | Sales sales1(2004, vals1, 12); 28 | LabeledSales sales2("Blogstar",2005, vals2, 12 ); 29 | 30 | cout << "First try block:\n"; 31 | try 32 | { 33 | int i; 34 | cout << "Year = " << sales1.Year() << endl; 35 | for (i = 0; i < 12; ++i) 36 | { 37 | 38 | cout << sales1[i] << ' '; 39 | if (i % 6 == 5) 40 | cout << endl; 41 | } 42 | cout << "Year = " << sales2.Year() << endl; 43 | cout << "Label = " << sales2.Label() << endl; 44 | for (i = 0; i <= 12; ++i) 45 | { 46 | cout << sales2[i] << ' '; 47 | if (i % 6 == 5) 48 | cout << endl; 49 | } 50 | cout << "End of try block 1.\n"; 51 | } 52 | catch(Sales::bad_index & bad) 53 | { 54 | cout << bad.what(); 55 | if ( typeid(LabeledSales::nbad_index &) == typeid(bad) ) 56 | { 57 | LabeledSales::nbad_index & nbi = 58 | dynamic_cast(bad); 59 | cout << "Company: " << nbi.label_val() << endl; 60 | } 61 | cout << "bad index: " << bad.bi_val() << endl; 62 | } 63 | cout << "\nNext try block:\n"; 64 | try 65 | { 66 | sales2[2] = 37.5; 67 | sales1[20] = 23345; 68 | cout << "End of try block 2.\n"; 69 | } 70 | catch(Sales::bad_index & bad) 71 | { 72 | cout << bad.what(); 73 | if ( typeid(LabeledSales::nbad_index &) == typeid(bad) ) 74 | { 75 | LabeledSales::nbad_index & nbi = 76 | dynamic_cast(bad); 77 | cout << "Company: " << nbi.label_val() << endl; 78 | } 79 | cout << "bad index: " << bad.bi_val() << endl; 80 | } 81 | cout << "done\n"; 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Chapter17/pe17-4.cpp: -------------------------------------------------------------------------------- 1 | // pe17-4.cpp -- description here 2 | // This is exercise 4 of chapter 17 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char * argv[]) 9 | { 10 | using namespace std; 11 | 12 | // check to see that appropriate files exist 13 | if (argc < 4) 14 | { 15 | cerr << "Error, three arguments representing filenames are missing" 16 | << endl; 17 | exit(EXIT_FAILURE); 18 | } 19 | else if (argc > 4) 20 | cerr << "More arguments than necessary are detected" << endl 21 | << "Only the first three arguments will be used" << endl; 22 | 23 | cout << "Warning: this will erase the contents of " << argv[3] 24 | << ". Proceed? (y/n) "; 25 | 26 | char ch; 27 | cin >> ch; 28 | if (ch != 'y' && ch !='Y') 29 | return 0; 30 | else 31 | { 32 | ifstream infile_first; 33 | infile_first.open(argv[1]); 34 | if (!infile_first.is_open()) 35 | { 36 | cerr << "Error, could not open " << argv[1] << endl; 37 | exit(EXIT_FAILURE); 38 | } 39 | 40 | ifstream infile_second; 41 | infile_second.open(argv[2]); 42 | if (!infile_second.is_open()) 43 | { 44 | cerr << "Error, could not open " << argv[2] << endl; 45 | exit(EXIT_FAILURE); 46 | } 47 | 48 | ofstream outfile; 49 | outfile.open(argv[3]); 50 | if (!outfile.is_open()) 51 | { 52 | cerr << "Error, could not open " << argv[3] << endl; 53 | exit(EXIT_FAILURE); 54 | } 55 | 56 | while (!infile_first.eof() && !infile_second.eof()) 57 | { 58 | while(infile_first.get(ch) && ch != '\n') 59 | outfile.put(ch); 60 | outfile.put(' '); 61 | while(infile_second.get(ch) && ch != '\n') 62 | outfile.put(ch); 63 | outfile.put('\n'); 64 | // In event that file is terminated by \n then EOF, rather than EOF 65 | infile_first.peek(); // modifies flags if error occurs 66 | infile_second.peek(); 67 | } 68 | if (infile_first.eof() && !infile_second.eof()) 69 | { 70 | while(infile_second.get(ch) && ch != '\n') 71 | outfile.put(ch); 72 | outfile.put('\n'); 73 | } 74 | else if (infile_second.eof() && !infile_first.eof()) 75 | { 76 | while(infile_first.get(ch) && ch != '\n') 77 | outfile.put(ch); 78 | outfile.put('\n'); 79 | } 80 | 81 | infile_first.close(); 82 | infile_second.close(); 83 | outfile.close(); 84 | return 0; 85 | } 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Chapter12/Cow.cpp: -------------------------------------------------------------------------------- 1 | // Cow.cpp -- implementation of the Cow class 2 | // This is exercise 1 of chapter 12 in C++ Primer Plus by Stephen Prata 3 | // compile with questionOne.cpp 4 | 5 | #include "Cow.h" // interface for Cow class 6 | #include // for strlen(), strncpy(), and strcpy() 7 | #include // for cout and endl 8 | 9 | Cow::Cow() 10 | { 11 | using std::cout; 12 | using std::endl; 13 | 14 | name[0] = '\0'; 15 | hobby = NULL; 16 | weight = 0; 17 | 18 | cout << "Default constructor used" << endl; 19 | } 20 | 21 | Cow::Cow(const char * nm, const char * ho, double wt) 22 | { 23 | using std::strncpy; 24 | using std::strcpy; 25 | using std::strlen; 26 | using std::cout; 27 | using std::endl; 28 | 29 | strncpy(name, nm, 20); 30 | name[19] = '\0'; 31 | hobby = new char[strlen(ho) + 1]; 32 | strcpy(hobby, ho); 33 | if (wt > 0) 34 | weight = wt; 35 | else 36 | weight = 0; 37 | 38 | cout << "Explicit constructor used to create " << name << endl; 39 | } 40 | 41 | Cow::Cow(const Cow & c) 42 | { 43 | using std::strcpy; 44 | using std::strlen; 45 | using std::cout; 46 | using std::endl; 47 | 48 | strcpy(name, c.name); 49 | if (NULL == c.hobby) 50 | hobby = NULL; 51 | else 52 | { 53 | hobby = new char[strlen(c.hobby) + 1]; 54 | strcpy(hobby, c.hobby); 55 | } 56 | weight = c.weight; 57 | 58 | cout << "Copy constructor used to create " << name << endl; 59 | } 60 | 61 | Cow::~Cow() 62 | { 63 | using std::cout; 64 | using std::endl; 65 | 66 | delete [] hobby; 67 | cout << name << " destroyed" << endl; 68 | } 69 | 70 | Cow & Cow::operator=(const Cow & c) 71 | { 72 | using std::strcpy; 73 | using std::strlen; 74 | using std::cout; 75 | using std::endl; 76 | 77 | if (this == &c) 78 | return *this; 79 | 80 | strcpy(name, c.name); 81 | if (NULL == c.hobby) 82 | hobby = NULL; 83 | else 84 | { 85 | hobby = new char[strlen(c.hobby) + 1]; 86 | strcpy(hobby, c.hobby); 87 | } 88 | weight = c.weight; 89 | 90 | cout << "Assignment operator used on " << name << endl; 91 | return *this; 92 | } 93 | 94 | void Cow::ShowCow() const 95 | { 96 | using std::cout; 97 | using std::endl; 98 | 99 | cout << "Name: " << name << endl; 100 | cout << "Hobby: " << (NULL == hobby ? "NULL" : hobby) << endl; 101 | cout << "Weight: " << weight << endl; 102 | } 103 | -------------------------------------------------------------------------------- /Chapter17/pe17-5.cpp: -------------------------------------------------------------------------------- 1 | // pe17-5.cpp -- description here 2 | // This is exercise 5 of chapter 17 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | void check(ifstream & infile, string filename); 15 | void check(ofstream & infile, string filename); 16 | bool stringComp(string s1, string s2); // case insensitive < comparison 17 | bool stringEqual(string s1, string s2); // case insensitive == comparison 18 | 19 | int main(void) 20 | { 21 | 22 | ifstream patfile("pat.dat"); 23 | ifstream matfile("mat.dat"); 24 | check(patfile, "pat.dat"); 25 | check(matfile, "mat.dat"); 26 | string name; 27 | vector patlist; 28 | vector matlist; 29 | 30 | while (getline(patfile, name) && name != "") 31 | patlist.push_back(name); 32 | while (getline(matfile, name) && name != "") 33 | matlist.push_back(name); 34 | 35 | vector matnpat_list; 36 | back_insert_iterator< vector > iiter(matnpat_list); 37 | copy(patlist.begin(), patlist.end(), iiter); 38 | copy(matlist.begin(), matlist.end(), iiter); 39 | sort(matnpat_list.begin(), matnpat_list.end(), stringComp); 40 | vector::iterator iv = 41 | unique(matnpat_list.begin(), matnpat_list.end(), stringEqual); 42 | matnpat_list.erase(iv, matnpat_list.end()); 43 | ostream_iterator os_iter (cout, "\n"); 44 | 45 | cout << "Here is your list of guests: " << endl; 46 | copy(matnpat_list.begin(), matnpat_list.end(), os_iter); 47 | 48 | return 0; 49 | } 50 | 51 | void check(ifstream & infile, string filename) 52 | { 53 | if (!infile.is_open()) 54 | { 55 | cerr << "Error, cannot open " << filename << endl; 56 | exit(EXIT_FAILURE); 57 | } 58 | } 59 | 60 | void check(ofstream & infile, string filename) 61 | { 62 | if (!infile.is_open()) 63 | { 64 | cerr << "Error, cannot open " << filename << endl; 65 | exit(EXIT_FAILURE); 66 | } 67 | } 68 | 69 | bool stringComp(string s1, string s2) 70 | { 71 | transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 72 | transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 73 | return s1 < s2; 74 | } 75 | 76 | bool stringEqual(string s1, string s2) 77 | { 78 | transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 79 | transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 80 | return s1 == s2; 81 | } 82 | -------------------------------------------------------------------------------- /Chapter17/pe17-7.cpp: -------------------------------------------------------------------------------- 1 | // pe17-7.cpp -- description here 2 | // This is exercise 7 of chapter 17 in C++ Primer Plus 5e by Stephen Prata 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Store 13 | { 14 | private: 15 | ofstream * pfout; 16 | public: 17 | Store(ofstream & fout) {pfout = &fout;} 18 | ~Store() {} 19 | Store & operator()(const string & str) 20 | { 21 | size_t len = str.length(); 22 | pfout->write((char *) &len, sizeof(size_t)); 23 | pfout->write(str.data(), len); 24 | return *this; 25 | } 26 | }; 27 | 28 | void ShowStr(const std::string & str); 29 | void GetStrs(std::ifstream & fin, std::vector & vistr); 30 | 31 | int main(void) 32 | { 33 | using namespace std; 34 | vector vostr; 35 | string temp; 36 | 37 | // acquire strings 38 | cout << "Enter strings (empty line to quit):\n"; 39 | while (getline(cin, temp) && temp[0] != '\0') 40 | vostr.push_back(temp); 41 | cout << "Here is your input.\n"; 42 | for_each(vostr.begin(), vostr.end(), ShowStr); 43 | 44 | // store in a file 45 | ofstream fout("strings.dat", ios_base::out | ios_base::binary); 46 | for_each(vostr.begin(), vostr.end(), Store(fout)); 47 | fout.close(); 48 | 49 | // recover file contents 50 | vector vistr; 51 | ifstream fin("strings.dat", ios_base::in | ios_base::binary); 52 | if (!fin.is_open()) 53 | { 54 | cerr << "Could not open file for input.\n"; 55 | exit(EXIT_FAILURE); 56 | } 57 | GetStrs(fin, vistr); 58 | cout << "\nHere are the strings read from the file:\n"; 59 | for_each(vistr.begin(), vistr.end(), ShowStr); 60 | 61 | return 0; 62 | } 63 | 64 | void ShowStr(const std::string & str) 65 | { 66 | using std::cout; 67 | using std::endl; 68 | cout << str << endl; 69 | } 70 | 71 | void GetStrs(ifstream & fin, vector & vistr) 72 | { 73 | size_t len; 74 | string str; 75 | char ch; 76 | // check that there is something to read 77 | while (fin.peek() && !fin.eof()) 78 | { 79 | fin.read((char *) &len, sizeof(size_t)); 80 | for (size_t i = 0; i < len; i++) 81 | { 82 | fin.read(&ch, sizeof(char)); 83 | str.push_back(ch); 84 | } 85 | vistr.push_back(str); 86 | str.clear(); 87 | } 88 | } 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /Chapter11/stonewt2.cpp: -------------------------------------------------------------------------------- 1 | // stonewt2.cpp -- modified version of stonewt.cpp 2 | // This is exercise 5 of chapter 11 in C++ Primer Plus by Stephen Prata 3 | // see stonewt2.cpp or listing 11.17 in C++ Primer Plus for the original 4 | 5 | #include 6 | using std::cout; 7 | #include "stonewt2.h" 8 | 9 | // construct Stonewt object from double value 10 | Stonewt::Stonewt(double lbs, char st) 11 | { 12 | stone = int (lbs) / Lbs_per_stn; // integer division 13 | pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs); 14 | pounds = lbs; 15 | 16 | if ('1' == st || '2' == st || '3' == st) 17 | state = st; 18 | else 19 | { 20 | cout << "State must be '1', '2', or '3'. "; 21 | cout << "Setting state to '1'\n"; 22 | state = '1'; 23 | } 24 | } 25 | 26 | // construct Stonewt object from stone, double values 27 | Stonewt::Stonewt(int stn, double lbs, char st) 28 | { 29 | stone = stn; 30 | pds_left = lbs; 31 | pounds = stn * Lbs_per_stn + lbs; 32 | 33 | if ('1' == st || '2' == st || '3' == st) 34 | state = st; 35 | else 36 | { 37 | cout << "State must be '1', '2', or '3'. "; 38 | cout << "Setting state to '1'\n"; 39 | state = '1'; 40 | } 41 | } 42 | 43 | Stonewt::Stonewt() // default constructor, wt = 0 44 | { 45 | stone = pounds = pds_left = 0; 46 | state = '1'; 47 | } 48 | 49 | Stonewt::~Stonewt() // destructor 50 | { 51 | } 52 | 53 | void Stonewt::set_state(char st) 54 | { 55 | if ('1' == st || '2' == st || '3' == st) 56 | state = st; 57 | else 58 | { 59 | cout << "State must be '1', '2', or '3'. "; 60 | cout << "Setting state to '1'\n"; 61 | state = '1'; 62 | } 63 | } 64 | 65 | Stonewt operator+(const Stonewt & s1, const Stonewt & s2) 66 | { 67 | double tot_pounds = s1.pounds + s2.pounds; 68 | return Stonewt(tot_pounds); 69 | } 70 | 71 | Stonewt operator-(const Stonewt & s1, const Stonewt & s2) 72 | { 73 | double tot_pounds = s1.pounds - s2.pounds; 74 | return Stonewt(tot_pounds); 75 | } 76 | 77 | Stonewt operator*(const Stonewt & s, double d) 78 | { 79 | double tot_pounds = s.pounds * d; 80 | return Stonewt(tot_pounds); 81 | } 82 | 83 | Stonewt operator*(double d, const Stonewt & s) 84 | { 85 | return s * d; 86 | } 87 | 88 | std::ostream & operator<<(std::ostream & os, const Stonewt & s) 89 | { 90 | if ('1' == s.state) 91 | os << s.stone << " stone, " << s.pds_left << " pounds"; 92 | else if ('2' == s.state) 93 | os << int (s.pounds) << " pounds"; 94 | else if ('3' == s.state) 95 | os << s.pounds << " pounds"; 96 | 97 | return os; 98 | } 99 | -------------------------------------------------------------------------------- /Chapter9/pe9-1.cpp: -------------------------------------------------------------------------------- 1 | // pe9-1.cpp -- uses the golf header file 2 | // This is exercise 1 of chapter 9 in C++ Primer Plus by Stephen Prata 3 | // pe9-1.cpp is accompanied by golf.h and golf.cpp 4 | 5 | #include 6 | #include "golf.h" 7 | 8 | const int size = 5; 9 | 10 | int main(void) 11 | { 12 | using std::cout; 13 | using std::cin; 14 | using std::endl; 15 | 16 | // players are entered and cataloged 17 | cout << "You may enter up to " << size << " players." << endl; 18 | int players = 0; 19 | golf list[size]; 20 | while (players < size && setgolf(list[players])) 21 | { 22 | players++; 23 | } 24 | 25 | // players are displayed 26 | cout << "Here are your players: " << endl; 27 | if (players > 0) 28 | { 29 | for (int i = 0; i < players; i++) 30 | { 31 | showgolf(list[i]); 32 | } 33 | 34 | // user enters new handicap value 35 | cout << "Enter a number, please: "; 36 | int newHandicap; 37 | cin.clear(); 38 | 39 | while(!(cin >> newHandicap)) 40 | { 41 | cin.clear(); 42 | while (cin.get() != '\n') 43 | continue; 44 | cout << "Try again: "; 45 | } 46 | 47 | // all players have handicap value reset to that given by 48 | // the user and players are redisplayed 49 | cout << "Here are your players with their new handicaps: " 50 | << endl; 51 | for (int i = 0; i < players; i++) 52 | { 53 | handicap(list[i], newHandicap); 54 | showgolf(list[i]); 55 | } 56 | } 57 | else 58 | { 59 | cout << "No players on file." << endl; 60 | } 61 | 62 | cout << "Before you go, would you like to make " 63 | << "one more player (yes or no): "; 64 | char ch; 65 | cin.get(ch); 66 | while (cin.get() != '\n') 67 | continue; 68 | if (ch == 'y' || ch == 'Y') 69 | { 70 | char lastPlayerName[Len]; 71 | int lastPlayerHandicap; 72 | cout << "Enter the player name: "; 73 | 74 | cin.get(lastPlayerName, Len); 75 | while (cin.get() != '\n') 76 | continue; 77 | 78 | cout << "Enter the player handicap: "; 79 | while(!(cin >> lastPlayerHandicap)) 80 | { 81 | cin.clear(); 82 | while (cin.get() != '\n') 83 | continue; 84 | cout << "Try again: "; 85 | } 86 | 87 | golf lastPlayer; 88 | setgolf(lastPlayer, lastPlayerName, lastPlayerHandicap); 89 | cout << "Here's what you entered: " << endl; 90 | showgolf(lastPlayer); 91 | } 92 | 93 | return 0; 94 | } 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /Chapter12/queue.cpp: -------------------------------------------------------------------------------- 1 | // queue.cpp -- Queue and Customer methods 2 | // Note: this code is listing 12.11 and was given for the problem 3 | // This is exercise 5 of chapter 12 in C++ Primer Plus by Stephen Prata 4 | // compile with pe12_5.cpp 5 | 6 | #include "queue.h" 7 | #include // (or stdlib.h) for rand() 8 | 9 | // Queue methods 10 | Queue::Queue(int qs) : qsize(qs) 11 | { 12 | front = rear = NULL; 13 | items = 0; 14 | } 15 | 16 | Queue::~Queue() 17 | { 18 | Node * temp; 19 | while (front != NULL) // while queue is not yet empty 20 | { 21 | temp = front; // save address of front item 22 | front = front->next;// reset pointer to next item 23 | delete temp; // delete former front 24 | } 25 | } 26 | 27 | bool Queue::isempty() const 28 | { 29 | return items == 0; 30 | } 31 | 32 | bool Queue::isfull() const 33 | { 34 | return items == qsize; 35 | } 36 | 37 | int Queue::queuecount() const 38 | { 39 | return items; 40 | } 41 | 42 | // Add item to queue 43 | bool Queue::enqueue(const Item & item) 44 | { 45 | if (isfull()) 46 | return false; 47 | Node * add = new Node; // create node 48 | if (add == NULL) 49 | return false; // quit if none available 50 | add->item = item; // set node pointers 51 | add->next = NULL; 52 | items++; 53 | if (front == NULL) // if queue is empty, 54 | front = add; // place item at front 55 | else 56 | rear->next = add; // else place at rear 57 | rear = add; // have rear point to new node 58 | return true; 59 | } 60 | 61 | // Place front item into item variable and remove from queue 62 | bool Queue::dequeue(Item & item) 63 | { 64 | if (front == NULL) 65 | return false; 66 | item = front->item; // set item to first item in queue 67 | items--; 68 | Node * temp = front; // save location of first item 69 | front = front->next; // reset front to next item 70 | delete temp; // delete former first item 71 | if (items == 0) 72 | rear = NULL; 73 | return true; 74 | } 75 | 76 | // customer method 77 | 78 | // when is the time at which the customer arrives 79 | // the arrival time is set to when and the processing 80 | // time set to a random value in the range 1 - 3 81 | void Customer::set(long when) 82 | { 83 | processtime = std::rand() % 3 + 1; 84 | arrive = when; 85 | } 86 | -------------------------------------------------------------------------------- /Chapter10/pe10-8.cpp: -------------------------------------------------------------------------------- 1 | // pe10-8.cpp - uses the List class ADT 2 | // This is exercise 8 of chapter 10 in C++ Primer Plus by Stephen Prata 3 | // questionEight.cpp is accompanied by list.h and list.cpp 4 | 5 | #include 6 | #include "list.h" 7 | 8 | // prototypes for our functions (return void and take a reference) 9 | inline void doubleValue(double & x) {x = 2*x;} 10 | inline void square(double & x) {x = x*x;} 11 | inline void halve(double & x) {x = x/2;} 12 | inline void reciprocal(double & x) {x = 1/x;} 13 | 14 | int main(void) 15 | { 16 | // declare using directives 17 | using std::cout; 18 | using std::endl; 19 | using std::cin; 20 | 21 | // prompt user for values 22 | cout << "Using the List class, we can store up to \n" 23 | << "as many entries as we want, but for this \n" 24 | << "example, we limit it at 10" << endl; 25 | List l; 26 | 27 | // while (list not full || number not neg) 28 | // prompt and add values 29 | cout << "Enter a number (q to quit): "; 30 | double d; 31 | while (cin >> d && l.isFull() == 0) 32 | { 33 | l.add(d); 34 | if (l.isFull() == 0) 35 | cout << "Enter a number (q to quit): "; 36 | } 37 | cin.clear(); 38 | cin.ignore(256, '\n'); 39 | cout << "You have entered " << l.numberOfItems() 40 | << " items" << endl; 41 | 42 | // display items 43 | cout << "Here is your list: " << endl;; 44 | l.printList(); 45 | 46 | // while (q is not entered) 47 | // prompt user to choose a value from a menu of functions 48 | // display values of items 49 | cout << "Choose from the following (q to quit): " 50 | << endl; 51 | cout << "1. doubleValue() 2. square() \n" 52 | << "3. halve() 4. reciprocal() \n"; 53 | int choice; 54 | while (cin >> choice) 55 | { 56 | switch (choice) 57 | { 58 | case 1: 59 | l.visit(doubleValue); 60 | break; 61 | case 2: 62 | l.visit(square); 63 | break; 64 | case 3: 65 | l.visit(halve); 66 | break; 67 | case 4: 68 | l.visit(reciprocal); 69 | break; 70 | default: 71 | cout << "Not an option" << endl; 72 | break; 73 | } 74 | cout << "Here is your new list: " << endl; 75 | l.printList(); 76 | cout << "Choose from the following (q to quit): " 77 | << endl; 78 | cout << "1. doubleValue() 2. square() \n" 79 | << "3. halve() 4. reciprocal() \n"; 80 | } 81 | 82 | cout << "Bye!" << endl; 83 | return 0; 84 | } 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /Chapter10/golf2.cpp: -------------------------------------------------------------------------------- 1 | // golf2.cpp -- function definitions for Golf class 2 | // This is exercise 3 of chapter 9 in C++ Primer Plus by Stephen Prata 3 | // golf2.cpp is accompanied by golf2.h and questionThree.cpp 4 | 5 | #include "golf2.h" 6 | #include 7 | #include 8 | 9 | Golf::Golf() 10 | { 11 | } 12 | 13 | // non-interactive version: 14 | // function sets golf structure to provided name, handicap 15 | // using values passed as arguments to the function 16 | Golf::Golf(const char * fullname, int handicap) 17 | { 18 | if ('\0' == fullname[0]) 19 | return; 20 | else 21 | { 22 | std::strcpy(this->fullname, fullname); 23 | this->handicap = handicap; 24 | return; 25 | } 26 | } 27 | 28 | // interactive version: 29 | // function solicits name and handicap from user 30 | // and sets the members of g to the values entered 31 | // returns 1 if name is entered, 0 if name is empty string 32 | int Golf::setgolf() 33 | { 34 | // prompts user for values and calls a constructor 35 | // to create a temporary object which is assigned 36 | // to the object that called the function. 37 | 38 | using std::cout; 39 | using std::cin; 40 | 41 | cout << "Enter the player's full name (enter nothing to quit): "; 42 | 43 | // store values in local variables 44 | char fullname[Len]; 45 | int handicap; 46 | 47 | cin.get(fullname, Len); 48 | if ((fullname)[0] == '\0') 49 | { 50 | cin.clear(); // clear error flags 51 | cin.get(); // discard newline character 52 | return 0; 53 | } 54 | else 55 | { 56 | while (cin.get() != '\n') 57 | continue; 58 | cout << "Enter the player's handicap: "; 59 | 60 | while(!(cin >> handicap)) 61 | { 62 | cin.clear(); 63 | while(cin.get() != '\n') 64 | continue; 65 | cout << "Try again: "; 66 | } 67 | cin.get(); // discard newline character from buffer 68 | 69 | // create temporary object and assign it 70 | // to invoking object 71 | Golf player(fullname, handicap); 72 | *this = player; 73 | 74 | return 1; 75 | } 76 | } 77 | 78 | // function resets handicap to new value 79 | void Golf::setHandicap(int handicap) 80 | { 81 | this->handicap = handicap; 82 | return; 83 | } 84 | 85 | // function displays contents of golf structure 86 | void Golf::showgolf() const 87 | { 88 | using std::cout; 89 | using std::endl; 90 | 91 | cout << "Player name: " << this->fullname << endl; 92 | cout << "Player handicap: " << (this->handicap) << endl; 93 | return; 94 | } 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | --------------------------------------------------------------------------------