├── Section_10 ├── sect10_ex9a.txt ├── sect10_ex9b.txt ├── sect10_ex1.txt ├── sect10_ex9.txt ├── mydata.txt ├── sect10_ex5.cpp ├── sect10_ex11.txt ├── sect10_ex5_in.txt ├── Section_10.vcxproj.user ├── sect10_ex10_input.txt ├── sect10_ex2.txt ├── sect10_ex5_out.txt ├── sect10_ex4.txt ├── sect10_ex10_output.txt ├── sect10_drill1.cpp ├── sect10_ex8.txt ├── sect10_ex1.cpp ├── sect10_ex11.cpp ├── sect10_drill2.cpp ├── sect10_drill3.cpp ├── sect10_ex8.cpp ├── sect10_drill4.cpp ├── Section_10.vcxproj.filters ├── sect10_drill5.cpp ├── sect10_ex6.cpp └── sect10_ex9.cpp ├── Section_11 ├── sect11_ex12.txt ├── sect11_ex13.txt ├── sect11_ex3.cpp ├── sect11_ex6.cpp ├── sect11_ex7.cpp ├── sect11_ex8.cpp ├── Punct_stream.hpp ├── sect11_ex11.cpp ├── Section_11.vcxproj.user ├── sect11_2_2_try.cpp ├── sect11_ex1.cpp ├── sect11_ex1.txt ├── sect11_2_4_try.cpp ├── sect11_ex2.cpp ├── sect11_2_1_try.cpp ├── sect11_drill8.cpp ├── Section_11.vcxproj.filters ├── Punct_stream.cpp ├── sect11_ex10.cpp ├── sect11_drill9.cpp ├── sect11_2_5_try.cpp ├── sect11_drill10.cpp ├── sect11_ex9.cpp ├── sect11_ex9_bin ├── sect11_ex12.cpp └── sect11_ex13.cpp ├── PPP-style.pdf ├── Section_12 ├── image.jpg ├── snow_cpp.gif ├── sect12_ex2.cpp ├── sect12_drill2.cpp ├── sect12_drill3.cpp ├── utility.hpp ├── Section_12.vcxproj.user ├── Point.hpp ├── fltk_test_window.cpp ├── sect12_drill1.cpp ├── sect12_ex3.cpp ├── sect12_ex4.cpp ├── sect12_ex1.cpp ├── fltk.hpp ├── Simple_window.hpp ├── Window.hpp ├── Section_12.vcxproj.filters └── GUI.cpp ├── Section_7 ├── sect7_ex1.cpp ├── sect7_ex2.cpp ├── sect7_ex3.cpp ├── sect7_ex4.cpp ├── sect7_ex5.cpp ├── sect7_ex6.cpp ├── sect7_ex7.cpp ├── sect7_ex8.cpp ├── sect7_drill10.cpp ├── sect7_drill11.cpp ├── sect7_drill6.cpp ├── sect7_drill7_8.cpp ├── sect7_drill9.cpp ├── sect7_scratch.cpp ├── sect7_drill1_2_3_4_5.cpp └── Section_7.vcxproj.user ├── Section_8 ├── sect8_ex1.cpp ├── sect8_ex9.cpp ├── sect8_drill1 │ ├── my.hpp │ ├── my.cpp │ └── use.cpp ├── Section_8.vcxproj.user ├── sect8_ex2.cpp ├── sect8_ex14.cpp ├── sect8_drill3.cpp ├── sect8_ex10.cpp ├── sect8_ex5.cpp ├── symbol_table.cpp ├── Section_8.vcxproj.filters ├── sect8_ex3.cpp ├── symbol_table.hpp ├── sect8_ex12.cpp ├── sect8_ex13.cpp ├── sect8_ex6.cpp └── sect8_ex8.cpp ├── vcpkg.json ├── Section_5 ├── sect5_ex14.cpp ├── Section_5.vcxproj.user ├── scaffolding.cpp ├── sect5_6_try.cpp ├── sect5_3_2_try.cpp ├── sect5_7a_try.cpp ├── sect5_3_1_try.cpp ├── sect5_ex2.cpp ├── sect5_ex4.cpp ├── sect5_ex3.cpp ├── sect5_ex11.cpp ├── section5.hpp ├── sect5_7b_try.cpp └── sect5_ex6.cpp ├── Readme_captions ├── view_all.png └── pause_option.png ├── Section_4 ├── sect4_drill6.cpp ├── Section_4.vcxproj.user ├── sect4_4_try4.cpp ├── sect4_4_try3.cpp ├── sect4_5_try.cpp ├── sect4_ex18.cpp ├── Section_4.vcxproj.filters ├── section4.hpp ├── sect4_drill1.cpp ├── sect4_ex15.cpp ├── sect4_ex12.cpp ├── sect4_4_try1.cpp ├── sect4_ex13.cpp ├── sect4_ex21.cpp ├── sect4_ex3.cpp ├── sect4_ex11.cpp ├── sect4_6_try.cpp ├── sect4_ex20.cpp ├── sect4_drill2.cpp ├── sect4_ex14.cpp ├── sect4_ex6.cpp ├── sect4_ex19.cpp ├── sect4_drill3.cpp ├── sect4_ex2.cpp ├── sect4_ex5.cpp ├── sect4_ex17.cpp ├── sect4_drill4.cpp └── sect4_ex16.cpp ├── Section_3 ├── Section_3.vcxproj.user ├── sect3_7_try.cpp ├── sect3_4_try.cpp ├── sect3_ex8.cpp ├── sect3_ex2.cpp ├── sect3_3_try.cpp ├── section3.hpp ├── Section_3.vcxproj.filters ├── sect3_ex9.cpp ├── sect3_9_try.cpp ├── sect3_ex4.cpp ├── sect3_ex5.cpp ├── sect3_5_try.cpp ├── sect3_ex6.cpp └── sect3_ex7.cpp ├── .gitignore ├── Section_6 ├── sect6_ex5.txt ├── Section_6.vcxproj.user ├── sect6_ex7.txt └── section6.hpp ├── Section_2 ├── Section_2.vcxproj.user └── HelloWorld.cpp └── Section_9 ├── Section_9.vcxproj.user ├── Library.hpp ├── sect9_ex16.cpp ├── Rational.hpp ├── Money.hpp ├── sect9_ex8.cpp ├── Patron.hpp ├── sect9_ex10.cpp ├── Patron.cpp ├── sect9_ex6.cpp ├── sect9_ex5.cpp ├── sect9_ex7.cpp ├── sect9_ex14.cpp ├── Book.hpp ├── sect9_ex4.cpp ├── sect9_ex13.cpp ├── Book.cpp ├── sect9_ex12.cpp ├── sect9_ex15.cpp ├── sect9_ex11.cpp ├── Section_9.vcxproj.filters └── Rational.cpp /Section_10/sect10_ex9a.txt: -------------------------------------------------------------------------------- 1 | Hello World -------------------------------------------------------------------------------- /Section_11/sect11_ex12.txt: -------------------------------------------------------------------------------- 1 | lkjhgfdsa -------------------------------------------------------------------------------- /Section_10/sect10_ex9b.txt: -------------------------------------------------------------------------------- 1 | Is this great? -------------------------------------------------------------------------------- /Section_10/sect10_ex1.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 100 5 6 7 2 | -------------------------------------------------------------------------------- /Section_11/sect11_ex13.txt: -------------------------------------------------------------------------------- 1 | parrot Blue Norwegian -------------------------------------------------------------------------------- /Section_10/sect10_ex9.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | Is this great? 3 | -------------------------------------------------------------------------------- /Section_10/mydata.txt: -------------------------------------------------------------------------------- 1 | 1, 2 2 | 3, 4 3 | 5, 6 4 | 7, 8 5 | 9, 10 6 | 11, 12 7 | 13, 14 8 | -------------------------------------------------------------------------------- /PPP-style.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/PPP-style.pdf -------------------------------------------------------------------------------- /Section_12/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_12/image.jpg -------------------------------------------------------------------------------- /Section_12/snow_cpp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_12/snow_cpp.gif -------------------------------------------------------------------------------- /Section_7/sect7_ex1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex1.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex2.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex3.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex4.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex5.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex6.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex7.cpp -------------------------------------------------------------------------------- /Section_7/sect7_ex8.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_ex8.cpp -------------------------------------------------------------------------------- /Section_8/sect8_ex1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_8/sect8_ex1.cpp -------------------------------------------------------------------------------- /Section_8/sect8_ex9.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_8/sect8_ex9.cpp -------------------------------------------------------------------------------- /vcpkg.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ppp", 3 | "version-string": "0", 4 | "dependencies": [ 5 | "fltk" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /Section_10/sect10_ex5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_10/sect10_ex5.cpp -------------------------------------------------------------------------------- /Section_11/sect11_ex3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_11/sect11_ex3.cpp -------------------------------------------------------------------------------- /Section_11/sect11_ex6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_11/sect11_ex6.cpp -------------------------------------------------------------------------------- /Section_11/sect11_ex7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_11/sect11_ex7.cpp -------------------------------------------------------------------------------- /Section_11/sect11_ex8.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_11/sect11_ex8.cpp -------------------------------------------------------------------------------- /Section_12/sect12_ex2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_12/sect12_ex2.cpp -------------------------------------------------------------------------------- /Section_5/sect5_ex14.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_5/sect5_ex14.cpp -------------------------------------------------------------------------------- /Readme_captions/view_all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Readme_captions/view_all.png -------------------------------------------------------------------------------- /Section_11/Punct_stream.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_11/Punct_stream.hpp -------------------------------------------------------------------------------- /Section_11/sect11_ex11.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_11/sect11_ex11.cpp -------------------------------------------------------------------------------- /Section_12/sect12_drill2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_12/sect12_drill2.cpp -------------------------------------------------------------------------------- /Section_12/sect12_drill3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_12/sect12_drill3.cpp -------------------------------------------------------------------------------- /Section_4/sect4_drill6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_4/sect4_drill6.cpp -------------------------------------------------------------------------------- /Section_7/sect7_drill10.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_drill10.cpp -------------------------------------------------------------------------------- /Section_7/sect7_drill11.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_drill11.cpp -------------------------------------------------------------------------------- /Section_7/sect7_drill6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_drill6.cpp -------------------------------------------------------------------------------- /Section_7/sect7_drill7_8.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_drill7_8.cpp -------------------------------------------------------------------------------- /Section_7/sect7_drill9.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_drill9.cpp -------------------------------------------------------------------------------- /Section_7/sect7_scratch.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_scratch.cpp -------------------------------------------------------------------------------- /Readme_captions/pause_option.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Readme_captions/pause_option.png -------------------------------------------------------------------------------- /Section_7/sect7_drill1_2_3_4_5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp/HEAD/Section_7/sect7_drill1_2_3_4_5.cpp -------------------------------------------------------------------------------- /Section_10/sect10_ex11.txt: -------------------------------------------------------------------------------- 1 | Write a program that produces the sum of all the whitespace-separated integers in a text file. 2 | For example, bears: 17 elephants 9 end should output ? -------------------------------------------------------------------------------- /Section_8/sect8_drill1/my.hpp: -------------------------------------------------------------------------------- 1 | // Drill 1 of section 8 2 | 3 | #pragma once 4 | 5 | extern int foo; // declared global variable 6 | 7 | void print_foo(); 8 | 9 | void print( int ); 10 | -------------------------------------------------------------------------------- /Section_3/Section_3.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | -------------------------------------------------------------------------------- /Section_10/sect10_ex5_in.txt: -------------------------------------------------------------------------------- 1 | { year 1990 } 2 | { year 1991 3 | { month jun }} 4 | { year 1992 5 | { month jan (1 0 61.5)} 6 | { month feb (1 1 64 )(2 2 65.2)}} 7 | { year 2000 8 | { month feb (1 1 68)(2 3 66.66)(1 0 67.2) } 9 | { month dec (15 15 -9.2)(15 14 -8.8)(14 0 -2)}} 10 | -------------------------------------------------------------------------------- /Section_8/sect8_drill1/my.cpp: -------------------------------------------------------------------------------- 1 | // Drill 1 of section 8 2 | 3 | #include "my.hpp" 4 | #include "../../includes/ppp.hpp" 5 | 6 | int foo = 0; // initialize global variable 7 | 8 | void print_foo() 9 | { 10 | std::cout << foo << "\n"; 11 | } 12 | 13 | void print( int i ) 14 | { 15 | std::cout << i << "\n"; 16 | } 17 | -------------------------------------------------------------------------------- /Section_8/sect8_drill1/use.cpp: -------------------------------------------------------------------------------- 1 | // Drill 1 of section 8 2 | 3 | #include "my.hpp" 4 | #include 5 | 6 | int main() 7 | { 8 | foo = 7; 9 | print_foo(); 10 | print( 99 ); 11 | 12 | // to keep output open to see results of the program 13 | char cc; 14 | std::cin >> cc; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | *.vs 5 | *.VC.db 6 | *.exe 7 | *.ilk 8 | *.pdb 9 | */x64/Debug 10 | */Debug 11 | /Section_9/sandbox.cpp 12 | */x64/Release 13 | */Release 14 | /vcpkg_installed/x64-windows-static 15 | -------------------------------------------------------------------------------- /Section_6/sect6_ex5.txt: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // Date 21 Nov 2015 3 | // Programming: Principles and Practice Using C++ Second Edition 4 | /* 5 | Sentence: 6 | Noun Verb // e.g., C++ rules 7 | Sentence Conjunction Sentence // e.g., Birds fly but fish swim 8 | Conjunction: 9 | "and" 10 | "or" 11 | "but" 12 | Verb: 13 | "rules" 14 | "fly" 15 | "swim" 16 | Noun: 17 | "birds" 18 | "fish" 19 | "C++" 20 | Article: 21 | "the" 22 | */ -------------------------------------------------------------------------------- /Section_12/utility.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // run-time checked narrowing cast (type conversion). 8 | template 9 | Target narrow_cast(const Source& a) 10 | { 11 | auto r = static_cast(a); 12 | if(static_cast(r) != a){ 13 | throw std::runtime_error("narrow_cast<>() failed"); 14 | } 15 | return r; 16 | } 17 | -------------------------------------------------------------------------------- /Section_2/Section_2.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_4/Section_4.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_5/Section_5.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_6/Section_6.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_7/Section_7.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_8/Section_8.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_9/Section_9.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_10/Section_10.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_11/Section_11.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_12/Section_12.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Section_2/HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // Date 20 March 2017 3 | // Programming: Principles and Practice Using C++ Second Edition 4 | // Simple hello world program 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | std::cout << "Hello, World!\nC++ is fun :)\n"; 11 | 12 | std::cin.get(); //wait for a character to be entered 13 | 14 | return 0; // optional return statement in the main function but good to be explicit 15 | } 16 | -------------------------------------------------------------------------------- /Section_10/sect10_ex10_input.txt: -------------------------------------------------------------------------------- 1 | !; 2 | 1+4!; 3 | !+!; 4 | 5 | 1+2+3+4+5+6+7+8 6 | 1-2-3-4 7 | !+2 8 | ;;; 9 | (1+3; 10 | (1+); 11 | 1*2/3%4+5-6; 12 | 8*7; 13 | (); 14 | 1+; 15 | +1 16 | 1++; 17 | 1/0 18 | 1/0; 19 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 20 | Mary had a little lamb 21 | srtvrqtiewcbet7rewaewre-wqcntrretewru754389652743nvcqnwq; 22 | !@#$%^&*()~:; 23 | 24 | let x = 5.2; 25 | +1; 26 | x; 27 | pow(x-.2,2); 28 | () 29 | !+2 30 | 1++2;q -------------------------------------------------------------------------------- /Section_12/Point.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //using Callback = void( * )( void *, void * ); 4 | 5 | namespace GraphLib 6 | { 7 | struct Point 8 | { 9 | int x, y; 10 | Point() :x{}, y{} {} 11 | Point( int xx, int yy ) : x{ xx }, y{ yy } {} 12 | }; 13 | 14 | inline bool operator==( const Point &a, const Point &b ) { return a.x == b.x && a.y == b.y; } 15 | inline bool operator!=( const Point &a, const Point &b ) { return !( a == b ); } 16 | 17 | } //namespace GraphLib 18 | -------------------------------------------------------------------------------- /Section_3/sect3_7_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3.7 try this exercise 7 | find and fix the errors 8 | */ 9 | 10 | #include "section3.hpp" //custom header 11 | 12 | //int Main() 13 | int main() 14 | { 15 | //std::STRING s = "Goodbye, cruel world! "; 16 | std::string s = "Goodbye, cruel world! "; 17 | //cOut << S << '\n'; 18 | std::cout << s << '\n'; 19 | 20 | keep_window_open(); 21 | } 22 | -------------------------------------------------------------------------------- /Section_6/sect6_ex7.txt: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // Date 30 Nov 2015 3 | // Programming: Principles and Practice Using C++ Second Edition 4 | // Grammar for bitwise logical expressions 5 | 6 | /* 7 | Expression 8 | 9 | OR : 10 | XOR 11 | OR "|" XOR // OR 12 | 13 | XOR : 14 | AND 15 | XOR "^" AND // exclusive or 16 | 17 | AND : 18 | Primary 19 | And "&" Primary // And 20 | 21 | Primary : 22 | Number 23 | "(" Expression ")" // grouping 24 | Number "!" Primary // NOT 25 | Number "~" Primary // Complement 26 | 27 | Number : 28 | number 29 | */ 30 | -------------------------------------------------------------------------------- /Section_5/scaffolding.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 4 | 5 | /* 6 | Add comments here 7 | */ 8 | 9 | #include "section5.hpp" 10 | 11 | int main() 12 | try 13 | { 14 | /*<>*/ 15 | keep_window_open(); 16 | return 0; 17 | } 18 | catch( std::exception& e ) 19 | { 20 | std::cerr << "error: " << e.what() << '\n'; 21 | keep_window_open(); 22 | return 1; 23 | } 24 | catch( ... ) 25 | { 26 | std::cerr << "Oops: unknown exception!\n"; 27 | keep_window_open(); 28 | return 2; 29 | } 30 | -------------------------------------------------------------------------------- /Section_3/sect3_4_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 23 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3.4 try this exercise 7 | */ 8 | 9 | #include "section3.hpp" //custom header 10 | 11 | int main() 12 | { 13 | using namespace std; 14 | 15 | cout << "Please enter a value: "; 16 | int n; 17 | cin >> n; 18 | 19 | cout << format( "n == {} \nn + 1 == {} \nthree * n == {} \ntwice n == {} \nn squared == {} \nhalf of n == {} \nsquare root of n == {} \n", 20 | n, n + 1, n * 3, n + n, n * n, n / 2.0, std::sqrt( n ) ); 21 | 22 | keep_window_open(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Section_10/sect10_ex2.txt: -------------------------------------------------------------------------------- 1 | 17 -62.28 2 | 00 -59.22 3 | 14 94.84 4 | 17 4.18 5 | 15 -10.59 6 | 23 70.54 7 | 00 -56.50 8 | 16 93.79 9 | 19 -6.17 10 | 23 3.14 11 | 02 17.16 12 | 22 116.62 13 | 12 -3.41 14 | 03 -33.97 15 | 08 -21.26 16 | 02 88.81 17 | 11 14.49 18 | 19 133.33 19 | 20 -48.92 20 | 17 12.78 21 | 11 101.62 22 | 01 -15.45 23 | 14 17.53 24 | 10 7.78 25 | 08 -94.97 26 | 18 3.03 27 | 07 -35.89 28 | 12 7.55 29 | 05 -30.69 30 | 03 -53.40 31 | 16 -61.29 32 | 04 -98.25 33 | 20 115.03 34 | 08 -41.26 35 | 23 88.29 36 | 04 8.58 37 | 17 90.83 38 | 14 16.25 39 | 01 91.02 40 | 15 5.62 41 | 06 18.31 42 | 09 74.05 43 | 10 -37.25 44 | 20 32.10 45 | 09 83.59 46 | 13 2.09 47 | 20 16.58 48 | 18 40.44 49 | 23 -55.28 50 | 10 -82.92 51 | -------------------------------------------------------------------------------- /Section_5/sect5_6_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 20 Oct 2015 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 5 Try This 5.6 7 | See what an uncaught exception error looks like using the error() function. 8 | */ 9 | 10 | #include "section5.hpp" 11 | 12 | int main() 13 | { 14 | error( "so this is what an uncaught error looks like" ); 15 | // compile and run using gcc in command line and got following results 16 | // terminate called after throwing an instance of 'std::runtime_error' 17 | // what(): so this is what an uncaught error looks like 18 | // This application has requested the Runtime to terminate it in an unusual way. 19 | // Please contact the application's support team for more information. 20 | } 21 | -------------------------------------------------------------------------------- /Section_5/sect5_3_2_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 19 Oct 2015 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 5 Try This 5.3.2 7 | compile the error examples from this section and see how the compiler responds. 8 | */ 9 | 10 | #include "section5.hpp" 11 | 12 | int area( int length, int width ); 13 | 14 | int main() 15 | { 16 | //int x0 = arena(7); 17 | // error is identifier "arena" is undefined 18 | 19 | //int x1 = area(7); 20 | // error is too few arguments in function call 21 | 22 | //int x2 = area("seven", 2); 23 | // error is argument of type "const char *" is incompatible with parameter of type "int" 24 | 25 | //int x3 = area(1, 2, 3); 26 | // error is too many arguments in function call 27 | } 28 | -------------------------------------------------------------------------------- /Section_11/sect11_2_2_try.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 7 June 2024 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Complete the code fragment above to make it into a program. 7 | Try the suggested input; then type in 1234 1234 1234 1234 Explain the results. Try other inputs to see what happens. 8 | If you type in 1234 4d2 2322 2322 this will print 1234 1234 1234 1234 9 | */ 10 | 11 | #include 12 | 13 | int main() 14 | { 15 | int a; 16 | int b; 17 | int c; 18 | int d; 19 | std::cin >> a >> std::hex >> b >> std::oct >> c >> d; 20 | std::cout << a << '\t' << b << '\t' << c << '\t' << d << '\n'; 21 | 22 | ppp::keep_window_open(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Section_12/fltk_test_window.cpp: -------------------------------------------------------------------------------- 1 | // Stroustrup, Bjarne.Programming: Principles and Practice Using C++ 2 | // https://www.fltk.org/doc-1.3/basics.html 3 | 4 | #include "fltk.hpp" 5 | 6 | //int main() 7 | //{ 8 | // Fl_Window window( 400, 400, "Window title" ); 9 | // Fl_Box box( 0, 0, 200, 200, "Hey, I mean, Hello, World!" ); 10 | // window.show(); 11 | // return Fl::run(); 12 | //} 13 | 14 | int main( int argc, char **argv ) 15 | { 16 | Fl_Window *window = new Fl_Window( 340, 180 ); 17 | Fl_Box *box = new Fl_Box( 20, 40, 300, 100, "Hello, World!" ); 18 | box->box( FL_UP_BOX ); 19 | box->labelfont( FL_BOLD + FL_ITALIC ); 20 | box->labelsize( 36 ); 21 | box->labeltype( FL_SHADOW_LABEL ); 22 | window->end(); 23 | window->show( argc, argv ); 24 | return Fl::run(); 25 | } 26 | -------------------------------------------------------------------------------- /Section_4/sect4_4_try4.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Try This 4.4.2.3 7 | The character 'b' is char('a'+1), 'c' is char('a'+2), etc. 8 | Use a loop to write out a table of characters with their corresponding integer values: 9 | a 97 10 | b 98 11 | . . . 12 | z 122 13 | */ 14 | 15 | #include "section4.hpp" //custom header 16 | 17 | int main() 18 | { 19 | for( unsigned char character{ 'a' }; character <= 'z'; ++character ) 20 | { 21 | std::cout << std::format( "'{0}' = {0:<3d} '{1}' = {1:<3d}\n", 22 | static_cast( character ), static_cast( std::toupper( character ) ) ); 23 | } 24 | 25 | keep_window_open(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Section_3/sect3_ex8.cpp: -------------------------------------------------------------------------------- 1 | // written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | 6 | /* 7 | Section 3 exercise 8. 8 | A program to test an integer value to determine if it is odd or even. 9 | As always, make sure your output is clear and complete. 10 | In other words, don’t just output yes or no. Your output should stand alone, 11 | like The value 4 is an even number. 12 | */ 13 | 14 | #include "section3.hpp" 15 | 16 | int main() 17 | { 18 | using namespace std; 19 | 20 | cout << "Enter an integer: "; 21 | int val{}; 22 | if( !( cin >> val ) ) 23 | { 24 | simple_error( "Invalid integer value\n" ); 25 | } 26 | cout << std::format( "The value {} is an {} number.\n", val, ( val & 1 ) ? "odd" : "even" ); 27 | 28 | keep_window_open(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Section_10/sect10_ex5_out.txt: -------------------------------------------------------------------------------- 1 | year: 1990 2 | 3 | year: 1991 4 | month: June 5 | 6 | year: 1992 7 | month: January 8 | day: 1 9 | hour: 0 10 | temp: 61.5 11 | month: February 12 | day: 1 13 | hour: 1 14 | temp: 64 15 | day: 2 16 | hour: 2 17 | temp: 65.2 18 | 19 | year: 2000 20 | month: February 21 | day: 1 22 | hour: 0 23 | temp: 67.2 24 | hour: 1 25 | temp: 68 26 | day: 2 27 | hour: 3 28 | temp: 66.66 29 | month: December 30 | day: 14 31 | hour: 0 32 | temp: -2 33 | day: 15 34 | hour: 14 35 | temp: -8.8 36 | hour: 15 37 | temp: -9.2 38 | 39 | -------------------------------------------------------------------------------- /Section_3/sect3_ex2.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3 exercise 2. 7 | converts from miles to kilometers (1.609 kilometers to the mile) 8 | */ 9 | 10 | #include "section3.hpp" 11 | 12 | int main() 13 | { 14 | using namespace std; 15 | 16 | cout << "Enter the number of miles you want to convert to kilometers: "; 17 | double miles{}; 18 | if( cin >> miles ) 19 | { 20 | cout << std::format( "{} {} equals {} {}\n", 21 | miles, miles == 1 ? "mile" : "miles", 22 | miles * 1.609, miles * 1.609 == 1 ? "kilometer" : "kilometers" ); 23 | } 24 | else 25 | { 26 | simple_error( "Invalid entry" ); 27 | } 28 | 29 | keep_window_open(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Section_4/sect4_4_try3.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2016 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Try This 4.4.2.1 7 | The character 'b' is char('a'+1), 'c' is char('a'+2), etc. 8 | Use a loop to write out a table of characters with their corresponding integer values: 9 | a 97 10 | b 98 11 | . . . 12 | z 122 13 | */ 14 | 15 | #include "section4.hpp" 16 | 17 | int main() 18 | { 19 | // 97 = 'a' lower case letters 122 = 'z' 20 | unsigned char character{ 'a' }; 21 | while( character <= 'z' ) 22 | { 23 | std::cout << std::format( "'{0}' = {0:<3d} '{1}' = {1:<3d}\n", 24 | static_cast( character ), static_cast( std::toupper( character ) ) ); 25 | ++character; 26 | } 27 | 28 | keep_window_open(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Section_8/sect8_ex2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 21 Dec 2018 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | Section 8 Exercise 2 6 | Write a function print() that prints a vector of ints to cout. 7 | Inputs: string for labeling 8 | vector 9 | Outputs: void 10 | */ 11 | 12 | #include "..\includes\ppp.hpp" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | void print( const std::string& label, const std::vector& v ) 19 | { 20 | std::cout << label << '\n'; 21 | for( auto i : v ) 22 | { 23 | std::cout << i << '\n'; 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | std::string label{ "Printing some integers from a vector." }; 30 | std::vector vi{ 1, 2, 3, 4, 5 }; 31 | print( label, vi ); 32 | 33 | ppp::keep_window_open(); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Section_10/sect10_ex4.txt: -------------------------------------------------------------------------------- 1 | 04 020.55 c 2 | 23 024.99 f 3 | 11 142.37 f 4 | 07 005.62 f 5 | 19 042.79 f 6 | 12 -16.36 c 7 | 19 -72.90 f 8 | 14 009.68 c 9 | 06 -54.40 f 10 | 17 093.03 f 11 | 13 004.70 f 12 | 20 -55.76 c 13 | 02 082.52 f 14 | 16 055.72 c 15 | 11 085.51 f 16 | 13 -73.15 f 17 | 13 021.00 f 18 | 19 127.87 f 19 | 15 -47.48 c 20 | 21 -30.85 f 21 | 05 -45.93 c 22 | 20 -15.60 f 23 | 13 055.76 c 24 | 15 -32.60 c 25 | 21 -52.03 c 26 | 18 -24.69 f 27 | 02 -50.93 c 28 | 06 040.04 c 29 | 12 095.60 f 30 | 13 122.76 f 31 | 23 071.58 f 32 | 09 -78.70 f 33 | 21 010.21 c 34 | 23 0-9.75 f 35 | 00 -18.47 c 36 | 17 0-4.90 c 37 | 14 049.64 f 38 | 03 -22.89 f 39 | 06 016.05 f 40 | 17 113.41 f 41 | 05 039.20 f 42 | 13 -58.29 f 43 | 10 -96.09 f 44 | 15 047.03 c 45 | 21 -50.72 c 46 | 03 -25.51 c 47 | 12 122.93 f 48 | 08 -28.06 c 49 | 11 -58.10 c 50 | 19 064.85 c 51 | -------------------------------------------------------------------------------- /Section_3/sect3_3_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 22 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3.3 try this exercise 7 | Get the “name and age” example to run. 8 | Then, modify it to write out the age in months: 9 | read the input in years and multiply (using the * operator) by 12. 10 | Read the age into a double to allow for children who can be very 11 | proud of being five and a half years old rather than just five. 12 | */ 13 | 14 | #include "section3.hpp" //custom header 15 | 16 | int main() 17 | { 18 | std::cout << "Please enter your first name: "; 19 | std::string first_name; 20 | std::cin >> first_name; 21 | 22 | std::cout << "Please enter your age: "; 23 | double age; 24 | std::cin >> age; 25 | 26 | std::cout << std::format( "Hello, {} you are {} months old.\n", first_name, age * 12 ); 27 | 28 | keep_window_open(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Section_8/sect8_ex14.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 12 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 13 7 | Can we declare a non-reference function const (e.g., void f(const int);)? 8 | - yes. 9 | What might that mean? 10 | - read only argument. functional will not modify the argument is a compiler error if you try. 11 | Why don't people do that often? 12 | - does not really matter copy of a int value and a reference of a value is the same9 the sizof ref is basically the same as sizof an int. 13 | */ 14 | 15 | #include"../includes/ppp.hpp" 16 | 17 | void f( const int n ) 18 | { 19 | //n = 25; 20 | 21 | std::cout << n << std::endl; 22 | } 23 | 24 | int main() 25 | { 26 | int arg{ 15 }; 27 | f( arg ); 28 | 29 | arg = 69; 30 | f( arg ); 31 | 32 | ppp::keep_window_open(); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Section_8/sect8_drill3.cpp: -------------------------------------------------------------------------------- 1 | #include "../includes/ppp.hpp" 2 | 3 | namespace X 4 | { 5 | int var{}; 6 | 7 | void print() 8 | { 9 | std::cout << "Printing from namespace X the value " << var << "\n"; 10 | } 11 | } 12 | 13 | namespace Y 14 | { 15 | int var{}; 16 | 17 | void print() 18 | { 19 | std::cout << "Printing from namespace Y the value " << var << "\n"; 20 | } 21 | } 22 | 23 | namespace Z 24 | { 25 | int var{}; 26 | 27 | void print() 28 | { 29 | std::cout << "Printing from namespace Z the value " << var << "\n"; 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | X::var = 7; 36 | X::print(); 37 | using namespace Y; 38 | var = 9; 39 | print(); 40 | { 41 | using Z::var; 42 | using Z::print; 43 | var = 11; 44 | print(); 45 | } 46 | print(); 47 | X::print(); 48 | 49 | ppp::keep_window_open(); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Section_9/Library.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "Book.hpp" 5 | #include "Patron.hpp" 6 | #include "Chrono.hpp" 7 | 8 | namespace library 9 | { 10 | class Library 11 | { 12 | public: 13 | struct Transaction 14 | { 15 | Book book; 16 | Patron patron; 17 | Chrono::Date date; 18 | Transaction( Book b, Patron p, Chrono::Date d ) 19 | : book( b ), patron( p ), date( d ) 20 | {} 21 | }; 22 | 23 | Library(); 24 | 25 | void add_book(); 26 | void add_patron(); 27 | void check_out_book( Book& b, Patron& p ); 28 | std::vector> get_outstanding_fees(); 29 | 30 | private: 31 | std::vector m_books; 32 | std::vector m_patrons; 33 | std::vector transactions; 34 | 35 | std::string get_string(); 36 | }; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Section_11/sect11_ex1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | WRITTEN BY JTAIM 3 | DATE 19 SEPT 2019 4 | STROUSTRUP, BJARNE. PROGRAMMING: PRINCIPLES AND PRACTICE USING C++. PEARSON EDUCATION. KINDLE EDITION. 5 | 6 | SECTION 11 EXERCISE 1 7 | WRITE A PROGRAM THAT READS A TEXT FILE AND CONVERTS ITS INPUT TO ALL LOWER CASE, PRODUCING A NEW FILE. 8 | */ 9 | 10 | #include "ppp.hpp" 11 | 12 | int main() 13 | try 14 | { 15 | std::ofstream ofs{ "sect11_ex1.txt" }; 16 | std::ifstream ifs{ "sect11_ex1.cpp" }; 17 | for( char c; ifs.get( c ); ) 18 | { 19 | ofs << static_cast( std::tolower( static_cast( c ) ) ); 20 | } 21 | 22 | ppp::keep_window_open(); 23 | return 0; 24 | } 25 | catch( std::exception &e ) 26 | { 27 | std::cerr << "exception: " << e.what() << std::endl; 28 | ppp::keep_window_open(); 29 | return 1; 30 | } 31 | catch( ... ) 32 | { 33 | std::cerr << "exception\n"; 34 | ppp::keep_window_open(); 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /Section_11/sect11_ex1.txt: -------------------------------------------------------------------------------- 1 | /* 2 | written by jtaim 3 | date 19 sept 2019 4 | stroustrup, bjarne. programming: principles and practice using c++. pearson education. kindle edition. 5 | 6 | section 11 exercise 1 7 | write a program that reads a text file and converts its input to all lower case, producing a new file. 8 | */ 9 | 10 | #include "ppp.hpp" 11 | 12 | int main() 13 | try 14 | { 15 | std::ofstream ofs{ "sect11_ex1.txt" }; 16 | std::ifstream ifs{ "sect11_ex1.cpp" }; 17 | for( char c; ifs.get( c ); ) 18 | { 19 | ofs << static_cast( std::tolower( static_cast( c ) ) ); 20 | } 21 | 22 | ppp::keep_window_open(); 23 | return 0; 24 | } 25 | catch( std::exception &e ) 26 | { 27 | std::cerr << "exception: " << e.what() << std::endl; 28 | ppp::keep_window_open(); 29 | return 1; 30 | } 31 | catch( ... ) 32 | { 33 | std::cerr << "exception\n"; 34 | ppp::keep_window_open(); 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /Section_9/sect9_ex16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Sept 4 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 16 7 | this is the test program Money class. 8 | */ 9 | 10 | #include"../includes/ppp.hpp" 11 | 12 | #include "Money.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | std::cout << "Enter a monitory amount prefixed by currency code (USD or DKK)\n"; 18 | Money m1, m2; 19 | std::cin >> m1 >> m2; 20 | std::cout << "m1 = " << m1 << '\n'; 21 | std::cout << "m2 = " << m2 << '\n'; 22 | 23 | std::cout << "m1 + m2 = " << m1 + m2 << "\n"; 24 | 25 | ppp::keep_window_open(); 26 | return 0; 27 | } 28 | catch( std::exception& e ) 29 | { 30 | std::cerr << "exception: " << e.what() << std::endl; 31 | ppp::keep_window_open(); 32 | return 1; 33 | } 34 | catch( ... ) 35 | { 36 | std::cerr << "exception\n"; 37 | ppp::keep_window_open(); 38 | return 2; 39 | } 40 | -------------------------------------------------------------------------------- /Section_10/sect10_ex10_output.txt: -------------------------------------------------------------------------------- 1 | > input > ERROR: ! is a bad token 2 | ignoring: ; 3 | > 1 + 4 ERROR: ! is a bad token 4 | ignoring: ; 5 | > ERROR: ! is a bad token 6 | ignoring: +!; 7 | > 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36 8 | > 1 - 2 - 3 - 4 = -8 9 | > ERROR: ! is a bad token 10 | ignoring: +2; 11 | > ( 1 + 3 ERROR: ')' expected 12 | ignoring: (1+); 13 | > 1 * 2 / 3 % 4 + 5 - 6 = -0.333333 14 | > 8 * 7 = 56 15 | > ( ) ERROR: primary expected 16 | ignoring: ; 17 | > 1 + ERROR: primary expected 18 | ignoring: +11++; 19 | > 1 / 0 ERROR: divide by zero 20 | ignoring: 1/0; 21 | > Stroustrup , ERROR: get: undefined name Stroustrup 22 | ignoring: Bjarne.Programming:PrinciplesandPracticeUsingC++.PearsonEducation.KindleEdition.Maryhadalittlelambsrtvrqtiewcbet7rewaewre-wqcntrretewru754389652743nvcqnwq; 23 | > ERROR: ! is a bad token 24 | ignoring: @#$%^&*()~:; 25 | > let x 5.2 = 5.2 26 | > + 1 = 1 27 | > x = 5.2 28 | > pow ( x - 0.2 , 2 ) = 25 29 | > ( ) ERROR: primary expected 30 | ignoring: !+21++2; 31 | > q -------------------------------------------------------------------------------- /Section_3/section3.hpp: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // 22 Mar 2017 3 | // Header file used for Programming: Principles and Practice Using C++ Second Edition 4 | // section 3 5 | // updated 18 May 2018 6 | // added cstdlib 7 | 8 | #pragma once 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // simple function to keep window console open 17 | inline void keep_window_open() 18 | { 19 | std::cin.clear(); 20 | std::cin.sync_with_stdio( false ); 21 | std::cin.ignore( std::cin.rdbuf()->in_avail() ); //clear buffer 22 | std::cout << "\nPress the Enter key to continue"; 23 | std::cin.get(); 24 | } 25 | 26 | // error function to be used (only) until error() is introduced in Chapter 5: 27 | [[noreturn]] void simple_error( const std::string s ) // write error: s and exit program 28 | { 29 | std::cout << std::format( "error: {}", s ) << std::endl; 30 | keep_window_open(); 31 | std::exit( 1 ); 32 | } 33 | -------------------------------------------------------------------------------- /Section_9/Rational.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Rational 4 | { 5 | public: 6 | Rational(); 7 | Rational( int n, int d ); 8 | 9 | //operator overload functions 10 | Rational& operator=( const Rational& a ); 11 | 12 | //implicit conversion 13 | operator double() const { return static_cast( m_numerator ) / m_denominator; } 14 | 15 | //Helper functions getters 16 | int get_numerator() const { return m_numerator; } 17 | int get_denominator() const { return m_denominator; } 18 | 19 | private: 20 | int m_numerator; 21 | int m_denominator; 22 | int gcd( int a, int b ) { return b == 0 ? a : gcd( b, a % b ); } 23 | }; 24 | 25 | //operator overload functions 26 | Rational operator+( const Rational& a, const Rational& b ); 27 | Rational operator-( const Rational& a, const Rational& b ); 28 | Rational operator*( const Rational& a, const Rational& b ); 29 | Rational operator/( const Rational& a, const Rational& b ); 30 | bool operator==( const Rational& a, const Rational& b ); 31 | 32 | 33 | -------------------------------------------------------------------------------- /Section_9/Money.hpp: -------------------------------------------------------------------------------- 1 | 2 | #ifndef MONEY_CLASS_H // begin header guard 3 | #define MONEY_CLASS_H 4 | 5 | #include 6 | #include 7 | 8 | class Money 9 | { 10 | public: 11 | Money( std::string m = "" ); 12 | 13 | std::string get_cents(); 14 | 15 | Money operator+( const Money& m ) const; 16 | friend std::ostream& operator<<( std::ostream& os, const Money& m ); 17 | friend std::istream& operator>>( std::istream& is, Money& m ); 18 | 19 | private: 20 | std::string m_currencyCode{ "USD" }; 21 | std::string m_currencySign{ "$" }; 22 | long m_cents{}; // amount in cents. lowest denomination stored in USD 23 | double m_conversion{}; 24 | 25 | std::string validate_currency( const std::string& m ); 26 | 27 | using currencyInfoTuple = std::tuple; 28 | const std::vector currencyInfo 29 | { 30 | {"USD", "$", 1}, // keep USD first because used as the standard stored currency 31 | {"DKK", "Kr", 6.8} 32 | }; 33 | }; 34 | 35 | #endif -------------------------------------------------------------------------------- /Section_11/sect11_2_4_try.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 7 June 2024 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Write some code to print the number 1234567.89 three times, first using defaultfloat, then fixed, then scientific. 7 | Which output form presents the user with the most accurate representation? Explain why. 8 | */ 9 | 10 | #include 11 | 12 | int main() 13 | { 14 | constexpr double df{ 1234567.89 }; 15 | 16 | std::cout << df << "\t" 17 | << std::fixed << df << "\t\t" 18 | << std::scientific << df << "\n"; 19 | std::cout << std::defaultfloat << std::setprecision(5) 20 | << df << "\t" 21 | << std::fixed << df << "\t\t" 22 | << std::scientific << df << "\n"; 23 | std::cout << std::defaultfloat << std::setprecision( 8 ) 24 | << df << "\t" 25 | << std::fixed << df << "\t" 26 | << std::scientific << df << "\n"; 27 | 28 | ppp::keep_window_open(); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Section_4/sect4_5_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Try This 4.5.1 7 | Implement square() without using the multiplication operator; 8 | that is, do the x*x by repeated addition (start a variable result at 0 and add x to it x times). 9 | Then run some version of “the first program” using that square(). 10 | */ 11 | 12 | #include "section4.hpp" 13 | 14 | // function forward declaration 15 | int square( int ); 16 | 17 | int main() 18 | { 19 | std::cout << "Program to find the squared results as the first program did in 1949.\n"; 20 | for( int num{}; num < 100; ++num ) 21 | { 22 | std::cout << std::format( "{:>3} squared = {}\n", num, square( num ) ); 23 | } 24 | 25 | keep_window_open(); 26 | return 0; 27 | } 28 | 29 | // do repeat addition not multiplication function 30 | int square( int i ) 31 | { 32 | int result{}; 33 | for( int j{}; j < i; j++ ) 34 | { 35 | result += i; 36 | } 37 | return result; 38 | } 39 | -------------------------------------------------------------------------------- /Section_4/sect4_ex18.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 18. 7 | Write a program to solve quadratic equations. 8 | ax^2 + bx + c = 0 9 | ax^2 + bx = -c 10 | x^2 + bx/a = -c/a 11 | x^2 + bx/a + (b/2a)^2 = -c/a + (b/2a)^2 complete the square 12 | (x + (b/2a))^2 = (b^2-4ac)/4a^2 13 | x = -b +/- sqrt(b^2-4ac)/2a 14 | */ 15 | 16 | #include "section4.hpp" 17 | 18 | int main() 19 | { 20 | std::cout << "Enter the a, b and c variables to solve the quadratic equation:\n"; 21 | double a{ 0.0 }; 22 | double b{ 1.0 }; 23 | double c{ 0.0 }; 24 | 25 | std::cin >> a >> b >> c; 26 | if( !std::cin ) 27 | { 28 | simple_error( "Entered invalid numbers" ); 29 | } 30 | 31 | double x1 = ( -1.0 * b + std::sqrt( b * b - 4.0 * a * c ) ) / ( 2.0 * a ); 32 | double x2 = ( -1.0 * b - std::sqrt( b * b - 4.0 * a * c ) ) / ( 2.0 * a ); 33 | std::cout << "x = " << x1 << " and x = " << x2 << '\n'; 34 | 35 | keep_window_open(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Section_9/sect9_ex8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 8 7 | Design and implement a Patron class under library namespace 8 | */ 9 | 10 | #include"../includes/ppp.hpp" 11 | 12 | #include "Patron.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | library::Patron patron{ "John Doe", 123456 }; 18 | if( !patron.is_outstanding_fee() ) 19 | { 20 | std::cout << std::format( "{} outstanding fees ${}\n", patron.get_name(), patron.get_fees() ); 21 | } 22 | 23 | patron.set_fees( 0.01 ); 24 | std::cout << std::format( "{} outstanding fees ${}\n", patron.get_name(), patron.get_fees() ); 25 | 26 | 27 | ppp::keep_window_open(); 28 | return 0; 29 | } 30 | catch( std::exception& e ) 31 | { 32 | std::cerr << "exception: " << e.what() << std::endl; 33 | ppp::keep_window_open(); 34 | return 1; 35 | } 36 | catch( ... ) 37 | { 38 | std::cerr << "exception\n"; 39 | ppp::keep_window_open(); 40 | return 2; 41 | } -------------------------------------------------------------------------------- /Section_9/Patron.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Patron class under library namespace 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | 14 | namespace library 15 | { 16 | class Patron 17 | { 18 | public: 19 | Patron(); 20 | Patron( const std::string name, const int card_number ); 21 | 22 | std::string get_name() const { return m_name; } 23 | int get_card_number() const { return m_card_number; } 24 | double get_fees() const; 25 | 26 | double set_fees( const double f ); 27 | bool is_outstanding_fee() const; 28 | 29 | friend std::ostream& operator<<( std::ostream& os, const Patron& p ); 30 | friend bool operator==( const Patron& p1, const Patron& p2 ); 31 | friend bool operator!=( const Patron& p1, const Patron& p2 ); 32 | 33 | private: 34 | std::string m_name; 35 | int m_card_number; 36 | double m_fees; 37 | }; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Section_9/sect9_ex10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 10 7 | A snippet of a leap year implementation for this chapters Date class 8 | */ 9 | 10 | /* 11 | formula for calculating leap years for the Gregorian calender: 12 | - The year is evenly divisible by 4; 13 | - If the year can be evenly divided by 100, it is NOT a leap year, unless; 14 | - The year is also evenly divisible by 400: Then it is a leap year. 15 | */ 16 | #include 17 | #include "..\includes\ppp.hpp" 18 | 19 | bool leap_year( int y ) 20 | { 21 | if( !( y % 400 ) ) 22 | { 23 | return true; 24 | } 25 | if( y % 100 && !( y % 4 ) ) 26 | { 27 | return true; 28 | } 29 | return false; 30 | } 31 | 32 | int main() 33 | { 34 | int years[]{ 1600, 1700, 1999, 2000, 2001, 2002, 2004 }; 35 | for( const auto y : years ) 36 | { 37 | std::cout << leap_year(y) << "\n"; 38 | } 39 | 40 | ppp::keep_window_open(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Section_12/sect12_drill1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | May 23 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 12 Drill 1 7 | Get an empty Simple_window with the size 600 by 400 and a label My window compiled, linked, and run. 8 | Note that you have to link the FLTK library as described in the README.md file; 9 | #include Graph.h and Simple_window.h in your code; and include Graph.cpp and Window.cpp in your project. 10 | */ 11 | 12 | #include "Simple_window.hpp" 13 | 14 | #include 15 | 16 | int main() 17 | try 18 | { 19 | GraphLib::Point top_left{ 100, 100 }; 20 | Simple_Window win{ top_left, 600, 400, "My Window" }; 21 | GraphLib::Polygon poly{ { 300,200 }, { 350,100 }, { 400,200 } }; 22 | poly.Set_Color( GraphLib::Color::Color_Type::red ); 23 | win.Attach( poly ); 24 | win.Wait_For_Button(); 25 | return 0; 26 | } 27 | catch( std::exception &e ) 28 | { 29 | std::println( "exception: {}", e.what() ); 30 | return 1; 31 | } 32 | catch( ... ) 33 | { 34 | std::println( "exception" ); 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /Section_12/sect12_ex3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Aug 4 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 12 Exercise 3 7 | Draw your initials 150 pixels high. 8 | Use a thick line. 9 | Draw each initial in a different color. 10 | */ 11 | 12 | #include "Simple_window.hpp" 13 | #include 14 | 15 | int main() 16 | try 17 | { 18 | using namespace GraphLib; 19 | 20 | Point top_left{ 100,100 }; 21 | Simple_Window win{ top_left,600,400,"Exercise #3" }; 22 | 23 | Text first( Point( 200, 250 ), "J" ); 24 | first.Set_Color( Color::Color_Type::red ); 25 | first.Set_Font_Size( 150 ); 26 | 27 | Text last( Point( 300, 250 ), "W" ); 28 | last.Set_Color( Color::Color_Type::blue ); 29 | last.Set_Font_Size( 150 ); 30 | 31 | win.Attach( first ); 32 | win.Attach( last ); 33 | 34 | win.Wait_For_Button(); 35 | 36 | return 0; 37 | } 38 | catch( std::exception &e ) 39 | { 40 | std::println( "exception: {}", e.what() ); 41 | return 1; 42 | } 43 | catch( ... ) 44 | { 45 | std::println( "exception" ); 46 | return 2; 47 | } 48 | -------------------------------------------------------------------------------- /Section_11/sect11_ex2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 19 Sept 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++. Pearson Education. Kindle Edition. 5 | 6 | Section 11 Exercise 2 7 | Write a program that given a file name and a word outputs each line that contains that word together with the line number. 8 | Hint: getline(). 9 | */ 10 | 11 | #include "ppp.hpp" 12 | 13 | int main() 14 | try 15 | { 16 | constexpr std::string_view word_match{ "return" }; 17 | std::ifstream ifs{ "sect11_ex2.cpp" }; 18 | std::string line; 19 | for( size_t line_number{ 1 }; std::getline( ifs, line ); ++line_number ) 20 | { 21 | if( std::string::npos != line.find( word_match ) ) 22 | { 23 | std::cout << line_number << line << "\n"; 24 | } 25 | } 26 | 27 | ppp::keep_window_open(); 28 | return 0; 29 | } 30 | catch( std::exception &e ) 31 | { 32 | std::cerr << "exception: " << e.what() << std::endl; 33 | ppp::keep_window_open(); 34 | return 1; 35 | } 36 | catch( ... ) 37 | { 38 | std::cerr << "exception\n"; 39 | ppp::keep_window_open(); 40 | return 2; 41 | } 42 | -------------------------------------------------------------------------------- /Section_3/Section_3.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /Section_4/Section_4.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /Section_4/section4.hpp: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // 25 Mar 2017 3 | // Header file used for Programming: Principles and Practice Using C++ Second Edition 4 | // section 4 5 | 6 | #ifndef SECTION4_H // begin header guard 7 | #define SECTION4_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | // simple function to keep window console open 25 | inline void keep_window_open() 26 | { 27 | std::cin.clear(); 28 | std::cin.sync_with_stdio( false ); 29 | std::cin.ignore( std::cin.rdbuf()->in_avail() ); //clear buffer 30 | std::cout << "\nPress the Enter key to continue"; 31 | std::cin.get(); 32 | } 33 | 34 | // error function to be used (only) until error() is introduced in Chapter 5: 35 | void simple_error( std::string s ) // write error: s and exit program 36 | { 37 | std::cerr << "error: " << s << '\n'; 38 | keep_window_open(); 39 | std::exit( 1 ); 40 | } 41 | #endif // close header guard 42 | -------------------------------------------------------------------------------- /Section_10/sect10_drill1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Feb 26 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Drill 1 7 | Start a program to work with points, discussed in section 10.4. 8 | Begin by defining the data type Point that has two coordinate members x and y. 9 | */ 10 | 11 | #include "../includes/ppp.hpp" 12 | #include "Point.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | Point a; 18 | Point b{ 2, 3 }; 19 | 20 | if( a != b ) {} 21 | std::cout << "Point a: " << a << "\n"; 22 | std::cout << "Point b: " << b << "\n"; 23 | 24 | if( !( std::cin >> a ) && !std::cin.eof() ) 25 | { 26 | std::cout << "bad input\n"; 27 | } 28 | else 29 | { 30 | std::cout << "Point a: " << a << "\n"; 31 | } 32 | 33 | ppp::keep_window_open(); 34 | return 0; 35 | } 36 | catch( std::exception &e ) 37 | { 38 | std::cerr << "exception: " << e.what() << std::endl; 39 | ppp::keep_window_open(); 40 | return 1; 41 | } 42 | catch( ... ) 43 | { 44 | std::cerr << "exception\n"; 45 | ppp::keep_window_open(); 46 | return 2; 47 | } 48 | -------------------------------------------------------------------------------- /Section_10/sect10_ex8.txt: -------------------------------------------------------------------------------- 1 | Contents of file sect10_ex5_in.txt 2 | { year 1990 } 3 | { year 1991 4 | { month jun }} 5 | { year 1992 6 | { month jan (1 0 61.5)} 7 | { month feb (1 1 64 )(2 2 65.2)}} 8 | { year 2000 9 | { month feb (1 1 68)(2 3 66.66)(1 0 67.2) } 10 | { month dec (15 15 -9.2)(15 14 -8.8)(14 0 -2)}} 11 | 12 | Contents of file sect10_ex5_out.txt 13 | year: 1990 14 | 15 | year: 1991 16 | month: June 17 | 18 | year: 1992 19 | month: January 20 | day: 1 21 | hour: 0 22 | temp: 61.5 23 | month: February 24 | day: 1 25 | hour: 1 26 | temp: 64 27 | day: 2 28 | hour: 2 29 | temp: 65.2 30 | 31 | year: 2000 32 | month: February 33 | day: 1 34 | hour: 0 35 | temp: 67.2 36 | hour: 1 37 | temp: 68 38 | day: 2 39 | hour: 3 40 | temp: 66.66 41 | month: December 42 | day: 14 43 | hour: 0 44 | temp: -2 45 | day: 15 46 | hour: 14 47 | temp: -8.8 48 | hour: 15 49 | temp: -9.2 50 | 51 | 52 | -------------------------------------------------------------------------------- /Section_5/sect5_7a_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 5 Try This 5.7 first one 7 | build and test the program from the section. 8 | Does it provide the correct results? 9 | What is the least amount of input you can give it to get it to fail? 10 | */ 11 | 12 | #include "section5.hpp" 13 | 14 | int main() 15 | { 16 | std::cout << "Enters some temperatures in Fahrenheit.\n"; 17 | std::vectortemps; 18 | for( double temp{}; std::cin >> temp;) 19 | { 20 | temps.push_back( temp ); 21 | } 22 | 23 | // check for empty vector 24 | if( temps.size() != 0 ) 25 | { 26 | std::sort( temps.begin(), temps.end() ); 27 | auto sum = std::accumulate( temps.begin(), temps.end(), 0.0 ); 28 | std::cout << "High temperature: " << temps.back() << std::endl; 29 | std::cout << "Low temperature: " << temps.front() << std::endl; 30 | std::cout << "Average temperature: " << sum / temps.size() << std::endl; 31 | } 32 | else 33 | { 34 | std::cout << "No temperatures entered.\n"; 35 | } 36 | 37 | keep_window_open(); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Section_11/sect11_2_1_try.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 7 June 2024 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Output your birth year in decimal, hexadecimal, and octal form. Label each value. 7 | Line up your output in columns using the tab character. Now output your age. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | constexpr std::chrono::year birth_year{ 1970 }; 14 | 15 | int main() 16 | { 17 | 18 | std::cout << "Birth Year "; 19 | std::cout << "\tDec: " << birth_year; 20 | std::cout << "\tHex: " << std::hex << birth_year; 21 | std::cout << "\tOct: " << std::oct << birth_year << "\n"; 22 | 23 | const auto now = std::chrono::system_clock::now(); 24 | const std::chrono::year_month_day ymd{ std::chrono::floor( now ) }; 25 | const auto age{ static_cast(ymd.year()) - static_cast( birth_year) }; 26 | 27 | std::cout << "Age"; 28 | std::cout << "\t\tDec: " << std::dec << age; 29 | std::cout << "\t\tHex: " << std::hex << age; 30 | std::cout << "\t\tOct: " << std::oct << age << std::endl; 31 | 32 | ppp::keep_window_open(); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Section_11/sect11_drill8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 12 Jan 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 11 Drill 8 7 | Try reading as octal, hexadecimal, etc.: 8 | cin >> a >> oct >> b >> hex >> c >> d; 9 | cout << a << '\t'<< b << '\t'<< c << '\t'<< d << '\n'; 10 | Run this code with the input 1234 1234 1234 1234 Explain the results. 11 | */ 12 | 13 | #include "ppp.hpp" 14 | 15 | int main() 16 | try 17 | { 18 | int a{}, b{}, c{}, d{}; 19 | std::cin >> a >> std::oct >> b >> std::hex >> c >> d; 20 | std::cout << a << '\t' << b << '\t' << c << '\t' << d << '\n'; 21 | 22 | // the output keeps previous set base until it is changed or program restarted. 23 | // you have to explicitly change the base. 24 | // the value is stored as the base value, so be careful 25 | 26 | ppp::keep_window_open(); 27 | return 0; 28 | } 29 | catch( std::exception &e ) 30 | { 31 | std::cerr << "exception: " << e.what() << std::endl; 32 | ppp::keep_window_open(); 33 | return 1; 34 | } 35 | catch( ... ) 36 | { 37 | std::cerr << "exception\n"; 38 | ppp::keep_window_open(); 39 | return 2; 40 | } 41 | -------------------------------------------------------------------------------- /Section_10/sect10_ex1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Mar 24 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Exercise 1 7 | A program that produces the sum of all the numbers in a file of whitespace-separated integers. 8 | */ 9 | 10 | #include "../includes/ppp.hpp" 11 | 12 | int main() 13 | try 14 | { 15 | constexpr std::string_view file_name{ "sect10_ex1.txt" }; 16 | 17 | std::ifstream fin{ file_name.data()}; 18 | fin.exceptions( fin.exceptions() | std::ios::badbit ); 19 | 20 | int sum{}; 21 | for( int n{}; fin >> n; sum += n ) {/*all done in for loop declaration*/ } 22 | if( fin.fail() && !fin.eof() ) 23 | { 24 | ppp::error( "bad input data found" ); 25 | } 26 | std::cout << "The sum of the integers from the " << file_name << " file is " << sum << ".\n"; 27 | 28 | ppp::keep_window_open(); 29 | return 0; 30 | } 31 | catch( std::exception &e ) 32 | { 33 | std::cerr << R"(exception: )" << e.what() << std::endl; 34 | ppp::keep_window_open(); 35 | return 1; 36 | } 37 | catch( ... ) 38 | { 39 | std::cerr << R"(exception)"; 40 | ppp::keep_window_open(); 41 | return 2; 42 | } 43 | -------------------------------------------------------------------------------- /Section_5/sect5_3_1_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 19 Oct 2015 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 5 Try This 5.3.1 7 | compile the error examples from this section and see how the compiler responds. 8 | */ 9 | 10 | #include "section5.hpp" 11 | 12 | int area( int length, int width ); 13 | 14 | int main() 15 | { 16 | //int s1 = area(7; 17 | //error was expected a ')' 18 | //fix 19 | int s1 = area( 7, 2 ); 20 | 21 | //int s2 = area(7) 22 | //error was too few arguments in function call 23 | //expected a ';' 24 | //fix 25 | int s2 = area( 7, 2 ); 26 | 27 | //Int s3 = area(7) 28 | //error was identifier "Int" is undefined 29 | //too few arguments in function call 30 | //expected a ';' 31 | //fix 32 | int s3 = area( 7, 2 ); 33 | 34 | //int s4 = area('7); 35 | //error was has opening quote int for some reason 36 | //too few arguments in function call 37 | //fix 38 | int s4 = area( 7, 2 ); 39 | } 40 | 41 | // another error is when linking is that function declaration was missing a definition 42 | // I added a definition below 43 | int area( int length, int width ) 44 | { 45 | return length * width; 46 | } 47 | -------------------------------------------------------------------------------- /Section_9/Patron.cpp: -------------------------------------------------------------------------------- 1 | #include "Patron.hpp" 2 | 3 | namespace library 4 | { 5 | 6 | Patron::Patron() 7 | : m_name( "" ), m_card_number( 0 ), m_fees( 0.0 ) 8 | {} 9 | 10 | Patron::Patron( const std::string name, const int card_number ) 11 | : m_name( name ), m_card_number( card_number ), m_fees( 0.0 ) 12 | {} 13 | 14 | double Patron::get_fees() const 15 | { 16 | if( m_fees < 0.01 ) 17 | { 18 | return 0.0; 19 | } 20 | return m_fees; 21 | } 22 | 23 | double Patron::set_fees( const double f ) 24 | { 25 | m_fees += f; 26 | return get_fees(); 27 | } 28 | 29 | bool Patron::is_outstanding_fee() const 30 | { 31 | return m_fees >= 0.01; 32 | } 33 | 34 | std::ostream& operator<<( std::ostream& os, const Patron& p ) 35 | { 36 | os << p.m_name << '\n' << p.m_card_number; 37 | return os; 38 | } 39 | 40 | bool operator==( const Patron& p1, const Patron& p2 ) 41 | { 42 | return ( p1.m_name == p2.m_name && p1.m_card_number == p2.m_card_number ); 43 | } 44 | 45 | bool operator!=( const Patron& p1, const Patron& p2 ) 46 | { 47 | return !( p1 == p2 ); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Section_5/sect5_ex2.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 4 Apr 2017 3 | //Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 4 | 5 | /* 6 | sect 5 exercise 2 7 | find errors and document 8 | */ 9 | 10 | #include "section5.hpp" 11 | 12 | // converts Celsius to Kelvin 13 | // throws an exception if input is below -273.15C 14 | static double ctok( double c ) 15 | { 16 | if( c < 273.15 ) 17 | { 18 | error( std::format( "{} Can not be below absolute zero!", c ) ); 19 | } 20 | constexpr double conversion_constant{ 273.15 }; 21 | 22 | double celsiusToKelvin{ c + conversion_constant }; 23 | 24 | return celsiusToKelvin; 25 | } 26 | 27 | int main() 28 | try 29 | { 30 | std::cout << "Enter a temperature in Celsius to covert to Kelvin:\n"; 31 | double celsius{}; 32 | std::cin >> celsius; 33 | double kelvin{ ctok( celsius ) }; 34 | std::cout << kelvin << '\n'; 35 | 36 | keep_window_open(); 37 | return 0; 38 | } 39 | 40 | catch( std::exception& e ) 41 | { 42 | std::cerr << "error: " << e.what() << '\n'; 43 | keep_window_open(); 44 | return 1; 45 | } 46 | catch( ... ) 47 | { 48 | std::cerr << "Oops: unknown exception!\n"; 49 | keep_window_open(); 50 | return 2; 51 | } 52 | -------------------------------------------------------------------------------- /Section_3/sect3_ex9.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | 6 | /* 7 | Section 3 exercise 9. 8 | a program that converts spelled-out numbers such as “zero” and “two” into digits, such as 0 and 2. 9 | When the user inputs a number, the program should print out the corresponding digit. 10 | Do it for the values 0, 1, 2, 3, and 4 and write out not a number I know if the user enters 11 | something that doesn't correspond, such as stupid computer!. 12 | */ 13 | 14 | #include "section3.hpp" 15 | 16 | int main() 17 | { 18 | using namespace std; 19 | 20 | constexpr string_view lookup[]{ "zero", "one", "two", "three", "four" }; 21 | 22 | cout << "Enter a spelled out number (example \"two\" for 2)\n"; 23 | for( string spell_num; cin >> spell_num; ) 24 | { 25 | auto const val = find( cbegin( lookup ), cend( lookup ), spell_num ); 26 | if( val == cend( lookup ) ) 27 | { 28 | cout << spell_num << " is not a number I know!\n"; 29 | break; 30 | } 31 | cout << std::format( "The entered number {} is the number {}.\n", spell_num, val - cbegin( lookup ) ); 32 | } 33 | 34 | keep_window_open(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Section_3/sect3_9_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3.9 try this exercise 7 | */ 8 | 9 | #include "section3.hpp" 10 | 11 | //exit EOF or ctrl-z 12 | int main() 13 | { 14 | // In simple programs this using statement is ok but never use in a header file and be careful in larger projects. you have been warned! 15 | using namespace std; 16 | 17 | double d{}; 18 | cout << "Shows how C++ implicitly converts types. EOF to exit\n"; 19 | while( cin >> d ) 20 | { 21 | int i = d; // conversion from 'double' to 'int', possible loss of data 22 | int j{ static_cast( d ) }; // the {} enforces error narrowing conversion for possible loss of data and will get a compiler error 23 | char c = i; // conversion from 'int' to 'char', possible loss of data 24 | int i2 = c; // OK, all char values can fit into an int 25 | cout << format( "the entered double value: {}\n", d ); 26 | cout << format( "double converted to int: {}\n", i ); 27 | cout << format( "int converted to char: {}\n", c ); 28 | cout << format( "char converted to int: {}\n", i2 ); 29 | } 30 | 31 | keep_window_open(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Section_5/sect5_ex4.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 4 Apr 2017 3 | //Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 4 | 5 | /* 6 | sect 5 exercise 4 7 | Celsius to Kelvin converter 8 | throws error in function if input is below absolute 0 9 | */ 10 | #include "section5.hpp" 11 | 12 | // converts Celsius to Kelvin 13 | // throws an exception if input is below -273.15C 14 | static double ctok( double c ) 15 | { 16 | if( c < -273.15 ) 17 | { 18 | error( "Can not be below absolute zero!\n" ); 19 | } 20 | constexpr double conversion_constant{ 273.15 }; 21 | double celsiusToKelvin{ c + conversion_constant }; 22 | return celsiusToKelvin; 23 | } 24 | 25 | int main() 26 | try 27 | { 28 | std::cout << "Enter a temperature in Celsius to covert to Kelvin:\n"; 29 | double celsius{}; 30 | std::cin >> celsius; 31 | double kelvin{ ctok( celsius ) }; 32 | std::cout << kelvin << std::endl; 33 | 34 | keep_window_open(); 35 | return 0; 36 | } 37 | catch( std::exception& e ) 38 | { 39 | std::cerr << "error: " << e.what() << '\n'; 40 | keep_window_open(); 41 | return 1; 42 | } 43 | catch( ... ) 44 | { 45 | std::cerr << "Oops: unknown exception!\n"; 46 | keep_window_open(); 47 | return 2; 48 | } 49 | -------------------------------------------------------------------------------- /Section_9/sect9_ex6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 6 7 | Design and implement a Book class under library namespace 8 | - add == operator to for ISBNs 9 | - add != operator to for ISBNs 10 | - add << to print title, author, and ISBN on separate lines 11 | */ 12 | 13 | #include"../includes/ppp.hpp" 14 | 15 | #include "Book.hpp" 16 | 17 | int main() 18 | try 19 | { 20 | library::Book book1( "0-321-99278-4", "Programming Principles and Practices Using C++ Second Edition", "Bjarne Stroustrup", 2014 ); 21 | std::cout << book1 << "\n\n"; 22 | library::Book book2; 23 | library::Book book3 = book1; 24 | 25 | if( book1 == book3 ) 26 | { 27 | std::cout << book3 << "\n\n"; 28 | } 29 | 30 | if( book1 != book2 ) 31 | { 32 | std::cout << book2 << "\n\n"; 33 | } 34 | 35 | ppp::keep_window_open(); 36 | return 0; 37 | } 38 | catch( std::exception& e ) 39 | { 40 | std::cerr << "exception: " << e.what() << std::endl; 41 | ppp::keep_window_open(); 42 | return 1; 43 | } 44 | catch( ... ) 45 | { 46 | std::cerr << "exception\n"; 47 | ppp::keep_window_open(); 48 | return 2; 49 | } -------------------------------------------------------------------------------- /Section_5/sect5_ex3.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 5 Apr 2017 3 | //Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 4 | 5 | /* 6 | sect 5 exercise 3 7 | Celsius to Kelvin converter 8 | throws error in main if input is below absolute 0 9 | */ 10 | 11 | #include "section5.hpp" 12 | 13 | // converts Celsius to Kelvin 14 | static double ctok( double c ) 15 | { 16 | constexpr double conversion_constant{ 273.15 }; 17 | double celsiusToKelvin{ c + conversion_constant }; 18 | return celsiusToKelvin; 19 | } 20 | 21 | int main() 22 | try 23 | { 24 | std::cout << "Enter a temperature in Celsius to covert to Kelvin:\n"; 25 | double celsius{}; 26 | std::cin >> celsius; 27 | // throws an exception if input is below -273.15C 28 | if( celsius < -273.15 ) 29 | { 30 | error( "Can not be below absolute zero!\n" ); 31 | } 32 | double kelvin = ctok( celsius ); 33 | std::cout << kelvin << std::endl; 34 | 35 | keep_window_open(); 36 | return 0; 37 | } 38 | catch( std::exception& e ) 39 | { 40 | std::cerr << "error: " << e.what() << '\n'; 41 | keep_window_open(); 42 | return 1; 43 | } 44 | catch( ... ) 45 | { 46 | std::cerr << "Oops: unknown exception!\n"; 47 | keep_window_open(); 48 | return 2; 49 | } 50 | -------------------------------------------------------------------------------- /Section_4/sect4_drill1.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Drill step 1. 7 | Write a program that consists of a while-loop that (each time around the loop) reads in 8 | two INTs and then prints them. Exit the program when a terminating '|' is entered. 9 | */ 10 | 11 | #include "section4.hpp" 12 | 13 | int main() 14 | { 15 | constexpr char terminationChar = '|'; //termination character 16 | char c{}; 17 | while( c != terminationChar ) 18 | { 19 | std::cout << std::format( "Enter two numbers or enter {} to exit\n", terminationChar ); 20 | 21 | int val1{}, val2{}; 22 | std::cin >> val1 >> val2; 23 | 24 | //check for valid input 25 | if( !std::cin.good() ) 26 | { 27 | std::cin.clear(); 28 | std::cin.get( c ); 29 | if( c != terminationChar ) 30 | { 31 | std::cout << "Invalid number or termination, please try again.\n"; 32 | } 33 | continue; //goto next loop cycle 34 | } 35 | 36 | //print out valid numbers 37 | std::cout << std::format( "Entered numbers: {} {}\n", val1, val2 ); 38 | } 39 | 40 | keep_window_open(); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Section_9/sect9_ex5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 5 7 | Design and implement a Book class under library namespace 8 | - 9 | */ 10 | 11 | #include"../includes/ppp.hpp" 12 | 13 | #include "Book.hpp" 14 | 15 | std::string book_status( const library::Book& b ) 16 | { 17 | return std::move( std::format( 18 | "ISBN: {}\n" 19 | "TITLE: {}\n" 20 | "AUTHOR: {}\n" 21 | "COPYRIGHT: {}\n" 22 | "checked out: {}\n\n", 23 | b.get_isbn(), b.get_title(), b.get_author(), b.get_copyright(), b.is_checked_out() ) ); 24 | } 25 | 26 | int main() 27 | try 28 | { 29 | library::Book book( "0-321-99278-4", "Programming Principles and Practices Using C++ Second Edition", "Bjarne Stroustrup", 2014 ); 30 | std::cout << book_status( book ); 31 | 32 | book.check_inout( true ); 33 | std::cout << book_status( book ); 34 | 35 | ppp::keep_window_open(); 36 | return 0; 37 | } 38 | catch( std::exception& e ) 39 | { 40 | std::cerr << "exception: " << e.what() << std::endl; 41 | ppp::keep_window_open(); 42 | return 1; 43 | } 44 | catch( ... ) 45 | { 46 | std::cerr << "exception\n"; 47 | ppp::keep_window_open(); 48 | return 2; 49 | } 50 | -------------------------------------------------------------------------------- /Section_11/Section_11.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Header Files 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | -------------------------------------------------------------------------------- /Section_9/sect9_ex7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 7 7 | Design and implement a Book class under library namespace 8 | - add == operator to for ISBNs 9 | - add != operator to for ISBNs 10 | - add << to print title, author, and ISBN on separate lines 11 | - add class enum Genre 12 | */ 13 | 14 | #include"../includes/ppp.hpp" 15 | 16 | #include "Book.hpp" 17 | 18 | int main() 19 | try 20 | { 21 | using library::Book; 22 | 23 | Book book1( "0-321-99278-4", "Programming Principles and Practices Using C++ Second Edition", "Bjarne Stroustrup", 2014, Book::Genre::nonfiction ); 24 | std::cout << book1 << "\n\n"; 25 | Book book2; 26 | Book book3 = book1; 27 | 28 | if( book1 == book3 ) 29 | { 30 | std::cout << book3 << "\n\n"; 31 | } 32 | 33 | if( book1 != book2 ) 34 | { 35 | std::cout << book2 << "\n\n"; 36 | } 37 | 38 | ppp::keep_window_open(); 39 | return 0; 40 | } 41 | catch( std::exception& e ) 42 | { 43 | std::cerr << "exception: " << e.what() << std::endl; 44 | ppp::keep_window_open(); 45 | return 1; 46 | } 47 | catch( ... ) 48 | { 49 | std::cerr << "exception\n"; 50 | ppp::keep_window_open(); 51 | return 2; 52 | } -------------------------------------------------------------------------------- /Section_4/sect4_ex15.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 15. 7 | A program that takes an input value n and then finds the first n primes 8 | */ 9 | 10 | #include "section4.hpp" 11 | 12 | int main() 13 | { 14 | // container to store the prime numbers 15 | std::vector primes{}; 16 | 17 | std::size_t wantedPrimes{}; 18 | std::cout << "enter number of primes you want to find." << std::endl; 19 | if( !( std::cin >> wantedPrimes ) ) 20 | { 21 | simple_error( "entered invalid integer value\n" ); 22 | } 23 | 24 | // loop to found n primes and start with 2. 25 | for( std::size_t i{ 2 }; primes.size() < wantedPrimes; ++i ) 26 | { 27 | auto check = std::find_if( primes.cbegin(), primes.cend(), [i]( auto p ) { return i % p == 0; } ); 28 | if( check == primes.cend() ) 29 | { 30 | primes.push_back( i ); 31 | } 32 | } 33 | 34 | // print out the prime numbers 35 | std::cout << std::format("The number of primes found is {}.\n", primes.size()); 36 | for( int j{ 1 }; auto x : primes ) 37 | { 38 | //10 per row 39 | std::cout << x << ( j % 10 != 0 ? '\t' : '\n' ); 40 | ++j; 41 | } 42 | std::cout << std::endl; 43 | 44 | keep_window_open(); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Section_11/Punct_stream.cpp: -------------------------------------------------------------------------------- 1 | #include "Punct_stream.hpp" 2 | #include 3 | 4 | bool Punct_stream::is_whitespace( const char c ) const 5 | { 6 | if( !white.empty() ) 7 | { 8 | return ( white.find( c ) != std::string::npos ) ? true : false; 9 | } 10 | else 11 | { 12 | auto ch = static_cast( c ); 13 | return ch != '\'' && std::ispunct( ch ) != 0 || std::isdigit( ch ) != 0; 14 | } 15 | } 16 | 17 | Punct_stream &Punct_stream::operator>>( std::string &s ) 18 | { 19 | while( !( buffer >> s ) ) // try to read from buffer 20 | { 21 | if( buffer.bad() || !source.good() ) 22 | { 23 | return *this; 24 | } 25 | buffer.clear(); 26 | 27 | std::string line; 28 | getline( source, line ); // get a line from source 29 | 30 | // do character replacement as needed: 31 | for( char &ch : line ) 32 | { 33 | if( is_whitespace( ch ) ) 34 | { 35 | ch = ' '; 36 | } 37 | else if( !sensitive ) 38 | { 39 | ch = static_cast( std::tolower( static_cast( ch ) ) ); 40 | } 41 | } 42 | buffer.str( line ); 43 | } 44 | return *this; 45 | } 46 | 47 | Punct_stream::operator bool() 48 | { 49 | return !( source.fail() || source.bad() ) && buffer.good(); 50 | } 51 | -------------------------------------------------------------------------------- /Section_11/sect11_ex10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 24 Nov 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 11 Exercise 10 7 | Write a function vector split(const string& s) that returns a vector of whitespace-separated substrings 8 | from the argument s. 9 | */ 10 | 11 | #include"ppp.hpp" 12 | #include 13 | 14 | std::vector split( const std::string &s ); 15 | 16 | int main() 17 | try 18 | { 19 | constexpr std::string_view str{ "Stroustrup, Bjarne. Programming: Principles and Practice Using C++. Pearson Education. Kindle Edition." }; 20 | auto white_space{ split( str.data() ) }; 21 | 22 | for( auto &s : white_space ) 23 | { 24 | std::println( "{}", s ); 25 | } 26 | 27 | ppp::keep_window_open(); 28 | return 0; 29 | } 30 | catch( std::exception &e ) 31 | { 32 | std::println( "exception: {}", e.what() ); 33 | ppp::keep_window_open(); 34 | return 1; 35 | } 36 | catch( ... ) 37 | { 38 | std::println( "exception" ); 39 | ppp::keep_window_open(); 40 | return 2; 41 | } 42 | 43 | std::vector split( const std::string &s ) 44 | { 45 | std::vector strings; 46 | std::istringstream is{ s }; 47 | for( std::string str; is >> str; ) 48 | { 49 | strings.push_back( str ); 50 | } 51 | return strings; 52 | } 53 | -------------------------------------------------------------------------------- /Section_4/sect4_ex12.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 12. 7 | Modify the program described in the previous exercise 11 to take an input value max 8 | and then find all prime numbers from 1 to max. 9 | */ 10 | 11 | #include "section4.hpp" 12 | 13 | int main() 14 | { 15 | // container to store the prime numbers 16 | std::vector primes{}; 17 | 18 | int max{}; 19 | std::cout << "enter number that want to find all primes up to it." << std::endl; 20 | if( !( std::cin >> max ) || max < 1 ) 21 | { 22 | simple_error( "entered invalid integer value or maximum is less than 1" ); 23 | } 24 | 25 | // start with 2 26 | for( int i{ 2 }; i <= max; ++i ) 27 | { 28 | auto check = std::find_if( primes.cbegin(), primes.cend(), [i]( auto p ) { return i % p == 0; } ); 29 | if( check == primes.cend() ) 30 | { 31 | primes.push_back( i ); 32 | } 33 | } 34 | 35 | // print out the prime numbers 36 | std::cout << std::format( "The amount of prime numbers between 1 and {} is {}.\n", max, primes.size() ); 37 | for( int j{ 1 }; auto x : primes ) 38 | { 39 | //10 per row 40 | std::cout << x << ( j % 10 != 0 ? '\t' : '\n' ); 41 | ++j; 42 | } 43 | 44 | std::cout << std::endl; 45 | keep_window_open(); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Section_8/sect8_ex10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 6 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 10 7 | - Write a function maxv(). 8 | - Return largest argument of the vectors elements 9 | - Check that vector is not empty. 10 | */ 11 | 12 | #include"../includes/ppp.hpp" 13 | 14 | template 15 | T maxv( const std::vector& v ) 16 | { 17 | if( v.empty() ) 18 | { 19 | ppp::error( "Vector is empty!" ); 20 | } 21 | T largest{ v.front() }; 22 | for( auto elem : v ) 23 | { 24 | if( largest < elem ) 25 | { 26 | largest = elem; 27 | } 28 | } 29 | return largest; 30 | } 31 | 32 | int main() 33 | try 34 | { 35 | std::vector price{ 3.99, 4.5, 3.45 }; 36 | auto d{ maxv( price ) }; 37 | std::cout << "Largest price: " << d << std::endl; 38 | std::vector number{ "one", "two", "three", "zero", "five" }; 39 | auto str{ maxv( number ) }; 40 | std::cout << "Highest alphabetical string: " << str << std::endl; 41 | 42 | ppp::keep_window_open(); 43 | return 0; 44 | } 45 | catch( std::exception& e ) 46 | { 47 | std::cerr << "exception: " << e.what() << std::endl; 48 | ppp::keep_window_open(); 49 | return 1; 50 | } 51 | catch( ... ) 52 | { 53 | std::cerr << "exception\n"; 54 | ppp::keep_window_open(); 55 | return 2; 56 | } -------------------------------------------------------------------------------- /Section_12/sect12_ex4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | 7 July 2024 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 12 Exercise 4 7 | Draw a 3-by-3 tic-tac-toe board of alternating white and red squares. 8 | */ 9 | 10 | #include "Simple_window.hpp" 11 | #include 12 | 13 | int main() 14 | try 15 | { 16 | using namespace GraphLib; 17 | 18 | Point top_left{ 100,100 }; 19 | Simple_Window win{ top_left,600,400,"Exercise #4" }; 20 | 21 | Point position{}; 22 | std::vector squares; 23 | for( int i{ 1 }; i <= 9; ++i ) 24 | { 25 | if( i == 4 || i == 7 ) 26 | { 27 | position.x = 0; 28 | position.y += 100; 29 | } 30 | Color::Color_Type wc = ( i % 2 ) ? Color::Color_Type::white : Color::Color_Type::red; 31 | GraphLib::Rectangle rectangle{ position, 100, 100 }; 32 | rectangle.Set_Color( wc ); 33 | rectangle.Set_Fill_Color( wc ); 34 | squares.push_back( rectangle ); 35 | position.x += 100; 36 | } 37 | 38 | for( auto &r : squares ) 39 | { 40 | win.Attach( r ); 41 | } 42 | 43 | win.Wait_For_Button(); 44 | 45 | return 0; 46 | } 47 | catch( std::exception &e ) 48 | { 49 | std::println( "exception: {}", e.what() ); 50 | return 1; 51 | } 52 | catch( ... ) 53 | { 54 | std::println( "exception" ); 55 | return 2; 56 | } 57 | -------------------------------------------------------------------------------- /Section_3/sect3_ex4.cpp: -------------------------------------------------------------------------------- 1 | // written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | 6 | /* 7 | Section 3 exercise 4. 8 | A program that prompts the user to enter two integer values. 9 | Store these values in int variables named val1 and val2. 10 | */ 11 | 12 | #include "section3.hpp" 13 | 14 | int main() 15 | { 16 | using namespace std; 17 | 18 | cout << "Enter two integer values:\n"; 19 | int val1{}; 20 | int val2{}; 21 | 22 | if( !( cin >> val1 >> val2 ) ) 23 | { 24 | simple_error( "Invalid entry!" ); 25 | } 26 | 27 | if( val1 < val2 ) 28 | { 29 | cout << std::format( "{} < {}\n", val1, val2 ); 30 | } 31 | else if( val1 > val2 ) 32 | { 33 | cout << std::format( "{} > {}\n", val1, val2 ); 34 | } 35 | else 36 | { 37 | cout << std::format( "{} == {}\n", val1, val2 ); 38 | } 39 | 40 | cout << std::format( "{} + {} = {}\n", val1, val2, val1 + val2 ); 41 | cout << std::format( "{} - {} = {}\n", val1, val2, val1 - val2 ); 42 | cout << std::format( "{} * {} = {}\n", val1, val2, val1 * val2 ); 43 | if( val2 == 0 ) 44 | { 45 | simple_error( "I don't divide by zero" ); 46 | } 47 | else 48 | { 49 | cout << std::format( "{} / {} = {}\n", val1, val2, static_cast( val1 ) / static_cast( val2 ) ); 50 | } 51 | 52 | keep_window_open(); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Section_10/sect10_ex11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Sept 8 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Exercise 11 7 | Write a program that produces the sum of all the whitespace-separated integers in a text file. 8 | For example, bears: 17 elephants 9 end should output 26. 9 | */ 10 | 11 | #include "../includes/ppp.hpp" 12 | 13 | constexpr std::string_view txt_file{ "sect10_ex11.txt" }; 14 | 15 | int main() 16 | try 17 | { 18 | std::ifstream fin{ txt_file.data() }; 19 | if( !fin ) 20 | { 21 | ppp::error( "could not open ", txt_file.data() ); 22 | } 23 | 24 | int sum{}; 25 | for( std::string s; fin >> s; ) 26 | { 27 | try 28 | { 29 | sum += std::stoi( s ); 30 | } 31 | // https://en.cppreference.com/w/cpp/language/attributes 32 | catch( [[maybe_unused]] std::invalid_argument &e ) 33 | { 34 | // catch the exception 35 | } 36 | } 37 | std::cout << "sum of white space separated integers is " << sum << "\n"; 38 | 39 | ppp::keep_window_open(); 40 | return 0; 41 | } 42 | catch( std::exception &e ) 43 | { 44 | std::cerr << R"(exception: )" << e.what() << std::endl; 45 | ppp::keep_window_open(); 46 | return 1; 47 | } 48 | catch( ... ) 49 | { 50 | std::cerr << R"(exception)"; 51 | ppp::keep_window_open(); 52 | return 2; 53 | } 54 | -------------------------------------------------------------------------------- /Section_3/sect3_ex5.cpp: -------------------------------------------------------------------------------- 1 | // written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | 6 | /* 7 | Section 3 exercise 5 8 | repeat exercise 4 using double values 9 | Write your program to determine the smaller, larger, sum, difference, 10 | product, and ratio of these values and report them to the user. 11 | */ 12 | 13 | #include "section3.hpp" 14 | 15 | int main() 16 | { 17 | using namespace std; 18 | 19 | cout << "Enter two double values:\n"; 20 | double val1{}; 21 | double val2{}; 22 | 23 | if (!(cin >> val1 >> val2)) 24 | { 25 | simple_error("Invalid entry!"); 26 | } 27 | 28 | if (val1 < val2) 29 | { 30 | cout << std::format("{} < {}\n", val1, val2); 31 | } 32 | else if (val1 > val2) 33 | { 34 | cout << std::format("{} > {}\n", val1, val2); 35 | } 36 | else if (val1 == val2) 37 | { 38 | cout << std::format("{} == {}\n", val1, val2); 39 | } 40 | else 41 | { 42 | simple_error("Not sure how I got here?"); 43 | } 44 | 45 | cout << std::format("{} + {} = {}\n", val1, val2, val1 + val2); 46 | cout << std::format("{} - {} = {}\n", val1, val2, val1 - val2); 47 | cout << std::format("{} * {} = {}\n", val1, val2, val1 * val2); 48 | if (val2 == 0) 49 | { 50 | simple_error("I don't divide by zero"); 51 | } 52 | else 53 | { 54 | cout << std::format("{} / {} = {}\n", val1, val2, val1 / val2); 55 | } 56 | 57 | keep_window_open(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Section_4/sect4_4_try1.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 25 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Try This 4.4.1.1 7 | program that converts yen, euros, and pounds into dollars 8 | */ 9 | 10 | #include "section4.hpp" 11 | 12 | int main() 13 | { 14 | using namespace std; 15 | 16 | constexpr double yen_to_dollar{ 113.21 }; 17 | constexpr double euro_to_dollar{ 0.88 }; 18 | constexpr double pound_to_dollar{ 0.77 }; 19 | 20 | long double amount{ 1.0L }; 21 | string currency; 22 | cout << "Please enter an amount followed by a currency to convert to dollar:\n"; 23 | cout << "yen, euro, or pound\n"; 24 | 25 | if( !( cin >> amount ) ) 26 | { 27 | simple_error( "Incorrect amount entry\n" ); 28 | } 29 | 30 | cin >> currency; 31 | if( currency == "yen" ) 32 | { 33 | cout << std::format( "{} {} == ${}\n", amount, currency, 1 / yen_to_dollar * amount ); 34 | } 35 | else if( currency == "euro" ) 36 | { 37 | cout << std::format( "{} {} == ${}\n", amount, currency, 1 / euro_to_dollar * amount ); 38 | } 39 | else if( currency == "pound" ) 40 | { 41 | cout << std::format( "{} {} == ${}\n", amount, currency, 1 / pound_to_dollar * amount ); 42 | } 43 | else 44 | { 45 | cout << std::format( "Sorry, I don't know a currency called {}\n", currency ); 46 | } 47 | 48 | keep_window_open(); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Section_12/sect12_ex1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Aug 2 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 12 Exercise 1 7 | Draw a rectangle as a Rectangle and as a Polygon. 8 | Make the lines of the Polygon red and the lines of the Rectangle blue. 9 | */ 10 | 11 | #include "Simple_window.hpp" 12 | #include "Window.hpp" 13 | #include 14 | 15 | //void quit_cb( Fl_Widget *w, void *p ); 16 | int main() 17 | try 18 | { 19 | using namespace GraphLib; 20 | 21 | Point top_left{ 100,100 }; 22 | Simple_Window win{ top_left,600,400,"Exercise #1" }; 23 | 24 | GraphLib::Rectangle rectangle{ Point{100,100}, 100, 100 }; 25 | rectangle.Set_Color( Color::Color_Type::blue ); 26 | rectangle.Set_Fill_Color( Color::Color_Type::blue ); 27 | win.Attach( rectangle ); 28 | 29 | // define with namespace or ambiguous with Polygon in wingdi.h 30 | GraphLib::Polygon poly; 31 | poly.Add( Point{ 300,100 } ); 32 | poly.Add( Point{ 400,100 } ); 33 | poly.Add( Point{ 400,200 } ); 34 | poly.Add( Point{ 300,200 } ); 35 | poly.Set_Color( Color::Color_Type::red ); 36 | poly.Set_Fill_Color( Color::Color_Type::red ); 37 | win.Attach( poly ); 38 | 39 | win.Wait_For_Button(); 40 | } 41 | catch( std::exception &e ) 42 | { 43 | std::println( "exception: {}", e.what() ); 44 | return 1; 45 | } 46 | catch( ... ) 47 | { 48 | std::println( "exception" ); 49 | return 2; 50 | } 51 | -------------------------------------------------------------------------------- /Section_9/sect9_ex14.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Feb 2 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 14 7 | this is the test program Money class. 8 | */ 9 | 10 | #include"../includes/ppp.hpp" 11 | 12 | #include "Money.hpp" 13 | int main() 14 | try 15 | { 16 | Money m1{ "$199.999" }; 17 | std::cout << m1.get_cents() << '\n'; 18 | 19 | Money m2{ "$5123.5689" }; 20 | std::cout << m2.get_cents() << '\n'; 21 | 22 | Money m3{ "$0.005" }; 23 | std::cout << m3.get_cents() << '\n'; 24 | 25 | Money m4{ "$0.004" }; 26 | std::cout << m4.get_cents() << '\n'; 27 | 28 | Money m5{ "$1" }; 29 | std::cout << m5.get_cents() << '\n'; 30 | 31 | Money m6{ "$.995" }; 32 | std::cout << m6.get_cents() << '\n'; 33 | 34 | //// missing currency type 35 | //Money m7{".995"}; 36 | //std::cout << m7.get_cents() << '\n'; 37 | 38 | //// malformed currency 39 | //Money m8{"$..995"}; 40 | //std::cout << m8.get_cents() << '\n'; 41 | 42 | //// malformed currency 43 | //Money m9{"$10.995D"}; 44 | //std::cout << m9.get_cents() << '\n'; 45 | 46 | ppp::keep_window_open(); 47 | return 0; 48 | } 49 | catch( std::exception& e ) 50 | { 51 | std::cerr << "exception: " << e.what() << std::endl; 52 | ppp::keep_window_open(); 53 | return 1; 54 | } 55 | catch( ... ) 56 | { 57 | std::cerr << "exception\n"; 58 | ppp::keep_window_open(); 59 | return 2; 60 | } 61 | -------------------------------------------------------------------------------- /Section_4/sect4_ex13.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 13. 7 | Create a program to find all the prime numbers between 1 and 100. 8 | There is a classic method for doing this, called the “Sieve of Eratosthenes.” 9 | If you don’t know that method, get on the web and look it up. Write your program using this method. 10 | https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 11 | */ 12 | 13 | #include "section4.hpp" 14 | 15 | int main() 16 | { 17 | std::size_t constexpr max{ 100 }; 18 | 19 | // build table 20 | std::vector sieve( max, true ); 21 | 22 | // set first 2 elements representing 0 and 1 to false as not prime 23 | sieve[0] = sieve[1] = false; 24 | 25 | for( std::size_t i{}; i < sieve.size(); ++i ) 26 | { 27 | if( sieve[i] == true ) 28 | { 29 | for( std::size_t j{ 2 }; ( j * i ) < sieve.size(); ++j ) 30 | { 31 | sieve[i * j] = false; 32 | } 33 | } 34 | } 35 | 36 | // print out the prime numbers 37 | for( std::size_t columnSize{ 1 }, index{}; index != sieve.size(); ++index ) 38 | { 39 | //10 columns 40 | if( sieve[index] == true ) 41 | { 42 | std::cout << index << ( columnSize % 10 != 0 ? '\t' : '\n' ); 43 | ++columnSize; 44 | } 45 | } 46 | 47 | std::cout << std::endl; 48 | keep_window_open(); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Section_10/sect10_drill2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Mar 24 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Drill 2 7 | Using the code and discussion in Section 10.4, prompt the user to input seven (x,y) pairs. 8 | As the data is entered, store it in a vector of Points called original_points. 9 | */ 10 | 11 | #include "../includes/ppp.hpp" 12 | #include "Point.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | constexpr size_t how_many{ 7 }; 18 | using point_type = int; 19 | 20 | std::cout << "Enter " << how_many << " ( x, y ) pairs:\n"; 21 | std::vector> original_points(how_many, Point() ); 22 | 23 | for( auto& p : original_points ) 24 | { 25 | while( !( std::cin >> p ) ) 26 | { 27 | if( std::cin.eof() ) 28 | { 29 | ppp::error( "EOF found before filling required data" ); 30 | } 31 | std::cout << "bad input try again\n"; 32 | std::cin.clear(); 33 | std::cin.ignore( std::numeric_limits::max(), '\n'); 34 | } 35 | } 36 | 37 | ppp::keep_window_open(); 38 | return 0; 39 | } 40 | catch( std::exception& e ) 41 | { 42 | std::cerr << "exception: " << e.what() << std::endl; 43 | ppp::keep_window_open(); 44 | return 1; 45 | } 46 | catch( ... ) 47 | { 48 | std::cerr << "exception\n"; 49 | ppp::keep_window_open(); 50 | return 2; 51 | } -------------------------------------------------------------------------------- /Section_10/sect10_drill3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Mar 24 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Drill 3 7 | Print the data in original_points to see what it looks like. 8 | */ 9 | 10 | #include "../includes/ppp.hpp" 11 | #include "Point.hpp" 12 | 13 | int main() 14 | try 15 | { 16 | constexpr size_t how_many{ 7 }; 17 | using point_type = int; 18 | 19 | std::cout << "Enter " << how_many << " ( x, y ) pairs:\n"; 20 | std::vector> original_points( how_many, Point() ); 21 | 22 | for( auto &p : original_points ) 23 | { 24 | while( !( std::cin >> p ) ) 25 | { 26 | if( std::cin.eof() ) 27 | { 28 | ppp::error( "EOF found before filling required data" ); 29 | } 30 | std::cout << "bad input try again\n"; 31 | std::cin.clear(); 32 | std::cin.ignore( std::numeric_limits::max(), '\n' ); 33 | } 34 | } 35 | 36 | for( const auto &p : original_points ) 37 | { 38 | std::cout << p << "\n"; 39 | } 40 | 41 | ppp::keep_window_open(); 42 | return 0; 43 | } 44 | catch( std::exception &e ) 45 | { 46 | std::cerr << "exception: " << e.what() << std::endl; 47 | ppp::keep_window_open(); 48 | return 1; 49 | } 50 | catch( ... ) 51 | { 52 | std::cerr << "exception\n"; 53 | ppp::keep_window_open(); 54 | return 2; 55 | } -------------------------------------------------------------------------------- /Section_9/Book.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 20 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Book class under library namespace 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | 13 | namespace library 14 | { 15 | class Book 16 | { 17 | public: 18 | enum class Genre { unknown, fiction, nonfiction, periodical, biography, children }; 19 | 20 | Book(); 21 | Book( std::string ISBN, std::string title, std::string author, int copyright_year, Genre genre = Genre::unknown ); 22 | 23 | std::string get_isbn() const { return m_isbn; } 24 | std::string get_title() const { return m_title; } 25 | std::string get_author() const { return m_author; } 26 | int get_copyright() const { return m_copyright_year; } 27 | Genre get_genre() const { return m_genre; } 28 | bool is_checked_out() const { return m_checked_out; } 29 | 30 | void check_inout( bool cio ) { m_checked_out = cio; } 31 | 32 | friend std::ostream& operator<<( std::ostream& os, const Book& b ); 33 | friend bool operator==( const Book& b1, const Book& b2 ); 34 | friend bool operator!=( const Book& b1, const Book& b2 ); 35 | 36 | private: 37 | std::string m_isbn; 38 | std::string m_title; 39 | std::string m_author; 40 | int m_copyright_year; 41 | Genre m_genre; 42 | bool m_checked_out; 43 | bool isbn_valid() const; 44 | }; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Section_9/sect9_ex4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 18 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 4 7 | */ 8 | 9 | /* 10 | // dangerously ugly code 11 | struct X { 12 | void f(int x) { 13 | struct Y { 14 | int f() { return 1; } int m; }; 15 | int m; 16 | m=x; Y m2; 17 | return f(m2.f()); } 18 | int m; void g(int m) { 19 | if (m) f(m+2); else { 20 | g(m+2); }} 21 | X() { } void m3() { 22 | } 23 | void main() { 24 | X a; a.f(2);} 25 | }; 26 | */ 27 | 28 | struct X 29 | { 30 | void f( int x ) 31 | { 32 | struct Y 33 | { 34 | int f() { return 1; } 35 | int m; 36 | }; 37 | int m; // declares a variable of type int hides the upper scpe of m 38 | m = x; // assign the function argument to m 39 | Y m2; // declares a variable of type Y 40 | return f( m2.f() ); // recursive function call which will throw a stack overflow error 41 | } 42 | 43 | int m; // top most scoped declaration variable m of type int 44 | 45 | void g( int m ) 46 | { 47 | if( m ) // statement is only false when m = 0; 48 | { 49 | f( m + 2 ); 50 | } 51 | else 52 | { 53 | g( m + 2 ); 54 | } 55 | } 56 | 57 | X() {} // constructor for X 58 | void m3() {} // member function of class which does nothing 59 | }; 60 | 61 | 62 | int main() 63 | { 64 | X a; 65 | a.f( 2 ); 66 | } -------------------------------------------------------------------------------- /Section_9/sect9_ex13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 31 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 13 7 | this is the test program Rational class. 8 | */ 9 | 10 | #include"../includes/ppp.hpp" 11 | 12 | #include "Rational.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | Rational r1{ 4,8 }; 18 | std::cout << "r1 = " << r1.get_numerator() << '/' << r1.get_denominator() << '\n'; 19 | 20 | Rational r2{ 3,12 }; 21 | std::cout << "r2 = " << r2.get_numerator() << '/' << r2.get_denominator() << '\n'; 22 | 23 | auto r3 = r1 + r2; 24 | std::cout << "r3 = " << r3.get_numerator() << '/' << r3.get_denominator() << '\n'; 25 | 26 | auto r4 = r3 - Rational( 2, 7 ); 27 | std::cout << "r4 = " << r4.get_numerator() << '/' << r4.get_denominator() << '\n'; 28 | 29 | auto r5 = r1 * Rational( 1, 2 ); 30 | std::cout << "r5 = " << r5.get_numerator() << '/' << r5.get_denominator() << '\n'; 31 | 32 | auto r6 = r1 / Rational( 3, 16 ); 33 | std::cout << "r6 = " << r6.get_numerator() << '/' << r6.get_denominator() << '\n'; 34 | 35 | double r7 = r6; 36 | std::cout << "r7 = " << r7 << '\n'; 37 | 38 | ppp::keep_window_open(); 39 | return 0; 40 | } 41 | catch( std::exception& e ) 42 | { 43 | std::cerr << "exception: " << e.what() << std::endl; 44 | ppp::keep_window_open(); 45 | return 1; 46 | } 47 | catch( ... ) 48 | { 49 | std::cerr << "exception\n"; 50 | ppp::keep_window_open(); 51 | return 2; 52 | } 53 | -------------------------------------------------------------------------------- /Section_6/section6.hpp: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // 8 Apr 2017 3 | // Header file used for Programming: Principles and Practice Using C++ Second Edition 4 | // section 6 5 | 6 | #ifndef SECTION6_H // begin header guard 7 | #define SECTION6_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | // simple function to keep window console open 22 | inline void keep_window_open() 23 | { 24 | std::cin.clear(); 25 | std::cin.sync_with_stdio( false ); 26 | std::cin.ignore( std::cin.rdbuf()->in_avail() ); //clear buffer 27 | std::cout << "\nPress the Enter key to continue"; 28 | std::cin.get(); 29 | } 30 | 31 | inline void error( const std::string& s ) 32 | { 33 | throw std::runtime_error( s ); 34 | } 35 | 36 | inline void error( const std::string& s, const std::string& s2 ) 37 | { 38 | error( s + s2 ); 39 | } 40 | 41 | inline void error( const std::string& s, int i ) 42 | { 43 | std::ostringstream os; 44 | os << s << ": " << i; 45 | error( os.str() ); 46 | } 47 | 48 | // run-time checked narrowing cast (type conversion). 49 | template 50 | Target narrow_cast( const Source& a ) 51 | { 52 | auto r = static_cast( a ); 53 | if( static_cast( r ) != a ) 54 | { 55 | error( "narrow_cast<>() failed" ); 56 | } 57 | return r; 58 | } 59 | #endif // close header guard 60 | -------------------------------------------------------------------------------- /Section_5/sect5_ex11.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 7 Apr 2017 3 | //Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 4 | 5 | /* 6 | Section 5 exercise 11 7 | A program that writes out the Fibonacci series up to largest to fit in an int type 8 | */ 9 | 10 | #include "section5.hpp" 11 | 12 | using vType = std::vector; 13 | vType::value_type add( vType::value_type n1, vType::value_type n2 ); 14 | 15 | int main() 16 | try 17 | { 18 | vType fibonacci{ 1,1 }; 19 | for( vType::difference_type i{}; /*intentional no conditional*/; ++i ) 20 | { 21 | std::cout << fibonacci[i] << std::endl; 22 | fibonacci.push_back( add( fibonacci[i], fibonacci[i + 1] ) ); 23 | } 24 | 25 | keep_window_open(); // yes I know will never get to this point 26 | return 0; 27 | } 28 | catch( std::exception& e ) 29 | { 30 | std::cerr << "error: " << e.what() << '\n'; 31 | keep_window_open(); 32 | return 1; 33 | } 34 | catch( ... ) 35 | { 36 | std::cerr << "Oops: unknown exception!\n"; 37 | keep_window_open(); 38 | return 2; 39 | } 40 | 41 | /* addition function. 42 | Inputs: two numbers 43 | outputs: valid resulting number 44 | Errors: element overflow 45 | */ 46 | vType::value_type add( vType::value_type n1, vType::value_type n2 ) 47 | { 48 | if( ( ( n2 > 0 ) && ( n1 > ( std::numeric_limits< vType::value_type>::max() - n2 ) ) ) || 49 | ( ( n2 < 0 ) && ( n1 < ( std::numeric_limits< vType::value_type>::min() - n2 ) ) ) ) 50 | { 51 | error( "overflow error" ); 52 | } 53 | return n1 + n2; 54 | } 55 | -------------------------------------------------------------------------------- /Section_9/Book.cpp: -------------------------------------------------------------------------------- 1 | #include "Book.hpp" 2 | 3 | #include 4 | 5 | namespace library 6 | { 7 | Book::Book() 8 | : m_isbn( "0-0-0-x" ), 9 | m_title( "?" ), 10 | m_author( "?" ), 11 | m_copyright_year( 9999 ), 12 | m_checked_out( false ) 13 | {} 14 | 15 | Book::Book( std::string ISBN, std::string title, std::string author, int copyright_year, Genre genre ) 16 | : m_isbn( ISBN ), m_title( title ), m_author( author ), m_copyright_year( copyright_year ), m_genre( genre ), m_checked_out( false ) 17 | { 18 | if( !isbn_valid() ) throw std::runtime_error( "Invalid ISBN" ); 19 | } 20 | 21 | bool Book::isbn_valid() const 22 | { 23 | std::stringstream ss; 24 | ss << m_isbn; 25 | int n1{}, n2{}, n3{}; 26 | char c1{}, c2{}, c3{}, c4{}; 27 | ss >> n1 >> c1 >> n2 >> c2 >> n3 >> c3 >> c4; 28 | if( ss.good() && c1 == '-' && c2 == '-' && c3 == '-' && isalnum( c4 ) ) 29 | { 30 | return true; 31 | } 32 | return false; 33 | } 34 | 35 | std::ostream& operator<<( std::ostream& os, const Book& b ) 36 | { 37 | os << "TITLE: " << b.m_title << '\n' 38 | << "AUTHOR: " << b.m_author << '\n' 39 | << "ISBN: " << b.m_isbn; 40 | return os; 41 | } 42 | 43 | bool operator==( const Book& b1, const Book& b2 ) 44 | { 45 | return b1.m_isbn == b2.m_isbn; 46 | } 47 | 48 | bool operator!=( const Book& b1, const Book& b2 ) 49 | { 50 | return !( b1 == b2 ); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Section_4/sect4_ex21.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 21. 7 | Modify exercise 19 so that when you enter a number, the program will output all the 8 | names with that score or score not found. 9 | */ 10 | 11 | #include "section4.hpp" 12 | 13 | int main() 14 | { 15 | using pType = std::vector>; 16 | pType::value_type name_score; 17 | pType ranks{ {"Moe",1}, {"Larry",3}, {"Curly", 3},{"Shemp",4} }; 18 | 19 | std::cout << "Enter a score and see all that have same score.\n"; 20 | 21 | double score{}; 22 | std::cin >> score; 23 | if( !std::cin ) 24 | { 25 | std::cout << "Invalid score entered!\n"; 26 | } 27 | else 28 | { 29 | // find all that have that score 30 | std::vector matches; 31 | for( auto i{ ranks.cbegin() }; i != ranks.cend(); ++i ) 32 | { 33 | if( i->second == score ) 34 | { 35 | matches.push_back( i ); 36 | } 37 | } 38 | 39 | // see if no match 40 | if( matches.empty() ) 41 | { 42 | std::cout << "No matching names with a score of " << score << ".\n"; 43 | } 44 | else 45 | { 46 | for( auto i : matches ) 47 | { 48 | std::cout << std::format("Name: {}\tScore: {}", i->first, i->second) << std::endl; 49 | } 50 | } 51 | } 52 | 53 | keep_window_open(); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Section_4/sect4_ex3.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 3. 7 | Read a sequence of double values into a vector. 8 | Think of each value as the distance between two cities along a given route. 9 | Compute and print the total distance (the sum of all distances). 10 | Find and print the smallest and greatest distance between two neighboring cities. 11 | Find and print the mean distance between two neighboring cities. 12 | */ 13 | 14 | #include "section4.hpp" 15 | 16 | int main() 17 | { 18 | std::vector distances; 19 | 20 | std::cout << "Please enter a sequence of numbers (representing distances).\n"; 21 | 22 | for( double distance{}; std::cin >> distance; ) 23 | { 24 | distances.push_back( distance ); 25 | } 26 | 27 | std::string output; 28 | if( distances.empty() ) 29 | { 30 | output = std::format( "no distances entered\n" ); 31 | } 32 | else 33 | { 34 | auto sum{ std::accumulate( distances.begin(), distances.end(), 0.0 ) }; 35 | output = std::format( "The total distance: {}\n", sum ); 36 | std::sort( distances.begin(), distances.end() ); 37 | output = std::format( "{}The smallest distance: {}\n", output, distances.front() ); 38 | output = std::format( "{}The greatest distance: {}\n", output, distances.back() ); 39 | output = std::format( "{}The mean distance: {}\n", output, sum / distances.size() ); 40 | } 41 | std::cout << output << std::endl; 42 | 43 | keep_window_open(); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Section_12/fltk.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define WIN32 4 | #define WIN32_LEAN_AND_MEAN 5 | //#define NOGDICAPMASKS 6 | //#define NOSYSMETRICS 7 | //#define NOMENUS 8 | //#define NOICONS 9 | //#define NOSYSCOMMANDS 10 | //#define NORASTEROPS 11 | //#define OEMRESOURCE 12 | //#define NOATOM 13 | //#define NOCLIPBOARD 14 | //#define NOCOLOR 15 | //#define NOCTLMGR 16 | //#define NODRAWTEXT 17 | //#define NOKERNEL 18 | //#define NONLS 19 | //#define NOMEMMGR 20 | //#define NOMETAFILE 21 | #define NOMINMAX 22 | //#define NOOPENFILE 23 | //#define NOSCROLL 24 | //#define NOSERVICE 25 | //#define NOSOUND 26 | //#define NOTEXTMETRIC 27 | //#define NOWH 28 | //#define NOCOMM 29 | //#define NOKANJI 30 | //#define NOHELP 31 | //#define NOPROFILER 32 | //#define NODEFERWINDOWPOS 33 | //#define NOMCX 34 | //#define NORPC 35 | //#define NOPROXYSTUB 36 | //#define NOIMAGE 37 | //#define NOTAPE 38 | //#define STRICT 39 | 40 | #include // for exit(0) 41 | 42 | #pragma warning(push) 43 | #pragma warning(disable : 4244) // 'argument' : conversion from 'type1' to 'type2', possible loss of data 44 | #pragma warning(disable : 4458) // declaration of 'identifier' hides class member 45 | #pragma warning(disable : 4100) // 'identifier' : unreferenced formal parameter 46 | 47 | #include "FL/Fl.H" 48 | #include "FL/Fl_Box.H" 49 | #include "FL/Fl_Window.H" 50 | #include "FL/Fl_Button.H" 51 | #include "FL/Fl_Input.H" 52 | #include "FL/Fl_Output.H" 53 | #include "FL/fl_draw.H" 54 | #include "FL/Enumerations.H" 55 | #include "FL/Fl_Group.H" 56 | 57 | #include "Fl/Fl_JPEG_Image.H" 58 | #include "Fl/Fl_GIF_Image.H" 59 | 60 | #pragma warning( pop ) 61 | -------------------------------------------------------------------------------- /Section_4/sect4_ex11.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 11. 7 | Create a program to find all the prime numbers between 1 and 100. 8 | One way to do this is to write a function that will check if a number is prime 9 | (i.e., see if the number can be divided by a prime number smaller than itself) 10 | using a vector of primes in order (so that if the vector is called primes, primes[0]==2, primes[1]==3, primes[2]==5, etc.). 11 | Then write a loop that goes from 1 to 100, checks each number to see if it is a prime, and stores each prime found in a vector. 12 | Write another loop that lists the primes you found. 13 | You might check your result by comparing your vector of prime numbers with primes. Consider 2 the first prime. 14 | */ 15 | 16 | #include "section4.hpp" 17 | 18 | int main() 19 | { 20 | int constexpr max{ 100 }; 21 | 22 | // container to store the prime numbers 23 | std::vector primes{}; 24 | 25 | // start with 2 26 | for( int i{ 2 }; i <= max; ++i ) 27 | { 28 | auto check = std::find_if( primes.cbegin(), primes.cend(), [i]( auto p ) { return i % p == 0; } ); 29 | if( check == primes.cend() ) 30 | { 31 | primes.push_back( i ); 32 | } 33 | } 34 | 35 | // print out the prime numbers 36 | for( int j{ 1 }; auto x : primes ) 37 | { 38 | //10 per row 39 | std::cout << x << ( j % 10 != 0 ? '\t' : '\n' ); 40 | ++j; 41 | } 42 | 43 | std::cout << '\n'; 44 | keep_window_open(); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Section_8/sect8_ex5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 22 Dec 2018 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 5 7 | Write 2 functions that reverse the element order in a vector. 8 | One function should produce a new vector with the elements reversed, not affecting the original vector. 9 | The other should reverse the elements with out using a new vector. 10 | */ 11 | 12 | #include "..\includes\ppp.hpp" 13 | 14 | #include 15 | #include 16 | 17 | using vect_values = std::vector; 18 | 19 | void reverse( const vect_values& v, vect_values& rv ) 20 | { 21 | rv.clear(); 22 | if( !v.empty() ) 23 | { 24 | for( auto iv{ v.crbegin() }; iv != v.crend(); ++iv ) 25 | { 26 | rv.push_back( *iv ); 27 | } 28 | } 29 | } 30 | 31 | void reverse( vect_values& v ) 32 | { 33 | if( !v.empty() ) 34 | { 35 | for( auto itr = v.begin(), ritr = v.end() - 1; itr < ritr; ++itr, --ritr ) 36 | { 37 | auto temp = *itr; 38 | *itr = *ritr; 39 | *ritr = temp; 40 | } 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | vect_values a{ 0,1,2,3,4,5,0,1,2,3,4,5 }; 47 | vect_values b; 48 | 49 | reverse( a, b ); 50 | for( auto n : b ) 51 | { 52 | std::cout << n << ' '; 53 | } 54 | std::cout << std::endl; 55 | 56 | reverse( b ); 57 | for( auto n : b ) 58 | { 59 | std::cout << n << ' '; 60 | } 61 | std::cout << std::endl; 62 | 63 | ppp::keep_window_open(); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Section_3/sect3_5_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 23 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | 6 | /* 7 | Section 3.5 try this exercise 8 | */ 9 | 10 | #include "section3.hpp" //custom header 11 | #include 12 | 13 | int main() 14 | { 15 | std::cout << "A program to check for repeating words. EOF or ctrl-z to exit\n"; 16 | std::cout << "Enter a sentence to check: "; 17 | 18 | std::string previous; 19 | std::string current; 20 | unsigned wordCount{}; 21 | while( std::cin >> current ) 22 | { 23 | ++wordCount; 24 | 25 | for( auto index{ current.begin() }; index < current.cend(); ++index ) 26 | { 27 | // check for and remove punctuations 28 | if( std::ispunct( static_cast( *index ) ) != 0 ) 29 | { 30 | current.resize( index - current.begin() ); 31 | break; 32 | } 33 | // make string lower case 34 | if( islower( static_cast( *index ) ) == 0 ) 35 | { 36 | *index = static_cast( std::tolower( static_cast( *index ) ) ); 37 | } 38 | } 39 | 40 | if( !current.empty() && previous == current ) 41 | { 42 | std::cout << std::format( "repeated word: {}\n", current ); 43 | } 44 | 45 | previous = current; 46 | } 47 | 48 | std::cout << std::format( "Total words: {}\n", wordCount ); 49 | 50 | keep_window_open(); 51 | return 0; 52 | } 53 | 54 | // She she laughed He he He because what he did did not look very very good good. 55 | 56 | -------------------------------------------------------------------------------- /Section_12/Simple_window.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "GUI.hpp" // for Simple_window only (doesn't really belong in Window.h) 4 | #include 5 | 6 | // Simple_window is basic scaffolding for ultra-simple interaction with graphics 7 | // it provides one window with one "next" button for ultra-simple animation 8 | 9 | struct Simple_Window : GraphLib::Window 10 | { 11 | Simple_Window( GraphLib::Point xy, int w, int h, const std::string &title ) 12 | : Window( xy, w, h, title ), 13 | button_pushed( false ), 14 | next_button( GraphLib::Point( Get_Width() - 70, 0 ), 70, 20, "Next", cb_next ) 15 | { 16 | this->callback( cb_quit ); 17 | Attach( next_button ); 18 | } 19 | void Wait_For_Button() 20 | // modified event loop: 21 | // handle all events (as per default), quit when button_pushed becomes true 22 | // this allows graphics without control inversion 23 | { 24 | Show(); 25 | while( !button_pushed ) 26 | { 27 | Wait(); 28 | Draw(); 29 | } 30 | button_pushed = false; 31 | Redraw(); 32 | } 33 | 34 | GraphLib::Button next_button; 35 | private: 36 | bool button_pushed; 37 | 38 | static void cb_next( GraphLib::Address, GraphLib::Address addr ) // callback for next_button 39 | // { reference_to(addr).next(); } 40 | { 41 | static_cast( addr )->Next(); 42 | } 43 | 44 | static void cb_quit( Fl_Widget*, GraphLib::Address ) 45 | { 46 | exit( 0 ); 47 | } 48 | 49 | void Next() 50 | { 51 | button_pushed = true; 52 | } 53 | 54 | }; 55 | -------------------------------------------------------------------------------- /Section_4/sect4_6_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Try This 4.6.4 7 | Write a program that "bleeps" out words that you don't like; that is, 8 | you read in words using cin and print them again on cout. 9 | If a word is among a few you have defined, you write out BLEEP instead of that word. 10 | Start with one "disliked word" such as 11 | string disliked = "Broccoli"; 12 | When that works, add a few more. 13 | */ 14 | 15 | #include "section4.hpp" 16 | 17 | //list of disliked words 18 | constexpr auto dislikedWords = std::to_array( { "broccoli", "peas", "spinach" } ); 19 | 20 | std::string bleep_disliked_word( const std::string& s ) 21 | { 22 | std::string temp = s; 23 | std::for_each( temp.begin(), temp.end(), 24 | []( char& c ) { c = static_cast( std::tolower( static_cast( c ) ) ); } ); 25 | if( std::find( dislikedWords.cbegin(), dislikedWords.cend(), temp ) != dislikedWords.cend() ) 26 | { 27 | return "\aBLEEP"; 28 | } 29 | return s; 30 | } 31 | 32 | int main() 33 | { 34 | std::vector words; 35 | // read whitespace-separated words 36 | for( std::string word; std::cin >> word && word != "quit"; ) 37 | { 38 | 39 | words.push_back( word ); // put into vector 40 | } 41 | 42 | std::cout << "Number of words: " << words.size() << std::endl; 43 | for( const auto& str : words ) 44 | { 45 | std::cout << bleep_disliked_word( str ) << " "; 46 | } 47 | std::cout << std::endl; 48 | 49 | keep_window_open(); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Section_8/symbol_table.cpp: -------------------------------------------------------------------------------- 1 | #include "symbol_table.hpp" 2 | #include "../includes/ppp.hpp" 3 | 4 | #include 5 | 6 | namespace calculator 7 | { 8 | // return the value of the Variable with the input name. 9 | double Symbol_Table::get_value( const std::string_view s ) 10 | { 11 | auto vt = find_name( var_table.begin(), var_table.end(), s ); 12 | if( vt == var_table.end() ) 13 | { 14 | ppp::error( "get: undefined name ", s.data() ); 15 | } 16 | return vt->value; 17 | } 18 | 19 | // set the Variable of the named to a double value. 20 | void Symbol_Table::set_value( const std::string_view s, const double d ) 21 | { 22 | auto vt = find_name( var_table.begin(), var_table.end(), s ); 23 | if( vt == var_table.end() ) 24 | { 25 | ppp::error( "set: undefined name ", s.data() ); 26 | } 27 | if( vt->is_const ) 28 | { 29 | ppp::error( "set: is a constant" ); 30 | } 31 | vt->value = d; 32 | } 33 | 34 | // is a name already declared 35 | bool Symbol_Table::is_declared( const std::string_view s ) 36 | { 37 | auto vt = find_name( var_table.begin(), var_table.end(), s ); 38 | return vt != var_table.end(); 39 | } 40 | 41 | // add name value to a vector of Variables 42 | double Symbol_Table::declare( const std::string_view s, const double d, const bool set_const ) 43 | { 44 | if( is_declared( s ) ) 45 | { 46 | ppp::error( s.data(), " declared twice" ); 47 | } 48 | var_table.push_back( Variable{ s.data(), d, set_const } ); 49 | return d; 50 | } 51 | } // calculator 52 | -------------------------------------------------------------------------------- /Section_5/section5.hpp: -------------------------------------------------------------------------------- 1 | // Written by Jtaim 2 | // 2 Apr 2017 3 | // Header file used for Programming: Principles and Practice Using C++ Second Edition 4 | // section 5 5 | 6 | #ifndef SECTION5_H // begin header guard 7 | #define SECTION5_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | // simple function to keep window console open 30 | inline void keep_window_open() 31 | { 32 | std::cin.clear(); 33 | std::cin.sync_with_stdio( false ); 34 | std::cin.ignore( std::cin.rdbuf()->in_avail() ); //clear buffer 35 | std::cout << "\nPress the Enter key to continue"; 36 | std::cin.get(); 37 | } 38 | 39 | inline void error( const std::string& s ) 40 | { 41 | throw std::runtime_error( s ); 42 | } 43 | 44 | inline void error( const std::string& s, const std::string& s2 ) 45 | { 46 | error( s + s2 ); 47 | } 48 | 49 | inline void error( const std::string& s, int i ) 50 | { 51 | error( std::format( "{}: {}", s, i ) ); 52 | } 53 | 54 | // run-time checked narrowing cast (type conversion). 55 | template 56 | Target narrow_cast( const Source& a ) 57 | { 58 | auto r = static_cast( a ); 59 | if( static_cast( r ) != a ) 60 | { 61 | error( "narrow_cast<>() failed" ); 62 | } 63 | return r; 64 | } 65 | #endif // close header guard 66 | -------------------------------------------------------------------------------- /Section_8/Section_8.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | Header Files 40 | 41 | 42 | -------------------------------------------------------------------------------- /Section_9/sect9_ex12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 27 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 12 7 | this is the test program. 8 | - change the representation of a Date to be number of days sense Jan 1, 1970 9 | 10 | */ 11 | 12 | #include"../includes/ppp.hpp" 13 | 14 | #include "Chrono.hpp" 15 | 16 | int main() 17 | try 18 | { 19 | Chrono::Date d1{ 2019,Chrono::Month::mar, 18 }; 20 | 21 | std::cout << d1 << " Week#: " << week_of_year( d1 ) << '\n'; 22 | 23 | Chrono::Date n_day{}; 24 | 25 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 26 | 27 | n_day.add_day( 367 ); 28 | 29 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 30 | 31 | n_day.add_year( 2 ); 32 | 33 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 34 | 35 | n_day.add_month( 1 ); 36 | n_day.add_day( 26 ); 37 | 38 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 39 | 40 | n_day.add_month( 11 ); 41 | 42 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 43 | 44 | n_day.add_month( 1 ); 45 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 46 | 47 | n_day.add_month( 1 ); 48 | std::cout << n_day << " Week#: " << week_of_year( n_day ) << '\n'; 49 | 50 | ppp::keep_window_open(); 51 | return 0; 52 | } 53 | catch( std::exception& e ) 54 | { 55 | std::cerr << "exception: " << e.what() << std::endl; 56 | ppp::keep_window_open(); 57 | return 1; 58 | } 59 | catch( ... ) 60 | { 61 | std::cerr << "exception\n"; 62 | ppp::keep_window_open(); 63 | return 2; 64 | } -------------------------------------------------------------------------------- /Section_4/sect4_ex20.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 20. 7 | Modify exercise 19 so that when you enter a name, the program will output the 8 | corresponding score or name not found. 9 | */ 10 | 11 | #include "section4.hpp" 12 | 13 | int main() 14 | { 15 | using pType = std::vector>; 16 | const pType::value_type termination{ "NoName",0 }; 17 | pType::value_type name_score; 18 | pType ranks{ {"Moe",1}, {"Larry",2}, {"Curly", 3},{"Shemp",4} }; 19 | 20 | std::cout << "Enter a name to find a score.\n"; 21 | for( auto i{ ranks.begin() }; i != ranks.end(); ++i ) 22 | { 23 | std::cout << i->first << ( ( ( i + 1 ) != ranks.end() ) ? ", " : "\n" ); 24 | } 25 | 26 | for( std::string name; std::cin >> name && name != termination.first; ) 27 | { 28 | // set name so first letter is capitalized and rest lower case 29 | name[0] = ( std::toupper( static_cast( name[0] ) ) ); 30 | std::transform( name.begin() + 1, name.end(), name.begin() + 1, 31 | []( unsigned char c ) { return static_cast( std::tolower( c ) ); } ); 32 | 33 | // is name in list 34 | auto itr{ std::find_if( ranks.cbegin(), ranks.cend(), [&name]( const auto& ns ) { return ns.first == name; } ) }; 35 | if( ranks.cend() == itr ) 36 | { 37 | std::cout << "Name not found.\n"; 38 | } 39 | else 40 | { 41 | std::cout << std::format("{}'s score is {}", name, itr->second) << std::endl; 42 | } 43 | } 44 | 45 | keep_window_open(); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Section_3/sect3_ex6.cpp: -------------------------------------------------------------------------------- 1 | // written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3 exercise 6. 7 | A program that prompts the user to enter three integer values, 8 | and then outputs the values in numerical sequence separated by commas. 9 | So, if the user enters the values 10 4 6, the output should be 4, 6, 10. 10 | If two values are the same, they should just be ordered together. 11 | So, the input 4 5 4 should give 4, 4, 5. 12 | */ 13 | 14 | #include "section3.hpp" 15 | 16 | int main() 17 | { 18 | using namespace std; 19 | 20 | cout << "A program that prompts the user to enter three integer values,\n" 21 | << "and then outputs the values in numerical sequence separated by commas.\n\n" 22 | << "Enter three integer values:\n"; 23 | 24 | int min{ std::numeric_limits::max() }; 25 | int mid{ std::numeric_limits::max() }; 26 | int max{ std::numeric_limits::max() }; 27 | 28 | decltype( min ) val{}; 29 | for( std::size_t i{}; i < 3 && cin >> val; ++i ) 30 | { 31 | decltype( min ) temp{}; 32 | if( val < min ) 33 | { 34 | temp = min; 35 | min = val; 36 | val = mid; 37 | mid = temp; 38 | max = val; 39 | } 40 | else if( val < mid ) 41 | { 42 | temp = mid; 43 | mid = val; 44 | max = temp; 45 | } 46 | else 47 | { 48 | max = val; 49 | } 50 | } 51 | 52 | if( !( cin ) ) 53 | { 54 | simple_error( "Invalid entry" ); 55 | } 56 | 57 | cout << std::format( "{}, {}, {}", min, mid, max ); 58 | 59 | keep_window_open(); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Section_4/sect4_drill2.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Drill step 2. 7 | 1. Write a program that consists of a while-loop that (each time around the loop) 8 | reads in two INTs and then prints them. Exit the program when a terminating '|' is entered. 9 | 2. Change the program to write out the smaller value is: followed by the smaller of the 10 | numbers and the larger value is: followed by the larger value. 11 | */ 12 | 13 | #include "section4.hpp" 14 | 15 | int main() 16 | { 17 | constexpr char terminationChar = '|'; //termination character 18 | char c{}; 19 | while( c != terminationChar ) 20 | { 21 | std::cout << std::format( "Enter two numbers or enter {} to exit\n", terminationChar ); 22 | 23 | int val1{}, val2{}; 24 | std::cin >> val1 >> val2; 25 | 26 | //check for valid input 27 | if( !std::cin.good() ) 28 | { 29 | std::cin.clear(); 30 | std::cin.get( c ); 31 | if( c != terminationChar ) 32 | { 33 | std::cout << "Invalid number or termination, please try again.\n"; 34 | } 35 | continue; //goto next loop cycle 36 | } 37 | 38 | //print out valid numbers 39 | std::cout << std::format( "Entered numbers: {} {}\n", val1, val2 ); 40 | 41 | //determine and print out smallest and biggest 42 | if( val1 > val2 ) 43 | { 44 | std::swap( val1, val2 ); 45 | } 46 | std::cout << std::format( "smaller value is: {}\n", val1 ); 47 | std::cout << std::format( "larger value is: {}\n", val2 ); 48 | } 49 | 50 | keep_window_open(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Section_11/sect11_drill9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 12 Jan 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 11 Drill 9 7 | Write some code to print the number 1234567.89 three times, first using default float, then fixed, then scientific forms. 8 | Which output form presents the user with the most accurate representation? 9 | Explain why. 10 | */ 11 | 12 | #include "ppp.hpp" 13 | #include 14 | 15 | int main() 16 | try 17 | { 18 | double number{ 123456.8923 }; 19 | 20 | /* 21 | std::cout << "The number in default: " << number << "\n" 22 | << "The number in fixed: " << std::fixed << number << "\n" 23 | << "The number in scientific: " << std::scientific << number << "\n" 24 | << "The number in default: " << std::defaultfloat << number << "\n\n"; 25 | */ 26 | 27 | // fixed seems to present the number more accurately. 28 | 29 | std::print( "{:25} = {}\n", "The number in default", number ); // default allowed by type and hides trailing decimal zeros 30 | std::print( "{:25} = {:f}\n", "The number in fixed", number ); // default precision of 6 31 | std::print( "{:25} = {:e}\n", "The number in scientific", number ); // default precision of 6 32 | std::print( "{:25} = {:.8g}\n", "The number in general", number ); // default precision of 6, but I set to 8 33 | 34 | ppp::keep_window_open(); 35 | return 0; 36 | } 37 | catch( std::exception &e ) 38 | { 39 | std::cerr << "exception: " << e.what() << std::endl; 40 | ppp::keep_window_open(); 41 | return 1; 42 | } 43 | catch( ... ) 44 | { 45 | std::cerr << "exception\n"; 46 | ppp::keep_window_open(); 47 | return 2; 48 | } 49 | -------------------------------------------------------------------------------- /Section_4/sect4_ex14.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 14. 7 | Modify exercise 13 to take an input value max and then find all prime numbers from 1 to max. 8 | */ 9 | 10 | #include "section4.hpp" 11 | 12 | int main() 13 | { 14 | int max{}; 15 | std::cout << "enter number that you want to find all the primes up to it." << std::endl; 16 | if( !( std::cin >> max ) || max < 1 ) 17 | { 18 | simple_error( "entered invalid integer value or maximum is less than 1\n" ); 19 | } 20 | 21 | // build table 22 | // add 1 to make room for 0 and 1 min size and mark as false 23 | std::vector sieve( max + 1, true ); 24 | sieve[0] = sieve[1] = false; 25 | 26 | for( std::size_t i{}; i < sieve.size(); ++i ) 27 | { 28 | if( sieve[i] == true ) 29 | { 30 | for( std::size_t j{ 2 }; ( j * i ) < sieve.size(); ++j ) 31 | { 32 | sieve[i * j] = false; 33 | } 34 | } 35 | } 36 | 37 | // print out the prime numbers 38 | constexpr std::size_t minSize = 3; // min array size to say we have at least 1 prime which is the number 2 39 | std::cout << std::format( "The prime numbers between 1 and {}{}\n", max, ( sieve.size() < minSize ? " is none." : ":" ) ); 40 | for( std::size_t columnSize{ 1 }, index{ minSize - 1 }; index != sieve.size(); ++index ) 41 | { 42 | //10 columns 43 | if( sieve[index] == true ) 44 | { 45 | std::cout << index << ( columnSize % 10 != 0 ? '\t' : '\n' ); 46 | ++columnSize; 47 | } 48 | } 49 | std::cout << std::endl; 50 | 51 | keep_window_open(); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Section_9/sect9_ex15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Feb 9 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 15 7 | this is the test program Money class. 8 | */ 9 | 10 | #include"../includes/ppp.hpp" 11 | 12 | #include "Money.hpp" 13 | int main() 14 | try 15 | { 16 | Money m1{ "$199.999" }; 17 | std::cout << m1.get_cents() << '\n'; 18 | 19 | Money m2{ "$5123.5689" }; 20 | std::cout << m2.get_cents() << '\n'; 21 | 22 | Money m3{ "$0.005" }; 23 | std::cout << m3.get_cents() << '\n'; 24 | 25 | Money m4{ "$0.004" }; 26 | std::cout << m4.get_cents() << '\n'; 27 | 28 | Money m5{ "$1" }; 29 | std::cout << m5.get_cents() << '\n'; 30 | 31 | Money m6{ "$.995" }; 32 | std::cout << m6.get_cents() << '\n'; 33 | 34 | /*Money m7{".995"}; 35 | std::cout << m7.get_cents() << '\n';*/ 36 | 37 | /*Money m8{"$..995"}; 38 | std::cout << m8.get_cents() << '\n';*/ 39 | 40 | Money m9{ "USD104.995" }; 41 | std::cout << m9.get_cents() << '\n'; 42 | 43 | Money m10{ "DKK109.995" }; 44 | std::cout << m10.get_cents() << '\n'; 45 | 46 | Money m11{ "Kr129.995" }; 47 | std::cout << m11.get_cents() << '\n'; 48 | 49 | Money m12 = m5 + Money{ "DKK6.8" }; 50 | std::cout << m12.get_cents() << '\n'; 51 | 52 | Money m13 = Money{ "DKK8.5" } + m5; 53 | std::cout << m13.get_cents() << '\n'; 54 | 55 | ppp::keep_window_open(); 56 | return 0; 57 | } 58 | catch( std::exception& e ) 59 | { 60 | std::cerr << "exception: " << e.what() << std::endl; 61 | ppp::keep_window_open(); 62 | return 1; 63 | } 64 | catch( ... ) 65 | { 66 | std::cerr << "exception\n"; 67 | ppp::keep_window_open(); 68 | return 2; 69 | } 70 | -------------------------------------------------------------------------------- /Section_8/sect8_ex3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 21 Dec 2018 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 3 7 | Create a vector of Fibonacci numbers and print them using the function from exercise 2. 8 | To create the vector, write a function, fibonacci ( x , y , v , n ), 9 | where x and y are ints, 10 | v is an empty vector < int >, 11 | and n is the number of elements to put into v; 12 | v[0] will be x and v[1] will be y. 13 | A fibonacci number is one that is part of a sequence where each element is the sum of the two previous ones. 14 | For example, starting with 1 and 2, we get 1, 2, 3, 5, 8, 13, 21, . . . . 15 | Your Fibonacci( ) function should make such a sequence starting with its x and y arguments. 16 | 17 | */ 18 | 19 | #include "..\includes\ppp.hpp" 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | void print( const std::string& label, const std::vector& v ) 26 | { 27 | std::cout << label << '\n'; 28 | for( auto i : v ) 29 | { 30 | std::cout << i << '\n'; 31 | } 32 | } 33 | 34 | void fibonacci( const int x, const int y, std::vector& v, const int n ) 35 | { 36 | v.push_back( x ); 37 | v.push_back( y ); 38 | for( int i{}; i < ( n - 2 ); ++i ) 39 | { 40 | v.push_back( v.at( i ) + v.at( i + 1 ) ); 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | int x{ 1 }; 47 | int y{ 2 }; 48 | int how_many{ 10 }; 49 | 50 | auto str = std::format("Print a Fibonacci sequence of length equal to {} and starting with {} and {}.", how_many, x, y); 51 | 52 | std::vector vi; 53 | 54 | fibonacci( x, y, vi, how_many ); 55 | print( str, vi ); 56 | 57 | ppp::keep_window_open(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Section_8/symbol_table.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SYMBOL_TABLE_H // begin header guard 2 | #define SYMBOL_TABLE_H 3 | 4 | #include 5 | #include 6 | 7 | namespace calculator 8 | { 9 | class Symbol_Table 10 | { 11 | public: 12 | double get_value( const std::string_view s ); 13 | void set_value( const std::string_view s, const double d ); 14 | bool is_declared( const std::string_view s ); 15 | double declare( const std::string_view s, const double d, const bool set_const = false ); 16 | 17 | Symbol_Table() = default; 18 | Symbol_Table( const Symbol_Table& st ) = delete; // delete copy constructor 19 | Symbol_Table( const Symbol_Table&& st ) = delete; // delete move constructor 20 | Symbol_Table& operator=( Symbol_Table& st ) = delete; // delete copy assignment operator 21 | Symbol_Table& operator=( Symbol_Table&& st ) = delete; // delete move assignment operator 22 | 23 | private: 24 | // place to hold variable name value 25 | struct Variable 26 | { 27 | std::string name; 28 | double value{}; 29 | bool is_const{ false }; 30 | }; 31 | 32 | // container for all the variables 33 | std::vector var_table; 34 | 35 | // private function to find name in var_table 36 | using vt_itr = std::vector::iterator; 37 | vt_itr find_name( vt_itr first, vt_itr last, const std::string_view value ) 38 | { 39 | for( ; first != last; ++first ) 40 | { 41 | if( first->name == value ) 42 | { 43 | return first; 44 | } 45 | } 46 | return last; 47 | } 48 | }; 49 | } // calculator 50 | 51 | #endif // close header guard 52 | -------------------------------------------------------------------------------- /Section_10/sect10_ex8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | April 13 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Exercise 8 7 | Write a program that accepts two file names and produces a new file that is the contents of the first 8 | file followed by the contents of the second; that is, the program concatenates the two files. 9 | 10 | the files I used are the input and output text files from exercise 5 11 | */ 12 | 13 | #include "../includes/ppp.hpp" 14 | 15 | constexpr std::array read_files{ 16 | "sect10_ex5_in.txt", 17 | "sect10_ex5_out.txt" 18 | }; 19 | 20 | constexpr std::string_view combined_files{ "sect10_ex8.txt" }; 21 | 22 | int main() 23 | try 24 | { 25 | // setup the file to capture read files 26 | std::ofstream fout{ combined_files.data() }; 27 | if( !fout ) 28 | { 29 | ppp::error( "could not open ", combined_files.data() ); 30 | } 31 | 32 | for( const auto fn : read_files ) 33 | { 34 | fout << "Contents of file " << fn << "\n"; 35 | std::ifstream fin{ fn.data() }; 36 | if( !fin ) 37 | { 38 | ppp::error( "could not open ", fn.data() ); 39 | } 40 | for( std::string data; std::getline( fin, data );) 41 | { 42 | fout << data << "\n"; 43 | } 44 | if( !fin.eof() ) 45 | { 46 | ppp::error( "file not completely copied" ); 47 | } 48 | fout << "\n"; 49 | } 50 | return 0; 51 | } 52 | catch( std::exception &e ) 53 | { 54 | std::cerr << R"(exception: )" << e.what() << std::endl; 55 | ppp::keep_window_open(); 56 | return 1; 57 | } 58 | catch( ... ) 59 | { 60 | std::cerr << R"(exception)"; 61 | ppp::keep_window_open(); 62 | return 2; 63 | } 64 | -------------------------------------------------------------------------------- /Section_9/sect9_ex11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 23 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 9 Exercise 11 7 | this is the test program. 8 | Add helper functions to Date class in Chrono namespace 9 | - next_workday() any day that is not Saturday or Sunday is a workday 10 | - week_of_year() week 1 is the week with January 1 and first day of week is Sunday 11 | */ 12 | 13 | #include"../includes/ppp.hpp" 14 | 15 | #include "Chrono.hpp" 16 | 17 | int main() 18 | try 19 | { 20 | Chrono::Date d1{ 2019,Chrono::Month::jan, 18 }; 21 | 22 | std::cout << d1 << " Week#: " << week_of_year( d1 ) << '\n'; 23 | 24 | auto n_wday{ Chrono::next_workday( d1 ) }; 25 | 26 | std::cout << n_wday << " Week#: " << week_of_year( n_wday ) << '\n'; 27 | 28 | n_wday = Chrono::next_sunday( n_wday ); 29 | 30 | std::cout << n_wday << " Week#: " << week_of_year( n_wday ) << '\n'; 31 | 32 | n_wday.add_year( 1 ); 33 | n_wday.add_month( 1 ); 34 | n_wday.add_day( 2 ); 35 | 36 | std::cout << n_wday << " Week#: " << week_of_year( n_wday ) << '\n'; 37 | 38 | n_wday.add_year( 1 ); 39 | 40 | std::cout << n_wday << " Week#: " << week_of_year( n_wday ) << '\n'; 41 | 42 | n_wday.add_month( 10 ); 43 | 44 | std::cout << n_wday << " Week#: " << week_of_year( n_wday ) << '\n'; 45 | 46 | n_wday.add_month( 11 ); 47 | n_wday.add_day( 30 ); 48 | 49 | std::cout << n_wday << " Week#: " << week_of_year( n_wday ) << '\n'; 50 | 51 | ppp::keep_window_open(); 52 | return 0; 53 | } 54 | catch( std::exception& e ) 55 | { 56 | std::cerr << "exception: " << e.what() << std::endl; 57 | ppp::keep_window_open(); 58 | return 1; 59 | } 60 | catch( ... ) 61 | { 62 | std::cerr << "exception\n"; 63 | ppp::keep_window_open(); 64 | return 2; 65 | } 66 | -------------------------------------------------------------------------------- /Section_11/sect11_2_5_try.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 7 June 2024 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Make a simple table including the last name, first name, telephone number, and email address for yourself 7 | and at least five of your friends. 8 | Experiment with different field widths until you are satisfied that the table is well presented. 9 | */ 10 | 11 | #include 12 | 13 | struct contact 14 | { 15 | std::string last_name; 16 | std::string first_name; 17 | std::string phone; 18 | std::string email; 19 | 20 | friend std::ostream &operator << ( std::ostream &os, const contact &out ); 21 | }; 22 | 23 | int main() 24 | { 25 | std::vector contacts{ 26 | {"Jennings","Nina", "1.123.456.7890", "Nina.Jennings@abc.com"}, 27 | {"Stevenson", "Jacey", "1.123.456.7890", "Jacey.Stevenson@cppreference.com"}, 28 | {"Middleton", "Lucy", "1.123.456.7890", "Lucy.Middleton@madeupcompany.com"}, 29 | {"Vang", "Ariel", "1.123.456.7890", "Ariel.Vang@VangEnterprises.com"}, 30 | {"Nunez", "Andreas", "1.123.456.7890", "Andreas.Nunez@Contact5.net"} 31 | }; 32 | 33 | for( const contact &c : contacts ) 34 | { 35 | std::cout << c << std::endl; 36 | } 37 | 38 | ppp::keep_window_open(); 39 | return 0; 40 | } 41 | 42 | std::ostream &operator<<( std::ostream &os, const contact &out ) 43 | { 44 | os << std::left 45 | << std::setw( 11 ) << "Last Name:" 46 | << std::setw( 15 ) << out.last_name 47 | << std::setw( 12 ) << "First Name:" 48 | << std::setw( 15 ) << out.first_name 49 | << std::setw( 7 ) << "Phone:" 50 | << std::setw( 20 ) << out.phone 51 | << std::setw( 7 ) << "Email:" 52 | << std::setw( 40 ) << out.email; 53 | return os; 54 | } 55 | -------------------------------------------------------------------------------- /Section_11/sect11_drill10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 12 Jan 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 11 Drill 10 7 | Make a simple table including last name, first name, telephone number, 8 | and email address for yourself and at least five of your friends. 9 | Experiment with different field widths until you are satisfied that the table is well presented. 10 | */ 11 | 12 | #include "ppp.hpp" 13 | 14 | struct Contact 15 | { 16 | std::string last_name; 17 | std::string first_name; 18 | std::string phone; 19 | std::string email; 20 | }; 21 | 22 | std::ostream &operator<<( std::ostream &os, const Contact &out ) 23 | { 24 | std::print( os, "{:30} {} {:30} {} {}", 25 | std::format( "{}, {}", out.last_name, out.first_name), 26 | "Phone:", out.phone, "Email:", out.email ); 27 | return os; 28 | } 29 | 30 | int main() 31 | try 32 | { 33 | std::vector contacts{ 34 | { "Mint", "Mary", "1.555.555.1111", "mary.mint@ppp.com" }, 35 | { "Basil", "Billy", "1.555.555.2222", "billy.basil@ppp.com" }, 36 | { "Rosemary", "Raymond", "1.555.555.3333", "raymond.rosemary@ppp.com" }, 37 | { "Cilantro", "Christine", "1.555.555.4444", "christina.cilantro@ppp.com" }, 38 | { "Lavender", "Lawrence", "1.555.555.5555", "lawrence.lavender@ppp.com" } 39 | }; 40 | 41 | for( const Contact &c : contacts ) 42 | { 43 | std::cout << c << std::endl; 44 | } 45 | 46 | ppp::keep_window_open(); 47 | return 0; 48 | } 49 | catch( std::exception &e ) 50 | { 51 | std::cerr << "exception: " << e.what() << std::endl; 52 | ppp::keep_window_open(); 53 | return 1; 54 | } 55 | catch( ... ) 56 | { 57 | std::cerr << "exception\n"; 58 | ppp::keep_window_open(); 59 | return 2; 60 | } 61 | -------------------------------------------------------------------------------- /Section_11/sect11_ex9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 11 Oct 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++. Pearson Education. Kindle Edition. 5 | 6 | Section 11 Exercise 9 7 | Split the binary I/O program from section 11.3.2 into two: 8 | one program that converts an ordinary text file into binary and one program that reads binary and converts it to text. 9 | Test these programs by comparing a text file with what you get by converting it to binary and back. 10 | */ 11 | 12 | #include "ppp.hpp" 13 | #include 14 | 15 | constexpr std::string_view ascii_file{ "sect11_ex9.cpp" }; 16 | constexpr std::string_view binary_file{ "sect11_ex9_bin" }; 17 | 18 | int main() 19 | try 20 | { 21 | std::ifstream ifs{ ascii_file.data(), std::ios_base::binary }; 22 | if( !ifs ) 23 | { 24 | ppp::error( std::format( "could not open {}", ascii_file ) ); 25 | } 26 | std::ofstream ofs{ binary_file.data(), std::ios_base::binary }; 27 | if( !ofs ) 28 | { 29 | ppp::error( std::format( "could not open {}", binary_file ) ); 30 | } 31 | 32 | for( char c{}; ifs.get(c); ) 33 | { 34 | ofs << c; 35 | } 36 | 37 | ifs.close(); 38 | ofs.close(); 39 | 40 | ifs = std::ifstream{ binary_file.data(), std::ios_base::binary }; 41 | if( !ifs ) 42 | { 43 | ppp::error( std::format( "could not open {}", binary_file ) ); 44 | } 45 | 46 | for( char c{}; ifs.read( &c, sizeof( char ) ); ) 47 | { 48 | std::print( "{}", c ); 49 | } 50 | 51 | ppp::keep_window_open(); 52 | return 0; 53 | } 54 | catch( std::exception &e ) 55 | { 56 | std::cerr << "exception: " << e.what() << std::endl; 57 | ppp::keep_window_open(); 58 | return 1; 59 | } 60 | catch( ... ) 61 | { 62 | std::cerr << "exception\n"; 63 | ppp::keep_window_open(); 64 | return 2; 65 | } 66 | -------------------------------------------------------------------------------- /Section_4/sect4_ex6.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 6. 7 | Make a vector holding the ten string values "zero", "one", . . . "nine". 8 | Use that in a program that converts a digit to its corresponding spelled-out value; e.g., the input 7 gives the output seven. 9 | Have the same program, using the same input loop, convert spelled-out numbers into their digit form; e.g., the input seven gives the output 7. 10 | */ 11 | 12 | #include "section4.hpp" 13 | 14 | int main() 15 | { 16 | const std::vector spelledNumbers{ 17 | "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" 18 | }; 19 | 20 | std::cout << "Enter a number between 0 and 9 either spelled out or as the number.\n"; 21 | 22 | std::string number; 23 | if( std::cin >> number && number.size() == 1 && std::isdigit( static_cast( number[0] ) ) ) 24 | { 25 | auto num{ atoi(number.data())}; 26 | std::cout << num << " == " << spelledNumbers[num] << "\n\n"; 27 | } 28 | // check if number is spelled out 29 | else 30 | { 31 | // convert to lower 32 | std::for_each( number.begin(), number.end(), []( auto& c ) { c = static_cast( tolower( static_cast( c ) ) ); } ); 33 | 34 | // check if has a spelled out number 35 | auto spelledIndex = std::find( spelledNumbers.begin(), spelledNumbers.end(), number ); 36 | if( spelledIndex != spelledNumbers.end() ) 37 | { 38 | std::cout << number << " == " << ( spelledIndex - spelledNumbers.begin() ) << "\n\n"; 39 | } 40 | else 41 | { 42 | simple_error( std::format( "{} is undefined input", number ) ); 43 | } 44 | } 45 | 46 | keep_window_open(); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Section_10/sect10_drill4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Mar 24 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Drill 4 7 | Open an ofstream and output each point to a file named mydata.txt. 8 | On Windows, we suggest the .txt suffix to make it easier to look at the data with an ordinary text editor. 9 | */ 10 | 11 | #include "../includes/ppp.hpp" 12 | #include "Point.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | constexpr size_t how_many{ 7 }; 18 | using point_type = int; 19 | constexpr std::string_view point_file{ "mydata.txt" }; 20 | 21 | std::cout << "Enter " << how_many << " ( x, y ) pairs:\n"; 22 | std::vector> original_points( how_many, Point() ); 23 | std::ofstream fout{ point_file.data()}; 24 | 25 | if( !fout ) 26 | { 27 | ppp::error( "Can't open output file ", point_file.data() ); 28 | } 29 | for( auto &p : original_points ) 30 | { 31 | while( !( std::cin >> p ) ) 32 | { 33 | if( std::cin.eof() ) 34 | { 35 | ppp::error( "EOF found before filling required data" ); 36 | } 37 | std::cout << "bad input try again\n"; 38 | std::cin.clear(); 39 | std::cin.ignore( std::numeric_limits::max(), '\n' ); 40 | } 41 | fout << p; 42 | } 43 | fout.close(); 44 | 45 | for( const auto &p : original_points ) 46 | { 47 | std::cout << p << "\n"; 48 | } 49 | 50 | ppp::keep_window_open(); 51 | return 0; 52 | } 53 | catch( std::exception &e ) 54 | { 55 | std::cerr << "exception: " << e.what() << std::endl; 56 | ppp::keep_window_open(); 57 | return 1; 58 | } 59 | catch( ... ) 60 | { 61 | std::cerr << "exception\n"; 62 | ppp::keep_window_open(); 63 | return 2; 64 | } 65 | -------------------------------------------------------------------------------- /Section_5/sect5_7b_try.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 5 Try This 5.7 part 2 7 | build and test the program from the section. 8 | Does it provide the correct results? 9 | What is the least amount of input you can give it to get it to fail? 10 | */ 11 | 12 | #include "section5.hpp" 13 | 14 | int main() 15 | { 16 | constexpr double min_temp{ -150.0 }; 17 | constexpr double max_temp{ 150.0 }; 18 | try 19 | { 20 | std::cout << "Enters some temperatures in Fahrenheit.\n"; 21 | std::vectortemps; 22 | for( double temp{}; std::cin >> temp;) 23 | { 24 | if( temp < min_temp || temp > max_temp ) 25 | { 26 | error( std::format( "{} exceeds the maximum or minimum temperature extremes", temp ) ); 27 | } 28 | temps.push_back( temp ); 29 | } 30 | 31 | // check for empty vector 32 | if( temps.size() != 0 ) 33 | { 34 | std::sort( temps.begin(), temps.end() ); 35 | auto sum = std::accumulate( temps.begin(), temps.end(), 0.0 ); 36 | std::cout << "High temperature: " << temps.back() << std::endl; 37 | std::cout << "Low temperature: " << temps.front() << std::endl; 38 | std::cout << "Average temperature: " << sum / temps.size() << std::endl; 39 | } 40 | else 41 | { 42 | std::cout << "No temperatures entered.\n"; 43 | } 44 | 45 | keep_window_open(); 46 | return 0; 47 | } 48 | catch( std::exception& e ) 49 | { 50 | std::cerr << "error: " << e.what() << '\n'; 51 | keep_window_open(); 52 | return 1; 53 | } 54 | catch( ... ) 55 | { 56 | std::cerr << "Oops: unknown exception!\n"; 57 | keep_window_open(); 58 | return 2; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Section_3/sect3_ex7.cpp: -------------------------------------------------------------------------------- 1 | // written by Jtaim 2 | //date 24 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 3 exercise 7. 7 | Do exercise 6, but with three string values. So, if the user enters 8 | the values Steinbeck, Hemingway, Fitzgerald, the output should be 9 | Fitzgerald, Hemingway, Steinbeck. 10 | */ 11 | 12 | #include "section3.hpp" 13 | 14 | int main() 15 | { 16 | using namespace std; 17 | 18 | cout << "Enter 3 words and output will place in alphabetic order.\n\n"; 19 | cout << "Enter three words:\n"; 20 | 21 | string min; 22 | string mid; 23 | string max; 24 | 25 | decltype( min ) val{}; 26 | 27 | for( std::size_t i{}; i < 3 && cin >> val; ++i ) 28 | { 29 | decltype( min ) temp{}; 30 | 31 | if( val < min || min.empty() ) 32 | { 33 | temp = min; 34 | min = val; 35 | val = mid; 36 | mid = temp; 37 | max = val; 38 | } 39 | else if( val < mid || mid.empty() ) 40 | { 41 | temp = mid; 42 | mid = val; 43 | max = temp; 44 | } 45 | else 46 | { 47 | max = val; 48 | } 49 | } 50 | 51 | if( !( cin ) ) 52 | { 53 | simple_error( "Invalid entry" ); 54 | } 55 | 56 | cout << std::format( "{}, {}, {}\n\n", min, mid, max ); 57 | 58 | // Why is a string starting with an upper case always less than a string starting with a lower case? 59 | // hint below 60 | string word1 = "Zero"; 61 | string word2 = "two"; 62 | if( word1 <= word2 ) 63 | { 64 | cout << "okay\n"; 65 | } 66 | else 67 | { 68 | cout << "why not\n"; 69 | } 70 | cout << std::format( "{}({}) {}({})\n", word1[0], static_cast(word1[0]), word2[0], static_cast( word2[0] ) ); 71 | 72 | 73 | keep_window_open(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Section_8/sect8_ex12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 11 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 12 7 | Improve print_until_s() from 8.5.2. 8 | Test it. 9 | What makes a good set of test cases? Give reasons. 10 | Then, write a print_until_ss() that prints until it sees a second occurrence of its quit argument. 11 | */ 12 | 13 | #include"../includes/ppp.hpp" 14 | 15 | void print_until_s( const std::vector& v, const std::string_view quit ) 16 | { 17 | for( const auto& s : v ) 18 | { 19 | if( s == quit ) 20 | { 21 | return; 22 | } 23 | std::cout << s << '\n'; 24 | } 25 | } 26 | 27 | void print_until_ss( const std::vector& v, const std::string_view quit) 28 | { 29 | if( !v.empty() ) 30 | { 31 | unsigned int i{}; 32 | for( const auto& s : v ) 33 | { 34 | if( s == quit ) 35 | { 36 | ++i; 37 | if( i == 2 ) 38 | { 39 | return; 40 | } 41 | } 42 | std::cout << s << '\n'; 43 | } 44 | if( i < 2 ) 45 | { 46 | std::cout << "No " << quit << " found."; 47 | } 48 | } 49 | else 50 | { 51 | std::cout << "no strings found"; 52 | } 53 | std::cout << "\n"; 54 | } 55 | 56 | int main() 57 | { 58 | std::vector v1{ "zero","one", "two", "eight", "two", "three", "four" }; 59 | std::vector v2{}; 60 | 61 | const std::string quit{ "two" }; 62 | 63 | std::cout << "numbers:\n"; 64 | print_until_s( v1, quit ); 65 | 66 | std::cout << "numbers:\n"; 67 | print_until_ss( v1, quit ); 68 | 69 | std::cout << "numbers:\n"; 70 | print_until_ss( v2, quit ); 71 | 72 | ppp::keep_window_open(); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Section_10/Section_10.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | -------------------------------------------------------------------------------- /Section_12/Window.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "fltk.hpp" 4 | #include "Point.hpp" 5 | 6 | #include 7 | #include 8 | 9 | namespace GraphLib 10 | { 11 | 12 | class Shape; // "forward declare" Shape 13 | class Widget; 14 | 15 | class Window : protected Fl_Window 16 | { 17 | public: 18 | Window( int w, int h, const std::string &title = "" ); // let the system pick the location 19 | Window( Point top_left, int w, int h, const std::string &title = "" ); 20 | virtual ~Window() {} 21 | 22 | int Get_Width() const; 23 | int Get_Height() const; 24 | void Resize( int new_width, int new_height ); 25 | 26 | std::string Get_Label() const; 27 | void Set_Label( const std::string &s ); 28 | 29 | void Attach( Shape &shape ); 30 | void Attach( Widget &widget ); 31 | void Detach( Shape &shape ); // remove s from shapes 32 | void Detach_Clear(); // remove all shapes 33 | void Detach( Widget &widget ); // remove w from window (deactivate callbacks) 34 | void Put_On_Top( Shape &priority ); // put p on top of other shapes 35 | 36 | void Fullscreen(); 37 | void Fullscreen_Off(); 38 | int Fullscreen_Active() const; 39 | void Resize( int x, int y, int w, int h ); 40 | void Reposition( int x, int y ); 41 | //protected: 42 | void Draw(); 43 | void Show(); 44 | void Hide(); 45 | int Wait(); 46 | void Redraw(); 47 | 48 | private: 49 | int m_width, m_height; // window size 50 | std::vector m_shapes; // shapes attached to window 51 | 52 | void Init(); 53 | }; 54 | 55 | int gui_main(); // invoke GUI library's main event loop 56 | 57 | inline int Get_Width() { return Fl::w(); } // width of screen in pixels 58 | inline int Get_Height() { return Fl::h(); } // height of screen in pixels 59 | 60 | } // GraphLib 61 | -------------------------------------------------------------------------------- /Section_11/sect11_ex9_bin: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 11 Oct 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++. Pearson Education. Kindle Edition. 5 | 6 | Section 11 Exercise 9 7 | Split the binary I/O program from section 11.3.2 into two: 8 | one program that converts an ordinary text file into binary and one program that reads binary and converts it to text. 9 | Test these programs by comparing a text file with what you get by converting it to binary and back. 10 | */ 11 | 12 | #include "ppp.hpp" 13 | #include 14 | #include 15 | 16 | constexpr std::string_view ascii_file{ "sect11_ex9.cpp" }; 17 | constexpr std::string_view binary_file{ "sect11_ex9_bin" }; 18 | 19 | int main() 20 | try 21 | { 22 | std::ifstream ifs{ ascii_file.data(), std::ios_base::binary }; 23 | if( !ifs ) 24 | { 25 | ppp::error( std::format( "could not open {}", ascii_file ) ); 26 | } 27 | std::ofstream ofs{ binary_file.data(), std::ios_base::binary }; 28 | if( !ofs ) 29 | { 30 | ppp::error( std::format( "could not open {}", binary_file ) ); 31 | } 32 | 33 | for( char c{}; ifs.get(c); ) 34 | { 35 | ofs << c; 36 | } 37 | 38 | ifs.close(); 39 | ofs.close(); 40 | 41 | ifs = std::ifstream{ binary_file.data(), std::ios_base::binary }; 42 | if( !ifs ) 43 | { 44 | ppp::error( std::format( "could not open {}", binary_file ) ); 45 | } 46 | 47 | for( char c{}; ifs.read( &c, sizeof( char ) ); ) 48 | { 49 | //std::string s{ c }; 50 | //auto i = static_cast(std::stoi( c ) ); 51 | std::print( "{}", c ); 52 | } 53 | 54 | ppp::keep_window_open(); 55 | return 0; 56 | } 57 | catch( std::exception &e ) 58 | { 59 | std::cerr << "exception: " << e.what() << std::endl; 60 | ppp::keep_window_open(); 61 | return 1; 62 | } 63 | catch( ... ) 64 | { 65 | std::cerr << "exception\n"; 66 | ppp::keep_window_open(); 67 | return 2; 68 | } 69 | -------------------------------------------------------------------------------- /Section_11/sect11_ex12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 30 June 2024 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++. Pearson Education. Kindle Edition. 5 | 6 | Section 11 Exercise 12 7 | Reverse the order of characters in a text file. 8 | For example, asdfghjkl becomes lkjhgfdsa. 9 | Warning: There is no really good, portable, and efficient way of reading a file backward. 10 | */ 11 | 12 | #include "ppp.hpp" 13 | #include 14 | 15 | constexpr std::string_view ascii_file{ "sect11_ex12.txt" }; 16 | constexpr std::string_view str{ "asdfghjkl" }; 17 | 18 | int main() 19 | try 20 | { 21 | // create file with some text data 22 | std::ofstream ofs{ ascii_file.data() }; 23 | if( !ofs ) 24 | { 25 | ppp::error( std::format( "could not open {}", ascii_file ) ); 26 | } 27 | ofs << str; 28 | 29 | ofs.close(); 30 | std::ifstream ifs{ ascii_file.data() }; 31 | if( !ifs ) 32 | { 33 | ppp::error( std::format( "could not open {}", ascii_file ) ); 34 | } 35 | 36 | // get data from the file 37 | std::vector vc; 38 | for( char c{}; ifs.get( c ); ) 39 | { 40 | vc.push_back( c ); 41 | std::print( "{}", c ); 42 | } 43 | std::cout << std::endl; 44 | 45 | // put reversed data back into the file 46 | ifs.close(); 47 | ofs.open( ascii_file.data() ); 48 | for( auto itr{ vc.rbegin() }; itr != vc.rend(); ++itr ) 49 | { 50 | ofs.put( *itr ); 51 | } 52 | 53 | // read the reversed contents 54 | ofs.close(); 55 | ifs.open( ascii_file.data() ); 56 | for( char c{}; ifs.get( c ); ) 57 | { 58 | std::print( "{}", c ); 59 | } 60 | std::cout << std::endl; 61 | 62 | ppp::keep_window_open(); 63 | return 0; 64 | } 65 | catch( std::exception &e ) 66 | { 67 | std::println( "exception: {}", e.what() ); 68 | ppp::keep_window_open(); 69 | return 1; 70 | } 71 | catch( ... ) 72 | { 73 | std::println( "exception" ); 74 | ppp::keep_window_open(); 75 | return 2; 76 | } 77 | -------------------------------------------------------------------------------- /Section_8/sect8_ex13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 12 Jan 2019 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 13 7 | - Write a function with input vector argument. 8 | - returns a vector containing the number of characters in each string. 9 | - also find the longest and shortest string. 10 | - lexicographically first and last string 11 | 12 | keep as one function and separate function? 13 | I guess depends on the situation but keeping 2 separate functions helps keeps each specialized. 14 | Could also just find size of each to find shortest and longest word. 15 | also for the lexicographically first and last string could use the sort function from the STL algorithm library 16 | */ 17 | 18 | #include"../includes/ppp.hpp" 19 | 20 | template 21 | void print_vector( const std::vector& v ) 22 | { 23 | if( !v.empty() ) 24 | { 25 | for( const auto& element : v ) 26 | { 27 | std::cout << element << '\n'; 28 | } 29 | std::cout << '\n'; 30 | } 31 | else 32 | { 33 | std::cout << "vector is empty\n"; 34 | } 35 | } 36 | 37 | std::vector find_size( const std::vector& vs ) 38 | { 39 | std::vector vn; 40 | if( !vs.empty() ) 41 | { 42 | for( const auto& s : vs ) 43 | { 44 | vn.push_back( static_cast( s.size() ) ); 45 | } 46 | } 47 | return std::move( vn ); 48 | } 49 | 50 | int main() 51 | { 52 | std::vector vs{ "one hundred", "zero", "one", "five thousand", "two", "ZERO", "three", "four" }; 53 | 54 | print_vector( vs ); 55 | print_vector( find_size( vs ) ); 56 | 57 | std::vector lengths = vs; 58 | std::stable_sort( lengths.begin(), lengths.end(), 59 | []( const std::string& a, const std::string& b ) { return a.size() < b.size(); } ); 60 | print_vector( lengths ); 61 | 62 | ppp::keep_window_open(); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Section_12/Section_12.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | Header Files 29 | 30 | 31 | Header Files 32 | 33 | 34 | Header Files 35 | 36 | 37 | Header Files 38 | 39 | 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | -------------------------------------------------------------------------------- /Section_4/sect4_ex19.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 19. 7 | A program where you first enter a set of name-and-value pairs, such as Joe 17 and Barbara 22. 8 | For each pair, add the name to a vector called names and the number to a vector called scores 9 | (in corresponding positions, so that if names[7]=="Joe" then scores[7]==17). 10 | Terminate input with NoName 0. 11 | Check that each name is unique and terminated with an error message if a name is entered twice. 12 | Write out all the (name, score) pairs, one per line. 13 | */ 14 | 15 | #include "section4.hpp" 16 | 17 | int main() 18 | { 19 | using pType = std::vector>; 20 | const pType::value_type termination{ "NoName",0 }; 21 | pType::value_type name_score; 22 | pType ranks; 23 | 24 | std::cout << "Enter names and scores:\n"; 25 | while( std::cin >> name_score.first >> name_score.second ) 26 | { 27 | if( name_score == termination ) 28 | { 29 | break; 30 | } 31 | 32 | std::transform( name_score.first.begin(), name_score.first.end(), name_score.first.begin(), 33 | []( unsigned char name ) { return static_cast( std::tolower( name ) ); } ); 34 | 35 | // check if duplicated 36 | if( ranks.cend() != std::find_if( ranks.cbegin(), ranks.cend(), 37 | [&name_score]( const auto& ns ) { return ns.first == name_score.first; } ) ) 38 | { 39 | simple_error( std::format( "{} has already been entered", name_score.first ) ); 40 | } 41 | 42 | ranks.push_back( name_score ); 43 | } 44 | 45 | if( ranks.empty() ) 46 | { 47 | std::cout << "nothing entered\n"; 48 | } 49 | else 50 | { 51 | // print contents to screen 52 | for( const auto& i : ranks ) 53 | { 54 | std::cout << std::format("name: {} \tscore: {}", i.first, i.second) << std::endl; 55 | } 56 | } 57 | 58 | keep_window_open(); 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Section_4/sect4_drill3.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 26 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 Drill step 3. 7 | 1. Write a program that consists of a while-loop that (each time around the loop) 8 | reads in two INTs and then prints them. Exit the program when a terminating '|' is entered. 9 | 2. Change the program to write out the smaller value is: followed by the smaller of the 10 | numbers and the larger value is: followed by the larger value. 11 | 3. Augment the program so that it writes the line the numbers are equal (only) if they are equal. 12 | */ 13 | 14 | #include "section4.hpp" 15 | 16 | int main() 17 | { 18 | constexpr char terminationChar = '|'; //termination character 19 | char c{}; 20 | while( c != terminationChar ) 21 | { 22 | std::cout << std::format( "Enter two numbers or enter {} to exit\n", terminationChar ); 23 | 24 | int val1{}, val2{}; 25 | std::cin >> val1 >> val2; 26 | 27 | //check for valid input 28 | if( !std::cin.good() ) 29 | { 30 | std::cin.clear(); 31 | std::cin.get( c ); 32 | if( c != terminationChar ) 33 | { 34 | std::cout << "Invalid number or termination, please try again.\n"; 35 | } 36 | continue; //goto next loop cycle 37 | } 38 | 39 | //print out valid numbers 40 | std::cout << std::format( "Entered numbers: {} {}\n", val1, val2 ); 41 | 42 | //determine and print if numbers are equal 43 | if( val1 == val2 ) 44 | { 45 | std::cout << "The entered numbers are equal.\n"; 46 | } 47 | else 48 | { 49 | //determine and print out smallest and biggest 50 | if( val1 > val2 ) 51 | { 52 | std::swap( val1, val2 ); 53 | } 54 | std::cout << std::format( "smaller value is: {}\n", val1 ); 55 | std::cout << std::format( "larger value is: {}\n", val2 ); 56 | } 57 | } 58 | 59 | keep_window_open(); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Section_4/sect4_ex2.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 30 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 2. 7 | If we define the median of a sequence as "a number so that exactly as many elements come before 8 | it in the sequence as come after it," fix the program in §4.6.3 so that it always prints out a median. 9 | Hint: A median need not be an element of the sequence. 10 | */ 11 | 12 | #include "section4.hpp" 13 | 14 | // read some temperatures into a vector 15 | int main() 16 | { 17 | std::vector temperatures; // store history of temperatures 18 | std::cout << "Enter some temperature values and find the medium.\n"; 19 | 20 | for( double temperature{}; std::cin >> temperature; ) 21 | { 22 | temperatures.push_back( temperature ); 23 | } 24 | 25 | // compute mean temperature: 26 | if( temperatures.empty() ) 27 | { 28 | std::cout << "No temperature data was found\n"; 29 | } 30 | else 31 | { 32 | auto sum = std::accumulate( temperatures.begin(), temperatures.end(), 0.0 ); 33 | std::cout << std::format( "Average number: {}\n", sum / temperatures.size() ); 34 | 35 | // compute median temperature: 36 | // If n is odd then Median (M) = value of ((n + 1)/2)th item term. 37 | // If n is even then Median (M) = value of [((n)/2)th item term + ((n)/2 + 1)th item term ]/2 38 | sort( temperatures.begin(), temperatures.end() ); // sort numbers 39 | if( ( temperatures.size() % 2 ) != 0 ) 40 | { 41 | std::cout << std::format( "Median number: {}\n", temperatures[temperatures.size() / 2] ); 42 | } 43 | else 44 | { 45 | // if have even amount, remember vector index starts at zero 46 | // get 2 middle indexes and divide by 2 47 | std::cout << std::format( "Median number: {}\n", 48 | ( temperatures[temperatures.size() / 2 - 1] + temperatures[temperatures.size() / 2] ) / 2 ); 49 | } 50 | } 51 | 52 | keep_window_open(); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Section_4/sect4_ex5.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 31 Mar 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 5. 7 | Write a program that performs as a very simple calculator. 8 | Your calculator should be able to handle the four basic math operations — add, subtract, multiply, and divide — on two input values. 9 | Your program should prompt the user to enter three arguments: two double values and a character to represent an operation. 10 | If the entry arguments are 35.6, 24.1, and '+', the program output should be The sum of 35.6 and 24.1 is 59.7. 11 | */ 12 | 13 | #include "section4.hpp" 14 | 15 | static double add( double arg1, double arg2 ) 16 | { 17 | return arg1 + arg2; 18 | } 19 | 20 | static double subtract( double arg1, double arg2 ) 21 | { 22 | return arg1 - arg2; 23 | } 24 | 25 | static double multiply( double arg1, double arg2 ) 26 | { 27 | return arg1 * arg2; 28 | } 29 | 30 | static double divide( double arg1, double arg2 ) 31 | { 32 | if( arg2 == 0 ) 33 | { 34 | simple_error( "division by zero is not allowed.\n" ); 35 | } 36 | return arg1 / arg2; 37 | } 38 | 39 | using math = double( * )( double, double ); // function pointer type 40 | constexpr auto calculate = std::to_array> 41 | ( { 42 | {'+', add}, 43 | {'-', subtract}, 44 | {'*', multiply}, 45 | {'/', divide} 46 | } ); 47 | 48 | int main() 49 | { 50 | std::cout << "Enter 2 numbers and an operation (+, -, *, /).\n"; 51 | double arg1{}, arg2{}; 52 | if( !( std::cin >> arg1 >> arg2 ) ) 53 | { 54 | simple_error( "incorrect numbers entered.\n" ); 55 | } 56 | 57 | char op{}; 58 | std::cin >> op; 59 | auto itr{ std::find_if( calculate.begin(), calculate.end(), [op]( const auto& c ) { return c.first == op; } ) }; 60 | if( itr == calculate.end() ) 61 | { 62 | simple_error( std::format( "{} is undefined operator", op ) ); 63 | } 64 | std::cout << std::format( "{} {} {} = {}", arg1, op, arg2, itr->second( arg1, arg2 ) ) << std::endl; 65 | 66 | keep_window_open(); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Section_8/sect8_ex6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 22 Dec 2018 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 6 7 | Write 2 functions that reverse the element order in a vector. 8 | One function should produce a new vector with the elements reversed, not affecting the original vector. 9 | The other should reverse the elements with out using a new vector. 10 | */ 11 | 12 | #include "..\includes\ppp.hpp" 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | using strvec = std::vector; 19 | 20 | void reverse( const strvec& v, strvec& rv ) 21 | { 22 | rv.clear(); 23 | if( !v.empty() ) 24 | { 25 | for( auto iv{ v.crbegin() }; iv != v.crend(); ++iv ) 26 | { 27 | rv.push_back( *iv ); 28 | } 29 | } 30 | } 31 | 32 | void reverse( strvec& v ) 33 | { 34 | if( !v.empty() ) 35 | { 36 | for( auto itr = v.begin(), ritr = v.end() - 1; itr < ritr; ++itr, --ritr ) 37 | { 38 | std::string temp = std::move( *itr ); 39 | *itr = *ritr; 40 | *ritr = std::move( temp ); 41 | } 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | strvec a{ "zero","one","two","three","four","five" }; 48 | strvec b; 49 | 50 | reverse( a, b ); 51 | std::cout << "vector a: "; 52 | for( auto n : a ) 53 | { 54 | std::cout << n << ' '; 55 | } 56 | std::cout << std::endl; 57 | 58 | std::cout << "vector b: "; 59 | for( auto n : b ) 60 | { 61 | std::cout << n << ' '; 62 | } 63 | std::cout << std::endl; 64 | 65 | // ---------------------------------- 66 | 67 | reverse( a ); 68 | std::cout << "\n\nvector a: "; 69 | for( auto n : a ) 70 | { 71 | std::cout << n << ' '; 72 | } 73 | std::cout << std::endl; 74 | 75 | reverse( b ); 76 | std::cout << "vector b: "; 77 | for( auto n : b ) 78 | { 79 | std::cout << n << ' '; 80 | } 81 | std::cout << std::endl; 82 | 83 | ppp::keep_window_open(); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /Section_11/sect11_ex13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 30 June 2024 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++. Pearson Education. Kindle Edition. 5 | 6 | Section 11 Exercise 13 7 | Reverse the order of words (defined as whitespace-separated strings) in a file. 8 | For example, Norwegian Blue parrot becomes parrot Blue Norwegian. 9 | You are allowed to assume that all the strings from the file will fit into memory at once. 10 | */ 11 | 12 | #include "ppp.hpp" 13 | #include 14 | 15 | constexpr std::string_view string_file{ "sect11_ex13.txt" }; 16 | constexpr std::string_view str{ "Norwegian Blue parrot" }; 17 | 18 | int main() 19 | try 20 | { 21 | // create file with some text data 22 | std::ofstream ofs{ string_file.data() }; 23 | if( !ofs ) 24 | { 25 | ppp::error( std::format( "could not open {}", string_file ) ); 26 | } 27 | ofs << str; 28 | 29 | ofs.close(); 30 | std::ifstream ifs{ string_file.data() }; 31 | if( !ifs ) 32 | { 33 | ppp::error( std::format( "could not open {}", string_file ) ); 34 | } 35 | 36 | // get data from the file 37 | std::vector vstr; 38 | for( std::string s; ifs >> s; ) 39 | { 40 | vstr.push_back( s ); 41 | std::print( "{} ", s ); 42 | } 43 | std::cout << std::endl; 44 | 45 | // put reversed data back into the file 46 | ifs.close(); 47 | ofs.open( string_file.data() ); 48 | for( auto itr{ vstr.rbegin() }; itr != vstr.rend(); ++itr ) 49 | { 50 | ofs << ( *itr + ' ' ); 51 | } 52 | 53 | // read the reversed contents 54 | ofs.close(); 55 | ifs.open( string_file.data() ); 56 | for( std::string s; ifs >> s; ) 57 | { 58 | std::print( "{} ", s ); 59 | } 60 | std::cout << std::endl; 61 | 62 | ppp::keep_window_open(); 63 | return 0; 64 | } 65 | catch( std::exception &e ) 66 | { 67 | std::println( "exception: {}", e.what() ); 68 | ppp::keep_window_open(); 69 | return 1; 70 | } 71 | catch( ... ) 72 | { 73 | std::println( "exception" ); 74 | ppp::keep_window_open(); 75 | return 2; 76 | } 77 | -------------------------------------------------------------------------------- /Section_4/sect4_ex17.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 17. 7 | finds the min, max and mode of a sequence of strings 8 | */ 9 | 10 | #include "section4.hpp" 11 | 12 | int main() 13 | { 14 | using vType = std::vector; 15 | std::cout << "Enter a set of strings to find the min, max and mode.\n"; 16 | std::cout << "EOF to exit.\n\n"; 17 | 18 | vType sequence; // vector to hold input sequence 19 | 20 | for( vType::value_type enteredValue{}; std::cin >> enteredValue; ) 21 | { 22 | std::transform( enteredValue.begin(), enteredValue.end(), enteredValue.begin(), 23 | []( unsigned char c ) { return static_cast( tolower( c ) ); } ); 24 | sequence.push_back( enteredValue ); 25 | } 26 | 27 | if( sequence.empty() ) 28 | { 29 | std::cout << "nothing entered\n"; 30 | } 31 | else 32 | { 33 | std::sort( sequence.begin(), sequence.end() ); 34 | vType::value_type mode{}; 35 | vType::difference_type count{}; 36 | 37 | // search sorted vector for most duplicates 38 | for( auto i{ sequence.cbegin() }, next{ sequence.cbegin() }; i != sequence.end(); i = next ) 39 | { 40 | next = std::find_if_not( i, sequence.cend(), [&i]( auto comp ) { return *i == comp; } ); 41 | auto temp_count{ next - i }; 42 | if( temp_count >= count ) 43 | { 44 | count = temp_count; 45 | mode = *i; 46 | } 47 | } 48 | 49 | // print out the MODE 50 | if( count > 1 ) 51 | { 52 | std::cout << std::format( "The MODE of entered sequence is {0}.\nThe number {0} was found {1} times.\n", mode, count ); 53 | } 54 | else 55 | { 56 | std::cout << "There is no MODE from this set.\n"; 57 | } 58 | 59 | std::cout << "Maximum string entered was " << sequence.back() << "\n"; 60 | std::cout << "Minimum string entered was " << sequence.front() << "\n"; 61 | } 62 | 63 | keep_window_open(); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Section_12/GUI.cpp: -------------------------------------------------------------------------------- 1 | #include "GUI.hpp" 2 | 3 | using namespace GraphLib; 4 | 5 | void Button::Attach( GraphLib::Window &win ) 6 | { 7 | pwidget = std::make_unique( location.x, location.y, this->width, this->height, label.c_str() ); 8 | pwidget->callback( reinterpret_cast( widget_callback ), &win ); // pass the window 9 | this->own = &win; 10 | } 11 | 12 | int In_Box::get_int() 13 | { 14 | Fl_Input &pi = reference_to( pwidget.get() ); 15 | // return atoi(pi.value()); 16 | const std::string_view p = pi.value(); 17 | if( !isdigit( p.at( 0 ) ) ) 18 | { 19 | return std::numeric_limits::min(); 20 | } 21 | return atoi( p.data() ); 22 | } 23 | 24 | std::string In_Box::get_string() 25 | { 26 | Fl_Input &pi = reference_to( pwidget.get() ); 27 | return std::string( pi.value() ); 28 | } 29 | 30 | void In_Box::Attach( GraphLib::Window &win ) 31 | { 32 | pwidget = std::make_unique( location.x, location.y, this->width, this->height, label.c_str() ); 33 | this->own = &win; 34 | } 35 | 36 | void Out_Box::put( int i ) 37 | { 38 | Fl_Output &po = reference_to( pwidget.get() ); 39 | po.value( std::to_string( i ).c_str() ); 40 | } 41 | 42 | void Out_Box::put( const std::string &s ) 43 | { 44 | reference_to( pwidget.get() ).value( s.c_str() ); 45 | } 46 | 47 | void Out_Box::Attach( GraphLib::Window &win ) 48 | { 49 | pwidget = std::make_unique( location.x, location.y, this->width, this->height, label.c_str() ); 50 | this->own = &win; 51 | } 52 | 53 | int Menu::Attach( Button &b ) 54 | { 55 | b.width = width; 56 | b.height = height; 57 | 58 | switch( kind ) 59 | { 60 | case Kind::horizontal: 61 | b.location = Point( location.x + offset, location.y ); 62 | offset += b.width; 63 | break; 64 | case Kind::vertical: 65 | b.location = Point( location.x, location.y + offset ); 66 | offset += b.height; 67 | break; 68 | } 69 | selection.push_back( &b ); 70 | return int( selection.Size() - 1 ); 71 | } 72 | 73 | int Menu::Attach( Button *p ) 74 | { 75 | // owned.push_back(p); 76 | return Attach( *p ); 77 | } 78 | -------------------------------------------------------------------------------- /Section_5/sect5_ex6.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 6 Apr 2017 3 | //Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 4 | 5 | /* 6 | sect 5 exercise 6 7 | Celsius to Fahrenheit converter 8 | Fahrenheit to Celsius converter 9 | */ 10 | 11 | #include "section5.hpp" 12 | 13 | // converts Celsius to Fahrenheit 14 | // throws an exception if input is below -273.15C 15 | static double ctof( double c ) 16 | { 17 | if( c < -273.15 ) 18 | { 19 | error( "Can not be below absolute zero!\n" ); 20 | } 21 | double c_to_f{ 9.0 / 5.0 * c + 32.0 }; 22 | return c_to_f; 23 | } 24 | 25 | // converts Fahrenheit to Celsius 26 | // throws an exception if input is below -459.67F 27 | static double ftoc( double f ) 28 | { 29 | if( f < -459.67 ) 30 | { 31 | error( "Can not be below absolute zero!\n" ); 32 | } 33 | double f_to_c{ 5.0 / 9.0 * ( f - 32.0 ) }; 34 | return f_to_c; 35 | } 36 | 37 | int main() 38 | try 39 | { 40 | std::cout << "Enter a temperature and unit of temperature (c = Celsius, f = Fahrenheit)\n"; 41 | 42 | // get measurement 43 | double temperature{}; 44 | if( !( std::cin >> temperature ) ) 45 | { 46 | error( "entered non-numeric temperature." ); 47 | } 48 | 49 | // get unit 50 | char unit{}; 51 | std::cin >> unit; 52 | unit = narrow_cast( tolower( static_cast( unit ) ) ); 53 | 54 | // convert temperatures 55 | switch( unit ) 56 | { 57 | case 'c': 58 | temperature = ctof( temperature ); 59 | unit = 'f'; 60 | break; 61 | case 'f': 62 | temperature = ftoc( temperature ); 63 | unit = 'c'; 64 | break; 65 | default: 66 | error( "Unknown unit of measure for temperature." ); 67 | } 68 | 69 | std::cout << "Converted temperature is " << temperature << unit << std::endl; 70 | 71 | keep_window_open(); 72 | return 0; 73 | } 74 | catch( std::exception& e ) 75 | { 76 | std::cerr << "error: " << e.what() << '\n'; 77 | keep_window_open(); 78 | return 1; 79 | } 80 | catch( ... ) 81 | { 82 | std::cerr << "Oops: unknown exception!\n"; 83 | keep_window_open(); 84 | return 2; 85 | } 86 | -------------------------------------------------------------------------------- /Section_4/sect4_drill4.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 24 Sept 2015 3 | //updated 10 Dec 2016 4 | //Programming: Principles and Practice Using C++ Second Edition 5 | 6 | /* 7 | Section 4 Drill step 4. 8 | 1. Write a program that consists of a while-loop that (each time around the loop) 9 | reads in two int numbers and then prints them. Exit the program when a terminating '|' is entered. 10 | 2. Change the program to write out the smaller value is: followed by the smaller of the 11 | numbers and the larger value is: followed by the larger value. 12 | 3. Augment the program so that it writes the line the numbers are equal (only) if they are equal. 13 | 4. Change the program so that it uses double instead of int. 14 | */ 15 | 16 | #include "section4.hpp" 17 | 18 | int main() 19 | { 20 | using inputType = double; 21 | constexpr char terminationChar = '|'; //termination character 22 | 23 | char c{}; 24 | while( c != terminationChar ) 25 | { 26 | std::cout << std::format( "Enter two numbers or enter {} to exit\n", terminationChar ); 27 | 28 | inputType val1{}, val2{}; 29 | std::cin >> val1 >> val2; 30 | 31 | //check for valid input 32 | if( !std::cin.good() ) 33 | { 34 | std::cin.clear(); 35 | std::cin.get( c ); 36 | if( c != terminationChar ) 37 | { 38 | std::cout << "Invalid number or termination, please try again.\n"; 39 | } 40 | continue; //goto next loop cycle 41 | } 42 | 43 | //print out valid numbers 44 | std::cout << std::format( "Entered numbers: {} {}\n", val1, val2 ); 45 | 46 | //determine and print if numbers are equal 47 | if( val1 == val2 ) 48 | { 49 | std::cout << "The entered numbers are equal.\n"; 50 | } 51 | else 52 | { 53 | //determine and print out smallest and biggest 54 | if( val1 > val2 ) 55 | { 56 | std::swap( val1, val2 ); 57 | } 58 | std::cout << std::format( "smaller value is: {}\n", val1 ); 59 | std::cout << std::format( "larger value is: {}\n", val2 ); 60 | } 61 | } 62 | 63 | keep_window_open(); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Section_9/Section_9.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | 41 | 42 | Header Files 43 | 44 | 45 | Header Files 46 | 47 | 48 | Header Files 49 | 50 | 51 | Header Files 52 | 53 | 54 | Header Files 55 | 56 | 57 | Header Files 58 | 59 | 60 | -------------------------------------------------------------------------------- /Section_9/Rational.cpp: -------------------------------------------------------------------------------- 1 | #include "Rational.hpp" 2 | #include 3 | 4 | Rational::Rational() 5 | : m_numerator{ 0 }, m_denominator{ 1 } 6 | {} 7 | 8 | Rational::Rational( int n, int d ) 9 | : m_numerator{ n }, m_denominator{ d } 10 | { 11 | if( m_denominator == 0 ) 12 | { 13 | throw std::invalid_argument( "denominator=0" ); 14 | } 15 | int g{ gcd( std::abs( n ),std::abs( d ) ) }; 16 | if( g != 1 ) 17 | { 18 | m_numerator /= g; 19 | m_denominator /= g; 20 | } 21 | } 22 | 23 | Rational& Rational::operator=( const Rational& a ) 24 | { 25 | // self-assignment guard 26 | if( this == &a ) 27 | { 28 | return *this; 29 | } 30 | 31 | // do the copy 32 | m_numerator = a.m_numerator; 33 | m_denominator = a.m_denominator; 34 | 35 | // return the existing object so we can chain this operator 36 | return *this; 37 | } 38 | 39 | Rational operator+( const Rational& a, const Rational& b ) 40 | { 41 | int numerator = a.get_numerator() * b.get_denominator() + a.get_denominator() * b.get_numerator(); 42 | int denominator = a.get_denominator() * b.get_denominator(); 43 | return Rational( numerator, denominator ); 44 | } 45 | 46 | Rational operator-( const Rational& a, const Rational& b ) 47 | { 48 | int numerator = a.get_numerator() * b.get_denominator() - a.get_denominator() * b.get_numerator(); 49 | int denominator = a.get_denominator() * b.get_denominator(); 50 | return Rational( numerator, denominator ); 51 | } 52 | 53 | Rational operator*( const Rational& a, const Rational& b ) 54 | { 55 | int numerator = a.get_numerator() * b.get_numerator(); 56 | int denominator = a.get_denominator() * b.get_denominator(); 57 | return Rational( numerator, denominator ); 58 | } 59 | 60 | Rational operator/( const Rational& a, const Rational& b ) 61 | { 62 | if( b.get_numerator() == 0 ) throw std::invalid_argument( "denominator=0" ); 63 | int numerator = a.get_numerator() * b.get_denominator(); 64 | int denominator = a.get_denominator() * b.get_numerator(); 65 | return Rational( numerator, denominator ); 66 | } 67 | 68 | bool operator==( const Rational& a, const Rational& b ) 69 | { 70 | return a.get_numerator() == b.get_numerator() && 71 | a.get_denominator() * b.get_denominator(); 72 | } 73 | -------------------------------------------------------------------------------- /Section_10/sect10_drill5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Mar 24 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Drill 5 7 | Close the ofstream and then open an ifstream for mydata.txt. 8 | Read the data from mydata.txt and store it in a new vector called processed_points. 9 | */ 10 | 11 | #include "../includes/ppp.hpp" 12 | #include "Point.hpp" 13 | 14 | int main() 15 | try 16 | { 17 | constexpr size_t how_many{ 7 }; 18 | using point_type = int; 19 | constexpr std::string_view point_file{ "mydata.txt" }; 20 | 21 | std::cout << "Enter " << how_many << " ( x, y ) pairs:\n"; 22 | std::vector> original_points( how_many, Point() ); 23 | std::ofstream fout{ point_file.data() }; 24 | 25 | if( !fout ) 26 | { 27 | ppp::error( "Can't open output file ", point_file.data() ); 28 | } 29 | for( auto &p : original_points ) 30 | { 31 | while( !( std::cin >> p ) ) 32 | { 33 | if( std::cin.eof() ) 34 | { 35 | ppp::error( "EOF found before filling required data" ); 36 | } 37 | std::cout << "bad input try again\n"; 38 | std::cin.clear(); 39 | std::cin.ignore( std::numeric_limits::max(), '\n' ); 40 | } 41 | fout << p; 42 | } 43 | fout.close(); 44 | 45 | //processed_points; 46 | std::ifstream fin{ point_file.data() }; 47 | if( !fin ) 48 | { 49 | ppp::error( "Can't open input file ", point_file.data() ); 50 | } 51 | std::vector processed_points; 52 | decltype( original_points.begin() )::value_type point{}; 53 | while( fin >> point ) 54 | { 55 | processed_points.push_back( point ); 56 | } 57 | 58 | for( const auto &p : processed_points ) 59 | { 60 | std::cout << p << "\n"; 61 | } 62 | 63 | ppp::keep_window_open(); 64 | return 0; 65 | } 66 | catch( std::exception &e ) 67 | { 68 | std::cerr << "exception: " << e.what() << std::endl; 69 | ppp::keep_window_open(); 70 | return 1; 71 | } 72 | catch( ... ) 73 | { 74 | std::cerr << "exception\n"; 75 | ppp::keep_window_open(); 76 | return 2; 77 | } 78 | -------------------------------------------------------------------------------- /Section_8/sect8_ex8.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Date 22 Dec 2018 4 | Programming Principles and Practice Using C++ Second Edition, Bjarne Stroustrup 5 | 6 | Section 8 Exercise 7 7 | - Read 5 names into vector name. 8 | - Prompt user for age of the people named. 9 | - Store the ages in a vector. 10 | - Print the 5 name age pairs. 11 | - Sort the names. 12 | - Print the sorted 5 name age pairs. 13 | Section 8 Exercise 8 14 | - add ability to create variable number of names 15 | */ 16 | 17 | #include "../includes/ppp.hpp" 18 | 19 | struct Name_Age 20 | { 21 | Name_Age() = default; 22 | Name_Age( std::string n, double a ) : name{ n }, age{ a } {} 23 | std::string name; 24 | double age{}; 25 | }; 26 | 27 | // print name age pair value vector, with label 28 | void print( const std::string& label, const std::vector& na ) 29 | { 30 | std::cout << label << '\n'; 31 | { 32 | for( auto& x : na ) 33 | { 34 | std::cout << std::format( "{} is {} years old\n", x.name, x.age ); 35 | } 36 | } 37 | } 38 | 39 | // get names and ages 40 | std::vector get_name_age() 41 | { 42 | std::vector na; 43 | while( true ) 44 | { 45 | std::string name; 46 | double age{}; 47 | std::cout << "Enter a name: "; 48 | std::cin >> name; 49 | if( name == "stop" ) 50 | { 51 | break; 52 | } 53 | std::cout << "Enter age for " << name << ": "; 54 | while( !( std::cin >> age ) ) 55 | { 56 | std::cin.clear(); 57 | std::cin.ignore( std::cin.rdbuf()->in_avail() ); //clear cin buffer 58 | std::cout << "Invalid age entered! Try again.\n"; 59 | } 60 | na.push_back( std::move( Name_Age{ name, age } ) ); 61 | } 62 | return std::move( na ); 63 | } 64 | 65 | int main() 66 | { 67 | auto names_ages{ get_name_age() }; 68 | 69 | std::cout << "\n\n"; 70 | print( "As entered name and age pair", names_ages ); 71 | 72 | std::sort( names_ages.begin(), names_ages.end(), []( Name_Age a, Name_Age b ) { return a.name < b.name; } ); 73 | 74 | std::cout << "\n\n"; 75 | print( "Sorted name and age pair", names_ages ); 76 | 77 | ppp::keep_window_open(); 78 | return 0; 79 | } -------------------------------------------------------------------------------- /Section_10/sect10_ex6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | April 8 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Exercise 6 7 | Define a Roman_int class for holding Roman numerals (as ints). 8 | Provide operator overloads for the << and >> operator. 9 | Provide Roman_int with an as_int() member that returns the int value, so that if r is a Roman_int. 10 | Example we can write cout << "Roman" << r << " equals " << r.as_int() << '\n';. 11 | 12 | This program is to test the Roman_int class 13 | Roman numerals have no concept of 0 or negative numbers. 14 | Also, this does not go above 3999, well guess could with adding more Ms but 15 | does not follow 4 = IV, 9 = IX ... when get above 3999. 16 | */ 17 | 18 | #include "../includes/ppp.hpp" 19 | #include "Roman_int.hpp" 20 | 21 | int main() 22 | try 23 | { 24 | Roman_int r1{ "CDIV" }; 25 | Roman_int r2{ "MMMCMXCIX" }; 26 | Roman_int r3{ "MMMI" }; 27 | std::cout << "Roman " << r1 << " equals " << r1.as_int() << '\n'; 28 | std::cout << "Roman " << r2 << " equals " << r2.as_int() << '\n'; 29 | std::cout << "Roman " << r3 << " equals " << r3.as_int() << '\n'; 30 | 31 | r1 = r3; 32 | Roman_int r4{ r2 }; 33 | std::cout << "Roman " << r1 << " equals " << r1.as_int() << '\n'; 34 | std::cout << "Roman " << r4 << " equals " << r4.as_int() << '\n'; 35 | 36 | std::cout << "Enter a roman number: "; 37 | Roman_int r5; 38 | std::cin >> r5; 39 | std::cout << "Roman " << r5 << " equals " << r5.as_int() << '\n'; 40 | 41 | Roman_int r6{ 3984 }; 42 | std::cout << "Roman " << r6 << " equals " << r6.as_int() << '\n'; 43 | 44 | r5 = 55; 45 | std::cout << "Roman " << r5 << " equals " << r5.as_int() << '\n'; 46 | r6 = "LV"; 47 | std::cout << "Roman " << r6 << " equals " << r6.as_int() << '\n'; 48 | 49 | if( r5 >= r6 ) 50 | { 51 | std::cout << "Cool that worked\n"; 52 | } 53 | 54 | ppp::keep_window_open(); 55 | return 0; 56 | } 57 | catch( std::exception &e ) 58 | { 59 | std::cerr << R"(exception: )" << e.what() << std::endl; 60 | ppp::keep_window_open(); 61 | return 1; 62 | } 63 | catch( ... ) 64 | { 65 | std::cerr << R"(exception)"; 66 | ppp::keep_window_open(); 67 | return 2; 68 | } 69 | -------------------------------------------------------------------------------- /Section_4/sect4_ex16.cpp: -------------------------------------------------------------------------------- 1 | //written by Jtaim 2 | //date 2 Apr 2017 3 | //Programming: Principles and Practice Using C++ Second Edition 4 | 5 | /* 6 | Section 4 exercise 16. 7 | A program that finds the mode of a set of positive integers 8 | The "mode" is the value that occurs most often. 9 | If no number is repeated, then there is no mode for the list. 10 | Refer to drill 3 11 | */ 12 | 13 | #include "section4.hpp" 14 | 15 | int main() 16 | { 17 | using vType = std::vector; 18 | std::cout << "Enter a sequence of positive numbers to find the MODE. (enter any non numeric to indicate last number)\n"; 19 | 20 | vType sequence; // vector to hold input sequence 21 | do 22 | { 23 | vType::value_type number{}; 24 | if( !( std::cin >> number ) ) 25 | { 26 | break; 27 | } 28 | else if( number >= 0 ) 29 | { 30 | sequence.push_back( number ); 31 | continue; 32 | } 33 | else 34 | { 35 | simple_error( std::format( "{} is less than zero", number ) ); 36 | } 37 | } while( true ); 38 | 39 | if( sequence.empty() ) 40 | { 41 | std::cout << "nothing entered\n"; 42 | } 43 | else 44 | { 45 | std::sort( sequence.begin(), sequence.end() ); 46 | vType::value_type mode{}; 47 | vType::difference_type count{}; 48 | 49 | // search sorted vector for most duplicates 50 | for( auto i{ sequence.cbegin() }, next{ sequence.cbegin() }; i != sequence.end(); i = next ) 51 | { 52 | // find_if_not searches for an element for which the predicate returns false. 53 | next = std::find_if_not( i, sequence.cend(), [i]( auto comp ) { return *i == comp; } ); 54 | auto temp_count{ next - i }; 55 | if( temp_count >= count ) 56 | { 57 | count = temp_count; 58 | mode = *i; 59 | } 60 | } 61 | 62 | // print out the MODE 63 | if( count > 1 ) 64 | { 65 | std::cout << std::format( "The MODE of entered sequence is {0}.\nThe number {0} was found {1} times.\n", mode, count ); 66 | } 67 | else 68 | { 69 | std::cout << "There is no MODE from this set.\n"; 70 | } 71 | } 72 | 73 | keep_window_open(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Section_10/sect10_ex9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Written by Jtaim 3 | Sept 8 2019 4 | Stroustrup, Bjarne. Programming: Principles and Practice Using C++ . Pearson Education. Kindle Edition. 5 | 6 | Section 10 Exercise 9 7 | Write a program that takes two files containing sorted whitespace-separated words and merges them, preserving order. 8 | */ 9 | 10 | #include "../includes/ppp.hpp" 11 | 12 | constexpr std::array ws_sep_word_files{ 13 | "sect10_ex9a.txt", 14 | "sect10_ex9b.txt" 15 | }; 16 | 17 | constexpr std::string_view combined_files{ "sect10_ex9.txt" }; 18 | 19 | int main() 20 | try 21 | { 22 | // setup the files to capture whitespace-separated words 23 | for( const std::string_view fn : ws_sep_word_files ) 24 | { 25 | std::ofstream fout( fn.data() ); 26 | if( !fout ) 27 | { 28 | ppp::error( "failed to open ", fn.data() ); 29 | } 30 | std::cout << "Enter some white spaced strings into file " 31 | << fn << ". EOF to exit(ctrl-z)!\n"; 32 | for( std::string s; std::cin >> s && fout; ) 33 | { 34 | fout << s << " "; 35 | } 36 | if( fout.fail() ) 37 | { 38 | ppp::error( "failed to enter white spaced strings into file" ); 39 | } 40 | std::cin.clear(); // clear eof 41 | fout.close(); 42 | } 43 | 44 | std::ofstream fout{ combined_files.data() }; 45 | if( !fout ) 46 | { 47 | ppp::error( "could not open ", combined_files.data() ); 48 | } 49 | 50 | for( const std::string_view fn : ws_sep_word_files ) 51 | { 52 | std::ifstream fin{ fn.data() }; 53 | if( !fin ) 54 | { 55 | ppp::error( "could not open ", fn.data() ); 56 | } 57 | for( std::string data; fin >> data;) 58 | { 59 | fout << data << " "; 60 | } 61 | if( !fin.eof() ) 62 | { 63 | ppp::error( "file not completely copied" ); 64 | } 65 | fout << "\n"; 66 | } 67 | 68 | ppp::keep_window_open(); 69 | return 0; 70 | } 71 | catch( std::exception &e ) 72 | { 73 | std::cerr << R"(exception: )" << e.what() << std::endl; 74 | ppp::keep_window_open(); 75 | return 1; 76 | } 77 | catch( ... ) 78 | { 79 | std::cerr << R"(exception)"; 80 | ppp::keep_window_open(); 81 | return 2; 82 | } 83 | --------------------------------------------------------------------------------