├── .gitignore ├── LICENSE ├── README.md ├── boilerplate.cpp ├── chapter02 ├── ch02_drill_hello.cpp └── ch02_notes.md ├── chapter03 ├── ch03_drill_letter.cpp ├── ch03_notes.md ├── ch03_tt_cruel.cpp ├── ch03_tt_name_age.cpp ├── ch03_tt_narrow.cpp ├── ch03_tt_operators.cpp ├── ch03_tt_repeat.cpp ├── ex02_m_to_k.cpp ├── ex03_bad_names.cpp ├── ex04-5_compare.cpp ├── ex06_sort_ints.cpp ├── ex07_sort_strings.cpp ├── ex08_odd_even.cpp ├── ex09_word_nums.cpp ├── ex10_operatus.cpp ├── ex11_checkout.cpp ├── x_input_name.cpp └── x_string_ops.cpp ├── chapter04 ├── ch04_drill.cpp ├── ch04_notes.md ├── ch04_tt1_money.cpp ├── ch04_tt2_switch_money.cpp ├── ch04_tt3_char_table.cpp ├── ch04_tt4_for_all_chars.cpp ├── ch04_tt5_archaic_square.cpp ├── ch04_tt6_bleep.cpp ├── ex02_median.cpp ├── ex03_distances.cpp ├── ex04_number_guess.cpp ├── ex05_calculator.cpp ├── ex06_digit_string.cpp ├── ex07_string_calc.cpp ├── ex08_chess_rice.cpp ├── ex09_most_rice.cpp ├── ex10_rps.cpp ├── ex11_prime.cpp ├── ex12_max_prime.cpp ├── ex13_sieve_eratosthenes.cpp ├── ex14_custom_sieve.cpp ├── ex15_prime_limit.cpp ├── ex16_mode.cpp ├── ex17_mode_strings.cpp ├── ex18_quadratic.cpp ├── ex19_name_value.cpp ├── ex20_search_name.cpp ├── ex21_search_score.cpp ├── x_dictionary.cpp ├── x_inches_to_cm.cpp ├── x_switch_example.cpp └── x_vectors.cpp ├── chapter05 ├── ch05_drill.cpp ├── ch05_notes.md ├── ch05_tt1_syntax_errors.cpp ├── ch05_tt2_type_errors.cpp ├── ch05_tt5_logic_errors.cpp ├── ch05_tt9_pre_post.cpp ├── ex02_celsius_to_kelvin.cpp ├── ex03_absolute_zero.cpp ├── ex04_ctok_err.cpp ├── ex05_ktoc.cpp ├── ex06_ctof.cpp ├── ex07_quadratic.cpp ├── ex08_n_integers.cpp ├── ex09_post_integers.cpp ├── ex10_double_diff.cpp ├── ex11_fibonacci.cpp ├── ex12_bulls_cows.cpp ├── ex13_random_bulls.cpp ├── ex14_day_sums.cpp └── x_bad_area.cpp ├── chapter06 ├── ch06_calculator.cpp ├── ch06_drill.cpp ├── ch06_notes.md ├── ch06_tt2_calculator00.cpp ├── ex04_Name_value.cpp ├── ex06_english.cpp ├── ex07_bitwise.cpp ├── ex08_bulls_again.cpp ├── ex09_intechars.cpp ├── ex10_permutations.cpp ├── x_ex02_braces.cpp └── x_ex03_factorial.cpp ├── chapter07 ├── ch07_calculator.cpp ├── ch07_drill.cpp ├── ch07_notes.md ├── x_ex01_underscore.cpp ├── x_ex02_assignment.cpp ├── x_ex03_constants.cpp ├── x_ex04_symbol_table.cpp ├── x_ex05_newline_print.cpp ├── x_ex06_help.cpp ├── x_ex07_quit_help.cpp └── x_ex08_grammar.cpp ├── chapter08 ├── ch08_drill_1_my.cpp ├── ch08_drill_1_my.h ├── ch08_drill_1_use.cpp ├── ch08_drill_2.cpp ├── ch08_drill_3.cpp ├── ch08_notes.md ├── ex01_calculator.cpp ├── ex02_int_print.h ├── ex03_fibonacci.cpp ├── ex05_reverse.cpp ├── ex06_rev_strings.cpp ├── ex07_name_age.cpp ├── ex08_names_unlimited.cpp ├── ex09_index.cpp ├── ex10_maxv.cpp ├── ex11_data_set.cpp ├── ex12_print_until.cpp ├── ex13_string_chars.cpp ├── ex14_const_value.cpp └── x_ex01_calculator.cpp ├── chapter09 ├── ch09_drill.cpp ├── ch09_notes.md ├── ex02_name_pairs.cpp ├── ex03_operator.cpp ├── ex04_gibberish.cpp ├── ex05-9_library │ ├── book.cpp │ ├── book.h │ ├── ex05_book.cpp │ ├── ex08_patron.cpp │ ├── ex09_library.cpp │ ├── library.cpp │ ├── library.h │ └── main.cpp ├── ex10-11_chrono │ ├── chrono.cpp │ ├── chrono.h │ └── main.cpp ├── ex12_date │ ├── bit_test.cpp │ ├── date.cpp │ ├── date.h │ └── main.cpp ├── ex13_rational │ ├── ex13_rational.cpp │ ├── main.cpp │ ├── rational.cpp │ └── rational.h └── ex14-16_money │ ├── main.cpp │ ├── money.cpp │ └── money.h ├── chapter10 ├── ch10_drill_points.cpp ├── ch10_notes.md ├── ex01_sum_ints.cpp ├── ex02_store_temps.cpp ├── ex03_temp_stats.cpp ├── ex04_temp_units.cpp ├── ex05_print_year.cpp ├── ex06_roman_int.cpp ├── ex08_file_concat.cpp ├── ex09_merge_sort.cpp ├── ex11_only_ints.cpp ├── input.txt ├── input_1.txt ├── input_2.txt ├── input_3.txt ├── mydata.txt ├── output.txt ├── output_1_2.txt ├── raw_temps.txt ├── reading_data.txt ├── white_ints.txt ├── x_reading.cpp └── x_scratch.cpp ├── chapter11 ├── adama.txt ├── ch11_drill_test_output.cpp ├── ch11_notes.md ├── ch11_tt_scratch.cpp ├── doubles.txt ├── ex01_lower.cpp ├── ex02_line_search.cpp ├── ex03_disemvowels.cpp ├── ex04_multi_input.cpp ├── ex05_char_class.cpp ├── ex06-8_punct_swap.cpp ├── ex09_binary_io.cpp ├── ex10_split.cpp ├── ex11_split_plus.cpp ├── ex12_reverse_char.cpp ├── ex13_reverse_words.cpp ├── ex14_charclass_count.cpp ├── ex15_scientific_8.cpp ├── ex16_ordered_count.cpp ├── ints.txt ├── lines.txt ├── output.txt └── x_punct_stream.cpp ├── chapter12 ├── ch12_drill.cpp ├── ch12_notes.md ├── ex01_many_shapes.cpp ├── ex07_house.cpp ├── ex08_olympic.cpp ├── ex09_image.cpp ├── ex10_file_diagram.cpp ├── ex11_polygons.cpp ├── ex12_superellipse.cpp ├── example.cpp ├── rob_bass.jpg └── snow_cpp.gif ├── chapter13 ├── ch13_drill.cpp ├── ch13_notes.md ├── ch13_tools.cpp ├── ch13_tools.h ├── ex01_arc.cpp ├── ex02_box.cpp ├── ex03_arrow.cpp ├── ex04_rect_connect.cpp ├── ex05_circ_connect.cpp ├── ex06_diagram.cpp ├── ex07_rgb_chart.cpp ├── ex08_reg_hex.cpp ├── ex09_hex_tile.cpp ├── ex10_reg_poly.cpp ├── ex11_ellipse.cpp ├── ex12_circ_mark.cpp ├── ex13_color_matrix.cpp ├── ex14_right_triangle.cpp ├── ex15_right_tile.cpp ├── ex16_hex_tile_2.cpp ├── ex17_hex_tile_3.cpp ├── ex18_poly.cpp └── ex19_star.cpp ├── chapter14 ├── ch14_drill.cpp ├── ch14_notes.md ├── ch14_tools.cpp ├── ch14_tools.h ├── ex01_smiley_frowny.cpp ├── ex02_cant_copy.cpp ├── ex03_abstract.cpp ├── ex04_immobile.cpp ├── ex05_striped_rect.cpp ├── ex06_striped_circ.cpp ├── ex08_octagon.cpp ├── ex09_group.cpp ├── ex10_pseudo_win.cpp └── ex11_binary_tree.cpp ├── chapter15 ├── ch15_drill_1.cpp ├── ch15_drill_2.cpp ├── ch15_example_3.cpp ├── ch15_example_5.cpp ├── ch15_notes.md ├── ch15_tools.cpp ├── ch15_tools.h ├── ex01_factorial.cpp ├── ex02_fct.cpp ├── ex03_precision.cpp ├── ex04_sin_cos.cpp ├── ex05_leibniz.cpp ├── ex06_bar_graph.cpp ├── ex07_bar_graph_2.cpp ├── ex08_heights.cpp ├── ex09_can_rivers.cpp ├── heights.txt └── rivers.txt ├── chapter16 ├── ch16_drill.cpp ├── ch16_notes.md ├── ch16_tools.cpp ├── ch16_tools.h ├── ex01_my_window.cpp └── ex02_checkerboard.cpp ├── chapter17 ├── ch17_drill_1.cpp ├── ch17_drill_2.cpp ├── ch17_notes.md ├── ch17_tt_destructors.cpp ├── ch17_tt_sizeof.cpp ├── ex03_to_lower.cpp ├── ex04_strdup.cpp ├── ex05_findx.cpp ├── ex06_mem_exhaust.cpp ├── ex07_cin_no_str.cpp ├── ex08_cin_str.cpp ├── ex09_memory_growth.cpp ├── ex11_list_of_gods.cpp ├── ex13_struct_god.cpp └── ex14_single_link.cpp ├── chapter18 ├── ch18_drill_1.cpp ├── ch18_drill_2.cpp ├── ch18_notes.md ├── ch18_tt_copy.cpp ├── ch18_tt_vector.cpp ├── ex01_to_04.cpp ├── ex05_to_07.cpp ├── ex08_palindrome.cpp ├── ex09_mem_layout.cpp ├── ex10_long_string.cpp ├── ex11_skip_list.cpp ├── ex12_wumpus.cpp └── scratch_array.cpp ├── chapter19 ├── ch19_drill.cpp ├── ch19_notes.md ├── ch19_vec_test.cpp ├── ch19_vector.h ├── ex01_vec_add.cpp ├── ex02_mult_sum.cpp ├── ex03_pair.cpp ├── ex04_link.cpp ├── ex05_int.cpp ├── ex06-7_number.cpp ├── ex08-9_allocate.cpp ├── ex10_unique.cpp ├── ex11_counted.cpp ├── ex12_file_handle.cpp ├── ex13_tracer.cpp ├── ex14-15_wumpus_gui.cpp └── test.txt ├── chapter20 ├── ch20_drill.cpp ├── ch20_notes.md ├── ch20_real_vec.cpp ├── ch20_test_type.h ├── ch20_tt_chars.cpp ├── ex01_tts.cpp ├── ex02_jack_jill.cpp ├── ex03_jj_palin.cpp ├── ex04_jj_fix.cpp ├── ex05_vector_io.cpp ├── ex06_08-10_doc.cpp ├── ex07_last_str.cpp ├── ex11_list_to_vec.cpp ├── ex12_list_1.cpp ├── ex13_list_2.cpp ├── ex14_slist.cpp ├── ex15_pvector.cpp ├── ex16-1_ovector.cpp ├── ex16-2_ovector.cpp ├── ex18-1_v_rit.cpp ├── ex18-2_v_rit.cpp ├── ex19_l_rit.cpp ├── ex20_timed.cpp ├── jack.txt └── jill.txt ├── chapter21 ├── ch21_drill_1_vec.cpp ├── ch21_drill_2_map.cpp ├── ch21_drill_3_vec.cpp ├── ch21_notes.md ├── ch21_order.cpp ├── ch21_order.h ├── ch21_tt1_finds.cpp ├── ch21_tt2_find_if.cpp ├── ch21_tt3_accum.cpp ├── ch21_tt4_map_stocks.cpp ├── ch21_tt5_unordered.cpp ├── ch21_tt6_file_copy.cpp ├── ex02_algorithms.cpp ├── ex03_count.cpp ├── ex04_count_if.cpp ├── ex05_not_found.cpp ├── ex06_ptr_set.cpp ├── ex07_binary_searches.cpp ├── ex08_sorted_seconds.cpp ├── ex09_order.cpp ├── ex10_compute_order.cpp ├── ex11_order_gui.cpp ├── ex12_order_query_gui.cpp ├── ex13_text_cleaner.cpp ├── ex14_word_query.cpp ├── input_floats.txt ├── input_items.txt ├── input_orders_1.txt ├── input_orders_2.txt ├── input_story.txt ├── output_orders_1.txt ├── output_orders_2.txt ├── output_orders_3.txt ├── output_story.txt ├── x_copy_if.cpp └── x_file_copy.cpp ├── chapter23 ├── ch23_drill.cpp ├── ch23_notes.md ├── ch23_tt1_mail_file.cpp ├── ch23_tt2_patterns.cpp ├── ex01-4_more_mail.cpp ├── ex05_huge_mail.cpp ├── ex06_dates.cpp ├── ex07_credit_cards.cpp ├── ex08_file_search.cpp ├── ex09-11_table_check.cpp ├── ex12_date_replace.cpp ├── ex13_dot_newline.cpp ├── ex14_line_spanning.cpp ├── ex15-16_non-pattern.cpp ├── input_cards.txt ├── input_dates.txt ├── input_dates_2.txt ├── input_mail.txt ├── input_mail_2.txt ├── input_table.txt ├── input_zip_codes.txt ├── output_dates.txt └── x_test.cpp ├── chapter24 ├── ch24_drill.cpp ├── ch24_notes.md ├── ch24_tt1_precision.cpp ├── ch24_tt2_overflow.cpp ├── ex01-2_triple.cpp ├── ex03_boost_bind.cpp ├── ex04-6_gaussian.cpp ├── ex07_no_lib.cpp ├── ex08_animated.cpp ├── ex09_deduction.cpp ├── ex10_random.cpp ├── ex11-12_more_matrix.cpp ├── x_math_errno.cpp ├── x_matrix_mult.cpp └── x_matrix_test.cpp ├── chapter25 ├── TEA_input.txt ├── TEA_output.txt ├── ch25_drill.cpp ├── ch25_notes.md ├── ch25_tt1_fragmentation.cpp ├── ch25_tt2_bits_out.cpp ├── ch25_tt3_infinte.cpp ├── ch25_tt4_TEA.cpp ├── ex03_bit_patterns.cpp ├── ex04_bitwise_calc.cpp ├── ex05_infinite.cpp ├── ex06_tricky_infinite.cpp ├── ex09_system_info.cpp ├── ex10_ppn.cpp ├── ex11_ppn_bitset.cpp ├── ex14_vector_pool.cpp ├── ex14_vector_pool_2.cpp └── ex15_performance_tests.cpp ├── chapter26 ├── ch26_Test.h ├── ch26_drill.cpp ├── ch26_my_binary.h ├── ch26_notes.md ├── drill_3_b.txt ├── drill_3_c.txt ├── ex01-5_binary_search_tests.cpp ├── ex11_matrix_perf.cpp ├── ex12_double_sort.cpp ├── ex13_string_sort.cpp ├── ex14_string_set.cpp ├── test_b_float.txt ├── test_b_reading.txt ├── test_b_search.txt └── test_b_string.txt ├── chapter27 ├── ch27_drill.c ├── ch27_notes.md ├── ch27_tt1-2_cat_cpy.c ├── ex01_my_string_h.c ├── ex02_intrusive_List.c ├── ex09_string_qsort.c ├── ex12_pair_table.c ├── ex14_array_ints.c └── ex15_inheritance.c ├── general_notes └── compiler_flags.txt ├── images ├── apple-logo.jpg └── cpp.jpg ├── scratch ├── accum.cpp ├── angle.cpp ├── array_size.cpp ├── error.cpp ├── fib.cpp ├── math.cpp ├── nullptr.cpp ├── pointer.cpp ├── scope.cpp ├── token.cpp ├── two_by.cpp ├── unique_chars.cpp └── virtual.cpp └── text_lib ├── Matrix.h ├── MatrixIO.h ├── calculator00.cpp ├── calculator01.cpp ├── calculator02.cpp └── std_lib_facilities.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac 2 | *.DS_Store 3 | 4 | # Vim 5 | *.swp 6 | 7 | # Libraries 8 | GUI 9 | FLTK 10 | 11 | # Compiled Object files 12 | *.slo 13 | *.lo 14 | *.o 15 | *.obj 16 | 17 | # Precompiled Headers 18 | *.gch 19 | *.pch 20 | 21 | # Compiled Dynamic libraries 22 | *.so 23 | *.dylib 24 | *.dll 25 | 26 | # Fortran module files 27 | *.mod 28 | *.smod 29 | 30 | # Compiled Static libraries 31 | *.lai 32 | *.la 33 | *.a 34 | *.lib 35 | 36 | # Executables 37 | *.exe 38 | *.out 39 | *.app 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Chris 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /boilerplate.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter XX Exercise XX 5 | // 6 | 7 | #include 8 | #include 9 | 10 | int main() 11 | try { 12 | // code 13 | } 14 | catch(std::exception& e) { 15 | std::cerr << "Exception: " << e.what() << '\n'; 16 | return 1; 17 | } 18 | catch(...) { 19 | std::cerr << "Unknown exception\n"; 20 | return 2; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /chapter02/ch02_drill_hello.cpp: -------------------------------------------------------------------------------- 1 | #include "./text_lib/std_lib_facilities.h" 2 | int main() // C++ programs start by executing the function main 3 | { 4 | cout << "Hello, programming!\nHere we go!\n"; // output "Hello, World!" 5 | keep_window_open(); // wait for a character to be entered 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /chapter02/ch02_notes.md: -------------------------------------------------------------------------------- 1 | # Programming Principles and Practice Using C++ 2 | 3 | ## Chapter 2 - Hello, World! 4 | 5 | 1. The "Hello, World!" program is our introduction to any new language. It puts the syntax on our lips and the process into our hands. 6 | 2. The four parts of a function are a return type, a name, a parameter list, and the function body. 7 | 3. Every C++ program must contain a main() function. 8 | 4. The return value of 0 indicates that the program has terminated successfully. 9 | 5. The compiler attempts to translate the C++ code into machine language. 10 | 6. The `#include` directive instructs the computer to make available the resources in the following file. Like a module? 11 | 7. The `.h` suffix at the end of a filename in C++ indicates a header file. A header file contains definitions such as `cout` that we can use. 12 | 8. The linker connects our compiled program with the standard C++ library to create an executable program. 13 | 9. A source file is our writted code, human readable. An object file is the compiled, computer readable output. 14 | 10. IDEs usually contain an editor, syntax highlighting and aids, compiling and debugging tools. 15 | 11. You gotta do it and do it. This isn't a textbook about how to read. 16 | -------------------------------------------------------------------------------- /chapter03/ch03_tt_cruel.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | string s = "Goodbye, cruel world!"; 6 | cout << s << '\n'; 7 | } 8 | -------------------------------------------------------------------------------- /chapter03/ch03_tt_name_age.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | cout << "Please enter your name and age\n"; 6 | 7 | string first_name = "???"; 8 | double age = 0; 9 | 10 | cin >> first_name >> age; 11 | 12 | int months = age * 12; 13 | 14 | cout << "Hello, " << first_name << " (age " << months << " months)\n"; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chapter03/ch03_tt_narrow.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | double d = 0; 6 | while (cin>>d) { 7 | 8 | int i = d; // squeeze double into int 9 | char c = i; // squeeze int into char 10 | int i2 = c; // get int value out of char 11 | cout << "d==" << d 12 | << " i==" << i 13 | << " i2==" << i2 14 | << " char(" << c << ")\n"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter03/ch03_tt_operators.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | cout << "Please enter an integer: "; 6 | int n; 7 | double m; 8 | cin >> n; 9 | m = n; 10 | cout << "n == " << n 11 | << "\nn+1 == " << n+1 12 | << "\nthree times n == " << 3*n 13 | << "\ntwice n == " << n+n 14 | << "\nn squared == " << n*n 15 | << "\nhalf of n == " << n/2 16 | << "\nn mod 5 == " << n%5 17 | << "\nsquare root of n == " << sqrt(m) 18 | << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /chapter03/ch03_tt_repeat.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | int number_of_words = 0; 6 | string previous = " "; 7 | string current; 8 | while (cin>>current) { 9 | ++number_of_words; 10 | if (previous == current) 11 | cout << "word number " << number_of_words 12 | << "repeated word: " << current << '\n'; 13 | previous = current; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /chapter03/ex02_m_to_k.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | cout << "Enter a measurement in miles to convert to kilometers:\n"; 6 | double miles; 7 | cin >> miles; 8 | double kilometers = miles * 1.609; 9 | cout << "There are " << kilometers 10 | << " kilometers in " << miles 11 | << " miles.\n"; 12 | } 13 | -------------------------------------------------------------------------------- /chapter03/ex03_bad_names.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // LOOKING BACK: I didn't really have much of an appetite for flexing the 4 | // compiler's error detection.. 5 | 6 | int main() 7 | { 8 | int ridic = 0.273; // only a warning! 9 | char $ = "w"; // error: double quotes is const char? 10 | double int = 17; // error: tries to combine int with double 11 | } 12 | -------------------------------------------------------------------------------- /chapter03/ex04-5_compare.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | cout << "Please enter two floating point values\n"; 6 | double val1; 7 | double val2; 8 | cin >> val1 >> val2; 9 | 10 | if (val1 < val2) 11 | cout << val1 << " is the lesser\n" 12 | << val2 << " is the greater\n"; 13 | if (val2 < val1) 14 | cout << val2 << " is the lesser\n" 15 | << val1 << " is the greater\n"; 16 | 17 | cout << "Their sum is " << val1+val2 << '\n' 18 | << "Their difference is " << val1-val2 << '\n' 19 | << "Their product is " << val1*val2 << '\n' 20 | << "Their ratio is " << val1/val2 << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /chapter03/ex06_sort_ints.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | cout << "Please enter 3 integer values to sort:\n"; 7 | int val1; 8 | int val2; 9 | int val3; 10 | int greatest = 0; 11 | int middlest = 0; 12 | int smallest = 0; 13 | cin >> val1 >> val2 >> val3; 14 | 15 | if (val1 < val2 && val1 < val3) { 16 | 17 | smallest = val1; 18 | 19 | if (val2 < val3) { 20 | middlest = val2; 21 | greatest = val3; 22 | } else { 23 | middlest = val3; 24 | greatest = val2; 25 | } 26 | 27 | } else if (val2 < val3){ 28 | 29 | smallest = val2; 30 | 31 | if (val1 < val3) { 32 | middlest = val1; 33 | greatest = val3; 34 | } else { 35 | middlest = val3; 36 | greatest = val1; 37 | } 38 | 39 | } else { 40 | 41 | smallest = val3; 42 | 43 | if (val1 < val2) { 44 | middlest = val1; 45 | greatest = val2; 46 | } else { 47 | middlest = val2; 48 | greatest = val3; 49 | } 50 | } 51 | 52 | cout << smallest << " " << middlest << " " << greatest << '\n'; 53 | } 54 | -------------------------------------------------------------------------------- /chapter03/ex07_sort_strings.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | cout << "Please enter 3 string values to sort:\n"; 7 | string val1; 8 | string val2; 9 | string val3; 10 | string greatest; 11 | string middlest; 12 | string smallest; 13 | cin >> val1 >> val2 >> val3; 14 | 15 | if (val1 < val2 && val1 < val3) { 16 | 17 | smallest = val1; 18 | 19 | if (val2 < val3) { 20 | middlest = val2; 21 | greatest = val3; 22 | } else { 23 | middlest = val3; 24 | greatest = val2; 25 | } 26 | 27 | } else if (val2 < val3){ 28 | 29 | smallest = val2; 30 | 31 | if (val1 < val3) { 32 | middlest = val1; 33 | greatest = val3; 34 | } else { 35 | middlest = val3; 36 | greatest = val1; 37 | } 38 | 39 | } else { 40 | 41 | smallest = val3; 42 | 43 | if (val1 < val2) { 44 | middlest = val1; 45 | greatest = val2; 46 | } else { 47 | middlest = val2; 48 | greatest = val3; 49 | } 50 | } 51 | 52 | cout << smallest << " " << middlest << " " << greatest << '\n'; 53 | } 54 | -------------------------------------------------------------------------------- /chapter03/ex08_odd_even.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | cout << "Is it odd or even? Let me tell you! Enter a number:\n"; 7 | int val; 8 | cin >> val; 9 | 10 | string result = "Its odd!\n"; 11 | 12 | if (val % 2 == 0) 13 | result = "Its even!\n"; 14 | 15 | cout << result; 16 | } 17 | -------------------------------------------------------------------------------- /chapter03/ex09_word_nums.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | cout << "Write a value from zero to four and I will return it!\n"; 7 | string string_val; 8 | string numeric_val; 9 | cin >> string_val; 10 | 11 | if (string_val == "zero") 12 | numeric_val = "0"; 13 | else if (string_val == "one") 14 | numeric_val = "1"; 15 | else if (string_val == "two") 16 | numeric_val = "2"; 17 | else if (string_val == "three") 18 | numeric_val = "3"; 19 | else if (string_val == "four") 20 | numeric_val = "4"; 21 | else 22 | numeric_val = "not a number I know.."; 23 | 24 | cout << "Your value is " << numeric_val << '\n'; 25 | } 26 | -------------------------------------------------------------------------------- /chapter03/ex10_operatus.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // LOOKING BACK: Not sure why I didn't use char's for the operators.. 4 | 5 | int main() 6 | { 7 | // Program 8 | cout << "Provide an operation and two values you'd like operated on:\n"; 9 | string operatus; 10 | double val1; 11 | double val2; 12 | cin >> operatus >> val1 >> val2; 13 | 14 | double result = 0; 15 | if (operatus == "+" || operatus == "plus") 16 | result = val1 + val2; 17 | else if (operatus == "-" || operatus == "minus") 18 | result = val1 - val2; 19 | else if (operatus == "*" || operatus == "mul") 20 | result = val1 * val2; 21 | else if (operatus == "/" || operatus == "div") 22 | result = val1 / val2; 23 | 24 | cout << "The result of your operation is " << result << '\n'; 25 | } 26 | -------------------------------------------------------------------------------- /chapter03/ex11_checkout.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // LOOKING BACK: This was straight out of my Java textbook in the semester of 4 | // computer science I took. 5 | 6 | int main() 7 | { 8 | // Program 9 | int pennies; 10 | int nickels; 11 | int dimes; 12 | int quarters; 13 | int half_dollars; 14 | int dollars; 15 | 16 | double total; 17 | 18 | cout << "How many pennies do you have?\n"; 19 | cin >> pennies; 20 | total += pennies * 0.01; 21 | cout << "How many nickels do you have?\n"; 22 | cin >> nickels; 23 | total += nickels * 0.05; 24 | cout << "How many dimes do you have?\n"; 25 | cin >> dimes; 26 | total += dimes * 0.1; 27 | cout << "How many quarters do you have?\n"; 28 | cin >> quarters; 29 | total += quarters * 0.25; 30 | cout << "How many half-dollars do you have?\n"; 31 | cin >> half_dollars; 32 | total += half_dollars * 0.5; 33 | cout << "How many dollars do you have?\n"; 34 | cin >> dollars; 35 | total += dollars * 1; 36 | 37 | cout << "You have " << pennies << " pennies.\n" 38 | << "You have " << nickels << " nickels.\n" 39 | << "You have " << dimes << " dimes.\n" 40 | << "You have " << quarters << " quarters.\n" 41 | << "You have " << half_dollars << " half-dollars.\n" 42 | << "You have " << dollars << " dollars.\n" 43 | << "The value of all of your coins is $" << total << ".\n"; 44 | } 45 | -------------------------------------------------------------------------------- /chapter03/x_input_name.cpp: -------------------------------------------------------------------------------- 1 | // read and write a first name 2 | #include "./text_lib/std_lib_facilities.h" 3 | 4 | int main() 5 | { 6 | cout << "Please enter your first name (followed by 'enter'):\n"; 7 | string first_name; 8 | cin >> first_name; 9 | cout << "Hello, " << first_name << "!\n"; 10 | } 11 | -------------------------------------------------------------------------------- /chapter03/x_string_ops.cpp: -------------------------------------------------------------------------------- 1 | // fun with string operations 2 | 3 | #include "../text_lib/std_lib_facilities.h" 4 | 5 | int main() 6 | { 7 | cout << "Please enter your first and second names\n"; 8 | string first; 9 | string second; 10 | cin >> first >> second; 11 | string name = first + " " + second; 12 | cout << "Hello, " << name << '\n'; 13 | 14 | if (first == second) cout << "that's the same name twice\n"; 15 | if (first < second) 16 | cout << first << " is alphabetically before " << second << '\n'; 17 | if (first > second) 18 | cout << first << " is alphabetically after " << second << '\n'; 19 | } 20 | -------------------------------------------------------------------------------- /chapter04/ch04_tt1_money.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | constexpr double yen_to_dollars = 0.0089381; 7 | constexpr double krona_to_dollars = 0.11; 8 | constexpr double pounds_to_dollars = 1.29; 9 | 10 | double amount = 1; 11 | 12 | char unit = ' '; 13 | 14 | cout << "Enter a foreign currency to convert (y, k, p):\n"; 15 | cin >> amount >> unit; 16 | 17 | if (unit == 'y') 18 | cout << amount << unit << " equals $" 19 | << amount * yen_to_dollars << '\n'; 20 | else if (unit == 'k') 21 | cout << amount << unit << " equals $" 22 | << amount * krona_to_dollars << '\n'; 23 | else if (unit == 'p') 24 | cout << amount << unit << " equals $" 25 | << amount * pounds_to_dollars << '\n'; 26 | else 27 | cout << "Sorry, I'm not able to convert currency '" << unit << "'.\n"; 28 | } 29 | -------------------------------------------------------------------------------- /chapter04/ch04_tt2_switch_money.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | constexpr double yen_to_dollars = 0.0089381; 7 | constexpr double krona_to_dollars = 0.11; 8 | constexpr double pounds_to_dollars = 1.29; 9 | 10 | double amount = 1; 11 | 12 | char unit = ' '; 13 | 14 | cout << "Enter a foreign currency to convert (y, k, p):\n"; 15 | cin >> amount >> unit; 16 | 17 | switch (unit) { 18 | case 'y': 19 | cout << amount << unit << " equals $" 20 | << amount * yen_to_dollars << '\n'; 21 | break; 22 | case 'k': 23 | cout << amount << unit << " equals $" 24 | << amount * krona_to_dollars << '\n'; 25 | break; 26 | case 'p': 27 | cout << amount << unit << " equals $" 28 | << amount * pounds_to_dollars << '\n'; 29 | break; 30 | default: 31 | cout << "Sorry, I'm not able to convert that currency.\n"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /chapter04/ch04_tt3_char_table.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | int i = 0; 6 | int unicode_val = 'a'; 7 | 8 | char alpha = 'a'; 9 | 10 | while (i<26) { 11 | alpha = unicode_val + i; 12 | cout << alpha << '\t' << (unicode_val + i) << '\n'; 13 | ++i; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /chapter04/ch04_tt4_for_all_chars.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | char alpha = 'a'; 6 | 7 | for (int i = 'a'; i <= 'z'; ++i) { 8 | alpha = i; 9 | cout << alpha << '\t' << i << '\n'; 10 | } 11 | 12 | cout << '\n'; 13 | 14 | for (int i = 'A'; i <= 'Z'; ++i) { 15 | alpha = i; 16 | cout << alpha << '\t' << i << '\n'; 17 | } 18 | 19 | cout << '\n'; 20 | 21 | for (int i = '0'; i <= '9'; ++i) { 22 | alpha = i; 23 | cout << alpha << '\t' << i << '\n'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /chapter04/ch04_tt5_archaic_square.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // Implement a squaring function without using multiplication. Use this 4 | // function to print all values from 0-99 and their square, separated by a tab 5 | // character. 6 | 7 | int archaic_square(int v) { 8 | int total = 0; 9 | 10 | for (int i = 0; i < v; ++i) { 11 | total += v; 12 | } 13 | 14 | return total; 15 | } 16 | 17 | int main() 18 | { 19 | for (int i = 0; i < 100; ++i) { 20 | cout << i << '\t' << archaic_square(i) << '\n'; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter04/ch04_tt6_bleep.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | vector words; 6 | for (string word; cin >> word; ) 7 | words.push_back(word); 8 | 9 | vector dislikes = { "php", "python", "C#" }; 10 | 11 | for (string word : words) { 12 | bool bad = false; 13 | 14 | for (string awful : dislikes) { 15 | if (word == awful) 16 | bad = true; 17 | } 18 | 19 | if (bad) 20 | cout << "BLEEP!" << '\n'; 21 | else 22 | cout << word << '\n'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /chapter04/ex02_median.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | vector temps; 6 | double median = 0; 7 | 8 | for (double temp; cin >> temp; ) 9 | temps.push_back(temp); 10 | 11 | int above_mid = temps.size() / 2; 12 | double sum = 0; 13 | for (double x : temps) sum += x; 14 | cout << "Average temperature: " << sum / temps.size() << '\n'; 15 | 16 | sort(temps); 17 | 18 | if (temps.size() % 2 == 0) { 19 | median = (temps[above_mid] + temps[above_mid - 1]) / 2; 20 | } else { 21 | median = temps[above_mid]; 22 | } 23 | 24 | cout << "Median temperature: " << median << '\n'; 25 | } 26 | -------------------------------------------------------------------------------- /chapter04/ex03_distances.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | vector distances; 6 | 7 | double distance; 8 | double mean; 9 | double smallest = 0; 10 | double largest = 0; 11 | double total_distance = 0; 12 | 13 | while(cin >> distance) { 14 | distances.push_back(distance); 15 | 16 | if (smallest == 0 && largest == 0) { 17 | 18 | smallest = distance; 19 | largest = distance; 20 | 21 | } else { 22 | 23 | if (distance < smallest) 24 | smallest = distance; 25 | else if (distance > largest) 26 | largest = distance; 27 | 28 | } 29 | 30 | total_distance += distance; 31 | } 32 | 33 | mean = total_distance / distances.size(); 34 | 35 | cout << "The smallest distance between cities was: " << smallest << '\n' 36 | << "The largest distance between cities was: " << largest << '\n' 37 | << "The total distance travelled was: " << total_distance << '\n' 38 | << "The mean distance between citeis was: " << mean << '\n'; 39 | } 40 | -------------------------------------------------------------------------------- /chapter04/ex05_calculator.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | double val1 = 0; 6 | double val2 = 0; 7 | double result = 0; 8 | 9 | char operation = ' '; 10 | 11 | cout << "Enter two values, in order, and an operation to perform\n"; 12 | while (cin >> val1 >> val2 >> operation) { 13 | 14 | switch (operation) { 15 | case '+': 16 | result = val1 + val2; 17 | break; 18 | case '-': 19 | result = val1 - val2; 20 | break; 21 | case '*': 22 | result = val1 * val2; 23 | break; 24 | case '/': 25 | result = val1 / val2; 26 | break; 27 | default: 28 | cout << "Error: unrecognized operation\n"; 29 | } 30 | 31 | cout << "That equals: " << result << " shall we do another?\n"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /chapter04/ex06_digit_string.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | int parsed; 6 | 7 | string num_string = ""; 8 | 9 | vector digits_in_strings = { "zero", "one", "two", "three", 10 | "four", "five", "six", "seven", "eight", "nine" }; 11 | 12 | vector digits_as_digits = { "0", "1", "2", "3", "4", "5", "6", 13 | "7", "8", "9" }; 14 | 15 | cout << "Enter a value from 0 - 9:\n"; 16 | 17 | while (cin >> num_string) { 18 | parsed = -1; 19 | 20 | for (int i = 0; i < digits_in_strings.size(); ++i) { 21 | if (digits_in_strings[i] == num_string) 22 | parsed = i; 23 | } 24 | 25 | if (parsed == -1) { 26 | for (int j = 0; j < digits_as_digits.size(); ++j) { 27 | if (digits_as_digits[j] == num_string) 28 | parsed = j; 29 | } 30 | } 31 | 32 | cout << parsed << '\n'; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /chapter04/ex08_chess_rice.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Number of rice per square 6 | int square = 1; 7 | int rice = 1; 8 | int target = 0; 9 | 10 | cout << "How many grains do you want?\n"; 11 | cin >> target; 12 | 13 | while (rice < target) { 14 | rice *= 2; 15 | square += 1; 16 | } 17 | 18 | cout << "# of grains of rice: " << rice 19 | << " chess square #: " << square << '\n'; 20 | } 21 | -------------------------------------------------------------------------------- /chapter04/ex09_most_rice.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Number of rice per square 6 | int square = 1; 7 | int rice = 1; 8 | int target = 0; 9 | 10 | cout << "How many squares should we try?\n"; 11 | cin >> target; 12 | 13 | while (square < target) { 14 | rice *= 2; 15 | square += 1; 16 | } 17 | 18 | cout << "# of grains of rice: " << rice 19 | << " chess square #: " << square << '\n'; 20 | } 21 | -------------------------------------------------------------------------------- /chapter04/ex11_prime.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Prime numbers to 100 6 | vector primes = { 2 }; 7 | 8 | bool is_prime; 9 | 10 | for (int i = 3; i < 100; ++i) { 11 | is_prime = true; 12 | for (int prime : primes) 13 | if (i % prime == 0) 14 | is_prime = false; 15 | 16 | if (is_prime) 17 | primes.push_back(i); 18 | } 19 | 20 | for (int prime : primes) 21 | cout << prime << '\n'; 22 | } 23 | -------------------------------------------------------------------------------- /chapter04/ex12_max_prime.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Prime numbers to 100 6 | vector primes = { 2 }; 7 | 8 | bool is_prime; 9 | 10 | int max; 11 | 12 | cout << "How high should we search for primes?\n"; 13 | cin >> max; 14 | 15 | for (int i = 3; i <= max; ++i) { 16 | is_prime = true; 17 | for (int prime : primes) 18 | if (i % prime == 0) 19 | is_prime = false; 20 | 21 | if (is_prime) 22 | primes.push_back(i); 23 | } 24 | 25 | for (int prime : primes) 26 | cout << prime << '\n'; 27 | } 28 | -------------------------------------------------------------------------------- /chapter04/ex13_sieve_eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Sieve of Eratosthenes 6 | vector primes; 7 | vector sieve; 8 | 9 | for (int i = 0; i <= 100; ++i) 10 | if (i < 2) 11 | sieve.push_back(0); 12 | else 13 | sieve.push_back(i); 14 | 15 | for (int i = 0; i < sieve.size(); ++i) { 16 | if (sieve[i] != 0) { 17 | primes.push_back(i); 18 | 19 | if (i * i < sieve.size()) 20 | for (int j = i; j < sieve.size(); j += i) 21 | sieve[j] = 0; 22 | } 23 | } 24 | 25 | for (int n : primes) 26 | cout << n << '\n'; 27 | } 28 | -------------------------------------------------------------------------------- /chapter04/ex14_custom_sieve.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Sieve of Eratosthenes 6 | vector primes; 7 | vector sieve; 8 | 9 | int max; 10 | cout << "How high shall we search for primes?\n"; 11 | cin >> max; 12 | 13 | for (int i = 0; i <= max; ++i) 14 | if (i < 2) 15 | sieve.push_back(0); 16 | else 17 | sieve.push_back(i); 18 | 19 | for (int i = 0; i < sieve.size(); ++i) { 20 | if (sieve[i] != 0) { 21 | primes.push_back(i); 22 | 23 | if (i * i < sieve.size()) 24 | for (int j = i; j < sieve.size(); j += i) 25 | sieve[j] = 0; 26 | } 27 | } 28 | 29 | for (int n : primes) 30 | cout << n << '\n'; 31 | } 32 | -------------------------------------------------------------------------------- /chapter04/ex15_prime_limit.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Prime numbers to 100 6 | vector primes = { 2 }; 7 | 8 | bool is_prime; 9 | 10 | int limit; 11 | cout << "How many primes should we find?\n"; 12 | cin >> limit; 13 | 14 | for (int i = 3; primes.size() < limit; ++i) { 15 | is_prime = true; 16 | for (int prime : primes) 17 | if (i % prime == 0) 18 | is_prime = false; 19 | 20 | if (is_prime) 21 | primes.push_back(i); 22 | } 23 | 24 | for (int prime : primes) 25 | cout << prime << '\n'; 26 | } 27 | -------------------------------------------------------------------------------- /chapter04/ex18_quadratic.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Program 6 | double a; 7 | double b; 8 | double c; 9 | 10 | cout << "Lets do a quadratic equation!\n" 11 | << "Please enter an 'a', 'b', and 'c' value:\n"; 12 | cin >> a >> b >> c; 13 | 14 | double negB = 0 - b; 15 | 16 | double x1 = (negB + sqrt(b * b - 4 * a * c)) / (2 * a); 17 | double x2 = (negB - sqrt(b * b - 4 * a * c)) / (2 * a); 18 | 19 | cout << "The solution values are: x1 = " << x1 20 | << ", x2 = " << x2 << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /chapter04/ex19_name_value.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Accept name value pairs, store them in vectors, print them in the end. 6 | 7 | vector names; 8 | vector scores; 9 | 10 | string name = ""; 11 | 12 | int score = 0; 13 | 14 | bool end = false; 15 | bool repeat = false; 16 | 17 | while (!end) { 18 | cout << "Enter a name and a value:\n"; 19 | cin >> name >> score; 20 | 21 | if (name == "NoName" && score == 0) 22 | end = true; 23 | else { 24 | for (string n : names) 25 | if (n == name) 26 | repeat = true; 27 | 28 | if (repeat) { 29 | cout << "Error: duplicate name\n"; 30 | repeat = false; 31 | } else { 32 | names.push_back(name); 33 | scores.push_back(score); 34 | } 35 | } 36 | } 37 | 38 | cout << "RESULTS:\n"; 39 | 40 | for (int i = 0; i < names.size(); ++i) 41 | cout << names[i] << '\t' << scores[i] << '\n'; 42 | } 43 | -------------------------------------------------------------------------------- /chapter04/ex20_search_name.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Accept name value pairs, store them in vectors, print them in the end. 6 | 7 | vector names; 8 | vector scores; 9 | 10 | string name = ""; 11 | 12 | int score = 0; 13 | 14 | bool end = false; 15 | bool repeat = false; 16 | 17 | while (!end) { 18 | cout << "Enter a name and a value:\n"; 19 | cin >> name >> score; 20 | 21 | if (name == "NoName" && score == 0) 22 | end = true; 23 | else { 24 | for (string n : names) 25 | if (n == name) 26 | repeat = true; 27 | 28 | if (repeat) { 29 | cout << "Error: duplicate name\n"; 30 | repeat = false; 31 | } else { 32 | names.push_back(name); 33 | scores.push_back(score); 34 | } 35 | } 36 | } 37 | 38 | string query =""; 39 | cout << "Who's score would you like to look up?\n"; 40 | cin >> query; 41 | 42 | int place = 0; 43 | bool match = false; 44 | 45 | for (int i = 0; i < names.size(); ++i) 46 | if (names[i] == query) { 47 | match = true; 48 | place = i; 49 | } 50 | 51 | if (match) 52 | cout << scores[place] << '\n'; 53 | else 54 | cout << "name not found\n"; 55 | } 56 | -------------------------------------------------------------------------------- /chapter04/ex21_search_score.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Accept name value pairs, store them in vectors, print them in the end. 6 | 7 | vector names; 8 | vector scores; 9 | 10 | string name = ""; 11 | 12 | int score = 0; 13 | 14 | bool end = false; 15 | bool repeat = false; 16 | 17 | while (!end) { 18 | cout << "Enter a name and a value:\n"; 19 | cin >> name >> score; 20 | 21 | if (name == "NoName" && score == 0) 22 | end = true; 23 | else { 24 | for (string n : names) 25 | if (n == name) 26 | repeat = true; 27 | 28 | if (repeat) { 29 | cout << "Error: duplicate name\n"; 30 | repeat = false; 31 | } else { 32 | names.push_back(name); 33 | scores.push_back(score); 34 | } 35 | } 36 | } 37 | 38 | int query = 0; 39 | cout << "Which score would you like to look up?\n"; 40 | cin >> query; 41 | 42 | vector matches; 43 | 44 | for (int i = 0; i < names.size(); ++i) 45 | if (scores[i] == query) 46 | matches.push_back(names[i]); 47 | 48 | if (matches.size() > 0) 49 | for (string m : matches) 50 | cout << m << '\n'; 51 | else 52 | cout << "score not found\n"; 53 | } 54 | -------------------------------------------------------------------------------- /chapter04/x_dictionary.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | vector words; 6 | for (string temp; cin >> temp; ) 7 | words.push_back(temp); 8 | cout << "Number of words: " << words.size() << '\n'; 9 | 10 | sort(words); 11 | 12 | for (int i = 0; i < words.size(); ++i) 13 | if (i == 0 || words[i - 1] != words[i]) 14 | cout << words[i] << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /chapter04/x_inches_to_cm.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // convert from inches to centimeters or centimeters to inches 4 | // a suffix 'i' or 'c' indicates the unit of input 5 | // any other suffix is an error 6 | 7 | int main() 8 | { 9 | constexpr double cm_per_inch = 2.54; // number of centimeters in 10 | // an inch 11 | double length = 1; // length in inches or 12 | // centimeters 13 | char unit = ' '; // a space is not a unit 14 | 15 | cout << "Please enter a length followed bya unit (c or i):\n"; 16 | cin >> length >> unit; 17 | 18 | if (unit == 'i') 19 | cout << length << "in == " << cm_per_inch * length << "cm\n"; 20 | else if (unit == 'c') 21 | cout << length << "cm == " << length / cm_per_inch << "in\n"; 22 | else 23 | cout << "Sorry, I don't know a unit called '" << unit << "'\n"; 24 | } 25 | -------------------------------------------------------------------------------- /chapter04/x_switch_example.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | constexpr double cm_per_inch = 2.54; 6 | 7 | double length = 1; 8 | 9 | char unit = 'a'; 10 | 11 | cout << "Please enter a length followed by a unit (c or i):\n"; 12 | cin >> length >> unit; 13 | switch (unit) { 14 | case 'i': 15 | cout << length << "in == " << cm_per_inch * length << "cm\n"; 16 | break; 17 | case 'c': 18 | cout << length << "cm == " << length / cm_per_inch << "in\n"; 19 | break; 20 | default: 21 | cout << "Sorry, I don't know a unit called '" << unit << "'\n"; 22 | break; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /chapter04/x_vectors.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | /* 6 | vector v = { 5, 7, 9, 4, 6, 8 }; 7 | for (int i = 0; i < v.size(); ++i) 8 | cout << v[i] << '\n'; 9 | */ 10 | 11 | /* 12 | vector c = { 'a', 'c', 'e', 'b', 'd' }; 13 | for (char x : c) 14 | cout << x << '\n'; 15 | */ 16 | 17 | vector v; 18 | v.push_back(2.7); 19 | v.push_back(5.6); 20 | v.push_back(7.9); 21 | 22 | for (double x : v) 23 | cout << x << '\n'; 24 | } 25 | -------------------------------------------------------------------------------- /chapter05/ch05_tt1_syntax_errors.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int area(int length, int width) { 4 | return length * width; 5 | } 6 | 7 | int main() 8 | { 9 | // Program 10 | int s1 = area(7; 11 | int s2 = area(7) 12 | Int s3 = area(7); 13 | int s4 = area('7); 14 | } 15 | -------------------------------------------------------------------------------- /chapter05/ch05_tt2_type_errors.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int area(int length, int width) { 4 | return length * width; 5 | } 6 | 7 | int main() 8 | { 9 | // Program 10 | int x0 = arena(7); // error: undeclared function 11 | int x1 = area(7); // error: wrong number of arguments 12 | int x2 = area("seven", 2); // error: 1st argument has a wrong type 13 | 14 | int x4 = area(10, -7); // OK: but how is -7 a width? 15 | int x5 = area(10.7, 9.3); // OK: but calls area (10,9) 16 | char x6 = area(100, 9999); // OK: but truncates the result 17 | } 18 | -------------------------------------------------------------------------------- /chapter05/ch05_tt5_logic_errors.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | // Example of logic errors 6 | vector temps; 7 | 8 | for (double temp; cin >> temp; ) 9 | temps.push_back(temp); 10 | 11 | double sum = 0; 12 | double high_temp = 0; // zero init may affect assignment 13 | double low_temp = 0; // zero init may affect assignment 14 | 15 | for (int x : temps) 16 | { 17 | if (high_temp == 0 && low_temp == 0) { 18 | high_temp = x; 19 | low_temp = x; 20 | } 21 | 22 | if (x > high_temp) high_temp = x; // find high 23 | if (x < low_temp) low_temp = x; // find low 24 | sum += x; 25 | } 26 | 27 | cout << "High temperature: " << high_temp << '\n'; 28 | cout << "Low temperature: " << low_temp << '\n'; 29 | cout << "Average temperature: " << sum / temps.size() << '\n'; 30 | } 31 | -------------------------------------------------------------------------------- /chapter05/ch05_tt9_pre_post.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int area(int length, int width) 4 | // calculate area of a rectangle 5 | // pre-conditions: length and width are positive 6 | // post-condition: returns a positive value that is area 7 | { 8 | if (length <= 0 || width <= 0) error("area() pre-condition"); 9 | int a = length * width; 10 | if (a <= 0) error("area() post-condition"); 11 | return a; 12 | } 13 | 14 | int main() 15 | try { 16 | // This is an example of the area function with pre & post conditionals 17 | 18 | int x; 19 | int y; 20 | for (; cin >> x >> y; ) 21 | cout << "Area: " << area(x, y) << '\n'; 22 | 23 | } 24 | catch (exception& e) { 25 | cerr << "error: " << e.what() << '\n'; 26 | return 1; 27 | } 28 | catch (...) { 29 | cerr << "Oops: unknown exception!\n"; 30 | return 2; 31 | } 32 | -------------------------------------------------------------------------------- /chapter05/ex02_celsius_to_kelvin.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | double ctok(double c) // converts Celsius to Kelvin 4 | { 5 | constexpr double k_conversion = 273.15; 6 | 7 | double k = c + k_conversion; 8 | return k; 9 | } 10 | 11 | int main() 12 | { 13 | double c = 0; // declare input variable 14 | cin >> c; // retrieve temperature to input variable 15 | double k = ctok(c); // convert temperature 16 | cout << k << '\n'; // print out temperature 17 | } 18 | 19 | /* Errors: 20 | * ctok: references a magic constant 21 | * ctok: k declared as int instead of double 22 | * ctok: returned int instead of k 23 | * ctok: omits ; 24 | * main: inits c then inputs into d 25 | * main: passes a string arg to a function expecting a double 26 | * main: output is minimally vague 27 | * main: cout had capital 'C' 28 | */ 29 | 30 | /* Original code 31 | double ctok(double c) // converts Celsius to Kelvin 32 | { 33 | int k = c + 273.15; 34 | return int 35 | } 36 | 37 | int main() 38 | { 39 | double c = 0; // declare input variable 40 | cin >> d; // retrieve temperature to input variable 41 | double k = ctok("c"); // convert temperature 42 | Cout << k << '\n'; // print out temperature 43 | } 44 | */ 45 | -------------------------------------------------------------------------------- /chapter05/ex03_absolute_zero.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | double ctok(double c) // converts Celsius to Kelvin 4 | { 5 | constexpr double k_conversion = 273.15; 6 | 7 | double k = c + k_conversion; 8 | return k; 9 | } 10 | 11 | int main() 12 | { 13 | constexpr double abs_0_c = -273.15; 14 | 15 | double c = 0; // declare input variable 16 | cin >> c; // retrieve temperature to input variable 17 | 18 | if (c < abs_0_c) error("Input temp is lower than absolute 0.\n"); 19 | 20 | double k = ctok(c); // convert temperature 21 | cout << k << '\n'; // print out temperature 22 | } 23 | -------------------------------------------------------------------------------- /chapter05/ex04_ctok_err.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | double ctok(double c) // converts Celsius to Kelvin 4 | { 5 | constexpr double k_conversion = 273.15; 6 | constexpr double abs_0_c = -273.15; 7 | 8 | if (c < abs_0_c) error("Input temp is lower than absolute zero.\n"); 9 | 10 | double k = c + k_conversion; 11 | return k; 12 | } 13 | 14 | int main() 15 | { 16 | double c = 0; // declare input variable 17 | cin >> c; // retrieve temperature to input variable 18 | 19 | double k = ctok(c); // convert temperature 20 | cout << k << '\n'; // print out temperature 21 | } 22 | -------------------------------------------------------------------------------- /chapter05/ex06_ctof.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | double ctof(double c) 4 | { 5 | double f = 9.0/5 * c + 32; 6 | return f; 7 | } 8 | 9 | double ftoc(double f) 10 | { 11 | double c = (f - 32) / (9.0 / 5); 12 | return c; 13 | } 14 | 15 | int main() 16 | { 17 | cout << "Enter a temperature in Celsius or Fahrenheit: (ex: 20 C)\n"; 18 | double temp; 19 | char unit; 20 | cin >> temp >> unit; 21 | 22 | double ret_temp = 0; 23 | char ret_unit = ' '; 24 | 25 | switch (unit) { 26 | case 'F': 27 | ret_temp = ftoc(temp); 28 | ret_unit = 'C'; 29 | break; 30 | case 'C': 31 | ret_temp = ctof(temp); 32 | ret_unit = 'F'; 33 | break; 34 | default: 35 | break; 36 | } 37 | 38 | cout << temp << unit << " converts to " << ret_temp << ret_unit << '\n'; 39 | } 40 | -------------------------------------------------------------------------------- /chapter05/ex07_quadratic.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | class Imaginary_number { }; 4 | double under_root(double a, double b, double c) 5 | { 6 | double val = b * b - 4 * a * c; 7 | if (val < 0) throw Imaginary_number{}; 8 | 9 | return val; 10 | } 11 | 12 | int main() 13 | try { 14 | // Program 15 | double a; 16 | double b; 17 | double c; 18 | 19 | cout << "Lets do a quadratic equation!\n" 20 | << "Please enter an 'a', 'b', and 'c' value:\n"; 21 | cin >> a >> b >> c; 22 | 23 | double negB = 0 - b; 24 | 25 | double x1 = (negB + sqrt(under_root(a, b, c))) / (2 * a); 26 | double x2 = (negB - sqrt(under_root(a, b, c))) / (2 * a); 27 | 28 | cout << "The solution values are: x1 = " << x1 29 | << ", x2 = " << x2 << '\n'; 30 | } 31 | catch(Imaginary_number) { 32 | cout << "Oops! Can't get the square root of a negative number.\n"; 33 | } 34 | catch(...) { 35 | cout << "Exception: something went wrong.\n"; 36 | } 37 | -------------------------------------------------------------------------------- /chapter05/ex08_n_integers.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | // Program 6 | cout << "Please enter the number of values you'd like to sum: \n"; 7 | int how_many = 0; 8 | cin >> how_many; 9 | 10 | vector values; 11 | cout << "Please enter some integers (press '|' to stop): \n"; 12 | for (int x; cin >> x; ) 13 | values.push_back(x); 14 | if (values.size() < how_many) 15 | error("Less values provided than initially indicated.\n"); 16 | 17 | int sum = 0; 18 | cout << "The sum of the first " << how_many << " numbers ( "; 19 | 20 | for (int i = 0; i < how_many; ++i) { 21 | cout << values[i] << " "; 22 | sum += values[i]; 23 | } 24 | 25 | cout << ") is " << sum << ".\n"; 26 | 27 | return 0; 28 | } 29 | catch(exception& e) { 30 | cerr << "Error: " << e.what() << '\n'; 31 | return 1; 32 | } 33 | catch(...) { 34 | cerr << "Unknown exception!\n"; 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /chapter05/ex09_post_integers.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | // Not sure what this question is asking for. Looked around online, could 6 | // not find a suitable answer. Seems like I'm supposed to do a post- 7 | // condition but unsure as to why. 8 | 9 | return 0; 10 | } 11 | catch(exception& e) { 12 | cerr << "Error: " << e.what() << '\n'; 13 | return 1; 14 | } 15 | catch(...) { 16 | cerr << "Unknown exception!\n"; 17 | return 2; 18 | } 19 | -------------------------------------------------------------------------------- /chapter05/ex10_double_diff.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | // Program 6 | cout << "Please enter the number of values you'd like to sum: \n"; 7 | int how_many = 0; 8 | cin >> how_many; 9 | 10 | vector values; 11 | cout << "Please enter some integers (press '|' to stop): \n"; 12 | for (double x; cin >> x; ) 13 | values.push_back(x); 14 | if (values.size() < how_many) 15 | error("Less values provided than initially indicated.\n"); 16 | 17 | vector diffs; 18 | for (int j = 1; j < values.size(); ++j) 19 | diffs.push_back(values[j] - values[j - 1]); 20 | 21 | double sum = 0; 22 | cout << "The sum of the first " << how_many << " numbers ( "; 23 | 24 | for (int i = 0; i < how_many; ++i) { 25 | cout << values[i] << " "; 26 | sum += values[i]; 27 | } 28 | 29 | cout << ") is " << sum << ".\n"; 30 | 31 | cout << "The differences between entries were:\n" << "( "; 32 | for(double d : diffs) 33 | cout << d << ' '; 34 | cout << ")\n"; 35 | 36 | return 0; 37 | } 38 | catch(exception& e) { 39 | cerr << "Error: " << e.what() << '\n'; 40 | return 1; 41 | } 42 | catch(...) { 43 | cerr << "Unknown exception!\n"; 44 | return 2; 45 | } 46 | -------------------------------------------------------------------------------- /chapter05/ex11_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int fibonacci(int count) 4 | { 5 | int pre = 0; 6 | int cur = 1; 7 | int sum = 0; 8 | 9 | if (count > 0) cout << 1 << ", "; 10 | 11 | for (int i = 1; i < count; ++i) { 12 | sum = pre + cur; 13 | cout << sum << ", "; 14 | pre = cur; 15 | cur = sum; 16 | } 17 | cout << '\n'; 18 | return 0; 19 | } 20 | 21 | int main() 22 | try { 23 | // Fibonacci 24 | cout << "How many Fibonacci numbers do you want?\n"; 25 | int count; 26 | cin >> count; 27 | fibonacci(count); 28 | 29 | return 0; 30 | } 31 | catch(exception& e) { 32 | cerr << "error: " << e.what() << '\n'; 33 | return 1; 34 | } 35 | catch(...) { 36 | cerr << "Unknown exception!\n"; 37 | return 2; 38 | } 39 | -------------------------------------------------------------------------------- /chapter05/ex12_bulls_cows.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | // Bulls and Cows 6 | vector hidden = { 1, 2, 3, 5 }; 7 | bool success = false; 8 | 9 | cout << "Let's play Bulls & Cows! Guess 4 integers w/o repeating:\n"; 10 | while (!success) { 11 | vector guess; 12 | 13 | int bulls = 0; 14 | int cows = 0; 15 | 16 | for (int val; guess.size() < 4; ) { 17 | cin >> val; 18 | if (!cin) error("Couldn't read integer from input\n"); 19 | guess.push_back(val); 20 | } 21 | 22 | for (int i = 0; i < guess.size(); ++i) 23 | for (int j = 0; j < hidden.size(); ++j) 24 | if (guess[i] == hidden[j]) { 25 | if (i == j) 26 | ++bulls; 27 | else 28 | ++cows; 29 | } 30 | 31 | cout << bulls << " bulls, " << cows << " cows.\n"; 32 | 33 | if (bulls == 4) success = true; 34 | } 35 | cout << "Success!!\n"; 36 | 37 | return 0; 38 | } 39 | catch(exception& e) { 40 | cerr << "error: " << e.what() << '\n'; 41 | return 1; 42 | } 43 | catch(...) { 44 | cerr << "Unknown exception!\n"; 45 | return 2; 46 | } 47 | -------------------------------------------------------------------------------- /chapter05/x_bad_area.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | class Bad_area { }; 4 | 5 | int area(int length, int width) 6 | { 7 | // Callee deals with errors (better) 8 | if (length <= 0 || width <= 0) throw Bad_area{}; 9 | 10 | return length * width; 11 | } 12 | 13 | int framed_area(int x, int y) 14 | { 15 | constexpr int frame_width = 2; 16 | 17 | return area(x - frame_width, y - frame_width); 18 | } 19 | 20 | int main() 21 | try { 22 | // Program 23 | int x = -1; 24 | int y = 2; 25 | int z = 4; 26 | 27 | /* Caller deals with errors (not optimal) 28 | if (x <= 0 || y <= 0) 29 | error("non-positive area() arg"); 30 | if (z <= 2) 31 | error("non-positive 2nd area() arg called by frame_area()"); 32 | if (y <= 2 || z <= 2) 33 | error("non-positive area() arg called by framed_area()"); 34 | */ 35 | 36 | int area1 = area(x, y); 37 | int area2 = framed_area(1, z); 38 | int area3 = framed_area(y, z); 39 | double ratio = double(area1) / area3; // convert to double to get 40 | // floating-point division 41 | } 42 | catch (Bad_area) { 43 | cout << "Oops! bad arguments to area()\n"; 44 | } 45 | -------------------------------------------------------------------------------- /chapter06/ex04_Name_value.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | class Name_value { 4 | public: 5 | string name; 6 | int value; 7 | Name_value(string n, int v) 8 | :name(n), value(v) { } 9 | }; 10 | 11 | int main() 12 | { 13 | // Accept name value pairs, store them in vectors, print them in the end. 14 | 15 | vector record; 16 | 17 | bool end = false; 18 | bool repeat = false; 19 | 20 | while (!end) { 21 | cout << "Enter a name and a value:\n"; 22 | string name; 23 | int score; 24 | cin >> name >> score; 25 | 26 | if (name == "NoName" && score == 0) 27 | end = true; 28 | else { 29 | for (Name_value n : record) 30 | if (n.name == name) 31 | repeat = true; 32 | 33 | if (repeat) { 34 | cout << "Error: duplicate name\n"; 35 | repeat = false; 36 | } else { 37 | record.push_back(Name_value{name, score}); 38 | } 39 | } 40 | } 41 | 42 | cout << "RESULTS:\n"; 43 | 44 | for (Name_value n : record) 45 | cout << n.name << '\t' << n.value << '\n'; 46 | } 47 | -------------------------------------------------------------------------------- /chapter06/ex07_bitwise.cpp: -------------------------------------------------------------------------------- 1 | GRAMMAR 2 | ======= 3 | 4 | OR: | 5 | 6 | EXCLUSIVE OR: ^ 7 | 8 | AND: & 9 | 10 | UNARY: !, ~ 11 | -------------------------------------------------------------------------------- /chapter06/ex08_bulls_again.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | // Bulls and Cows 6 | vector hidden = { 'c', 'j', 'o', 'b' }; 7 | bool success = false; 8 | 9 | cout << "Let's play Bulls & Cows! Guess 4 characters w/o repeating:\n"; 10 | while (!success) { 11 | vector guess; 12 | 13 | int bulls = 0; 14 | int cows = 0; 15 | 16 | for (char ch; guess.size() < 4; ) { 17 | cin >> ch; 18 | if (!cin) error("Couldn't read character from input\n"); 19 | guess.push_back(ch); 20 | } 21 | 22 | for (int i = 0; i < guess.size(); ++i) 23 | for (int j = 0; j < hidden.size(); ++j) 24 | if (guess[i] == hidden[j]) { 25 | if (i == j) 26 | ++bulls; 27 | else 28 | ++cows; 29 | } 30 | 31 | cout << bulls << " bulls, " << cows << " cows.\n"; 32 | 33 | if (bulls == 4) success = true; 34 | } 35 | cout << "Success!!\n"; 36 | 37 | return 0; 38 | } 39 | catch(exception& e) { 40 | cerr << "error: " << e.what() << '\n'; 41 | return 1; 42 | } 43 | catch(...) { 44 | cerr << "Unknown exception!\n"; 45 | return 2; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /chapter08/ch08_drill_1_my.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | #include "./ch08_drill_1_my.h" 3 | 4 | int foo; 5 | 6 | void print_foo() 7 | { 8 | cout << foo << '\n'; 9 | } 10 | 11 | void print(int i) 12 | { 13 | cout << i << '\n'; 14 | } 15 | -------------------------------------------------------------------------------- /chapter08/ch08_drill_1_my.h: -------------------------------------------------------------------------------- 1 | extern int foo; 2 | void print_foo(); 3 | void print(int); 4 | -------------------------------------------------------------------------------- /chapter08/ch08_drill_1_use.cpp: -------------------------------------------------------------------------------- 1 | #include "./ch08_drill_1_my.h" 2 | 3 | int main() 4 | { 5 | foo = 7; 6 | print_foo(); 7 | print(99); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chapter08/ch08_drill_2.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | void swap_v(int a, int b) 4 | { 5 | int temp; 6 | temp = a; 7 | a = b; 8 | b = temp; 9 | } 10 | 11 | void swap_r(int& a, int& b) 12 | { 13 | int temp; 14 | temp = a; 15 | a = b; 16 | b = temp; 17 | } 18 | 19 | /* 20 | void swap_cr(const int& a, const int& b) 21 | { 22 | int temp; 23 | temp = a; 24 | a = b; 25 | b = temp; 26 | } 27 | */ 28 | 29 | int main() 30 | { 31 | int x = 7; 32 | int y = 9; 33 | swap_r(x, y); 34 | swap_r(7, 9); 35 | const int cx = 7; 36 | const int cy = 9; 37 | swap_r(cx, cy); 38 | swap_r(7.7, 9.9); 39 | double dx = 7.7; 40 | double dy = 9.9; 41 | swap_r(dx, dy); 42 | swap_r(7.7, 9.9); 43 | 44 | cout << "x: " << x << '\n' 45 | << "y: " << y << '\n' 46 | << "cx: " << cx << '\n' 47 | << "cy: " << cy << '\n' 48 | << "dx: " << dx << '\n' 49 | << "dy: " << dy << '\n'; 50 | } 51 | -------------------------------------------------------------------------------- /chapter08/ch08_drill_3.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | namespace X { 4 | int var = 0; 5 | 6 | void print() 7 | { 8 | cout << "X: " << var << '\n'; 9 | } 10 | } 11 | 12 | namespace Y { 13 | int var = 0; 14 | 15 | void print() 16 | { 17 | cout << "Y: " << var << '\n'; 18 | } 19 | } 20 | 21 | namespace Z { 22 | int var = 0; 23 | 24 | void print() 25 | { 26 | cout << "Z: " << var << '\n'; 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | X::var = 7; 33 | X::print(); // print X's var 34 | using namespace Y; 35 | var = 9; 36 | print(); // print Y's var 37 | { 38 | using Z::var; 39 | using Z::print; 40 | var = 11; 41 | print(); // print Z's var 42 | } 43 | print(); // print Y's var 44 | X::print(); // print X's var 45 | } 46 | -------------------------------------------------------------------------------- /chapter08/ex02_int_print.h: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // QA EDIT: changed this file to a header that ex03 includes and removed the 4 | // identical ex_fac.h file. 5 | 6 | void int_printer(string label, vector& ints) 7 | { 8 | cout << label << ": "; 9 | 10 | for (int i : ints) 11 | cout << i << ' '; 12 | 13 | cout << '\n'; 14 | } 15 | -------------------------------------------------------------------------------- /chapter08/ex03_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | #include "./ex02_int_print.h" 3 | 4 | void fibonacci(int x, int y, vector& v, int n) 5 | { 6 | if (n < 2) error("desired size of vector is smaller than 2\n"); 7 | 8 | v.push_back(x); 9 | v.push_back(y); 10 | 11 | int pre = y; 12 | int cur = x + y; 13 | int temp = 0; 14 | for (int i = 2; i < n; ++i) { 15 | v.push_back(cur); 16 | temp = cur; 17 | cur = pre + cur; 18 | pre = temp; 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | int val1 = 1; 25 | int val2 = 2; 26 | vector ints; 27 | 28 | cout << "How many fibonacci numbers would you like to see?\n"; 29 | int number_of_fibs; 30 | cin >> number_of_fibs; 31 | 32 | fibonacci(val1, val2, ints, number_of_fibs); 33 | 34 | int_printer("fibonacci", ints); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /chapter08/ex07_name_age.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | vector names; 6 | vector ages; 7 | 8 | cout << "Enter 5 names:\n"; 9 | 10 | while (names.size() < 5) { 11 | string n; 12 | cin >> n; 13 | names.push_back(n); 14 | } 15 | 16 | for (string n : names) { 17 | cout << "How old is " << n << "? "; 18 | int a; 19 | cin >> a; 20 | ages.push_back(a); 21 | } 22 | 23 | if (names.size() != ages.size()) 24 | error("Somehow ages and names do not match up"); 25 | 26 | for (int i = 0; i < names.size(); ++i) 27 | cout << names[i] << ' ' << ages[i] << '\n'; 28 | 29 | vector unordered_names = names; 30 | vector ordered_ages; 31 | 32 | sort(names.begin(), names.end()); 33 | 34 | for (string n : names) 35 | for (int i = 0; i < unordered_names.size(); ++i) 36 | if (n == unordered_names[i]) ordered_ages.push_back(ages[i]); 37 | 38 | for (int i = 0; i < names.size(); ++i) 39 | cout << names[i] << ' ' << ordered_ages[i] << '\n'; 40 | } 41 | -------------------------------------------------------------------------------- /chapter08/ex08_names_unlimited.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | vector names; 6 | vector ages; 7 | 8 | cout << "Enter some names(terminate with 'DONE'):\n"; 9 | 10 | while (true) { 11 | string n; 12 | cin >> n; 13 | if (n == "DONE") break; 14 | names.push_back(n); 15 | } 16 | 17 | for (string n : names) { 18 | cout << "How old is " << n << "? "; 19 | int a; 20 | cin >> a; 21 | ages.push_back(a); 22 | } 23 | 24 | if (names.size() != ages.size()) 25 | error("Somehow ages and names do not match up"); 26 | 27 | cout << '\n' 28 | << "List of names and ages:\n"; 29 | for (int i = 0; i < names.size(); ++i) 30 | cout << names[i] << ' ' << ages[i] << '\n'; 31 | 32 | vector unordered_names = names; 33 | vector ordered_ages; 34 | 35 | sort(names.begin(), names.end()); 36 | 37 | for (string n : names) 38 | for (int i = 0; i < unordered_names.size(); ++i) 39 | if (n == unordered_names[i]) ordered_ages.push_back(ages[i]); 40 | 41 | cout << '\n' 42 | << "Sorted by name:\n"; 43 | for (int i = 0; i < names.size(); ++i) 44 | cout << names[i] << ' ' << ordered_ages[i] << '\n'; 45 | } 46 | -------------------------------------------------------------------------------- /chapter08/ex09_index.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // QA OBSERVATION: std::inner_product! 4 | 5 | double get_index(const vector& p, const vector& w) 6 | { 7 | if (p.size() != w.size()) error("get_index: different sized vectors"); 8 | 9 | double sum = 0; 10 | for (int i = 0; i < p.size(); ++i) 11 | sum += p[i] * w[i]; 12 | return sum; 13 | } 14 | 15 | int main() 16 | { 17 | vector price = { 1.23, 3.18, 0.93, 2.80, 6.75 }; 18 | vector weight = { 1.2, 1.6, 1.4, 1.9, 1.1 }; 19 | 20 | cout << get_index(price, weight) << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /chapter08/ex10_maxv.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int maxv(const vector& v) 4 | { 5 | int ele = 0; 6 | double max = v[0]; 7 | 8 | for (int i = 0; i < v.size(); ++i) { 9 | if (v[i] > max) { 10 | max = v[i]; 11 | ele = i; 12 | } 13 | } 14 | 15 | return ele; 16 | } 17 | 18 | int main() 19 | { 20 | vector test1 = { 1.2, 3.8, -1.5, 5, 2.2 }; 21 | vector test2 = { 20, 16, 8, 2, 12 }; 22 | vector test3 = { -12, -4.6, -10, -31.8 }; 23 | 24 | cout << "Test 1's max ele is at " << maxv(test1) << '\n' 25 | << "Test 2's max ele is at " << maxv(test2) << '\n' 26 | << "Test 3's max ele is at " << maxv(test3) << '\n'; 27 | } 28 | -------------------------------------------------------------------------------- /chapter08/ex11_data_set.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | struct Vector_data { 4 | double small = 0; 5 | double large = 0; 6 | double mean = 0; 7 | double median = 0; 8 | }; 9 | 10 | double get_median(vector v) 11 | { 12 | sort(v); 13 | double median = 0; 14 | int size = v.size(); 15 | 16 | if (size % 2 == 0) { 17 | int half = size / 2; 18 | median = (v[half] + v[half + 1]) / 2; 19 | } else { 20 | median = v[(size - 1) / 2]; 21 | } 22 | 23 | return median; 24 | } 25 | 26 | Vector_data get_data(vector v) 27 | { 28 | Vector_data data; 29 | double sum = 0; 30 | 31 | sort(v); 32 | 33 | for (double val : v) 34 | sum += val; 35 | 36 | data.small = v[0]; 37 | data.large = v[v.size() - 1]; 38 | data.mean = sum / v.size(); 39 | data.median = get_median(v); 40 | 41 | return data; 42 | } 43 | 44 | int main() 45 | { 46 | vector data_set = { 2.6, -1, 15.3, 8.8, 17.1, -6.9 }; 47 | 48 | Vector_data data = get_data(data_set); 49 | 50 | cout << "Smallest is: " << data.small << '\n' 51 | << "Largest is: " << data.large << '\n' 52 | << "Mean is: " << data.mean << '\n' 53 | << "Median is: " << data.median << '\n'; 54 | } 55 | -------------------------------------------------------------------------------- /chapter08/ex13_string_chars.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | vector get_lengths(const vector& s) 4 | { 5 | vector lengths; 6 | 7 | for (string str : s) 8 | lengths.push_back(str.size()); 9 | 10 | return lengths; 11 | } 12 | 13 | string get_smallest(const vector& s) 14 | { 15 | string small = s[0]; 16 | 17 | for (string str : s) 18 | if (str.size() < small.size()) small = str; 19 | 20 | return small; 21 | } 22 | 23 | string get_largest(const vector& s) 24 | { 25 | string large = s[0]; 26 | 27 | for (string str : s) 28 | if (str.size() > large.size()) large = str; 29 | 30 | return large; 31 | } 32 | 33 | int main() 34 | { 35 | vector games = { "Crackdown", "Halo", "Gears of War", "Forza", 36 | "Tomb Raider", "Bioshock", "Fallout" }; 37 | 38 | vector lengths = get_lengths(games); 39 | 40 | cout << "List of Games\n" 41 | << "=============\n"; 42 | 43 | for (int i = 0; i < games.size(); ++i) 44 | cout << games[i] << " " << lengths[i] << '\n'; 45 | 46 | cout << '\n' 47 | << "Smallest: " << get_smallest(games) << '\n' 48 | << "Largest: " << get_largest(games) << '\n'; 49 | 50 | sort(games); 51 | 52 | cout << "First: " << games[0] << '\n' 53 | << "Last: " << games[games.size() - 1] << '\n'; 54 | } 55 | -------------------------------------------------------------------------------- /chapter08/ex14_const_value.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 8 Exercise 14 5 | // 6 | // What are the implications of declaring a non-reference const argument? 7 | // 8 | // A non-reference const passes a copy of a value that you don't want to 9 | // modify. Maybe if you were returning the value in the end after using it 10 | // for side-effect purposes..? Perhaps declaring const can help an optimization 11 | // in the compiler..? If a value is smaller than a reference to itselt (int or 12 | // char) then maybe we don't want to pass a reference but we still want the 13 | // benefits of const? 14 | // 15 | 16 | #include 17 | #include 18 | 19 | double print_double(const double cd) 20 | // return value after using it but not modifying it 21 | // but why need to? obviously the caller has access to the value and 22 | // doesn't need it returned.. 23 | { 24 | std::cout << cd * 2 << '\n'; 25 | return cd; 26 | } 27 | 28 | int main() 29 | try { 30 | // code 31 | print_double(9.8); 32 | } 33 | catch(std::exception& e) { 34 | std::cerr << "Exception: " << e.what() << '\n'; 35 | return 1; 36 | } 37 | catch(...) { 38 | std::cerr << "Unknown exception\n"; 39 | return 2; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /chapter09/ex04_gibberish.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // dangerously ugly code 4 | struct X { 5 | void f(int x) { 6 | struct Y { 7 | int f() { return 1; } 8 | int m; 9 | }; 10 | int m; 11 | m=x; 12 | Y m2; 13 | return f(m2.f()); 14 | } 15 | int m; 16 | void g(int m) { 17 | if (m) 18 | f(m + 2); 19 | else { 20 | g(m+2); 21 | } 22 | } 23 | X() { } 24 | void m3() { } 25 | 26 | void main() { 27 | X a; 28 | a.f(2); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /chapter09/ex05-9_library/ex08_patron.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Patron { 4 | public: 5 | Patron(string n, int c); 6 | void inc_fees(double incFee); 7 | void set_fees(double newFee); 8 | bool owes_fees(); 9 | 10 | string get_name() const { return name; } 11 | int get_card_num() const { return card_num; } 12 | double get_fees() const { return fees; } 13 | 14 | private: 15 | string name; 16 | int card_num; 17 | double fees; 18 | }; 19 | 20 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 21 | 22 | Patron::Patron(string n, int c) 23 | :name{n}, card_num{c} 24 | { 25 | fees = 0; 26 | } 27 | 28 | void Patron::inc_fees(double incFee) 29 | { 30 | fees += incFee; 31 | } 32 | 33 | void Patron::set_fees(double newFee) 34 | { 35 | fees = newFee; 36 | } 37 | 38 | bool Patron::owes_fees() 39 | { 40 | return fees > 0; 41 | } 42 | -------------------------------------------------------------------------------- /chapter09/ex12_date/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 9 Exercise 12 5 | // 6 | // Change the representation of a Date to be the number of days since January 7 | // 1, 1970, represented as a long int and reimplement the functions from §9.8. 8 | // 9 | // Additional notes found in date.h 10 | // 11 | 12 | #include 13 | #include 14 | #include "date.h" 15 | 16 | using Chrono_days::Date; 17 | 18 | int main() 19 | try { 20 | Date d1 {0}; 21 | std::cout << d1 << '\n'; 22 | 23 | Date d2 {1}; 24 | std::cout << d2 << '\n'; 25 | 26 | Date d3 {365}; 27 | std::cout << d3 << '\n'; 28 | 29 | Date d4 {1096}; // Day 0 + 365 + 365 + 366 (1972 was leapyear) 30 | std::cout << d4 << '\n'; //> (1973, January, 1) 31 | 32 | Date d5 {3344}; 33 | std::cout << d5 << '\n'; //> (1979, February, 27) 34 | 35 | std::cout << "Enter a day in the format (YYYY, MM, DD):\n"; 36 | Date d6 {}; 37 | std::cin >> d6; 38 | std::cout << d6 << ": " << d6.get_days() << '\n'; 39 | } 40 | catch(std::exception& e) { 41 | std::cerr << "Exception: " << e.what() << '\n'; 42 | return 1; 43 | } 44 | catch(...) { 45 | std::cerr << "Unknown exception\n"; 46 | return 2; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /chapter09/ex13_rational/main.cpp: -------------------------------------------------------------------------------- 1 | #include "rational.h" 2 | #include 3 | 4 | using std::cout; 5 | using std::cin; 6 | using std::cerr; 7 | 8 | int main() 9 | try { 10 | cout << "Enter the integers of two rational numbers:\n"; 11 | int a, b, c, d; 12 | cin >> a >> b >> c >> d; 13 | Rational r1{a, b}; 14 | Rational r2{c, d}; 15 | 16 | cout << "Add: " << r1 + r2 << '\n' 17 | << "Sub: " << r1 - r2 << '\n' 18 | << "Mul: " << r1 * r2 << '\n' 19 | << "Div: " << r1 / r2 << '\n'; 20 | 21 | return 0; 22 | } 23 | catch(std::exception& e) { 24 | cerr << e.what() << '\n'; 25 | } 26 | catch(...) { 27 | cerr << "unknown exception" << '\n'; 28 | } 29 | -------------------------------------------------------------------------------- /chapter09/ex13_rational/rational.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::ostream; 4 | 5 | class Rational { 6 | public: 7 | // to throw as exception 8 | class Zero_Denom { }; 9 | 10 | // getters 11 | int get_num() const { return numerator; } 12 | int get_den() const { return denominator; } 13 | 14 | // constructor 15 | Rational(int n, int d); 16 | 17 | // double representation 18 | double to_double() const { return double(numerator) / denominator; } 19 | private: 20 | int numerator; 21 | int denominator; 22 | }; 23 | 24 | ostream& operator<<(ostream& os, const Rational& r); 25 | 26 | bool operator==(const Rational& a, const Rational& b); 27 | bool operator!=(const Rational& a, const Rational& b); 28 | 29 | Rational operator+(const Rational& a, const Rational& b); 30 | Rational operator-(const Rational& a, const Rational& b); 31 | Rational operator*(const Rational& a, const Rational& b); 32 | Rational operator/(const Rational& a, const Rational& b); 33 | -------------------------------------------------------------------------------- /chapter09/ex14-16_money/main.cpp: -------------------------------------------------------------------------------- 1 | #include "money.h" 2 | #include 3 | 4 | int main() 5 | { 6 | Money five_bucks{5.00}; 7 | Money ten_bucks{10.00}; 8 | 9 | std::cout << five_bucks << '\n' 10 | << ten_bucks << '\n' 11 | << five_bucks + ten_bucks << '\n'; 12 | 13 | Money loose_change{2.35}; 14 | Money extra_deci{17.312}; 15 | Money round_up{8.628}; 16 | 17 | std::cout << loose_change << '\n' 18 | << extra_deci << '\n' 19 | << round_up << '\n'; 20 | 21 | // International money 22 | Money canadian{"CDN", 53.20}; 23 | Money danish{"DKK", 16.07}; 24 | Money american{"USD", 1.72}; 25 | 26 | std::cout << canadian << '\n' 27 | << danish << '\n' 28 | << american << '\n'; 29 | } 30 | -------------------------------------------------------------------------------- /chapter09/ex14-16_money/money.cpp: -------------------------------------------------------------------------------- 1 | #include "money.h" 2 | 3 | Money::Money(double d) 4 | { 5 | double to_cents = d * 100; 6 | cents = int(to_cents + 0.5); 7 | } 8 | 9 | Money::Money(int c) 10 | { 11 | cents = c; 12 | } 13 | 14 | Money::Money(std::string s, double d) 15 | { 16 | currency = s; 17 | 18 | double to_cents = d * 100; 19 | cents = int(to_cents + 0.5); 20 | } 21 | 22 | std::ostream& operator<<(std::ostream& os, const Money& m) 23 | { 24 | return os << m.get_currency() << m.get_dollars(); 25 | } 26 | 27 | Money operator+(const Money& a, const Money& b) 28 | { 29 | return Money{a.get_cents() + b.get_cents()}; 30 | } 31 | 32 | Money operator-(const Money& a, const Money& b) 33 | { 34 | return Money{a.get_cents() - b.get_cents()}; 35 | } 36 | -------------------------------------------------------------------------------- /chapter09/ex14-16_money/money.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class Money { 5 | public: 6 | Money(double); 7 | Money(int); 8 | Money(std::string, double); 9 | 10 | int get_cents() const { return cents; } 11 | std::string get_currency() const { return currency; } 12 | double get_dollars() const { return double(cents) / 100; } 13 | private: 14 | long int cents; 15 | std::string currency = "$"; 16 | }; 17 | 18 | std::ostream& operator<<(std::ostream&, const Money&); 19 | //std::istream& operator>>(std::istream&, Money&); 20 | 21 | Money operator+(const Money&, const Money&); 22 | Money operator-(const Money&, const Money&); 23 | -------------------------------------------------------------------------------- /chapter10/ex01_sum_ints.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int sum_of_file(string fname) 4 | // returns the sum of all integers in a file 5 | { 6 | ifstream ist { fname }; 7 | if (!ist) error("could not read from file ", fname); 8 | 9 | int sum = 0; 10 | for (int i; ist >> i; ) 11 | sum += i; 12 | 13 | return sum; 14 | } 15 | 16 | int main() 17 | try { 18 | cout << sum_of_file("white_ints.txt") << '\n'; 19 | } 20 | catch (exception& e) { 21 | cerr << "Error: " << e.what() << '\n'; 22 | return 1; 23 | } 24 | catch (...) { 25 | cerr << "Something awful happened..\n"; 26 | return 2; 27 | } 28 | -------------------------------------------------------------------------------- /chapter10/ex04_temp_units.cpp: -------------------------------------------------------------------------------- 1 | // This exercise modifies ex02 and ex03 2 | -------------------------------------------------------------------------------- /chapter10/ex09_merge_sort.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | void fill_from_file(ifstream& ifs, vector& v) 4 | { 5 | ifs.exceptions(ifs.exceptions()|ios_base::badbit); 6 | 7 | for (string s; ifs >> s; ) v.push_back(s); 8 | if (ifs.eof()) return; 9 | error("encountered fail in file read"); 10 | } 11 | 12 | int main() 13 | try { 14 | cout << "Enter the first file of words:\n"; 15 | string file_1; 16 | cin >> file_1; 17 | ifstream ifs_1 { file_1 }; 18 | if (!ifs_1) error("could not read from file ", file_1); 19 | 20 | cout << "Enter the second file of words:\n"; 21 | string file_2; 22 | cin >> file_2; 23 | ifstream ifs_2 { file_2 }; 24 | if (!ifs_2) error("could not read from file ", file_2); 25 | 26 | vector words; 27 | 28 | fill_from_file(ifs_1, words); 29 | fill_from_file(ifs_2, words); 30 | 31 | sort(words); 32 | 33 | for (string w : words) 34 | cout << w << ' '; 35 | cout << '\n'; 36 | } 37 | catch(exception& e) { 38 | cerr << "Error: " << e.what() << '\n'; 39 | return 1; 40 | } 41 | catch(...) { 42 | cerr << "Something weird happened..\n"; 43 | return 2; 44 | } 45 | -------------------------------------------------------------------------------- /chapter10/ex11_only_ints.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | void fill_only_ints(ifstream& ifs, vector& v) 4 | // only fill with ints, otherwise discard as string and recurse 5 | { 6 | for (int i; ifs >> i; ) v.push_back(i); 7 | if (ifs.eof()) return; 8 | ifs.clear(); 9 | string discard; 10 | ifs >> discard; 11 | fill_only_ints(ifs, v); 12 | } 13 | 14 | int main() 15 | try { 16 | cout << "Enter the file with some ints:\n"; 17 | string fname; 18 | cin >> fname; 19 | ifstream ifs { fname }; 20 | 21 | if (!ifs) error("could not read from file ", fname); 22 | ifs.exceptions(ifs.exceptions()|ios_base::badbit); 23 | 24 | vector only_ints; 25 | fill_only_ints(ifs, only_ints); 26 | 27 | int sum = 0; 28 | for (int i : only_ints) sum += i; 29 | 30 | cout << "The sum of all ints in that file is: " << sum << '\n'; 31 | } 32 | catch(exception& e) { 33 | cerr << "Error: " << e.what() << '\n'; 34 | return 1; 35 | } 36 | catch(...) { 37 | cerr << "Something weird went wrong..\n"; 38 | return 2; 39 | } 40 | -------------------------------------------------------------------------------- /chapter10/input.txt: -------------------------------------------------------------------------------- 1 | 0 7 2 | 1 7 3 | 2 6 4 | 3 6 5 | 4 6 6 | 5 5 7 | 6 4 8 | 7 4 9 | 8 5 10 | 9 7 11 | 10 8 12 | 11 10 13 | 12 11 14 | 13 12 15 | 14 13 16 | 15 13 17 | 16 14 18 | 17 14 19 | 18 13 20 | 19 12 21 | 20 10 22 | 21 9 23 | 22 9 24 | 23 8 25 | -------------------------------------------------------------------------------- /chapter10/input_1.txt: -------------------------------------------------------------------------------- 1 | apple cat funicula javascript 2 | -------------------------------------------------------------------------------- /chapter10/input_2.txt: -------------------------------------------------------------------------------- 1 | aardvark dagger elephant roger 2 | -------------------------------------------------------------------------------- /chapter10/input_3.txt: -------------------------------------------------------------------------------- 1 | bears 13 of a brownish 5 color are 2 times more likely to be 17 but what about 1.1 ? 2 | -------------------------------------------------------------------------------- /chapter10/mydata.txt: -------------------------------------------------------------------------------- 1 | (1,2)(2,3)(3,4)(4,5)(5,6)(6,7)(7,8) -------------------------------------------------------------------------------- /chapter10/output.txt: -------------------------------------------------------------------------------- 1 | 1990: 2 | 1991: 3 | June 4 | 1992: 5 | January 6 | 1 0 61.5 7 | February 8 | 1 1 64 9 | 2 2 65.2 10 | 2000: 11 | February 12 | 1 0 67.2 13 | 1 1 68 14 | 2 3 66.66 15 | December 16 | 14 0 -2 17 | 15 14 -8.8 18 | 15 15 -9.2 19 | -------------------------------------------------------------------------------- /chapter10/output_1_2.txt: -------------------------------------------------------------------------------- 1 | setting the attractions of my good parts aside, I have no other charms. -------------------------------------------------------------------------------- /chapter10/raw_temps.txt: -------------------------------------------------------------------------------- 1 | 23 48.2 f 2 | 0 47.7 f 3 | 1 47.1 f 4 | 2 46.5 f 5 | 3 45.6 f 6 | 4 44.8 f 7 | 5 43.9 f 8 | 6 43.2 f 9 | 7 42.9 f 10 | 8 43 f 11 | 9 44.1 f 12 | 10 46.2 f 13 | 11 47.5 f 14 | 12 49.2 f 15 | 13 53.4 f 16 | 14 57.1 f 17 | 15 60.3 f 18 | 16 63.8 f 19 | 17 64.4 f 20 | 18 63.8 f 21 | 19 62.2 f 22 | 20 60.7 f 23 | 21 58.9 f 24 | 22 55.6 f 25 | 23 52.2 f 26 | 0 47.7 f 27 | 1 47.1 f 28 | 2 46.5 f 29 | 3 45.6 f 30 | 4 44.8 f 31 | 5 43.9 f 32 | 6 43.2 f 33 | 7 42.9 f 34 | 8 43 f 35 | 9 44.1 f 36 | 10 46.2 f 37 | 11 47.5 f 38 | 12 49.2 f 39 | 13 53.4 f 40 | 14 57.1 f 41 | 15 60.3 f 42 | 16 63.8 f 43 | 17 64.4 f 44 | 18 63.8 f 45 | 19 62.2 f 46 | 20 60.7 f 47 | 21 58.9 f 48 | 22 55.6 f 49 | 23 52.2 f 50 | 0 8 c 51 | 1 7 c 52 | 2 6 c 53 | 3 5 c 54 | 4 5 c 55 | 5 5 c 56 | 6 6 c 57 | 7 7 c 58 | 8 7 c 59 | 9 7 c 60 | 10 8 c 61 | 11 8 c 62 | 12 10 c 63 | 13 11 c 64 | 14 12 c 65 | 15 13 c 66 | 16 13 c 67 | 17 13 c 68 | 18 12 c 69 | 19 11 c 70 | 20 10 c 71 | 21 9 c 72 | 22 8 c 73 | 23 8 c 74 | -------------------------------------------------------------------------------- /chapter10/reading_data.txt: -------------------------------------------------------------------------------- 1 | { year 1990 } 2 | { year 1991 { month jun } } 3 | { year 1992 { month jan (1 0 61.5) } { month feb (1 1 64) (2 2 65.2) } } 4 | { year 2000 5 | { month feb (1 1 68) (2 3 66.66) (1 0 67.2) } 6 | { month dec (15 15 -9.2) (15 14 -8.8) (14 0 -2) } 7 | } 8 | -------------------------------------------------------------------------------- /chapter10/white_ints.txt: -------------------------------------------------------------------------------- 1 | 5 7 10 15 2 | -------------------------------------------------------------------------------- /chapter11/adama.txt: -------------------------------------------------------------------------------- 1 | The Cylon War is long over, yet we must not forget the reasons why so many sacrificed so much in the cause of freedom. The cost of wearing the uniform can be high ... [after looking at crowd] but sometimes it's too high. You know, when we fought the Cylons, we did it to save ourselves from extinction. But we never answered the question, why? Why are we as a people worth saving? We still commit murder because of greed, spite, jealousy. And we still visit all of our sins upon our children. We refuse to accept the responsibility for anything that we've done. Like we did with the Cylons. We decided to play God, create life. When that life turned against us, we comforted ourselves in the knowledge that it really wasn't our fault, not really. You cannot play God then wash your hands of the things that you've created. Sooner or later, the day comes when you can't hide from the things that you've done anymore. 2 | -------------------------------------------------------------------------------- /chapter11/ch11_drill_test_output.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // This pretty much is a redo of all of the try-this's from the chapter 4 | 5 | int main() 6 | try { 7 | int birth_year = 1979; 8 | 9 | cout << "decimal\t\t" << birth_year << '\n' << hex 10 | << "hexadecimal\t" << birth_year << '\n' << oct 11 | << "octal\t\t" << birth_year << '\n'; 12 | 13 | cout << dec << 38 << '\n'; 14 | 15 | cout << '|' << setw(8) << "Chris" << '|' << setw(8) << "Schick" 16 | << '|' << setw(24) << "seeschickrun@gmail.com" 17 | << '|' << setw(16) << "(204) 555-1234" << "|\n" 18 | << '|' << setw(8) << "Dave" << '|' << setw(8) << "Wesst" 19 | << '|' << setw(24) << "pocketchamp@gmail.com" 20 | << '|' << setw(16) << "(204) 555-4321" << "|\n"; 21 | } 22 | catch(exception& e) { 23 | cerr << "Error: " << e.what() << '\n'; 24 | return 1; 25 | } 26 | catch(...) { 27 | cerr << "Something troubling occurred..\n"; 28 | return 2; 29 | } 30 | -------------------------------------------------------------------------------- /chapter11/ch11_tt_scratch.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // This includes many of the 'Try This's from the chapter 4 | // The final 'try this' is in the chapter drill 5 | 6 | int main() 7 | try { 8 | // Integer output 9 | /* 10 | cout << "decimal\t\t" << 1979 << '\t' << 38 << '\n' << hex 11 | << "hexadecimal\t" << 1979 << '\t' << 38 << '\n' << oct 12 | << "octal\t\t" << 1979 << '\t' << 38 << '\n'; 13 | */ 14 | 15 | // Integer input 16 | /* 17 | int a; 18 | int b; 19 | int c; 20 | int d; 21 | cin >> a >> hex >> b >> oct >> c >> d; 22 | cout << a << '\t' << b << '\t' << c << '\t' << d << '\n'; 23 | */ 24 | 25 | // Floating point output 26 | cout << 1234.56789 << "\t\t(defaultfloat)\n" << fixed 27 | << 1234.56789 << "\t(fixed)\n" << scientific 28 | << 1234.56789 << "\t(scientific)\n"; 29 | 30 | return 0; 31 | } 32 | catch(exception& e) { 33 | cerr << "Error: " << e.what() << '\n'; 34 | return 1; 35 | } 36 | catch(...) { 37 | cerr << "Something terribad happened..\n"; 38 | return 2; 39 | } 40 | -------------------------------------------------------------------------------- /chapter11/doubles.txt: -------------------------------------------------------------------------------- 1 | 123456789 987654321 1122334455 6677889900 8723479823978 1289347187 2983741982734 2 | 2389749834 389457938475 2384927 298347298374 928374928374 283749234782 23423 3 | 238947298347 298347298374 72989873498273 9102730187 9028092843807 8293472992 4 | 289374928374 8974538745 1923187239 39485093845890 7123971623987 348973458 5 | -------------------------------------------------------------------------------- /chapter11/ex01_lower.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | cout << "Please enter a file to make lowercase:\n"; 6 | string iname; 7 | cin >> iname; 8 | ifstream ist {iname}; 9 | if (!ist) error("could not read from file ", iname); 10 | 11 | cout << "Please enter a destination for output:\n"; 12 | string oname; 13 | cin >> oname; 14 | ofstream ost {oname}; 15 | if (!ost) error("could not write to file ", oname); 16 | 17 | for (string s; ist >> s; ) { 18 | for (char& c : s) 19 | c = tolower(c); 20 | 21 | ost << s << ' '; 22 | } 23 | 24 | if (ist.eof()) 25 | cout << iname << " read complete\n"; 26 | else 27 | error("could not complete operation"); 28 | } 29 | catch(exception& e) { 30 | cerr << e.what() << '\n'; 31 | return 1; 32 | } 33 | catch(...) { 34 | cerr << "Something terrible happened!\n"; 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /chapter11/ex02_line_search.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | bool search_word(string& word, string& line) 4 | // search a line for a given word 5 | // if found return true else return false 6 | { 7 | stringstream ss {line}; 8 | 9 | for (string s; ss >> s; ) 10 | if (s == word) return true; 11 | 12 | return false; 13 | } 14 | 15 | vector get_lines(ifstream& ist) 16 | // retrieve the contents of a file stream by line 17 | { 18 | vector lines; 19 | 20 | while (ist) { 21 | string line; 22 | getline(ist, line); 23 | lines.push_back(line); 24 | } 25 | 26 | if (!ist && !ist.eof()) 27 | error("couldn't fill from file"); 28 | 29 | return lines; 30 | } 31 | 32 | int main() 33 | try { 34 | cout << "Enter the file to search:\n"; 35 | string iname; 36 | cin >> iname; 37 | ifstream ist {iname}; 38 | if (!ist) error("could not read from file ", iname); 39 | 40 | cout << "Enter a word to search for:\n"; 41 | string word; 42 | cin >> word; 43 | 44 | vector lines = get_lines(ist); 45 | 46 | for (int i = 0; i < lines.size(); ++i) 47 | if (search_word(word, lines[i])) 48 | cout << i + 1 << ' ' << lines[i] << '\n'; 49 | } 50 | catch(exception& e) { 51 | cerr << e.what() << '\n'; 52 | return 1; 53 | } 54 | catch(...) { 55 | cerr << "Error of unknown type\n"; 56 | return 2; 57 | } 58 | -------------------------------------------------------------------------------- /chapter11/ex03_disemvowels.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | string disemvowel(const string& s) 4 | // removes vowels from a string 5 | { 6 | string output = ""; 7 | vector vowels {'a', 'e', 'i', 'o', 'u'}; 8 | 9 | for (char ch : s) { 10 | bool vowel = false; 11 | 12 | for (char v : vowels) 13 | if (ch == v) vowel = true; 14 | 15 | if (!vowel) 16 | output += ch; 17 | } 18 | 19 | return output; 20 | } 21 | 22 | int main() 23 | try { 24 | cout << "Enter a file to disemvowel:\n"; 25 | string iname; 26 | cin >> iname; 27 | ifstream ist {iname}; 28 | if (!ist) error("could not read from file ", iname); 29 | 30 | cout << "Enter a destination for the output:\n"; 31 | string oname; 32 | cin >> oname; 33 | ofstream ost {oname}; 34 | if (!ost) error("could not write to file ", oname); 35 | 36 | while (ist) { 37 | string line; 38 | getline(ist, line); 39 | ost << disemvowel(line) << '\n'; 40 | } 41 | 42 | if (!ist && !ist.eof()) 43 | error ("trouble while reading from stream"); 44 | } 45 | catch(exception& e) { 46 | cerr << "Error: " << e.what() << '\n'; 47 | return 1; 48 | } 49 | catch(...) { 50 | cerr << "Something unexpected happened\n"; 51 | return 2; 52 | } 53 | -------------------------------------------------------------------------------- /chapter11/ex04_multi_input.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | cout << "Please enter some integers. Feel free to mix it up:\n"; 6 | 7 | while (cin) { 8 | string input; 9 | cin >> input; 10 | stringstream ss {input}; 11 | ss.unsetf(ios::dec); 12 | 13 | int value; 14 | ss >> value; 15 | 16 | cout << setw(12) << showbase; 17 | 18 | if (input[0] == '0') { 19 | if (input[1] == 'x' || input[1] == 'X') { 20 | cout << hex << value << " hexadecimal\t"; 21 | } else { 22 | cout << oct << value << " octal\t\t"; 23 | } 24 | } else { 25 | cout << dec << value << " decimal\t\t"; 26 | } 27 | 28 | cout << "converts to\t" 29 | << dec << value << " decimal\n"; 30 | } 31 | } 32 | catch(exception& e) { 33 | cerr << e.what() << '\n'; 34 | return 1; 35 | } 36 | catch(...) { 37 | cerr << "Something bad happened..\n"; 38 | return 2; 39 | } 40 | -------------------------------------------------------------------------------- /chapter11/ex05_char_class.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | vector get_classes(const char ch) 4 | // return a vector of class strings that apply to ch 5 | { 6 | vector classes; 7 | 8 | if (isspace(ch)) classes.push_back("whitespace"); 9 | if (isalpha(ch)) classes.push_back("letter"); 10 | if (isdigit(ch)) classes.push_back("decimal digit"); 11 | if (isxdigit(ch)) classes.push_back("hex digit"); 12 | if (isupper(ch)) classes.push_back("uppercase"); 13 | if (islower(ch)) classes.push_back("lowercase"); 14 | if (isalnum(ch)) classes.push_back("alphanumeric"); 15 | if (iscntrl(ch)) classes.push_back("control char"); 16 | if (ispunct(ch)) classes.push_back("punctuation"); 17 | if (isprint(ch)) classes.push_back("printable"); 18 | if (isgraph(ch)) classes.push_back("alphanumpunct"); 19 | 20 | return classes; 21 | } 22 | 23 | int main() 24 | { 25 | cout << "Please enter a string to classify:\n"; 26 | 27 | while (cin) { 28 | // get a line to catch whitespace 29 | string line; 30 | getline(cin, line); 31 | 32 | for (char ch : line) { 33 | vector classifications = get_classes(ch); 34 | 35 | cout << ch << '\t'; 36 | for (string c : classifications) 37 | cout << c << ' '; 38 | 39 | cout << '\n'; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /chapter11/ex06-8_punct_swap.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | // ex 6 4 | // replace punctuation with whitespace 5 | // don't touch characters within a pair of double quotes (?) 6 | // 7 | // ex 7 8 | // replace don't with do not, can't with cannot etc. 9 | // leave hyphens within words 10 | // convert all to lower case 11 | // 12 | // ex 8 13 | // make a dictionary (vector, no repeats) 14 | // run on multipage text file 15 | 16 | int main() 17 | { 18 | cout << "Enter a string to strip of punctuation:\n"; 19 | string line; 20 | getline(cin, line); 21 | 22 | for (char& ch : line) 23 | switch (ch) { 24 | case '.': case ';': case ',': case '?': case '-': case '\'': 25 | ch = ' '; 26 | } 27 | 28 | cout << line << '\n'; 29 | } 30 | -------------------------------------------------------------------------------- /chapter11/ex09_binary_io.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | vector binary_in(const string& fname) 4 | // read in a file and store as binary in vector 5 | { 6 | ifstream ifs {fname, ios_base::binary}; 7 | if (!ifs) error("can't read from file ", fname); 8 | 9 | vector v; 10 | 11 | for (int x; ifs.read(as_bytes(x), sizeof(int)); ) 12 | v.push_back(x); 13 | 14 | return v; 15 | } 16 | 17 | void binary_out(const string& fname, const vector& v) 18 | // write out to a file from binary 19 | { 20 | ofstream ofs {fname, ios_base::binary}; 21 | if (!ofs) error("could not write to file ", fname); 22 | 23 | for (int x : v) 24 | ofs.write(as_bytes(x), sizeof(int)); 25 | } 26 | 27 | int main() 28 | try { 29 | // open an istream for binary input from a file: 30 | cout << "Please enter input file name\n"; 31 | string iname; 32 | cin >> iname; 33 | vector bin = binary_in(iname); 34 | 35 | // open an ostream for binary output to a file: 36 | cout << "Please enter output file name\n"; 37 | string oname; 38 | cin >> oname; 39 | binary_out(oname, bin); 40 | 41 | return 0; 42 | } 43 | catch(exception& e) { 44 | cerr << "Exception: " << e.what() << '\n'; 45 | return 1; 46 | } 47 | catch(...) { 48 | cerr << "exception\n"; 49 | return 2; 50 | } 51 | -------------------------------------------------------------------------------- /chapter11/ex10_split.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | vector split(const string& s) 4 | // return a vector of whitespace separated substrings of s 5 | { 6 | vector v; 7 | istringstream ss {s}; 8 | 9 | for (string sub; ss >> sub; ) 10 | v.push_back(sub); 11 | 12 | return v; 13 | } 14 | 15 | int main() 16 | try { 17 | cout << "Enter a long-ish string:\n"; 18 | string line; 19 | getline(cin, line); 20 | 21 | vector split_up = split(line); 22 | 23 | for (string s : split_up) 24 | cout << s << '\n'; 25 | } 26 | catch(exception& e) { 27 | cerr << "Exception: " << e.what() << '\n'; 28 | return 1; 29 | } 30 | catch(...) { 31 | cerr << "Unknown error\n"; 32 | return 2; 33 | } 34 | -------------------------------------------------------------------------------- /chapter11/ex11_split_plus.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | vector split(const string& s, const string& w) 4 | // returns a vector of whitespace separated substrings 5 | // whitespace defined as regular whitespace and added w 6 | { 7 | string copy = s; 8 | 9 | for (char& ch : copy) 10 | for (char cw : w) 11 | if (ch == cw) ch = ' '; 12 | 13 | istringstream ss {copy}; 14 | vector v; 15 | 16 | for (string sub; ss >> sub; ) 17 | v.push_back(sub); 18 | 19 | return v; 20 | } 21 | 22 | int main() 23 | try { 24 | cout << "Enter some kind of string to split\n"; 25 | string line; 26 | getline(cin, line); 27 | 28 | cout << "Split using what character set?\n"; 29 | string char_set; 30 | getline(cin, char_set); 31 | 32 | vector split_up = split(line, char_set); 33 | 34 | for (string str : split_up) 35 | cout << str << '\n'; 36 | } 37 | catch(exception& e) { 38 | cerr << "Exception: " << e.what() << '\n'; 39 | return 1; 40 | } 41 | catch(...) { 42 | cerr << "Something serious...\n"; 43 | return 2; 44 | } 45 | -------------------------------------------------------------------------------- /chapter11/ex12_reverse_char.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | cout << "Please enter a file to read from:\n"; 6 | string iname; 7 | cin >> iname; 8 | ifstream ifs {iname}; 9 | if (!ifs) error("could not read from file ", iname); 10 | 11 | cout << "Please enter a destination file for output:\n"; 12 | string oname; 13 | cin >> oname; 14 | ofstream ofs {oname}; 15 | if (!ofs) error("could not write to file ", oname); 16 | 17 | vector lines; 18 | for (string line; getline(ifs, line); ) 19 | lines.push_back(line); 20 | 21 | for (int i = lines.size() - 1; i >= 0; --i) { 22 | 23 | for (int j = lines[i].size() - 1; j >= 0; --j) 24 | ofs << lines[i][j]; 25 | 26 | ofs << '\n'; 27 | } 28 | } 29 | catch(exception& e) { 30 | cerr << "Exception: " << e.what() << '\n'; 31 | return 1; 32 | } 33 | catch(...) { 34 | cerr << "Something serious happened\n"; 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /chapter11/ex13_reverse_words.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | { 5 | cout << "Enter a file to reverse the words of:\n"; 6 | string iname; 7 | cin >> iname; 8 | ifstream ifs {iname}; 9 | if (!ifs) error("could not read from file ", iname); 10 | 11 | cout << "Enter a destination file for output:\n"; 12 | string oname; 13 | cin >> oname; 14 | ofstream ofs {oname}; 15 | if (!ofs) error("could not write to file ", oname); 16 | 17 | vector v; 18 | for (string line; getline(ifs, line); ) 19 | v.push_back(line); 20 | 21 | for (int i = v.size() - 1; i >= 0; --i) { 22 | vector w; 23 | istringstream ss {v[i]}; 24 | 25 | for (string s; ss >> s; ) 26 | w.push_back(s); 27 | 28 | for (int j = w.size() - 1; j >= 0; --j) 29 | ofs << w[j] << ' '; 30 | 31 | ofs << '\n'; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter11/ex15_scientific_8.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | cout << "What file of numbers shall we format?\n"; 6 | string iname; 7 | cin >> iname; 8 | ifstream ifs {iname}; 9 | if (!ifs) error("could not read from file ", iname); 10 | 11 | cout << "What file shall we output to?\n"; 12 | string oname; 13 | cin >> oname; 14 | ofstream ofs {oname}; 15 | if (!ofs) error("could not write to file ", oname); 16 | 17 | vector vd; 18 | for (double d; ifs >> d; ) 19 | vd.push_back(d); 20 | 21 | ofs << scientific << setprecision(8); // they should stick 22 | 23 | for (int i = 0; i < vd.size(); ++i) { 24 | ofs << setw(20) << vd[i]; // setw applied for every val 25 | 26 | if (i % 4 == 3) ofs << '\n'; 27 | } 28 | } 29 | catch(exception& e) { 30 | cerr << "Exception: " << e.what() << '\n'; 31 | return 1; 32 | } 33 | catch(...) { 34 | cerr << "Error!\n"; 35 | return 2; 36 | } 37 | -------------------------------------------------------------------------------- /chapter11/ex16_ordered_count.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | int main() 4 | try { 5 | cout << "Enter a file of numbers to order & count:\n"; 6 | string iname; 7 | cin >> iname; 8 | ifstream ifs {iname}; 9 | if (!ifs) error("could not read from file ", iname); 10 | 11 | vector nums; 12 | for (int i; ifs >> i; ) 13 | nums.push_back(i); 14 | 15 | sort(nums); 16 | 17 | cout << "num" << '\t' << "count\n"; // headers 18 | 19 | int count = 0; 20 | for (int i = 0; i < nums.size(); ++i) { 21 | if (i == 0) { // first iteration 22 | 23 | cout << nums[i]; 24 | count += 1; 25 | 26 | } else if (nums[i] == nums[i -1]) { // same as previous 27 | 28 | count += 1; 29 | 30 | } else { // must be new number 31 | 32 | if (count > 1) cout << '\t' << count; 33 | count = 1; 34 | cout << '\n' << nums[i]; 35 | } 36 | } 37 | if (count > 1) cout << '\t' << count; 38 | cout << '\n'; 39 | } 40 | catch(exception& e) { 41 | cerr << "Exception: " << e.what() << '\n'; 42 | return 1; 43 | } 44 | catch(...) { 45 | cerr << "Error!!\n"; 46 | return 2; 47 | } 48 | -------------------------------------------------------------------------------- /chapter11/ints.txt: -------------------------------------------------------------------------------- 1 | 4 3 1 6 5 3 6 3 2 4 6 7 8 6 5 3 5 6 8 7 6 9 0 7 5 3 2 1 3 4 5 6 7 8 6 5 4 3 2 2 | -------------------------------------------------------------------------------- /chapter11/lines.txt: -------------------------------------------------------------------------------- 1 | lion tiger apple bear 2 | an apple a day is a good start 3 | tiger lion puma 4 | orange grape watermelon 5 | apple pie 6 | -------------------------------------------------------------------------------- /chapter11/output.txt: -------------------------------------------------------------------------------- 1 | 1.23456789e+08 9.87654321e+08 1.12233446e+09 6.67788990e+09 2 | 8.72347982e+12 1.28934719e+09 2.98374198e+12 2.38974983e+09 3 | 3.89457938e+11 2.38492700e+06 2.98347298e+11 9.28374928e+11 4 | 2.83749235e+11 2.34230000e+04 2.38947298e+11 2.98347298e+11 5 | 7.29898735e+13 9.10273019e+09 9.02809284e+12 8.29347299e+09 6 | 2.89374928e+11 8.97453874e+09 1.92318724e+09 3.94850938e+13 7 | 7.12397162e+12 3.48973458e+08 -------------------------------------------------------------------------------- /chapter12/ex09_image.cpp: -------------------------------------------------------------------------------- 1 | #include "./GUI/Simple_window.h" 2 | #include "./GUI/Graph.h" 3 | 4 | #include 5 | #include 6 | 7 | using namespace Graph_lib; 8 | 9 | int main() 10 | try { 11 | // program 12 | Point tl {100, 100}; 13 | Simple_window win {tl, 600, 400, "Chapter 12 Ex 9"}; 14 | 15 | Image ii {Point{100, 25}, "./rob_bass.jpg"}; 16 | win.attach(ii); 17 | 18 | Text caption {Point{120, 355}, "A robot playing bass"}; 19 | caption.set_font(Font::helvetica); 20 | caption.set_font_size(20); 21 | win.attach(caption); 22 | 23 | win.wait_for_button(); 24 | } 25 | catch(exception& e) { 26 | cerr << "exception: " << e.what() << '\n'; 27 | return 1; 28 | } 29 | catch(...) { 30 | cerr << "error\n"; 31 | return 2; 32 | } 33 | /* Compile command 34 | g++ -w -Wall -std=c++11 GUI/Graph.cpp GUI/Window.cpp GUI/GUI.cpp GUI/Simple_window.cpp ex09_image.cpp `fltk-config --ldflags --use-images` -o a.out 35 | */ 36 | -------------------------------------------------------------------------------- /chapter12/example.cpp: -------------------------------------------------------------------------------- 1 | #include "./GUI/Simple_window.h" 2 | #include "./GUI/Graph.h" 3 | 4 | int main() 5 | { 6 | using namespace Graph_lib; 7 | 8 | Point tl( 100, 100 ); 9 | 10 | Simple_window win ( tl, 600, 400, "Canvas" ); 11 | 12 | Polygon poly; 13 | 14 | poly.add(Point( 300, 200 )); 15 | poly.add(Point( 350, 100 )); 16 | poly.add(Point( 400, 200 )); 17 | 18 | poly.set_color(Color::red); 19 | 20 | win.attach(poly); 21 | 22 | win.wait_for_button(); 23 | } 24 | /* Compile command (change MY_CPP_FILE): 25 | g++ -w -Wall -std=c++11 GUI/Graph.cpp GUI/Window.cpp GUI/GUI.cpp GUI/Simple_window.cpp MY_CPP_FILE.cpp `fltk-config --ldflags --use-images` -o a.out 26 | */ 27 | -------------------------------------------------------------------------------- /chapter12/rob_bass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chrinkus/stroustrup-ppp/ea9e85d4ea9890038eb5611c3bc82734c8706ce7/chapter12/rob_bass.jpg -------------------------------------------------------------------------------- /chapter12/snow_cpp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chrinkus/stroustrup-ppp/ea9e85d4ea9890038eb5611c3bc82734c8706ce7/chapter12/snow_cpp.gif -------------------------------------------------------------------------------- /chapter13/ex01_arc.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./Arc.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Window 13 | Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 1"}; 15 | 16 | // Reference 17 | Circle c {Point{300, 200}, 75}; 18 | c.set_color(Color::yellow); 19 | win.attach(c); 20 | 21 | // Arc tests 22 | Arc a1 {Point{300, 200}, 50, 0, 90}; 23 | a1.set_color(Color::blue); 24 | win.attach(a1); 25 | 26 | Arc a2 {Point{300, 200}, 100, 90, 180}; 27 | a2.set_color(Color::red); 28 | win.attach(a2); 29 | 30 | Arc a3 {Point{300, 200}, 150, 90, 360}; 31 | a3.set_color(Color::green); 32 | win.attach(a3); 33 | 34 | Arc a4 {Point{300, 200}, 100, 270, 360}; 35 | a4.set_color(Color::red); 36 | win.attach(a4); 37 | 38 | win.wait_for_button(); 39 | } 40 | catch(exception& e) { 41 | cerr << "exception: " << e.what() << '\n'; 42 | return 1; 43 | } 44 | catch(...) { 45 | cerr << "error\n"; 46 | return 2; 47 | } 48 | /* Compile command 49 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp Arc.cpp ex01_arc.cpp `fltk-config --ldflags --use-images` -o a.out 50 | */ 51 | -------------------------------------------------------------------------------- /chapter13/ex02_box.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./Box.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Window 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 2"}; 15 | 16 | Box b1 {Point{200, 150}, 200, 100, 20}; 17 | b1.set_color(Color::red); 18 | win.attach(b1); 19 | 20 | Box b2 {Point{100, 100}, 120, 80, 10}; 21 | b2.set_style(Line_style{Line_style::solid,4}); 22 | b2.set_color(Color::blue); 23 | win.attach(b2); 24 | 25 | Box b3 {Point{400, 300}, 50, 50, 25}; 26 | win.attach(b3); 27 | 28 | win.wait_for_button(); 29 | } 30 | catch(exception& e) { 31 | cerr << "exception: " << e.what() << '\n'; 32 | return 1; 33 | } 34 | catch(...) { 35 | cerr << "error\n"; 36 | return 2; 37 | } 38 | /* Compile command 39 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp Arc.cpp Box.cpp ex02_box.cpp `fltk-config --ldflags --use-images` -o a.out 40 | */ 41 | -------------------------------------------------------------------------------- /chapter13/ex03_arrow.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./Arrow.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Window 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 12 Ex 3"}; 15 | 16 | Arrow a1 {Point{100, 100}, Point{250, 200}}; 17 | win.attach(a1); 18 | 19 | Arrow a2 {Point{500, 300}, Point{500, 100}}; 20 | a2.set_color(Color::dark_green); 21 | win.attach(a2); 22 | 23 | Arrow a3 {Point{525, 100}, Point{525, 300}}; 24 | a3.set_color(Color::blue); 25 | win.attach(a3); 26 | 27 | Arrow a4 {Point{300, 50}, Point{100, 300}}; 28 | a4.set_color(Color::red); 29 | win.attach(a4); 30 | 31 | win.wait_for_button(); 32 | } 33 | catch(exception& e) { 34 | cerr << "exception: " << e.what() << '\n'; 35 | return 1; 36 | } 37 | catch(...) { 38 | cerr << "error\n"; 39 | return 2; 40 | } 41 | /* Compile command 42 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp Arrow.cpp ex03_arrow.cpp `fltk-config --ldflags --use-images` -o a.out 43 | */ 44 | -------------------------------------------------------------------------------- /chapter13/ex06_diagram.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Window 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 6"}; 15 | 16 | Text_box shape {Point{50, 50}, 100, 50, "Shape"}; 17 | win.attach(shape); 18 | 19 | Text_box rect {Point{50, 200}, 100, 50, "Rectangle"}; 20 | win.attach(rect); 21 | 22 | Text_box tb {Point{50, 300}, 100, 50, "Textbox"}; 23 | win.attach(tb); 24 | 25 | Text_box circ {Point{200, 200}, 100, 50, "Circle"}; 26 | win.attach(circ); 27 | 28 | Arrow a1 {n(rect), s(shape)}; 29 | win.attach(a1); 30 | 31 | Arrow a2 {n(tb), s(rect)}; 32 | win.attach(a2); 33 | 34 | Arrow a3 {nw(circ), se(shape)}; 35 | win.attach(a3); 36 | 37 | win.wait_for_button(); 38 | } 39 | catch(exception& e) { 40 | cerr << "exception: " << e.what() << '\n'; 41 | return 1; 42 | } 43 | catch(...) { 44 | cerr << "error\n"; 45 | return 2; 46 | } 47 | /* Compile command 48 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex06_diagram.cpp `fltk-config --ldflags --use-images` -o a.out 49 | */ 50 | -------------------------------------------------------------------------------- /chapter13/ex07_rgb_chart.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // RGB Chart 13 | // Not sure how to enter colors as RGB values in FLTK 14 | } 15 | catch(exception& e) { 16 | cerr << "exception: " << e.what() << '\n'; 17 | return 1; 18 | } 19 | catch(...) { 20 | cerr << "error\n"; 21 | return 2; 22 | } 23 | /* Compile command 24 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex07_rgb_chart.cpp `fltk-config --ldflags --use-images` -o a.out 25 | */ 26 | -------------------------------------------------------------------------------- /chapter13/ex08_reg_hex.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Program 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 8"}; 15 | 16 | Reg_hex h1 {Point{300, 200}, 100}; 17 | win.attach(h1); 18 | 19 | Reg_hex h2 {Point{100, 100}, 25}; 20 | h2.set_style(Line_style(Line_style::dash, 4)); 21 | win.attach(h2); 22 | 23 | win.wait_for_button(); 24 | } 25 | catch(exception& e) { 26 | cerr << "exception: " << e.what() << '\n'; 27 | return 1; 28 | } 29 | catch(...) { 30 | cerr << "error\n"; 31 | return 2; 32 | } 33 | /* Compile command 34 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex08_reg_hex.cpp `fltk-config --ldflags --use-images` -o a.out 35 | */ 36 | -------------------------------------------------------------------------------- /chapter13/ex09_hex_tile.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Program 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 8"}; 15 | 16 | Vector_ref vr; 17 | 18 | int grid_size = 8; 19 | int hex_r = 25; 20 | 21 | for (int i = 0; i < grid_size; ++i) 22 | for (int j = 0; j < grid_size; ++j) { 23 | int x = 50 + j * hex_r * 3; 24 | int y = 50 + i * hex_r * 2; 25 | vr.push_back(new Reg_hex{Point{x, y}, hex_r}); 26 | win.attach(vr[vr.size()-1]); 27 | } 28 | 29 | win.wait_for_button(); 30 | } 31 | catch(exception& e) { 32 | cerr << "exception: " << e.what() << '\n'; 33 | return 1; 34 | } 35 | catch(...) { 36 | cerr << "error\n"; 37 | return 2; 38 | } 39 | /* Compile command 40 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex09_hex_tile.cpp `fltk-config --ldflags --use-images` -o a.out 41 | */ 42 | -------------------------------------------------------------------------------- /chapter13/ex10_reg_poly.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Program 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 8"}; 15 | 16 | Reg_poly rp1 {Point{300, 200}, 8, 25}; 17 | win.attach(rp1); 18 | 19 | Reg_poly rp2 {Point{300, 200}, 10, 40}; 20 | rp2.set_style(Line_style(Line_style::dot, 2)); 21 | win.attach(rp2); 22 | 23 | Reg_poly rp3 {Point{300, 200}, 13, 70}; 24 | rp3.set_style(Line_style(Line_style::solid, 4)); 25 | rp3.set_color(Color::red); 26 | win.attach(rp3); 27 | 28 | Reg_poly rp4 {Point{300, 200}, 3, 100}; 29 | rp4.set_style(Line_style(Line_style::solid, 6)); 30 | rp4.set_color(Color::dark_green); 31 | win.attach(rp4); 32 | 33 | win.wait_for_button(); 34 | } 35 | catch(exception& e) { 36 | cerr << "exception: " << e.what() << '\n'; 37 | return 1; 38 | } 39 | catch(...) { 40 | cerr << "error\n"; 41 | return 2; 42 | } 43 | /* Compile command 44 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex10_reg_poly.cpp `fltk-config --ldflags --use-images` -o a.out 45 | */ 46 | -------------------------------------------------------------------------------- /chapter13/ex12_circ_mark.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | constexpr double PI = 3.14159265; 11 | 12 | int main() 13 | try { 14 | // Program 15 | const Point tl {100, 100}; 16 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 11"}; 17 | 18 | Circle c {Point{300, 200}, 125}; 19 | c.set_style(Line_style(Line_style::solid, 2)); 20 | c.set_color(Color::white); 21 | win.attach(c); 22 | 23 | int ox = c.center().x + c.radius(); 24 | int oy = c.center().y; 25 | Mark m {Point{ox, oy}, 'x'}; 26 | win.attach(m); 27 | 28 | win.wait_for_button(); 29 | 30 | for (int i = 0; i < 360; i += 15) { 31 | int dx = ox - (c.center().x + cos(i*PI/180)*c.radius()); 32 | int dy = oy - (c.center().y + sin(i*PI/180)*c.radius()); 33 | 34 | m.move(-dx, -dy); 35 | win.wait_for_button(); 36 | 37 | ox -= dx; 38 | oy -= dy; 39 | } 40 | 41 | win.wait_for_button(); 42 | } 43 | catch(exception& e) { 44 | cerr << "exception: " << e.what() << '\n'; 45 | return 1; 46 | } 47 | catch(...) { 48 | cerr << "error\n"; 49 | return 2; 50 | } 51 | /* Compile command 52 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ex12_circ_mark.cpp `fltk-config --ldflags --use-images` -o a.out 53 | */ 54 | -------------------------------------------------------------------------------- /chapter13/ex13_color_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | 4 | #include 5 | #include 6 | 7 | using namespace Graph_lib; 8 | 9 | int main() 10 | try { 11 | // Program 12 | const Point tl {100, 100}; 13 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 12"}; 14 | 15 | Vector_ref vr; 16 | 17 | for (int i = 0; i < 16; ++i) 18 | for (int j = 0; j < 16; ++j) { 19 | vr.push_back(new Rectangle{Point{i*20, j*20}, 20, 20}); 20 | vr[vr.size()-1].set_fill_color(Color{i * 16 + j}); 21 | vr[vr.size()-1].set_color(Color::invisible); 22 | win.attach(vr[vr.size()-1]); 23 | } 24 | 25 | win.wait_for_button(); 26 | } 27 | catch(exception& e) { 28 | cerr << "exception: " << e.what() << '\n'; 29 | return 1; 30 | } 31 | catch(...) { 32 | cerr << "error\n"; 33 | return 2; 34 | } 35 | /* Compile command 36 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ex13_color_matrix.cpp `fltk-config --ldflags --use-images` -o a.out 37 | */ 38 | -------------------------------------------------------------------------------- /chapter13/ex15_right_tile.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Right_triangle tile 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 15"}; 15 | 16 | int a = 50; 17 | int b = 30; 18 | 19 | Vector_ref vr; 20 | 21 | for (int i = 0; i < 400 / b; ++i) { 22 | int y = i * b + b; 23 | for (int j = 0; j < 600 / a; ++j) { 24 | int x = j * a; 25 | vr.push_back(new Right_triangle{Point{x,y}, a, b}); 26 | win.attach(vr[vr.size()-1]); 27 | } 28 | } 29 | 30 | win.wait_for_button(); 31 | } 32 | catch(exception& e) { 33 | cerr << "exception: " << e.what() << '\n'; 34 | return 1; 35 | } 36 | catch(...) { 37 | cerr << "error\n"; 38 | return 2; 39 | } 40 | /* Compile command 41 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex15_right_tile.cpp `fltk-config --ldflags --use-images` -o a.out 42 | */ 43 | -------------------------------------------------------------------------------- /chapter13/ex16_hex_tile_2.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Hexagon tile 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 16"}; 15 | 16 | Vector_ref vr; 17 | int r = 25; 18 | 19 | for (int i = 0; i < 20; ++i) 20 | for (int j = 0; j < 12; ++j) { 21 | 22 | int x = j * r * 3; 23 | int y = i * sin(60*PI/180) * r; 24 | 25 | if (i % 2 == 1) x += r + cos(60*PI/180) * r; 26 | 27 | vr.push_back(new Reg_hex{Point{x, y}, r}); 28 | win.attach(vr[vr.size()-1]); 29 | } 30 | 31 | win.wait_for_button(); 32 | } 33 | catch(exception& e) { 34 | cerr << "exception: " << e.what() << '\n'; 35 | return 1; 36 | } 37 | catch(...) { 38 | cerr << "error\n"; 39 | return 2; 40 | } 41 | /* Compile command 42 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex16_hex_tile_2.cpp `fltk-config --ldflags --use-images` -o a.out 43 | */ 44 | -------------------------------------------------------------------------------- /chapter13/ex17_hex_tile_3.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Hexagon tile 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 17"}; 15 | 16 | Vector_ref vr; 17 | int r = 25; 18 | 19 | for (int i = 0; i < win.y_max() / r; ++i) 20 | for (int j = 0; j < win.x_max() / (2 * r); ++j) { 21 | 22 | int x = j * r * 3; 23 | int y = i * sin(60*PI/180) * r; 24 | 25 | if (i % 2 == 1) x += r + cos(60*PI/180) * r; 26 | 27 | vr.push_back(new Reg_hex{Point{x, y}, r}); 28 | vr[vr.size()-1].set_fill_color(Color((i+j)%8)); 29 | vr[vr.size()-1].set_color(Color::invisible); 30 | win.attach(vr[vr.size()-1]); 31 | } 32 | 33 | win.wait_for_button(); 34 | } 35 | catch(exception& e) { 36 | cerr << "exception: " << e.what() << '\n'; 37 | return 1; 38 | } 39 | catch(...) { 40 | cerr << "error\n"; 41 | return 2; 42 | } 43 | /* Compile command 44 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex17_hex_tile_3.cpp `fltk-config --ldflags --use-images` -o a.out 45 | */ 46 | -------------------------------------------------------------------------------- /chapter13/ex18_poly.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Polygon w/checks 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 18"}; 15 | 16 | // Need more linear algebra study before attempting this 17 | 18 | win.wait_for_button(); 19 | } 20 | catch(exception& e) { 21 | cerr << "exception: " << e.what() << '\n'; 22 | return 1; 23 | } 24 | catch(...) { 25 | cerr << "error\n"; 26 | return 2; 27 | } 28 | /* Compile command 29 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex18_poly.cpp `fltk-config --ldflags --use-images` -o a.out 30 | */ 31 | -------------------------------------------------------------------------------- /chapter13/ex19_star.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch13_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Star 13 | const Point tl {100, 100}; 14 | Simple_window win {tl, 600, 400, "Chapter 13 Ex 19"}; 15 | 16 | Star s1 {Point{300, 200}, 100, 5, 2}; 17 | s1.set_fill_color(Color::red); 18 | win.attach(s1); 19 | 20 | Star s2 {Point{100, 100}, 50, 7, 3}; 21 | s2.set_color(Color::blue); 22 | s2.set_fill_color(Color::white); 23 | s2.set_style(Line_style(Line_style::dash, 2)); 24 | win.attach(s2); 25 | 26 | Star s3 {Point{400, 300}, 60, 10, 3}; 27 | s3.set_color(Color::dark_green); 28 | win.attach(s3); 29 | 30 | Star s4 {Point{350, 100}, 40, 8, 3}; 31 | s4.set_color(Color::invisible); 32 | s4.set_fill_color(Color::magenta); 33 | win.attach(s4); 34 | 35 | win.wait_for_button(); 36 | } 37 | catch(exception& e) { 38 | cerr << "exception: " << e.what() << '\n'; 39 | return 1; 40 | } 41 | catch(...) { 42 | cerr << "error\n"; 43 | return 2; 44 | } 45 | /* Compile command 46 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch13_tools.cpp ex19_star.cpp `fltk-config --ldflags --use-images` -o a.out 47 | */ 48 | -------------------------------------------------------------------------------- /chapter14/ch14_drill.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cout; 5 | using std::string; 6 | 7 | struct B1 { 8 | virtual void vf() { cout << "B1::vf()\n"; } 9 | void f() { cout << "B1::f()\n"; } 10 | virtual void pvf() =0; 11 | }; 12 | 13 | struct D1 : B1 { 14 | void vf() override { cout << "D1::vf()\n"; } 15 | void f() { cout << "D1::f()\n"; } 16 | }; 17 | 18 | struct D2 : D1 { 19 | void pvf() override { cout << "D2::pvf()\n"; } 20 | }; 21 | 22 | struct B2 { 23 | virtual void pvf() =0; 24 | }; 25 | 26 | struct D21 : B2 { 27 | string s = "stuffs"; 28 | void pvf() override { cout << s << '\n'; } 29 | }; 30 | 31 | struct D22 : B2 { 32 | int i = 37; 33 | void pvf() override { cout << i << '\n'; } 34 | }; 35 | 36 | void f (B2& b) 37 | { 38 | b.pvf(); 39 | } 40 | 41 | int main() 42 | { 43 | /* 44 | B1 b; 45 | b.vf(); 46 | b.f(); 47 | 48 | D1 d; 49 | d.vf(); 50 | d.f(); 51 | 52 | B1& r {d}; 53 | r.vf(); 54 | r.f(); 55 | 56 | D2 dd; 57 | dd.vf(); 58 | dd.f(); 59 | dd.pvf(); 60 | */ 61 | 62 | D21 two_one; 63 | D22 two_two; 64 | 65 | f(two_one); 66 | f(two_two); 67 | } 68 | -------------------------------------------------------------------------------- /chapter14/ex01_smiley_frowny.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Smiley_hat sm {Point{150, 200}, 100}; 13 | win.attach(sm); 14 | 15 | Frowny_hat fr {Point{450, 200}, 100}; 16 | win.attach(fr); 17 | 18 | win.wait_for_button(); 19 | } 20 | catch (exception& e) { 21 | std::cerr << "Exception: " << e.what() << '\n'; 22 | return 1; 23 | } 24 | catch (...) { 25 | std::cerr << "Unknown error!\n"; 26 | return 2; 27 | } 28 | /* Compile command 29 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex01_smiley_frowny.cpp `fltk-config --ldflags --use-images` -o a.out 30 | */ 31 | -------------------------------------------------------------------------------- /chapter14/ex02_cant_copy.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Circle c {Point{300, 200}, 100}; 13 | win.attach(c); 14 | 15 | Circle d {c}; // Error: call to implicitly-deleted copy constructor 16 | 17 | win.wait_for_button(); 18 | } 19 | catch (exception& e) { 20 | std::cerr << "Exception: " << e.what() << '\n'; 21 | return 1; 22 | } 23 | catch (...) { 24 | std::cerr << "Unknown error!\n"; 25 | return 2; 26 | } 27 | /* Compile command 28 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex02_cant_copy.cpp `fltk-config --ldflags --use-images` -o a.out 29 | */ 30 | -------------------------------------------------------------------------------- /chapter14/ex03_abstract.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | struct Vehicle { 8 | void horn() const { cout << "beep beep!\n"; } 9 | 10 | protected: 11 | Vehicle() { } 12 | }; 13 | 14 | int main() 15 | try { 16 | const Point tl {100, 100}; 17 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 18 | 19 | Vehicle v; // Error: calling a protected constructor 20 | 21 | win.wait_for_button(); 22 | } 23 | catch (exception& e) { 24 | std::cerr << "Exception: " << e.what() << '\n'; 25 | return 1; 26 | } 27 | catch (...) { 28 | std::cerr << "Unknown error!\n"; 29 | return 2; 30 | } 31 | /* Compile command 32 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex03_abstract.cpp `fltk-config --ldflags --use-images` -o a.out 33 | */ 34 | -------------------------------------------------------------------------------- /chapter14/ex04_immobile.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Immobile_circle ic {Point{300, 200}, 100}; 13 | ic.move(100, 100); 14 | win.attach(ic); 15 | 16 | win.wait_for_button(); 17 | } 18 | catch (exception& e) { 19 | std::cerr << "Exception: " << e.what() << '\n'; 20 | return 1; 21 | } 22 | catch (...) { 23 | std::cerr << "Unknown error!\n"; 24 | return 2; 25 | } 26 | /* Compile command 27 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex04_immobile.cpp `fltk-config --ldflags --use-images` -o a.out 28 | */ 29 | -------------------------------------------------------------------------------- /chapter14/ex05_striped_rect.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Striped_rectangle sr {Point{200, 150}, 200, 100, 10}; 13 | sr.set_stripe_color(Color::magenta); 14 | win.attach(sr); 15 | 16 | win.wait_for_button(); 17 | } 18 | catch (exception& e) { 19 | std::cerr << "Exception: " << e.what() << '\n'; 20 | return 1; 21 | } 22 | catch (...) { 23 | std::cerr << "Unknown error!\n"; 24 | return 2; 25 | } 26 | /* Compile command 27 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ../chapter13/ch13_tools.cpp ch14_tools.cpp ex05_striped_rect.cpp `fltk-config --ldflags --use-images` -o a.out 28 | */ 29 | -------------------------------------------------------------------------------- /chapter14/ex06_striped_circ.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Striped_circle sc {Point{300, 200}, 100, 10}; 13 | win.attach(sc); 14 | 15 | win.wait_for_button(); 16 | } 17 | catch (exception& e) { 18 | std::cerr << "Exception: " << e.what() << '\n'; 19 | return 1; 20 | } 21 | catch (...) { 22 | std::cerr << "Unknown error!\n"; 23 | return 2; 24 | } 25 | /* Compile command 26 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex06_striped_circ.cpp `fltk-config --ldflags --use-images` -o a.out 27 | */ 28 | -------------------------------------------------------------------------------- /chapter14/ex08_octagon.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Octagon oc {Point{300, 200}, 100}; 13 | oc.move(80, -60); 14 | oc.set_color(Color::red); 15 | oc.set_style(Line_style(Line_style::dash, 4)); 16 | oc.set_fill_color(Color::white); 17 | 18 | /* 19 | cout << "Line style: " << oc.style() << '\n' 20 | << "Line color: " << oc.color() << '\n' 21 | << "Fill color: " << oc.fill_color() << '\n' 22 | << "Num points: " << oc.number_of_points() << '\n'; 23 | */ 24 | 25 | for (int i = 0; i < oc.number_of_points(); ++i) { 26 | cout << "Point " << i << ": " << oc.point(i).x << ", " 27 | << oc.point(i).y << '\n'; 28 | } 29 | 30 | win.attach(oc); 31 | 32 | win.wait_for_button(); 33 | } 34 | catch (exception& e) { 35 | std::cerr << "Exception: " << e.what() << '\n'; 36 | return 1; 37 | } 38 | catch (...) { 39 | std::cerr << "Unknown error!\n"; 40 | return 2; 41 | } 42 | /* Compile command 43 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex08_octagon.cpp `fltk-config --ldflags --use-images` -o a.out 44 | */ 45 | -------------------------------------------------------------------------------- /chapter14/ex09_group.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Group grp; 13 | 14 | Circle circ {Point{100, 100}, 50}; 15 | win.attach(circ); 16 | grp.add(circ); 17 | 18 | Rectangle rect {Point{200, 200}, 60, 120}; 19 | win.attach(rect); 20 | grp.add(rect); 21 | 22 | Octagon oct {Point{400, 100}, 48}; 23 | win.attach(oct); 24 | grp.add(oct); 25 | 26 | win.wait_for_button(); 27 | 28 | grp.set_color(Color::red); 29 | win.wait_for_button(); 30 | 31 | grp.set_fill_color(Color::yellow); 32 | win.wait_for_button(); 33 | 34 | grp.move(100, 25); 35 | win.wait_for_button(); 36 | 37 | grp.set_style(Line_style(Line_style::dot, 4)); 38 | win.wait_for_button(); 39 | } 40 | catch (exception& e) { 41 | std::cerr << "Exception: " << e.what() << '\n'; 42 | return 1; 43 | } 44 | catch (...) { 45 | std::cerr << "Unknown error!\n"; 46 | return 2; 47 | } 48 | /* Compile command 49 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch14_tools.cpp ex09_group.cpp `fltk-config --ldflags --use-images` -o a.out 50 | */ 51 | -------------------------------------------------------------------------------- /chapter14/ex10_pseudo_win.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 1"}; 11 | 12 | Pseudo_window pw {Point{50, 50}, "../images/cpp.jpg"}; 13 | win.attach(pw); 14 | 15 | win.wait_for_button(); 16 | } 17 | catch (exception& e) { 18 | std::cerr << "Exception: " << e.what() << '\n'; 19 | return 1; 20 | } 21 | catch (...) { 22 | std::cerr << "Unknown error!\n"; 23 | return 2; 24 | } 25 | /* Compile command 26 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ../chapter13/ch13_tools.cpp ch14_tools.cpp ex10_pseudo_win.cpp `fltk-config --ldflags --use-images` -o a.out 27 | */ 28 | -------------------------------------------------------------------------------- /chapter14/ex11_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch14_tools.h" 4 | 5 | #include 6 | 7 | int main() 8 | try { 9 | const Point tl {100, 100}; 10 | Simple_window win {tl, 600, 400, "Chapter 14 Ex 11"}; 11 | 12 | Binary_tree bt {Point{300, 100}, 5}; 13 | cout << bt.number_of_nodes() << '\n'; 14 | 15 | win.wait_for_button(); 16 | } 17 | catch (exception& e) { 18 | std::cerr << "Exception: " << e.what() << '\n'; 19 | return 1; 20 | } 21 | catch (...) { 22 | std::cerr << "Unknown error!\n"; 23 | return 2; 24 | } 25 | /* Compile command 26 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ../chapter13/ch13_tools.cpp ch14_tools.cpp ex11_binary_tree.cpp `fltk-config --ldflags --use-images` -o a.out 27 | */ 28 | -------------------------------------------------------------------------------- /chapter15/ex01_factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int recur_fac(int n) { return n > 1 ? n * recur_fac(n - 1) : 1; } 6 | 7 | int iter_fac(int n) 8 | { 9 | int r = 1; 10 | while (n > 1) { 11 | r *= n; 12 | --n; 13 | } 14 | return r; 15 | } 16 | 17 | int main() 18 | { 19 | cout << "Recursive factorial: " << recur_fac(10) << '\n' 20 | << "Iterative factorial: " << iter_fac(10) << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /chapter15/ex03_precision.cpp: -------------------------------------------------------------------------------- 1 | // TODO - come back to this after learning about templates 2 | -------------------------------------------------------------------------------- /chapter15/ex06_bar_graph.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch15_tools.h" 4 | 5 | #include 6 | 7 | using namespace Graph_lib; 8 | 9 | int main() 10 | try { 11 | // Boilerplate 12 | constexpr int xmax = 600; 13 | constexpr int ymax = 400; 14 | 15 | Simple_window win {Point{100, 100}, xmax, ymax, "Chapter 15"}; 16 | 17 | // Ex 6 - Bar Graph 18 | vector data {6.2, 8.7, 7.3, 5.5, 9.1, 9.4, 6, 2.4, 4.2, 3.3, 8}; 19 | Bar_graph bg {data, xmax, ymax}; 20 | win.attach(bg); 21 | 22 | win.wait_for_button(); 23 | } 24 | catch (exception& e) { 25 | std::cerr << "Exception: " << e.what() << '\n'; 26 | return 1; 27 | } 28 | catch (...) { 29 | std::cerr << "Unknown exception\n"; 30 | return 2; 31 | } 32 | /* Compile command 33 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch15_tools.cpp ex06_bar_graph.cpp `fltk-config --ldflags --use-images` -o a.out 34 | */ 35 | -------------------------------------------------------------------------------- /chapter15/ex07_bar_graph_2.cpp: -------------------------------------------------------------------------------- 1 | #include "../GUI/Simple_window.h" 2 | #include "../GUI/Graph.h" 3 | #include "./ch15_tools.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace Graph_lib; 9 | 10 | int main() 11 | try { 12 | // Boilerplate 13 | constexpr int xmax = 600; 14 | constexpr int ymax = 400; 15 | 16 | Simple_window win {Point{100, 100}, xmax, ymax, "Chapter 15"}; 17 | 18 | // Ex 6 - Bar Graph 19 | vector data {6.2, 8.7, 7.3, 5.5, 9.1, 9.4, 6, 2.4, 4.2, 3.3, 8}; 20 | Bar_graph bg {data, xmax, ymax}; 21 | bg.set_labels("Bar Graph Fun", "x axis", "y axis"); 22 | win.attach(bg); 23 | 24 | win.wait_for_button(); 25 | } 26 | catch (exception& e) { 27 | std::cerr << "Exception: " << e.what() << '\n'; 28 | return 1; 29 | } 30 | catch (...) { 31 | std::cerr << "Unknown exception\n"; 32 | return 2; 33 | } 34 | /* Compile command 35 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch15_tools.cpp ex07_bar_graph_2.cpp `fltk-config --ldflags --use-images` -o a.out 36 | */ 37 | -------------------------------------------------------------------------------- /chapter15/heights.txt: -------------------------------------------------------------------------------- 1 | (170,7) 2 | (175,9) 3 | (180,23) 4 | (185,17) 5 | (190,6) 6 | (195,1) 7 | -------------------------------------------------------------------------------- /chapter15/rivers.txt: -------------------------------------------------------------------------------- 1 | Mackenzie River 4241 2 | Yukon River 3185 3 | Saint Lawrence River 3058 4 | Nelson River 2575 5 | Slave River 2338 6 | Columbia River 2000 7 | Saskatchewan River 1939 8 | Peace River 1923 9 | -------------------------------------------------------------------------------- /chapter16/ch16_notes.md: -------------------------------------------------------------------------------- 1 | # Programming Principles & Practice 2 | 3 | ## Chapter 16 - Graphical User Interfaces 4 | 5 | I'm hitting a wall with all this graphics stuff. I see the benefit, I understand the purpose of the exercise, I just don't enjoy this. It feels like doing JavaScript and I can do JavaScript. My strong desire to learn more about memory management and the "meat" of C++ is pulling me forward. 6 | 7 | I'll move on to Chapter 17 for the awesome stuff therein. Perhaps I will come back to this in the future. 8 | -------------------------------------------------------------------------------- /chapter16/ch16_tools.cpp: -------------------------------------------------------------------------------- 1 | #include "./ch16_tools.h" 2 | 3 | // Ex 1 - My_window 4 | 5 | My_window::My_window(Point xy, int w, int h, const string& title) 6 | : Window(xy, w, h, title), 7 | next_button{Point{x_max() - 150, 0}, 70, 20, "Next", 8 | [](Address, Address pw) { reference_to(pw).next(); } }, 9 | quit_button{Point{x_max() - 70, 0}, 70, 20, "Quit", 10 | [](Address, Address pw) { reference_to(pw).quit(); } } 11 | { 12 | attach(next_button); 13 | attach(quit_button); 14 | } 15 | 16 | void My_window::next() 17 | { 18 | // not sure what next is supposed to do.. 19 | } 20 | 21 | void My_window::quit() 22 | { 23 | hide(); 24 | } 25 | -------------------------------------------------------------------------------- /chapter16/ch16_tools.h: -------------------------------------------------------------------------------- 1 | #include "../GUI/GUI.h" 2 | 3 | using namespace Graph_lib; 4 | 5 | // Ex 1 - My_window 6 | 7 | struct My_window : Graph_lib::Window { 8 | My_window(Point xy, int w, int y, const string& title); 9 | 10 | private: 11 | Button next_button; 12 | Button quit_button; 13 | 14 | void next(); 15 | void quit(); 16 | }; 17 | -------------------------------------------------------------------------------- /chapter16/ex01_my_window.cpp: -------------------------------------------------------------------------------- 1 | #include "./ch16_tools.h" 2 | 3 | #include 4 | 5 | int main() 6 | try { 7 | My_window win {Point{100, 100}, 600, 400, "My Window"}; 8 | return gui_main(); 9 | } 10 | catch(exception& e) { 11 | std::cerr << "Error: " << e.what() << '\n'; 12 | return 1; 13 | } 14 | catch(...) { 15 | std::cerr << "Unknown exception\n"; 16 | return 2; 17 | } 18 | /* Compile command 19 | g++ -w -Wall -std=c++11 ../GUI/Graph.cpp ../GUI/Window.cpp ../GUI/GUI.cpp ../GUI/Simple_window.cpp ch16_tools.cpp ex01_my_window.cpp `fltk-config --ldflags --use-images` -o a.out 20 | */ 21 | -------------------------------------------------------------------------------- /chapter17/ch17_drill_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | ostream& print_array(ostream& os, int* a, int n) 8 | { 9 | for (int i = 0; i < n; ++i) os << a[i] << '\n'; 10 | return os; 11 | } 12 | 13 | ostream& print_vector(ostream& os, vector& v) 14 | { 15 | for (int vv : v) os << vv << '\n'; 16 | return os; 17 | } 18 | 19 | int main() 20 | try { 21 | // Drill - free-store allocation 22 | 23 | int* arr20 = new int[20]; 24 | 25 | int x = 100; 26 | for (int i = 0; i < 20; ++i) { 27 | arr20[i] = x; 28 | ++x; 29 | } 30 | 31 | print_array(cout, arr20, 20); 32 | delete[] arr20; 33 | 34 | vector v10(10); 35 | 36 | int y = 100; 37 | for (int& v : v10) { 38 | v = y; 39 | ++y; 40 | } 41 | 42 | print_vector(cout, v10); 43 | } 44 | catch(exception& e) { 45 | cerr << "Exception: " << e.what() << '\n'; 46 | return 1; 47 | } 48 | catch(...) { 49 | cerr << "Unknown exception\n"; 50 | return 2; 51 | } 52 | -------------------------------------------------------------------------------- /chapter17/ex03_to_lower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | 5 | // Replace all uppercase characters with lowercase in a C-style string. No 6 | // standard library functions. 7 | 8 | void to_lower(char* s) 9 | // If any char code falls between A & Z increase their code by 32, the 10 | // difference between upper and lower-case counterparts in ASCII. 11 | { 12 | constexpr int A = 65; 13 | constexpr int Z = 90; 14 | constexpr int diff = 32; 15 | 16 | for (int i = 0; s[i]; ++i) { 17 | int code = int(s[i]); 18 | 19 | if (A <= code && code <= Z) 20 | s[i] += diff; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | char ac1[] = "HowdY"; 27 | to_lower(ac1); 28 | 29 | cout << ac1 << '\n'; 30 | 31 | char ac2[] = "ThiS serIAl KiLleR leTTeR iS sCarY, OR NOT!!"; 32 | to_lower(ac2); 33 | 34 | cout << ac2 << '\n'; 35 | } 36 | 37 | // BEGIN STRIKEOUT 38 | // I had to look up C-style strings and found this method of initialization. 39 | // The chapter does not actually indicate how to create a C-style string so 40 | // I've gone with this. If it's wrong its wrong. 41 | // 42 | // The print out still prints the '0' but as it is a C-style string I assume it 43 | // should stay. 44 | // END STRIKEOUT 45 | // 46 | // EDIT: I looked ahead in the text and found this C-string solution. 47 | // 48 | // The original test for '0' was incorrect as the terminating character is NULL 49 | // and not zero. The truthy check is great! 50 | -------------------------------------------------------------------------------- /chapter17/ex04_strdup.cpp: -------------------------------------------------------------------------------- 1 | /* Chapter 17 Exercise 4 2 | * Write a function strdup to copy a C-string into memory allocated on the free 3 | * store. Do not use any standard library functions. 4 | */ 5 | #include 6 | 7 | using std::cout; 8 | 9 | int c_str_len(const char* s) 10 | // return the length of a C-string in a char array including terminator 11 | { 12 | int n = 0; 13 | while(s[n]) ++n; 14 | cout << n << '\n'; 15 | return n + 1; // + 1 to include terminator 16 | } 17 | 18 | char* strdup(const char* c) 19 | { 20 | const int n = c_str_len(c); 21 | 22 | char* s = new char[n]; 23 | 24 | for (int i = 0; i < n; ++i) 25 | s[i] = c[i]; 26 | 27 | return s; 28 | } 29 | 30 | int main() 31 | { 32 | char cs1[] = "Allocate me like one of your French strings"; 33 | 34 | char* cs2 = strdup(cs1); 35 | 36 | for (int i = 0; cs2[i]; ++i) 37 | cout << cs2[i]; 38 | 39 | cout << '\n'; 40 | 41 | delete[] cs2; 42 | } 43 | 44 | // How can I test to make sure I'm not leaking memory? Is cs1 automatically 45 | // freed (automatic storage?) 46 | -------------------------------------------------------------------------------- /chapter17/ex05_findx.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 17 Exercise 5 5 | // 6 | // Write a function, char* findx(const char* s, const char* x), that finds the 7 | // first occurrence of the C-style string x in s. 8 | // 9 | 10 | #include 11 | #include 12 | 13 | using std::cout; 14 | 15 | const char* findx(const char* s, const char* x) 16 | { 17 | for (int i = 0; s[i]; ++i) 18 | if (s[i] == x[0]) { 19 | for (int j = 1; ; ++j) 20 | if (!x[j]) { 21 | return &s[i]; 22 | } else if (x[j] != s[i + j]) { 23 | break; 24 | } 25 | } 26 | 27 | return nullptr; // returned for falsey test 28 | } 29 | 30 | int main() 31 | try { 32 | char cs1[] = "For the Horde?"; 33 | char cs2[] = "Horde"; 34 | char cs3[] = "Alliance"; 35 | 36 | const char* found = findx(cs1, cs2); 37 | const char* not_found = findx(cs1, cs3); 38 | 39 | if (found) 40 | cout << "Glory to the Horde!!" << found << '\n'; 41 | 42 | if (not_found) 43 | cout << "Glory to the Alliance!!" << not_found << '\n'; 44 | } 45 | catch(std::exception& e) { 46 | std::cerr << "Exception: " << e.what() << '\n'; 47 | return 1; 48 | } 49 | catch(...) { 50 | std::cerr << "unknown exception\n"; 51 | return 2; 52 | } 53 | -------------------------------------------------------------------------------- /chapter17/ex06_mem_exhaust.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 17 Exercise 6 5 | // 6 | // Write a program with an infinite loop that allocates but never deallocates. 7 | // What happens and how much memory can you allocate before failing? 8 | #include 9 | #include 10 | 11 | unsigned long long int n = 0; 12 | 13 | int main() 14 | try { 15 | while(true) { 16 | double* p = new double[100000]; 17 | n += 1; 18 | std::cout << n << '\n'; 19 | } 20 | } 21 | catch(std::exception& e) { 22 | std::cerr << "Error: " << e.what() << n << '\n'; 23 | return 1; 24 | } 25 | catch(...) { 26 | std::cerr << "Unkown Error: " << n << '\n'; 27 | } 28 | 29 | // This seems to never crash. There are slight pauses now and then but my 30 | // computer is able to get much higher than anyone else who posted about this. 31 | // Do Mac's dump memory to save themselves? 32 | -------------------------------------------------------------------------------- /chapter17/ex08_cin_str.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 17 Exercise 8 5 | // 6 | // Repeat last exercise (read in chars to memory) but read into a string 7 | // instead. 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main() 16 | try { 17 | // code 18 | string store = ""; 19 | 20 | cout << "Enter some characters. Terminate with '!'\n"; 21 | char ch; 22 | while (cin.get(ch) && ch != '!') 23 | store += ch; 24 | 25 | cout << store << '\n'; 26 | } 27 | catch(exception& e) { 28 | cerr << "Exception: " << e.what() << '\n'; 29 | return 1; 30 | } 31 | catch(...) { 32 | cerr << "Unknown exception\n"; 33 | return 2; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /chapter17/ex09_memory_growth.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 17 Exercise 9 5 | // 6 | // Write a program to determine how the stack and free store grow: up or down. 7 | // Result: Free store address grow upwards and stack addresses grow down! 8 | 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | void stack_check() 15 | { 16 | int x = 7; 17 | cout << "Stack: " << &x << '\n'; 18 | } 19 | 20 | void double_stack() 21 | { 22 | int y = 10; 23 | cout << "Stack: " << &y << '\n'; 24 | stack_check(); 25 | 26 | int z = 37; 27 | cout << "Stack: " << &z << '\n'; 28 | } 29 | 30 | void free_check() 31 | { 32 | int* arr = new int[5]; 33 | 34 | for (int i = 0; i < 5; ++i) 35 | cout << "Free: " << &arr[i] << '\n'; 36 | 37 | delete[] arr; 38 | } 39 | 40 | int main() 41 | try { 42 | // code 43 | stack_check(); 44 | free_check(); 45 | double_stack(); 46 | } 47 | catch(exception& e) { 48 | cerr << "Exception: " << e.what() << '\n'; 49 | return 1; 50 | } 51 | catch(...) { 52 | cerr << "Unknown exception\n"; 53 | return 2; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /chapter18/ch18_drill_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 18 Array Drill 5 | // 6 | 7 | #include 8 | #include 9 | 10 | // 1 Define a global int array 11 | int ga[10] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }; 12 | 13 | void f(int ai[], int n) 14 | { 15 | int la[10] = { }; 16 | for (int i = 0; i < n; ++i) la[i] = ai[i]; 17 | 18 | std::cout << "Local copy\n"; 19 | for (const auto& a : la) 20 | std::cout << a << ' '; 21 | std::cout << '\n'; 22 | 23 | int* p = new int[n]; 24 | for (int i = 0; i < n; ++i) p[i] = ai[i]; 25 | 26 | std::cout << "Pointer to free store\n"; 27 | for (int i = 0; i < n; ++i) 28 | std::cout << p[i] << ' '; 29 | std::cout << '\n'; 30 | 31 | delete[] p; 32 | } 33 | 34 | int fac(int n) { return n > 1 ? n*fac(n-1) : 1; } 35 | 36 | int main() 37 | try { 38 | // code 39 | std::cout << "Binary array\n"; 40 | f(ga, 10); 41 | 42 | int aa[10] = {}; 43 | for (int i = 0; i < 10; ++i) 44 | aa[i] = fac(i + 1); 45 | 46 | std::cout << "Factorial array\n"; 47 | f(aa, 10); 48 | } 49 | catch(std::exception& e) { 50 | std::cerr << "Exception: " << e.what() << '\n'; 51 | return 1; 52 | } 53 | catch(...) { 54 | std::cerr << "Unknown exception\n"; 55 | return 2; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /chapter18/ch18_drill_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 18 Vector Drill 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | vector gv {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; 14 | 15 | void f(vector v) 16 | { 17 | vector lv(10); 18 | lv = v; 19 | for (auto& a : lv) 20 | cout << a << '\t'; 21 | cout << '\n'; 22 | 23 | vector lv2 = v; 24 | for (auto& a : lv2) 25 | cout << a << '\t'; 26 | cout << '\n'; 27 | } 28 | 29 | int fac(int n) { return n > 1 ? n * fac(n - 1) : 1; } 30 | 31 | int main() 32 | try { 33 | // code 34 | f(gv); 35 | 36 | vector vv(10); 37 | for (int i = 0; i < vv.size(); ++i) 38 | vv[i] = fac(i + 1); 39 | f(vv); 40 | } 41 | catch(std::exception& e) { 42 | std::cerr << "Exception: " << e.what() << '\n'; 43 | return 1; 44 | } 45 | catch(...) { 46 | std::cerr << "Unknown exception\n"; 47 | return 2; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /chapter18/ex05_to_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | std::string cat_dot(const std::string& s1, const std::string& s2) 7 | { 8 | return s1 + "." + s2; 9 | } 10 | 11 | std::string cat_dot(const std::string& s1, const std::string& s2, 12 | const std::string& s3) 13 | { 14 | return s1 + s3 + s2; 15 | } 16 | */ 17 | 18 | char* cat_dot(const char* s1, const char* s2, char c = '.') 19 | { 20 | int n1 = 0; 21 | while (s1[n1]) ++n1; 22 | 23 | int n2 = 0; 24 | while (s2[n2]) ++n2; 25 | 26 | int n3 = n1 + 1 + n2 + 1; 27 | 28 | char* p = new char[n3]; 29 | 30 | for (int i = 0; i < n1; ++i) 31 | p[i] = s1[i]; 32 | 33 | p[n1] = c; 34 | 35 | for (int i = 0; i < n2; ++i) 36 | p[n1 + 1 + i] = s2[i]; 37 | 38 | p[n3 - 1] = s2[n2]; 39 | 40 | return p; 41 | } 42 | 43 | int main() 44 | { 45 | /* 46 | std::cout << cat_dot("Jacob", "Schick") << '\n'; 47 | std::cout << cat_dot("Chris", "Schick", "-") << '\n'; 48 | */ 49 | char str1[] = "Nora"; 50 | char str2[] = "Schick"; 51 | 52 | char* str3 = cat_dot(str1, str2); 53 | char* str4 = cat_dot(str2, str1, '*'); 54 | 55 | std::cout << str3 << '\n'; 56 | std::cout << str4 << '\n'; 57 | 58 | delete[] str3; 59 | delete[] str4; 60 | } 61 | -------------------------------------------------------------------------------- /chapter18/ex09_mem_layout.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 18 Exercise 9 5 | // 6 | // Write a program to demonstrate the manner in which static storage, the 7 | // stack, and the free-store are laid out in memory. Do they grow upwards or 8 | // downwards? 9 | // 10 | 11 | #include 12 | #include 13 | 14 | int gai[] = { 1, 2, 3, 4, 5 }; 15 | 16 | void f(int ai[], int sz) 17 | { 18 | for (int i = 0; i < sz; ++i) 19 | std::cout << ai[i] << ' ' << &ai[i] << '\n'; 20 | } 21 | 22 | int main() 23 | try { 24 | // code 25 | int lai[] = { 1, 1, 2, 3, 5 }; 26 | 27 | int* pai = new int[5]{ 3, 6, 9, 12, 15 }; 28 | 29 | std::cout << "Static storage:\n"; 30 | f(gai, 5); 31 | std::cout << '\n'; 32 | std::cout << "Stack storage:\n"; 33 | f(lai, 5); 34 | std::cout << '\n'; 35 | std::cout << "Free-store:\n"; 36 | f(pai, 5); 37 | std::cout << '\n'; 38 | 39 | delete[] pai; 40 | } 41 | catch(std::exception& e) { 42 | std::cerr << "Exception: " << e.what() << '\n'; 43 | return 1; 44 | } 45 | catch(...) { 46 | std::cerr << "Unknown exception\n"; 47 | return 2; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /chapter18/ex10_long_string.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 18 Exercise 10 5 | // 6 | // Fix the palindrome solution in chapter to allow for long strings. First 7 | // report if the string was too long then accommodate a long string. 8 | // 9 | // NOT YET COMPLETE 10 | 11 | #include 12 | #include 13 | 14 | bool is_palindrome(const char s[], int n) 15 | { 16 | int first = 0; 17 | int last = n - 1; 18 | while (first < last) { 19 | if (s[first] != s[last]) return false; 20 | ++first; 21 | --last; 22 | } 23 | return true; 24 | } 25 | 26 | std::istream& read_word(std::istream& is, char* buffer, int max) 27 | { 28 | is.width(max); 29 | is >> buffer; 30 | 31 | if (strlen(buffer) >= max && buffer[max]) 32 | throw std::runtime_error("string too large to check"); 33 | 34 | return is; 35 | } 36 | 37 | int main() 38 | try { 39 | constexpr int max = 8; 40 | for (char s[max]; read_word(std::cin, s, max); ) { 41 | std::cout << s << " is"; 42 | if (!is_palindrome(s, strlen(s))) std::cout << " not"; 43 | std::cout << " a plaindrome\n"; 44 | } 45 | } 46 | catch(std::exception& e) { 47 | std::cerr << "Exception: " << e.what() << '\n'; 48 | return 1; 49 | } 50 | catch(...) { 51 | std::cerr << "Unknown exception\n"; 52 | return 2; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /chapter19/ex01_vec_add.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 19 Exercise 1 5 | // 6 | // Write a template function that adds the elements of one vector to another. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | template 15 | void f(std::vector& v1, std::vector& v2) 16 | { 17 | if (v1.size() > v2.size()) 18 | throw std::runtime_error("not enough elements in v2"); 19 | 20 | for (int i = 0; i < v1.size(); ++i) 21 | v1[i] += v2[i]; 22 | } 23 | 24 | int main() 25 | try { 26 | std::vector va { 1, 2, 3, 4 }; 27 | std::vector vb { 3, 6, 9, 12 }; 28 | 29 | f(va, vb); 30 | 31 | for (auto& a : va) 32 | std::cout << a << ' '; 33 | std::cout << '\n'; 34 | 35 | std::vector vs1 { "fire", "oak", "foo" }; 36 | std::vector vs2 { "truck", "bank", "bar" }; 37 | 38 | f(vs1, vs2); 39 | 40 | for (auto& a : vs1) 41 | std::cout << a << ' '; 42 | std::cout << '\n'; 43 | } 44 | catch(std::exception& e) { 45 | std::cerr << "Exception: " << e.what() << '\n'; 46 | return 1; 47 | } 48 | catch(...) { 49 | std::cerr << "Unknown exception\n"; 50 | return 2; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /chapter19/ex02_mult_sum.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 19 Exercise 2 5 | // 6 | // Write a template function that takes two vectors and sums the product of 7 | // their corresponding elements. 8 | // 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | template 15 | T product_sum(std::vector& vt, std::vector& vu) 16 | { 17 | if (vt.size() != vu.size()) 18 | throw std::runtime_error("cannot sum different sized vectors"); 19 | 20 | T sum = 0; 21 | for (int i = 0; i < vt.size(); ++i) sum += vt[i] * vu[i]; 22 | 23 | return sum; 24 | } 25 | 26 | int main() 27 | try { 28 | std::vector vi1 { 1, 2, 3, 4 }; 29 | std::vector vi2 { 5, 6, 7, 8 }; 30 | std::vector vd { 1.1, 2.2, 3.3, 4.4 }; 31 | 32 | std::cout << product_sum(vi1, vi2) << '\n'; 33 | std::cout << product_sum(vi1, vd) << '\n'; 34 | std::cout << product_sum(vd, vi1) << '\n'; 35 | } 36 | catch(std::exception& e) { 37 | std::cerr << "Exception: " << e.what() << '\n'; 38 | return 1; 39 | } 40 | catch(...) { 41 | std::cerr << "Unknown exception\n"; 42 | return 2; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /chapter19/ex12_file_handle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 19 Exercise 12 5 | // 6 | // Define a File_handle class that opens a file in its constructor and closes 7 | // it in its destructor. 8 | // 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | class File_handle { 16 | std::fstream fs; 17 | public: 18 | File_handle(const std::string& s) 19 | : fs{s} 20 | { 21 | std::cout << "File opened\n"; 22 | } 23 | 24 | ~File_handle() 25 | { 26 | fs.close(); 27 | std::cout << "File closed\n"; 28 | } 29 | 30 | void readFile(std::string& s) 31 | { 32 | for (std::string t; fs >> t; ) 33 | s += t + ' '; 34 | } 35 | }; 36 | 37 | int main() 38 | try { 39 | File_handle test {"./test.txt"}; 40 | std::string s; 41 | test.readFile(s); 42 | 43 | std::cout << s << '\n'; 44 | } 45 | catch(std::exception& e) { 46 | std::cerr << "Exception: " << e.what() << '\n'; 47 | return 1; 48 | } 49 | catch(...) { 50 | std::cerr << "Unknown exception\n"; 51 | return 2; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /chapter19/ex14-15_wumpus_gui.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 19 Exercise 14 5 | // 6 | // Provide a GUI interface to the "Hunt the Wumpus" game from chapter 18. 7 | // 8 | // Chapter 19 Exercise 15 9 | // 10 | // Allow the user to mark rooms based on knowledge and guesses. 11 | // 12 | 13 | // These will be applied to the separate Hunt the Wumpus game repository 14 | -------------------------------------------------------------------------------- /chapter19/test.txt: -------------------------------------------------------------------------------- 1 | Going back to the mansion.. 2 | -------------------------------------------------------------------------------- /chapter20/ch20_real_vec.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 20 std::vector comparison 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main() 12 | try { 13 | std::vector vc (10); 14 | 15 | char ch = 'a'; 16 | for (auto it = vc.begin(); it != vc.end(); ++it) 17 | *it = ch++; 18 | 19 | for (auto c : vc) 20 | std::cout << c << ' '; 21 | std::cout << '\n'; 22 | 23 | auto it = vc.begin(); 24 | std::cout << "deref iter: " << *it << '\n'; // a 25 | std::cout << "ss 1 iter: " << it[1] << '\n'; // b 26 | std::cout << "ss 5 iter: " << it[5] << '\n'; // f 27 | 28 | try { 29 | --it; 30 | std::cout << "before begin: " << *it << '\n'; 31 | } catch(std::exception& e) { 32 | std::cout << "Bad: " << e.what() << '\n'; 33 | } 34 | 35 | try { 36 | std::cout << "ss 100 iter: " << it[100] << '\n'; 37 | } catch(std::exception& e) { 38 | std::cout << "Bad: " << e.what() << '\n'; 39 | } 40 | } 41 | catch(std::exception& e) { 42 | std::cerr << "Exception: " << e.what() << '\n'; 43 | return 1; 44 | } 45 | catch(...) { 46 | std::cerr << "Unknown exception\n"; 47 | return 2; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /chapter20/ch20_test_type.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Test_type { 5 | std::string s; 6 | unsigned long i; 7 | double d; 8 | char c; 9 | 10 | Test_type() : s{"def"}, i{0}, d{0.0}, c{'\0'} { } 11 | Test_type(const std::string& ss) 12 | : s{ss}, i{ss.size()}, d{ss.size() / 3.14159}, c{ss[0]} { } 13 | 14 | ~Test_type() { std::cout << s << " destructed!\n"; } 15 | }; 16 | 17 | std::ostream& operator<<(std::ostream& os, const Test_type& tt) 18 | { 19 | return os << tt.s << ' ' 20 | << tt.i << ' ' 21 | << tt.d << ' ' 22 | << tt.c << '\n'; 23 | } 24 | -------------------------------------------------------------------------------- /chapter20/ex07_last_str.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 20 Exercise 7 5 | // 6 | // Find the lexicographical last string in an unsorted vector 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | template 15 | Iter greatest(Iter first, Iter last) 16 | { 17 | Iter g = first; 18 | for (Iter p = first; p != last; ++p) 19 | if (*g < *p) g = p; 20 | return g; 21 | } 22 | 23 | int main() 24 | try { 25 | std::vector vs { "Tommy", "Dola", "High", "Cookie", "Jim B", 26 | "Andy", "Hogan", "Squirrel" }; 27 | 28 | auto p = greatest(vs.begin(), vs.end()); 29 | 30 | std::cout << "The lexi-last string was: " << *p << '\n'; 31 | } 32 | catch(std::exception& e) { 33 | std::cerr << "Exception: " << e.what() << '\n'; 34 | return 1; 35 | } 36 | catch(...) { 37 | std::cerr << "Unknown exception\n"; 38 | return 2; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /chapter20/ex11_list_to_vec.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 20 Exercise 11 5 | // 6 | // copy the elements from a list of ints to a vector of doubles. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | std::vector list_to_vec(const std::list& l) 15 | { 16 | std::vector v (l.size()); 17 | 18 | std::copy(l.begin(), l.end(), v.begin()); 19 | 20 | return v; 21 | } 22 | 23 | int main() 24 | try { 25 | std::list l { 1, 2, 3, 4, 5, 6, 7, 8 }; 26 | 27 | std::vector v = list_to_vec(l); 28 | 29 | for (auto& a : v) 30 | std::cout << a << ' '; 31 | std::cout << '\n'; 32 | } 33 | catch(std::exception& e) { 34 | std::cerr << "Exception: " << e.what() << '\n'; 35 | return 1; 36 | } 37 | catch(...) { 38 | std::cerr << "Unknown exception\n"; 39 | return 2; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /chapter20/jack.txt: -------------------------------------------------------------------------------- 1 | 31.2 44 51.7 55.5 56.2 78.2 23.6 66.6 12.1 11.8 59.9 63.4 2 | -------------------------------------------------------------------------------- /chapter20/jill.txt: -------------------------------------------------------------------------------- 1 | 22.2 16.2 35.7 28.4 19.0 10.7 37.1 30.6 18.0 27.3 13.5 28.3 2 | -------------------------------------------------------------------------------- /chapter21/ch21_order.h: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Order.h 5 | // 6 | // This is a header for the multiple exercises that use the Order class. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 14 | struct Purchase { 15 | std::string name; 16 | double unit_price = 0; 17 | int count = 0; 18 | }; 19 | 20 | std::istream& operator>>(std::istream& is, Purchase& p); 21 | std::ostream& operator<<(std::ostream& os, const Purchase& p); 22 | 23 | // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 24 | struct Order { 25 | std::string name; 26 | std::string address; 27 | std::string data; // ? taken as payment method 28 | std::vector cart; 29 | 30 | double total() const; 31 | }; 32 | 33 | std::ostream& operator<<(std::ostream& os, const Order& o); 34 | std::istream& operator>>(std::istream& is, Order& o); 35 | 36 | -------------------------------------------------------------------------------- /chapter21/ch21_tt5_unordered.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Try This: unordered_map test 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main() 13 | try { 14 | std::unordered_map game_releases = { 15 | { "Resident Evil", 1996 }, 16 | { "Resident Evil 2", 1998 }, 17 | { "Resident Evil 3", 1999 }, 18 | { "Resident Evil 4", 2005 }, 19 | { "Resident Evil 5", 2009 }, 20 | { "Resident Evil 6", 2012 }, 21 | { "Resident Evil 7", 2017 } 22 | }; 23 | 24 | for (const auto& g : game_releases) 25 | std::cout << g.first << '\t' << g.second << '\n'; 26 | } 27 | catch(std::exception& e) { 28 | std::cerr << "Exception: " << e.what() << '\n'; 29 | return 1; 30 | } 31 | catch(...) { 32 | std::cerr << "Unknown exception\n"; 33 | return 2; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /chapter21/ex03_count.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Exercise 3 5 | // 6 | // Implement count() and test it. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | template 15 | unsigned int my_count(Iter first, Iter last, const T& val) 16 | { 17 | unsigned int count = 0; 18 | while (first != last) { 19 | if (*first == val) 20 | ++count; 21 | ++first; 22 | } 23 | 24 | return count; 25 | } 26 | 27 | int main() 28 | try { 29 | std::vector vc {'a', 'b', 'c', 'd', 'e'}; 30 | std::list li {1, 2, 3, 1, 4, 5, 1}; 31 | 32 | std::cout << "a's in vc: " << my_count(vc.begin(), vc.end(), 'a') << '\n' 33 | << "g's in vc: " << my_count(vc.begin(), vc.end(), 'g') << '\n' 34 | << "1's in li: " << my_count(li.begin(), li.end(), 1) << '\n' 35 | << "6's in li: " << my_count(li.begin(), li.end(), 6) << '\n'; 36 | } 37 | catch(std::exception& e) { 38 | std::cerr << "Exception: " << e.what() << '\n'; 39 | return 1; 40 | } 41 | catch(...) { 42 | std::cerr << "Unknown exception\n"; 43 | return 2; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /chapter21/ex08_sorted_seconds.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Exercise 8 5 | // 6 | // Modify word frequency example from §21.6.1 to output the words in order of 7 | // frequency. 8 | // 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | int main() 17 | try { 18 | const std::string iname { "./input_story.txt" }; 19 | 20 | std::ifstream ifs { iname }; 21 | if (!ifs) throw std::runtime_error("Could not read from file " + iname); 22 | 23 | std::map words; // keep (word,frequency) pairs 24 | 25 | for (std::string s; ifs >> s; ) 26 | ++words[s]; // words is subscripted by a string 27 | 28 | /* 29 | for (const auto& p : words) 30 | std::cout << p.first << ": " << p.second << '\n'; 31 | */ 32 | 33 | // solution 34 | std::multimap freqs; 35 | 36 | for (const auto& w : words) 37 | freqs.insert(std::pair(w.second, w.first)); 38 | 39 | for (auto it = freqs.rbegin(); it != freqs.rend(); ++it) 40 | std::cout << it->first << ": " << it->second << '\n'; 41 | } 42 | catch(std::exception& e) { 43 | std::cerr << "Exception: " << e.what() << '\n'; 44 | return 1; 45 | } 46 | catch(...) { 47 | std::cerr << "Unknown exception\n"; 48 | return 2; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /chapter21/ex11_order_gui.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Exercise 11 5 | // 6 | // Provide a GUI for entering Orders into files 7 | // 8 | // NOTE: I will come back to all GUI exercises after reviewing ch12-16 9 | // 10 | 11 | #include 12 | #include 13 | 14 | int main() 15 | try { 16 | // code 17 | } 18 | catch(std::exception& e) { 19 | std::cerr << "Exception: " << e.what() << '\n'; 20 | return 1; 21 | } 22 | catch(...) { 23 | std::cerr << "Unknown exception\n"; 24 | return 2; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /chapter21/ex12_order_query_gui.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Exercise 12 5 | // 6 | // Provide a GUI for querying a file of Orders. 7 | // 8 | // NOTE: I will come back to all GUI exercises after reviewing ch12-16 9 | // 10 | 11 | #include 12 | #include 13 | 14 | int main() 15 | try { 16 | // code 17 | } 18 | catch(std::exception& e) { 19 | std::cerr << "Exception: " << e.what() << '\n'; 20 | return 1; 21 | } 22 | catch(...) { 23 | std::cerr << "Unknown exception\n"; 24 | return 2; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /chapter21/input_floats.txt: -------------------------------------------------------------------------------- 1 | 54.1 2 | 234.5 3 | 345.7 4 | 45645.34 5 | 34.4 6 | 23423.876 7 | 455.3 8 | 574.564 9 | 890.345 10 | 894 11 | 345.345 12 | 34234.23 13 | 340.9 14 | 7897.4 15 | 5978.3453 16 | 435.34 17 | 345 18 | 5656.65 19 | 54 20 | 3453.4353 21 | -------------------------------------------------------------------------------- /chapter21/input_items.txt: -------------------------------------------------------------------------------- 1 | monitor 58 199.99 2 | book 14 62.73 3 | table 4 287.74 4 | couch 2011 787.80 5 | cup 5 11.46 6 | bread 17 3.56 7 | water 2 11.98 8 | phone 6 500.32 9 | chair 15 298.73 10 | pan 83 73.44 11 | -------------------------------------------------------------------------------- /chapter21/input_story.txt: -------------------------------------------------------------------------------- 1 | Today we moved our 4 year old son out of his toddler bed to his own double bed in a separate room. We bought a double because we currently don't have an extra bed for company should someone need to sleep over due to weather or excessive drunken-ness. As of this moment, we have said our goodnights and are waiting to see if he falls asleep or sneaks out with another negotiation attempt. 2 | My wife started up a final load of laundry so I'm unable to hear what's happening in his room, if he's talking to himself, jumping on the bed, or, I fear, crying. It's not easy but he's a big boy now and he was nearly touching either end of his toddler bed as it was. Hopefully he's able to sleep the whole night but if not I am prepared to spend the night beside him should he arrive at our bedside full of sadness or fear. 3 | -------------------------------------------------------------------------------- /chapter21/output_orders_1.txt: -------------------------------------------------------------------------------- 1 | Christie Crisp 2 | 511 Honeydew Lane 3 | MasterCard 6543 4 | 5 | 1 Fire_Extinguisher 23.89 6 | 7 | Christmas Jones 8 | 373 3rd Ave 9 | Cash 10 | 11 | 3 Multi_Coloured_Lights 24.99 12 | 1 Fake_Spruce_Tree 19.76 13 | 14 | Jeremiah Sledge 15 | 407 Turnip Ave 16 | Visa 9876 17 | 18 | 1 14'_Fiberglass_Ladder 88.75 19 | 1 Rubber_Gloves 3.97 20 | 21 | John 22 | 117 Crow's Nest Pass 23 | MasterCard 1231 24 | 25 | 1 1/2L_Oil 16.78 26 | 27 | Kim Shaunders 28 | 1045 2nd St 29 | Visa 4321 30 | 31 | 3 Flamingos 4.98 32 | 1 Deluxe_BBQ 199.98 33 | 34 | Luigi Fastenzza 35 | 28 Sycamore Rd 36 | Amex 4567 37 | 38 | 1 Pipe_Wrench 38.89 39 | 2 1-1/2"_Fittings 3.65 40 | 2 Gold_Buttons 14.77 41 | 1 Mustache_Comb 2.6 42 | 50 Zip_Ties 0.34 43 | 44 | Matthew Markus 45 | 22 Mahonee Dr 46 | Amex 7654 47 | 48 | 1 1/2_G_White_Paint 23.66 49 | 1 1/2_G_Purple_Paint 21.5 50 | 1 1/2_G_Olive_Paint 21.5 51 | 1 Paint_Roller 2.67 52 | 53 | Rob Bothouse 54 | 557 Omega Way 55 | Cash 56 | 57 | 4 9W_LED_Bulbs 9.87 58 | 2 13W_LED_Par_Bulbs 12.2 59 | 2 13W_LED_Par_Bulbs_Soft 12.2 60 | 61 | Sammy Swanson 62 | 29 Aspen Dr 63 | MasterCard 5678 64 | 65 | 2 50x1_1/2_screws 6.87 66 | 1 Hammer 28.74 67 | 1 Leather_Gloves 8.99 68 | 1 Safety_Glasses 2.99 69 | 1 Knee_pads 5.47 70 | 1 Hard_hat 9.99 71 | 72 | Tom Patrick 73 | 13 Chesterfield Way 74 | MasterCard 1234 75 | 76 | 1 Hacksaw 14.54 77 | 5 Rolls_of_Tape 1.35 78 | 1 Bag_of_Popcorn 0.99 79 | 80 | -------------------------------------------------------------------------------- /chapter21/output_orders_2.txt: -------------------------------------------------------------------------------- 1 | Alan Woke 2 | 1 Lonely Island Dr 3 | Visa 9874 4 | 5 | 1 Bag_Popcorn 1.99 6 | 7 | Christie Crisp 8 | 511 Honeydew Lane 9 | MasterCard 6543 10 | 11 | 1 Fire_Extinguisher 23.89 12 | 13 | Francis Underwood 14 | 1650 White Rd 15 | Visa 7654 16 | 17 | 1 Spade_Shovel 12.99 18 | 1 50'_Roll_Poly 28.76 19 | 20 | Jaina Proudly 21 | 345 Swamp Meadow Lane 22 | Visa 3838 23 | 24 | 1 Roll_Masking_Tape 2.34 25 | 1 Bottle_Peroxide 6.55 26 | 1 Trident_Gum 0.98 27 | 2 Rolls_Aluminum_Foil 4.33 28 | 29 | Jeremiah Sledge 30 | 407 Turnip Ave 31 | Visa 9876 32 | 33 | 1 14'_Fiberglass_Ladder 88.75 34 | 1 Rubber_Gloves 3.97 35 | 36 | Jillian Jacobs 37 | 97 Juniper Cres 38 | Amex 8877 39 | 40 | 2 Leather_Gloves 8.99 41 | 1 Medium_Axe 12.87 42 | 5 Furnace_Filters 16.99 43 | 44 | Matthew Markus 45 | 22 Mahonee Dr 46 | Amex 7654 47 | 48 | 1 1/2_G_White_Paint 23.66 49 | 1 1/2_G_Purple_Paint 21.5 50 | 1 1/2_G_Olive_Paint 21.5 51 | 1 Paint_Roller 2.67 52 | 53 | Sammy Swanson 54 | 29 Aspen Dr 55 | MasterCard 5678 56 | 57 | 2 50x1_1/2_screws 6.87 58 | 1 Hammer 28.74 59 | 1 Leather_Gloves 8.99 60 | 1 Safety_Glasses 2.99 61 | 1 Knee_pads 5.47 62 | 1 Hard_hat 9.99 63 | 64 | Sister Catherine 65 | 32 Holy Redeemer Blvd 66 | MasterCard 5678 67 | 68 | 6 Yard_Sticks 5.87 69 | 70 | Sonya Sommes 71 | 1034 Swamp Meadow Lane 72 | Cash 73 | 74 | 1 Lava_Lamp 13.87 75 | 4 Drink_Coasters 0.99 76 | 1 Electric_Drill 165.44 77 | 78 | -------------------------------------------------------------------------------- /chapter21/output_story.txt: -------------------------------------------------------------------------------- 1 | Today we moved our 4 year old son out of his toddler bed to his own double bed in a separate room. We bought a double because we currently don't have an extra bed for company should someone need to sleep over due to weather or excessive drunken-ness. As of this moment, we have said our goodnights and are waiting to see if he falls asleep or sneaks out with another negotiation attempt. My wife started up a final load of laundry so I'm unable to hear what's happening in his room, if he's talking to himself, jumping on the bed, or, I fear, crying. It's not easy but he's a big boy now and he was nearly touching either end of his toddler bed as it was. Hopefully he's able to sleep the whole night but if not I am prepared to spend the night beside him should he arrive at our bedside full of sadness or fear. -------------------------------------------------------------------------------- /chapter21/x_copy_if.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 23 Scratch - std::copy_if() use 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main() 13 | try { 14 | std::array ai { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 15 | 16 | std::vector vd (ai.size()); 17 | 18 | auto it = std::copy_if(ai.begin(), ai.end(), vd.begin(), 19 | [](int x) { return x % 2; }); 20 | 21 | vd.resize(std::distance(vd.begin(), it)); 22 | 23 | for (const auto& d : vd) 24 | std::cout << d << ' '; 25 | std::cout << '\n'; 26 | } 27 | catch(std::exception& e) { 28 | std::cerr << "Exception: " << e.what() << '\n'; 29 | return 1; 30 | } 31 | catch(...) { 32 | std::cerr << "Unknown exception\n"; 33 | return 2; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /chapter21/x_file_copy.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 21 Scratch - playing with istream & ostream iterators. 5 | // 6 | // I wanted to see what the results of piping an istream iterator into an 7 | // ostream iterator would be. Since writing to and from stream iterators is 8 | // like using '>>' and '<<' the resulting output file was devoid of whitespace. 9 | // 10 | // Adding a separator doesn't fix the issue as you only get THAT type of ws. 11 | // It seems the only way would be to use std::getline and append '\n' to the 12 | // end of each read. 13 | // 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | int main() 21 | try { 22 | std::cout << "enter source and destination files:\n"; 23 | std::string src, dest; 24 | std::cin >> src >> dest; 25 | 26 | std::ifstream ifs {src}; 27 | std::ofstream ofs {dest}; 28 | 29 | std::istream_iterator ii {ifs}; 30 | std::istream_iterator eos; 31 | std::ostream_iterator oo {ofs, " "}; 32 | 33 | std::copy(ii, eos, oo); 34 | } 35 | catch(std::exception& e) { 36 | std::cerr << "Exception: " << e.what() << '\n'; 37 | return 1; 38 | } 39 | catch(...) { 40 | std::cerr << "Unknown exception\n"; 41 | return 2; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /chapter23/ch23_drill.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 23 - Regex Zip Code Example 5 | // 6 | // Becomes the Drill for the chapter 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | int main() 18 | try { 19 | std::ifstream in {"input_zip_codes.txt"}; 20 | if (!in) std::cerr << "no file\n"; 21 | 22 | std::regex pat {R"(\w{2}\s*\d{5}(-\d{4})?)"}; 23 | 24 | int lineno = 0; 25 | for (std::string line; std::getline(in,line); ) { 26 | ++lineno; 27 | std::smatch matches; 28 | if (std::regex_search(line, matches, pat)) { 29 | std::cout << lineno << ": " << matches[0] << '\n'; 30 | if (1 11 | #include 12 | #include 13 | #include 14 | 15 | int main() 16 | try { 17 | std::string test = "\n"; 18 | 19 | std::regex pat {R"(.)"}; 20 | 21 | if (std::regex_search(test,pat)) 22 | std::cout << "Dot matches newline!\n"; 23 | else 24 | std::cout << "Nope!\n"; 25 | } 26 | catch(std::exception& e) { 27 | std::cerr << "Exception: " << e.what() << '\n'; 28 | return 1; 29 | } 30 | catch(...) { 31 | std::cerr << "Unknown exception\n"; 32 | return 2; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /chapter23/ex15-16_non-pattern.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 23 Exercise 15 5 | // 6 | // Describe a pattern that cannot be expressed as a regular expression. 7 | // - This is weird. I was able to reach the catch clause in the pattern 8 | // generating exercises with extra '\'s but is that a pattern? No, it was 9 | // always a mis-typed character. 10 | // 11 | // Exercise 16 12 | // 13 | // Prove that the pattern found in the previous exercise really isn't a regular 14 | // expression. 15 | // - the escape character alone isn't really a regex as the rules state that a 16 | // single backslash needs to be escaped itself if we want to try to match it or 17 | // it shall preceed a special character to denote it's special-ness. 18 | // 19 | // Not sure if the above answers are what Bjarne was going for but there you 20 | // go. 21 | // 22 | 23 | #include 24 | #include 25 | 26 | int main() 27 | try { 28 | // code 29 | } 30 | catch(std::exception& e) { 31 | std::cerr << "Exception: " << e.what() << '\n'; 32 | return 1; 33 | } 34 | catch(...) { 35 | std::cerr << "Unknown exception\n"; 36 | return 2; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /chapter23/input_cards.txt: -------------------------------------------------------------------------------- 1 | Is this: 1234567890123456 a cc number? 2 | How about this: 1234-129 53rd St? 3 | One from the website: 5304102643217066 4 | From the Luhn example: 4556737586899855 5 | Here's a whole bunch from the website: 6 | 5375156078687270 7 | 5112042430525234 8 | 2720993542079265 9 | 4532166054649312 10 | 4485465292178978 11 | 4539987943806680964 12 | 6011621169117451 13 | 6011441626735823 14 | 6011180914225354245 15 | 346322381363101 16 | 379674892190435 17 | 345710245361025 18 | 19 | -------------------------------------------------------------------------------- /chapter23/input_dates.txt: -------------------------------------------------------------------------------- 1 | I remember 2/5/1997 like it never happened 2 | I remember 12/24/2008 like it was yesterday.. 3 | Tues Feb 27 2018 was my birthday again! 4 | 9Tues, May 5 2004 was ruined by a 9 5 | It was the winter cold that wore me on December 16, 21050 6 | Sometimes 30 26 36 1/2 is enough. 7 | Thurs, Feb 27 1997 was a different time.. 8 | Once, on August 28, 1963, I had a dream. 9 | -------------------------------------------------------------------------------- /chapter23/input_dates_2.txt: -------------------------------------------------------------------------------- 1 | "Please state your name for the court, and your date of birth, in mm/dd/yyyy format." 2 | 3 | "My name is Connie Watt, I was born 10/13/1981." 4 | 5 | "Thank you, and what was your relationship to the victim?" 6 | 7 | "Desmond was a very dear friend." 8 | 9 | "Miss Watt, please tell the court where you were on the night of 12/31/2017." 10 | 11 | Connie thought for a moment. Mr Schweiber had instructed her to take her time to think before each answer but this was not an exercise in calmness. It was 02/16/2018 today, a month and a half since that fateful New Years Eve. She knew where she WAS on that day but could not remember the name of the building.. 12 | 13 | Dammit.. what was it.. called again? 14 | 15 | "The Staples Room in the Fairmont Hotel. In the evening." 16 | 17 | She barely remembered saying the words. She knew but she didn't. 18 | 19 | "And when did you meet Mr Carlin" 20 | 21 | "A long time ago, but I can tell you the date, 11/15/2001, the date of the original XBOX launch." 22 | -------------------------------------------------------------------------------- /chapter23/input_mail.txt: -------------------------------------------------------------------------------- 1 | xxx 2 | xxx 3 | ---- 4 | From: John Doe 5 | To: Mary Smith 6 | Subject: Saying Hello 7 | Date: Fri, 21 Nov 1997 09:55:06 -0600 8 | Message-ID: <1234@local.machine.example> 9 | 10 | This is a message just to say hello. 11 | So, "Hello". 12 | ---- 13 | From: Joe Q. Public 14 | To: Mary Smith <@machine.tld:mary@example.net>, , jdoe@test .example 15 | Date: Tue, 1 Jul 2003 10:52:37 +0200 16 | Message-ID: <5678.21-Nov-1997@example.com> 17 | 18 | Hi everyone. 19 | ---- 20 | To: "Mary Smith: Personal Account" 21 | From: John Doe 22 | Subject: Re: Saying Hello 23 | Date: Fri, 21 Nov 1997 11:00:00 -0600 24 | Message-ID: 25 | In-Reply-To: <3456@example.net> 26 | References: <1234@local.machine.example> <3456@example.net> 27 | 28 | This is a reply to your reply. 29 | ---- 30 | ---- 31 | -------------------------------------------------------------------------------- /chapter23/input_table.txt: -------------------------------------------------------------------------------- 1 | KLASSE ANTAL DRENGE ANTAL PIGER ELEVER IALT 2 | 0A 12 11 23 3 | 1A 7 8 15 4 | 1B 4 11 15 5 | 2A 10 13 23 6 | 3A 10 12 22 7 | 4A 7 7 14 8 | 4B 10 5 15 9 | 5A 19 8 27 10 | 6A 10 9 19 11 | 6B 9 10 19 12 | 7A 7 19 26 13 | 7G 3 5 8 14 | 7I 7 3 10 15 | 8A 10 16 26 16 | 9A 12 15 27 17 | 0MO 3 2 5 18 | 0P1 1 1 2 19 | 0P2 0 5 5 20 | 10B 4 4 8 21 | 10CE 0 1 1 22 | 1MO 8 5 13 23 | 2CE 8 5 13 24 | 3DCE 3 3 6 25 | 4MO 4 1 5 26 | 6CE 3 4 7 27 | 8CE 4 4 8 28 | 9CE 4 9 13 29 | REST 5 6 11 30 | Alle klasser 184 202 386 31 | -------------------------------------------------------------------------------- /chapter23/input_zip_codes.txt: -------------------------------------------------------------------------------- 1 | address TX77845 2 | ffff tx 77843 asasasasaa 3 | ggg TX3456-23456 4 | howdy 5 | zzz TX23456-3456sss ggg TX33456-1234 6 | cvzcv TX77845-1234 sdsas 7 | xxxTx77845xxx 8 | TX12345-123456 9 | -------------------------------------------------------------------------------- /chapter23/output_dates.txt: -------------------------------------------------------------------------------- 1 | "Please state your name for the court, and your date of birth, in mm/dd/yyyy format." 2 | 3 | "My name is Connie Watt, I was born 1981-10-13." 4 | 5 | "Thank you, and what was your relationship to the victim?" 6 | 7 | "Desmond was a very dear friend." 8 | 9 | "Miss Watt, please tell the court where you were on the night of 2017-12-31." 10 | 11 | Connie thought for a moment. Mr Schweiber had instructed her to take her time to think before each answer but this was not an exercise in calmness. It was 2018-02-16 today, a month and a half since that fateful New Years Eve. She knew where she WAS on that day but could not remember the name of the building.. 12 | 13 | Dammit.. what was it.. called again? 14 | 15 | "The Staples Room in the Fairmont Hotel. In the evening." 16 | 17 | She barely remembered saying the words. She knew but she didn't. 18 | 19 | "And when did you meet Mr Carlin" 20 | 21 | "A long time ago, but I can tell you the date, 2001-11-15, the date of the original XBOX launch." 22 | -------------------------------------------------------------------------------- /chapter23/x_test.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 23 - Regex testing 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | int main() 14 | try { 15 | /* 16 | std::regex reg1 {"\\s\\w{3}\\s"}; 17 | std::regex reg2 {R"(\s\w{3}\s)"}; 18 | std::regex reg3; 19 | 20 | std::string pat = "\\s\\w{3}\\s"; 21 | reg3 = pat; 22 | 23 | std::string test = "bake cab cabbage"; 24 | std::smatch matches; 25 | 26 | if (std::regex_search(test,matches,reg1)) 27 | for (int i = 0; i < matches.size(); ++i) 28 | std::cout << matches[i] << '\n'; 29 | */ 30 | 31 | std::regex floater {R"(\d+\.\d+)"}; 32 | 33 | std::string test2 = "You owe me $4.37, pal!"; 34 | std::smatch matches2; 35 | if (std::regex_search(test2,matches2,floater)) 36 | std::cout << matches2[0] << '\n'; 37 | 38 | std::stringstream ss; 39 | ss << matches2[0]; 40 | double res; 41 | ss >> res; 42 | 43 | std::cout << "Double: " << res << '\n'; 44 | } 45 | catch(std::exception& e) { 46 | std::cerr << "Exception: " << e.what() << '\n'; 47 | return 1; 48 | } 49 | catch(...) { 50 | std::cerr << "Unknown exception\n"; 51 | return 2; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /chapter24/ch24_tt1_precision.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 24 Try This - Precision 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main() 12 | try { 13 | const float divisor = 333; 14 | float x = 1.0/divisor; 15 | float sum = 0; 16 | for (int i=0; i 8 | #include 9 | 10 | void f(int i, double fpd) 11 | { 12 | char c = i; // yes: chars really are very small integers 13 | short s = i; // beware: an int may not fit in a short int 14 | i = i + 1; // what if i was the largest int? 15 | long lg = i * i; // beware: a long may not be any larger than an int 16 | float fps = fpd; // beware: a large double may not fit in a float 17 | i = fpd; // truncates: eg., 5.7 -> 5 18 | fps = i; // you can lose precision (for very large int values) 19 | 20 | std::cout << "c: " << c << " s: " << s << " i: " << i << '\n'; 21 | } 22 | 23 | void g() 24 | { 25 | char ch = 0; 26 | for (int i = 0; i < 500; ++i) 27 | std::cout << int(ch++) << '\t'; 28 | } 29 | 30 | int main() 31 | try { 32 | //g(); 33 | f(52, 3.14159); 34 | f(2000000, 5.5); 35 | } 36 | catch(std::exception& e) { 37 | std::cerr << "Exception: " << e.what() << '\n'; 38 | return 1; 39 | } 40 | catch(...) { 41 | std::cerr << "Unknown exception\n"; 42 | return 2; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /chapter24/ex03_boost_bind.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 24 Exercise 3 5 | // 6 | // Write an apply(f,a) that can take a void (T&), a T (const T&) and their 7 | // function object equivalents. 8 | // 9 | // This is indicated to be "Expert only" so I'll put this on the back-burner 10 | // and potentially use it when I look at Boost more closely after this book. 11 | // 12 | 13 | #include 14 | #include 15 | 16 | int main() 17 | try { 18 | // code 19 | } 20 | catch(std::exception& e) { 21 | std::cerr << "Exception: " << e.what() << '\n'; 22 | return 1; 23 | } 24 | catch(...) { 25 | std::cerr << "Unknown exception\n"; 26 | return 2; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /chapter24/ex08_animated.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 24 Exercise 8 5 | // 6 | // Animate the Gaussian elimination. 7 | // 8 | // I'll come back to this after my overhaul of the GUI support library. 9 | // 10 | 11 | #include 12 | #include 13 | 14 | int main() 15 | try { 16 | // code 17 | } 18 | catch(std::exception& e) { 19 | std::cerr << "Exception: " << e.what() << '\n'; 20 | return 1; 21 | } 22 | catch(...) { 23 | std::cerr << "Unknown exception\n"; 24 | return 2; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /chapter24/x_math_errno.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 24 Scratch - errno 5 | // 6 | // This was an attempt to see how errno worked but for whatever reason math 7 | // errors are not setting errno, it remains 0. 8 | // 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main() 16 | try { 17 | errno = 0; 18 | double bad_num = sqrt(-1.0); 19 | if (errno == EDOM) { 20 | std::cout << "sqrt(-1) failed: " << std::strerror(errno) << '\n'; 21 | } 22 | std::cout << errno << ' ' << bad_num << '\n'; 23 | } 24 | catch(std::exception& e) { 25 | std::cerr << "Exception: " << e.what() << '\n'; 26 | return 1; 27 | } 28 | catch(...) { 29 | std::cerr << "Unknown exception\n"; 30 | return 2; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /chapter25/TEA_output.txt: -------------------------------------------------------------------------------- 1 | The Tiny Encryption Algorithm is one of the fastest and most efficient cryptographic algorithms in existence. 2 | It was developed by David Wheeler and Roger Needham at the Computer Laboratory of Cambridge University. 3 | It is a Feistel cipher which uses operations from mixed (orthogonal) algebraic groups - XOR, ADD and SHIFT in this case. 4 | This is a very clever way of providing Shannon's twin properties of diffusion and confusion which are 5 | necessary for a secure block cipher, without the explicit need for P-boxes and S-boxes respectively. 6 | It encrypts 64 data bits at a time using a 128-bit key. It seems highly resistant to differential 7 | cryptanalysis, and achieves complete diffusion (where a one bit difference in the plaintext will 8 | cause approximately 32 bit differences in the ciphertext) after only six rounds. Performance on a 9 | modern desktop computer or workstation is very impressive. 10 | 000000 -------------------------------------------------------------------------------- /chapter25/ch25_tt2_bits_out.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 25 Try This - Bit output 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main() 12 | try { 13 | for (unsigned i; std::cin >> i; ) 14 | std::cout << std::dec << i << " == " 15 | << std::hex << "0x" << i << " == " 16 | << std::bitset<8*sizeof(unsigned)>{i} << '\n'; 17 | } 18 | catch(std::exception& e) { 19 | std::cerr << "Exception: " << e.what() << '\n'; 20 | return 1; 21 | } 22 | catch(...) { 23 | std::cerr << "Unknown exception\n"; 24 | return 2; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /chapter25/ch25_tt3_infinte.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 25 Try This - Infinite loop 5 | // 6 | 7 | #include 8 | #include 9 | 10 | void infinite() 11 | // i will never reach 160 because the largest positive value of a signed 12 | // char is 127 at which point it rolls over to -128 and counts back to 0. 13 | { 14 | unsigned char max = 160; // very large 15 | for (signed char i = 0; i < max; ++i) std::cout << int(i) << '\n'; 16 | } 17 | 18 | int main() 19 | try { 20 | infinite(); 21 | } 22 | catch(std::exception& e) { 23 | std::cerr << "Exception: " << e.what() << '\n'; 24 | return 1; 25 | } 26 | catch(...) { 27 | std::cerr << "Unknown exception\n"; 28 | return 2; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /chapter25/ex05_infinite.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 25 Exercise 5 - Infinite loop 5 | // 6 | 7 | #include 8 | #include 9 | 10 | int main() 11 | try { 12 | while(true) { 13 | std::cout << "here I am, doin stuff.. infinitely...\n"; 14 | } 15 | } 16 | catch(std::exception& e) { 17 | std::cerr << "Exception: " << e.what() << '\n'; 18 | return 1; 19 | } 20 | catch(...) { 21 | std::cerr << "Unknown exception\n"; 22 | return 2; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /chapter25/ex06_tricky_infinite.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 25 Exercise 6 - Infinite loop w/resource termination 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | struct Node { 14 | int* pi; 15 | double* pd; 16 | std::complex* pcd; 17 | std::string ss; 18 | }; 19 | 20 | Node* recurse_forever(std::vector& v, unsigned long n) 21 | { 22 | std::cout << n << '\n'; 23 | v.push_back(recurse_forever(v, n + 1)); 24 | 25 | int x = 37; 26 | double y = 2.718281828; 27 | std::complex z {sqrt(3), sqrt(n)}; 28 | std::string s {"Let's exhaust some stack memory!"}; 29 | 30 | return new Node{&x, &y, &z, s}; 31 | } 32 | 33 | int main() 34 | try { 35 | std::vector vn; 36 | recurse_forever(vn, 1); 37 | } 38 | catch(std::exception& e) { 39 | std::cerr << "Exception: " << e.what() << '\n'; 40 | return 1; 41 | } 42 | catch(...) { 43 | std::cerr << "Unknown exception\n"; 44 | return 2; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /chapter25/ex09_system_info.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 25 Exercise 9 5 | // 6 | // Compute the number of bits in an int and determine whether char is signed 7 | // or unsigned. 8 | // 9 | 10 | #include 11 | #include 12 | 13 | int main() 14 | try { 15 | std::cout << "Bits in an int: " << 8 * sizeof(int) << '\n'; 16 | 17 | char c = -1; 18 | 19 | std::cout << "Is char signed? " << int(c) << '\n'; 20 | } 21 | catch(std::exception& e) { 22 | std::cerr << "Exception: " << e.what() << '\n'; 23 | return 1; 24 | } 25 | catch(...) { 26 | std::cerr << "Unknown exception\n"; 27 | return 2; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /chapter26/ch26_Test.h: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 26 Test Framework 5 | // 6 | 7 | #ifndef TEST_H 8 | #define TEST_H 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | struct Test { 15 | std::string label; 16 | int val; 17 | std::vector seq; 18 | bool res; 19 | }; 20 | 21 | std::istream& operator>>(std::istream& is, Test& t) 22 | { 23 | char ch1, ch2; 24 | std::string lab; 25 | int val; 26 | is >> ch1 >> lab >> val >> ch2; 27 | 28 | if (!is || (ch1 != '{' || ch2 != '{')) 29 | return is; 30 | 31 | std::vector vi; 32 | for (int v; is >> v; ) 33 | vi.push_back(v); 34 | 35 | is.clear(); 36 | 37 | bool result; 38 | is >> ch1 >> result >> ch2; 39 | if (!is) 40 | return is; 41 | 42 | t.label = lab; 43 | t.val = val; 44 | t.seq = vi; 45 | t.res = result; 46 | 47 | return is; 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /chapter26/ch26_my_binary.h: -------------------------------------------------------------------------------- 1 | // 2 | // Stroustrup - Programming Principles & Practice 3 | // 4 | // Chapter 26 My Binary Search 5 | // 6 | 7 | #ifndef MY_BINARY_SEARCH_H 8 | #define MY_BINARY_SEARCH_H 9 | 10 | template 11 | bool my_binary(Iter first, Iter last, const T& val) 12 | { 13 | if (first == last) return false; // empty sequence 14 | 15 | while (first <= last) { 16 | Iter mid = first + (std::distance(first, last) / 2); 17 | 18 | if (val == *mid) 19 | return true; 20 | else if (val < *mid) 21 | last = --mid; 22 | else 23 | first = ++mid; 24 | } 25 | 26 | return false; 27 | } 28 | 29 | template 30 | bool my_binary(Iter first, Iter last, const T& val, BinOp cmp) 31 | { 32 | if (first == last) return false; 33 | 34 | while (first <= last) { 35 | Iter mid = first + (std::distance(first, last) / 2); 36 | 37 | if (val == *mid) 38 | return true; 39 | else if (cmp(val, *mid)) 40 | last = --mid; 41 | else 42 | first = ++mid; 43 | } 44 | 45 | return false; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /chapter26/drill_3_c.txt: -------------------------------------------------------------------------------- 1 | { random_elements_0 0 { } 0 } 2 | { random_elements_1 3 { 3 } 1 } 3 | { random_elements_2 1 { 0 3 } 0 } 4 | { random_elements_3 8 { 5 6 8 } 1 } 5 | { random_elements_4 9 { 0 4 6 9 } 1 } 6 | { random_elements_5 3 { 3 7 9 11 11 } 1 } 7 | { random_elements_6 0 { 5 8 8 8 9 12 } 0 } 8 | { random_elements_7 10 { 0 1 3 4 8 10 15 } 1 } 9 | { random_elements_8 16 { 3 5 9 12 13 17 20 24 } 0 } 10 | { random_elements_9 8 { 2 4 5 7 8 10 10 12 17 } 1 } 11 | -------------------------------------------------------------------------------- /chapter26/test_b_float.txt: -------------------------------------------------------------------------------- 1 | { 1.1 2.2 { 1.1 2.2 3.3 5.5 8.8 13.13 21.21 } 1 } 2 | { 2.1 1.1 { } 0 } 3 | { 2.1 0.0 { } 0 } 4 | { 3.1 1.1 { 1.1 } 1 } 5 | { 3.1 0 { 1.1 } 0 } 6 | { 4.1 3.3 { 1.1 2.2 3.3 4.4 } 1 } 7 | { 4.2 5.5 { 1.1 2.2 3.3 4.4 } 0 } 8 | { 5.1 3.3 { 1.1 2.2 3.3 4.4 5.5 } 1 } 9 | { 5.2 0.0 { 1.1 2.2 3.3 4.4 5.5 } 0 } 10 | -------------------------------------------------------------------------------- /chapter26/test_b_reading.txt: -------------------------------------------------------------------------------- 1 | { 1 1.1 } 2 | { 2 0.0 } 3 | { 3 -1.1 } 4 | { 4 -1.4 } 5 | { 5 -1.7 } 6 | { 6 -2.2 } 7 | { 7 -1.3 } 8 | { 8 2.2 } 9 | { 9 4.8 } 10 | { 10 7.1 } 11 | { 11 9.2 } 12 | { 12 11.7 } 13 | -------------------------------------------------------------------------------- /chapter26/test_b_search.txt: -------------------------------------------------------------------------------- 1 | { 1.1 2 { 1 2 3 5 8 13 21 } 1 } 2 | { 1.2 5 { 1 2 3 5 8 13 21 } 1 } 3 | { 1.3 8 { 1 2 3 5 8 13 21 } 1 } 4 | { 1.4 21 { 1 2 3 5 8 13 21 } 1 } 5 | { 1.5 4 { 1 2 3 5 8 13 21 } 0 } 6 | { 1.6 -7 { 1 2 3 5 8 13 21 } 0 } 7 | { 2.1 1 { } 0 } 8 | { 2.1 0 { } 0 } 9 | { 3.1 1 { 1 } 1 } 10 | { 3.1 0 { 1 } 0 } 11 | { 4.1 3 { 1 2 3 4 } 1 } 12 | { 4.2 5 { 1 2 3 4 } 0 } 13 | { 5.1 3 { 1 2 3 4 5 } 1 } 14 | { 5.2 0 { 1 2 3 4 5 } 0 } 15 | { 6.1 1 { 1 1 1 1 1 1 1 1 } 1 } 16 | { 6.2 -1 { 1 1 1 1 1 1 1 1 } 0 } 17 | { 7.1 0 { 0 1 1 1 1 1 1 1 1 1 1 } 1 } 18 | { 7.2 1 { 0 1 1 1 1 1 1 1 1 1 1 } 1 } 19 | { 7.3 2 { 0 1 1 1 1 1 1 1 1 1 1 } 0 } 20 | { 8.1 0 { 0 0 0 0 0 0 0 0 0 0 1 } 1 } 21 | { 8.2 1 { 0 0 0 0 0 0 0 0 0 0 1 } 1 } 22 | { 8.3 -1 { 0 0 0 0 0 0 0 0 0 0 1 } 0 } 23 | -------------------------------------------------------------------------------- /chapter26/test_b_string.txt: -------------------------------------------------------------------------------- 1 | { 1.1 Darwin { Bohr Darwin Einstein Lavoisier Newton Stroustrup Turing } 1 } 2 | { 1.2 Lavoisier { Bohr Darwin Einstein Lavoisier Newton Stroustrup Turing } 1 } 3 | { 1.3 Newton { Bohr Darwin Einstein Lavoisier Newton Stroustrup Turing } 1 } 4 | { 1.4 Turing { Bohr Darwin Einstein Lavoisier Newton Stroustrup Turing } 1 } 5 | { 1.5 Knuth { Bohr Darwin Einstein Lavoisier Newton Stroustrup Turing } 0 } 6 | { 1.6 Meyers { Bohr Darwin Einstein Lavoisier Newton Stroustrup Turing } 0 } 7 | { 2.1 Sutter { } 0 } 8 | { 2.1 Einstein { } 0 } 9 | { 3.1 Turing { Turing } 1 } 10 | { 3.1 Newton { Turing } 0 } 11 | -------------------------------------------------------------------------------- /chapter27/ch27_drill.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Stroustrup - Programming Principles & Practice 3 | * 4 | * Chapter 27 Drill 5 | * 6 | * 1. Write the Hello World in C 7 | * 8 | * 2. Define variables for Hello and World and concatenate them to output. 9 | * Using the provided cat() function felt like cheating so I used printf with 10 | * arguments. 11 | * 12 | * 3. Define a C function that takes a char* and an int and prints their values 13 | * in provided format. 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | void print_out(const char* p, const int x) 20 | { 21 | printf("p is\"%s\" and x is %i\n", p, x); 22 | } 23 | 24 | int main() 25 | { 26 | /* Ex 1 */ 27 | printf("Hello World!\n"); 28 | 29 | /* Ex 2 */ 30 | char* first = "Hello"; 31 | char* last = "World!"; 32 | printf("%s %s\n", first, last); 33 | 34 | /* Ex 3 */ 35 | print_out("May", 6); 36 | print_out("Chris", 39); 37 | print_out("C", 89); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /chapter27/ch27_tt1-2_cat_cpy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Stroustrup - Programming Principles & Practice 3 | * 4 | * Chapter 27 Try This - cat & bs_strcpy 5 | * 6 | * Test cat(). Fix errors. 7 | * 8 | * Test bs_strcpy implementation and explain. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | char* bs_strcpy(char* p, const char* q) 16 | /* Assign the char that q points to to p and increment both p and q. 17 | * Do this as long as the value assigned to p is not the null terminator. 18 | */ 19 | { 20 | while ((*p++ = *q++)); 21 | return p; 22 | } 23 | 24 | char* cat(const char* id, const char* addr) 25 | { 26 | int sz = strlen(id) + strlen(addr) + 2; /* sz = id + addr + 2 (@,\0) */ 27 | char* res = (char*) malloc(sz); /* allocate sz bytes */ 28 | bs_strcpy(res,id); /* copy id into res */ 29 | res[strlen(id)] = '@'; /* replace nullchar with @ */ 30 | bs_strcpy(res + strlen(id) + 1, addr); /* copy addr into res after @ */ 31 | /*res[sz-1] = 0; should not be necessary, we have addr's already */ 32 | return res; 33 | } 34 | 35 | int main() 36 | { 37 | printf("Hello C89!\n"); 38 | char* email = cat("chrinkus", "hotmail"); 39 | printf("%s\n", email); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /chapter27/ex09_string_qsort.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Stroustrup - Programming Principles & Practice 3 | * 4 | * Chapter 27 Exercise 9 5 | * 6 | * Read a sequence of words from stdin and write them to stdout in 7 | * lexicographical order. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #define MAX_COUNT 10 /* number of strings to read in */ 15 | #define MAX_SIZE 30 /* largest expected string */ 16 | 17 | int cmp_string(const void* a, const void* b) 18 | { 19 | return strcmp(*(const char**)a, *(const char**)b); 20 | } 21 | 22 | int main() 23 | { 24 | char* strings[MAX_COUNT]; 25 | int count, i, c; 26 | 27 | printf("ENTER 10 WORDS TO SORT:\n"); 28 | for (count = 0; count < MAX_COUNT; ++count) { 29 | char* buf = (char*)malloc(MAX_SIZE); 30 | 31 | for (i = 0; (c = getchar()) && c != '\n'; ++i) 32 | buf[i] = c; 33 | buf[i] = '\0'; 34 | 35 | strings[count] = buf; 36 | } 37 | 38 | qsort(strings, MAX_COUNT, sizeof(char*), cmp_string); 39 | 40 | printf("SORTED WORDS:\n"); 41 | for (i = 0; i < MAX_COUNT; ++i) 42 | printf("%s\n", strings[i]); 43 | } 44 | -------------------------------------------------------------------------------- /general_notes/compiler_flags.txt: -------------------------------------------------------------------------------- 1 | Compiler Flags/Settings 2 | ======================= 3 | 4 | Graph_lib compiler command: 5 | 6 | g++ :compiler (gcc.gnu.org) 7 | -w :inhibit all warning messages 8 | -Wall :all W-warnings options enabled 9 | -std=c++11 :determines the language standard 10 | ../GUI/Graph.cpp :source file 11 | ../GUI/Window.cpp :source file 12 | ../GUI/GUI.cpp :source file 13 | ../GUI/Simple_window.cpp :source file 14 | ch15_tools.cpp :source file 15 | ex09_can_rivers.cpp :source file w/main() 16 | `fltk-config :pkg-config for compiling libraries 17 | --ldflags 18 | --use-images` 19 | -o :place the output of compilation in following file 20 | a.out 21 | 22 | 23 | -------------------------------------------------------------------------------- /images/apple-logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chrinkus/stroustrup-ppp/ea9e85d4ea9890038eb5611c3bc82734c8706ce7/images/apple-logo.jpg -------------------------------------------------------------------------------- /images/cpp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chrinkus/stroustrup-ppp/ea9e85d4ea9890038eb5611c3bc82734c8706ce7/images/cpp.jpg -------------------------------------------------------------------------------- /scratch/accum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int evens(int x, int y) { return y % 2 == 0 ? x + y : x; } 6 | 7 | struct Mults_3x10 { 8 | int m = 10; 9 | int operator()(int x, int y) { return y % 3 == 0 ? x + y * m : x; } 10 | } m3; 11 | 12 | int main() 13 | { 14 | int num_arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 15 | std::vector num_vec { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 16 | 17 | std::cout << "default accumulate: "; 18 | std::cout << std::accumulate(num_arr, num_arr+10, 0); 19 | std::cout << '\n'; 20 | 21 | std::cout << "vector accumulate: "; 22 | std::cout << std::accumulate(num_vec.begin(), num_vec.end(), 0); 23 | std::cout << '\n'; 24 | 25 | std::cout << "vector evens: "; 26 | std::cout << std::accumulate(num_vec.begin(), num_vec.end(), 0, evens); 27 | std::cout << '\n'; 28 | 29 | std::cout << "vector odds lambda: "; 30 | std::cout << std::accumulate(num_vec.begin(), num_vec.end(), 0, 31 | [](int x, int y) { return y % 2 != 0 ? x + y : x; }); 32 | std::cout << '\n'; 33 | 34 | std::cout << "vector mults of 3 struct: "; 35 | std::cout << std::accumulate(num_vec.begin(), num_vec.end(), 0, m3); 36 | std::cout << '\n'; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /scratch/angle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | const double PI = 3.14159; 9 | // atan2(y, x) 10 | cout << atan2(-8, 1) * 180 / PI << '\n'; 11 | cout << atan2(-5, -5) * 180 / PI << '\n'; 12 | cout << atan2(-2, 4) * 180 / PI << '\n'; 13 | cout << atan2(7, 2) * 180 / PI << '\n'; 14 | cout << atan2(0, 10) * 180 / PI << '\n'; 15 | } 16 | -------------------------------------------------------------------------------- /scratch/array_size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int ai[50] {1, 2, 3, 4, 5}; 6 | int* p = ai; 7 | 8 | std::cout << "Array ai = " << ai << " size of ai = " << sizeof(ai) << '\n'; 9 | std::cout << "Pointer p = " << p << " size of p = " << sizeof(p) << '\n'; 10 | 11 | int n = 100; 12 | for (int* pi = ai; pi < &ai[sizeof(ai) / sizeof(int)]; ++pi) 13 | *pi = n++; 14 | 15 | std::cout << "The last element in ai is: " << ai[49] << '\n'; 16 | 17 | char ch[100] {'a', 'b', 'c'}; 18 | char* pc = ch; 19 | 20 | std::cout << "pc: " << &pc << " size of p is " << sizeof(pc) << '\n'; 21 | } 22 | -------------------------------------------------------------------------------- /scratch/error.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | try { 8 | cout << "Try entering something.."; 9 | string input; 10 | cin >> input; 11 | 12 | if (input != "safe") throw runtime_error("Totally not safe"); 13 | 14 | cout << "Must have been safe!\n"; 15 | } 16 | catch(exception& e) { 17 | cout << "Error: " << e.what() << '\n'; 18 | return 1; 19 | } 20 | catch(...) { 21 | cout << "Exception: something unexpected went wrong\n"; 22 | return 2; 23 | } 24 | -------------------------------------------------------------------------------- /scratch/fib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | unsigned long long int fibonacciFinderN(int n) 6 | { 7 | unsigned long long int pre = 0; 8 | unsigned long long int cur = 1; 9 | unsigned long long int fib = 0; 10 | 11 | for (int i = 2; i < n; ++i) { 12 | fib = pre + cur; 13 | pre = cur; 14 | cur = fib; 15 | } 16 | 17 | return fib; 18 | } 19 | 20 | int main() 21 | { 22 | cout << fibonacciFinderN(300) << '\n'; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /scratch/math.cpp: -------------------------------------------------------------------------------- 1 | // sin example from cplusplus.com 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | constexpr double PI = 3.14159265; 8 | double param, result; 9 | param = 30.0; 10 | result = sin(param*PI/180); 11 | std::cout << "The sine of " << param << " degrees is " << result << '\n'; 12 | } 13 | -------------------------------------------------------------------------------- /scratch/nullptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | double* p0 = nullptr; 6 | 7 | if (p0) 8 | std::cout << "What does this actually prove?\n"; 9 | else 10 | std::cout << "Its a null pointer?\n"; 11 | } 12 | -------------------------------------------------------------------------------- /scratch/pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cout; 5 | 6 | int main() 7 | { 8 | cout << "Pointer fun:\n\n"; 9 | 10 | int x = 37; 11 | int* p1 = &x; 12 | 13 | cout << "address " << p1 << " contains " << *p1 << '\n' 14 | << "address " << &x << " contains " << x << '\n'; 15 | cout << '\n'; 16 | 17 | cout << "Array fun:\n\n"; 18 | 19 | int sz = 10; 20 | int* p2 = new int[sz]; 21 | 22 | for (int i = 0; i < sz; ++i) 23 | p2[i] = pow(2, i); 24 | 25 | int n = 7; 26 | cout << "Cherry picking " << n << "th element: \n" 27 | << "address " << p2 + (n-1) << " contains " << p2[n-1] << '\n'; 28 | 29 | int* p3 = p2; 30 | 31 | cout << "Array copying:\n"; 32 | for (int j = 0; j < sz; ++j) { 33 | cout << "address " << p2 + j << " contains " << p2[j] << '\n'; 34 | cout << "address " << p3 + j << " contains " << p3[j] << '\n'; 35 | } 36 | cout << '\n'; 37 | } 38 | -------------------------------------------------------------------------------- /scratch/scope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 8; 6 | 7 | { 8 | int a = 12; 9 | std::cout << a << '\n'; 10 | } 11 | 12 | std::cout << a << '\n'; 13 | } 14 | -------------------------------------------------------------------------------- /scratch/token.cpp: -------------------------------------------------------------------------------- 1 | #include "../text_lib/std_lib_facilities.h" 2 | 3 | class Token { 4 | public: 5 | char kind; 6 | double value; 7 | Token(char ch) 8 | :kind(ch), value(0) { } 9 | Token(char ch, double val) 10 | :kind(ch), value(val) { } 11 | }; 12 | 13 | int main() 14 | try { 15 | cout << "Define a Token: \n"; 16 | Token t {' '}; 17 | cin >> t.kind; 18 | 19 | if (cin) 20 | cin >> t.value; 21 | 22 | cout << "Token kind = " << t.kind << " Token value = " << t.value << '\n'; 23 | 24 | return 0; 25 | } 26 | catch (exception& e) { 27 | cerr << "Error " << e.what() << '\n'; 28 | return 1; 29 | } 30 | catch (...) { 31 | cerr << "I have no clue what just happened..\n"; 32 | return 2; 33 | } 34 | -------------------------------------------------------------------------------- /scratch/two_by.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | vector< vector > v; 9 | 10 | for (int k = 0; k < 4; ++k) 11 | for (int l = 0; l < 4; ++l) 12 | v[k].push_back(9); 13 | 14 | 15 | for (int i = 0; i < v.size(); ++i) 16 | for (int j = 0; j < v[i].size(); ++j) 17 | cout << i << '\t' << j << '\t' << v[i][j] << '\n'; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /scratch/unique_chars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | bool find_repeats(string s) 9 | { 10 | sort(s.begin(), s.end()); 11 | 12 | for (int i = 1; i < s.length(); ++i) 13 | if (s[i] == s[i - 1]) 14 | return true; 15 | 16 | return false; 17 | } 18 | 19 | int main() 20 | { 21 | vector test_strings = { 22 | "abcdefghijklmnopqrstuvwxyz", 23 | "123456!@#$%^789&*()0", 24 | "
Jump!", 25 | 26 | "Chris Chrinkus", 27 | "fask;j.2347.[]", 28 | "a=b=c=d=e=f=g=h=i" 29 | }; 30 | 31 | for (string t : test_strings) 32 | if (find_repeats(t)) 33 | cout << "duplicates found.\n"; 34 | else 35 | cout << "all unique.\n"; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /scratch/virtual.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cout; 5 | 6 | struct B { 7 | virtual void f() const { cout << "B::f"; } 8 | void g() const { cout << "B::g"; } // not virtual 9 | }; 10 | 11 | struct D : B { 12 | // explicit override is useful in large, complicated class hierarchies 13 | void f() const override { cout << "D::f"; } // overrides B::f 14 | void g() { cout << "D::g"; } 15 | }; 16 | 17 | struct DD : D { 18 | void f() { cout << "DD::f"; } // doesn't override D::f (not const) 19 | void g() const { cout << "DD::g"; } 20 | }; 21 | 22 | void call(const B& b) 23 | // a D is a kind of B, so call() can accept a D 24 | // a DD is a kind of D and a D is a kind of B, so call() can accept a DD 25 | { 26 | b.f(); 27 | b.g(); 28 | } 29 | 30 | int main() 31 | { 32 | B b; 33 | D d; 34 | DD dd; 35 | 36 | call(b); 37 | call(d); 38 | call(dd); 39 | 40 | b.f(); 41 | b.g(); 42 | 43 | d.f(); 44 | d.g(); 45 | 46 | dd.f(); 47 | dd.g(); 48 | 49 | cout << '\n'; 50 | } 51 | --------------------------------------------------------------------------------