├── Accelerated C++ Errata.txt ├── README.md ├── borland-source ├── chapter00 │ ├── hello.cpp │ └── makefile ├── chapter01 │ ├── frame.cpp │ ├── greet.cpp │ └── makefile ├── chapter02 │ ├── frame.cpp │ └── makefile ├── chapter03 │ ├── avg.cpp │ ├── makefile │ └── med.cpp ├── chapter04 │ ├── Student_info.cpp │ ├── Student_info.h │ ├── grade.cpp │ ├── grade.h │ ├── grade1 │ ├── grade2 │ ├── main1.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── makefile │ ├── median.cpp │ ├── median.h │ └── read_hw ├── chapter05 │ ├── ext_main.cpp │ ├── fails_iters.cpp │ ├── fails_list.cpp │ ├── fails_vec1.cpp │ ├── fails_vec2.cpp │ ├── grade.cpp │ ├── grade.h │ ├── list_main.cpp │ ├── makefile │ ├── pics.cpp │ ├── pics.h │ ├── pics_main.cpp │ ├── split.cpp │ ├── split.h │ ├── split_main.cpp │ └── split_main2.cpp ├── chapter06 │ ├── analysis.cpp │ ├── analysis.h │ ├── ext_fails1.cpp │ ├── ext_fails2.cpp │ ├── grade.cpp │ ├── grade.h │ ├── grade_analysis.cpp │ ├── grade_check.cpp │ ├── makefile │ ├── palin.cpp │ ├── split.cpp │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter07 │ ├── grammar.cpp │ ├── makefile │ ├── wc.cpp │ └── xref.cpp ├── chapter08 │ ├── find2.h │ ├── makefile │ ├── median.h │ ├── my_max.cpp │ ├── split.h │ ├── split_main.cpp │ └── stl_algs.h ├── chapter09 │ ├── Student_info.cpp │ ├── Student_info.h │ ├── Student_info_read1 │ ├── grade.cpp │ ├── grade.h │ ├── grade_headers │ ├── grading_prog.cpp │ └── makefile ├── chapter10 │ ├── concat_files.cpp │ ├── copyfile.cpp │ ├── in │ ├── letter_grade.cpp │ ├── makefile │ ├── pointer_example.cpp │ └── write_args.cpp ├── chapter11 │ ├── Student_info.cpp │ ├── Student_info.h │ ├── Vec.h │ ├── analysis.cpp │ ├── analysis.h │ ├── grade.cpp │ ├── grade.h │ ├── grade_analysis.cpp │ ├── grade_check.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── makefile │ ├── median.h │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter12 │ ├── Str.cpp │ ├── Str.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── analysis.cpp │ ├── analysis.h │ ├── grade.cpp │ ├── grade.h │ ├── grade_analysis.cpp │ ├── grade_check.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── makefile │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter13 │ ├── Core.cpp │ ├── Core.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── grade.cpp │ ├── grade.h │ ├── main_core.cpp │ ├── main_core_ptrs.cpp │ ├── main_grad.cpp │ ├── main_orig.cpp │ ├── makefile │ └── read_hw.cpp ├── chapter14 │ ├── Core.cpp │ ├── Core.h │ ├── Handle.h │ ├── Ptr.h │ ├── Ref_handle.h │ ├── Str.cpp │ ├── Str.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── Vec.h │ ├── Vec_clone.cpp │ ├── grade.cpp │ ├── grade.h │ ├── makefile │ ├── read_hw.cpp │ ├── std_main1.cpp │ ├── std_main2.cpp │ ├── std_main3.cpp │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter15 │ ├── Pbase_pad │ ├── Pic.cpp │ ├── Pic.h │ ├── main1.cpp │ ├── main2.cpp │ └── makefile ├── chapter16 │ ├── Core.cpp │ ├── Core.h │ ├── Pic.cpp │ ├── Pic.h │ ├── Ptr.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── grade.cpp │ ├── grade.h │ ├── histo.cpp │ ├── makefile │ ├── median.h │ └── read_hw.cpp └── data │ ├── grades │ ├── grades_core │ ├── grades_grad │ ├── grammar_input │ ├── kids_grades │ ├── mixed_grades │ ├── name1 │ ├── name2 │ ├── numbers │ ├── orig_grammar_input │ ├── palins_input │ ├── single_grade │ └── urls.htm ├── chapter00 ├── hello.cpp └── makefile ├── chapter01 ├── frame.cpp ├── greet.cpp └── makefile ├── chapter02 ├── frame.cpp └── makefile ├── chapter03 ├── avg.cpp ├── makefile └── med.cpp ├── chapter04 ├── Student_info.cpp ├── Student_info.h ├── grade.cpp ├── grade.h ├── grade1 ├── grade2 ├── main1.cpp ├── main2.cpp ├── main3.cpp ├── makefile ├── median.cpp ├── median.h └── read_hw ├── chapter05 ├── ext_main.cpp ├── fails_iters.cpp ├── fails_list.cpp ├── fails_vec1.cpp ├── fails_vec2.cpp ├── grade.cpp ├── grade.h ├── list_main.cpp ├── makefile ├── pics.cpp ├── pics.h ├── pics_main.cpp ├── split.cpp ├── split.h ├── split_main.cpp └── split_main2.cpp ├── chapter06 ├── analysis.cpp ├── analysis.h ├── ext_fails1.cpp ├── ext_fails2.cpp ├── grade.cpp ├── grade.h ├── grade_analysis.cpp ├── grade_check.cpp ├── makefile ├── palin.cpp ├── split.cpp ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter07 ├── grammar.cpp ├── makefile ├── wc.cpp └── xref.cpp ├── chapter08 ├── find2.h ├── makefile ├── median.h ├── my_max.cpp ├── split.h ├── split_main.cpp └── stl_algs.h ├── chapter09 ├── Student_info.cpp ├── Student_info.h ├── Student_info_read1 ├── grade.cpp ├── grade.h ├── grade_headers ├── grading_prog.cpp └── makefile ├── chapter10 ├── concat_files.cpp ├── copyfile.cpp ├── in ├── letter_grade.cpp ├── makefile ├── pointer_example.cpp └── write_args.cpp ├── chapter11 ├── Student_info.cpp ├── Student_info.h ├── Vec.h ├── analysis.cpp ├── analysis.h ├── grade.cpp ├── grade.h ├── grade_analysis.cpp ├── grade_check.cpp ├── main2.cpp ├── main3.cpp ├── makefile ├── median.h ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter12 ├── Str.cpp ├── Str.h ├── Student_info.cpp ├── Student_info.h ├── analysis.cpp ├── analysis.h ├── grade.cpp ├── grade.h ├── grade_analysis.cpp ├── grade_check.cpp ├── main2.cpp ├── main3.cpp ├── makefile ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter13 ├── Core.cpp ├── Core.h ├── Student_info.cpp ├── Student_info.h ├── grade.cpp ├── grade.h ├── main_core.cpp ├── main_core_ptrs.cpp ├── main_grad.cpp ├── main_orig.cpp ├── makefile └── read_hw.cpp ├── chapter14 ├── Core.cpp ├── Core.h ├── Handle.h ├── Ptr.h ├── Ref_handle.h ├── Str.cpp ├── Str.h ├── Student_info.cpp ├── Student_info.h ├── Vec.h ├── Vec_clone.cpp ├── grade.cpp ├── grade.h ├── makefile ├── read_hw.cpp ├── std_main1.cpp ├── std_main2.cpp ├── std_main3.cpp ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter15 ├── Pbase_pad ├── Pic.cpp ├── Pic.h ├── main1.cpp ├── main2.cpp └── makefile ├── chapter16 ├── Core.cpp ├── Core.h ├── Pic.cpp ├── Pic.h ├── Ptr.h ├── Student_info.cpp ├── Student_info.h ├── grade.cpp ├── grade.h ├── histo.cpp ├── makefile ├── median.h └── read_hw.cpp ├── data ├── grades ├── grades_core ├── grades_grad ├── grammar_input ├── kids_grades ├── mixed_grades ├── name1 ├── name2 ├── numbers ├── orig_grammar_input ├── palins_input ├── single_grade └── urls.htm ├── unix-source ├── README ├── chapter00 │ ├── hello.cc │ └── makefile ├── chapter01 │ ├── frame.cc │ ├── greet.cc │ └── makefile ├── chapter02 │ ├── frame.cc │ └── makefile ├── chapter03 │ ├── avg.cc │ ├── makefile │ └── med.cc ├── chapter04 │ ├── Student_info.cc │ ├── Student_info.h │ ├── grade.cc │ ├── grade.h │ ├── main1.cc │ ├── main2.cc │ ├── main3.cc │ ├── makefile │ ├── median.cc │ └── median.h ├── chapter05 │ ├── ext_main.cc │ ├── fails_iters.cc │ ├── fails_list.cc │ ├── fails_vec1.cc │ ├── fails_vec2.cc │ ├── grade.cc │ ├── grade.h │ ├── list_main.cc │ ├── makefile │ ├── pics.cc │ ├── pics.h │ ├── pics_main.cc │ ├── split.cc │ ├── split.h │ ├── split_main.cc │ └── split_main2.cc ├── chapter06 │ ├── analysis.cc │ ├── analysis.h │ ├── ext_fails1.cc │ ├── ext_fails2.cc │ ├── grade.cc │ ├── grade.h │ ├── grade_analysis.cc │ ├── grade_check.cc │ ├── makefile │ ├── palin.cc │ ├── split.cc │ ├── urls.cc │ ├── urls.h │ └── urls_main.cc ├── chapter07 │ ├── grammar.cc │ ├── makefile │ ├── wc.cc │ └── xref.cc ├── chapter08 │ ├── find2.h │ ├── makefile │ ├── median.h │ ├── my_max.cc │ ├── split.h │ ├── split_main.cc │ └── stl_algs.h ├── chapter09 │ ├── Student_info.cc │ ├── Student_info.h │ ├── Student_info_read1 │ ├── grade.cc │ ├── grade.h │ ├── grade_headers │ ├── grading_prog.cc │ └── makefile ├── chapter10 │ ├── concat_files.cc │ ├── copyfile.cc │ ├── in │ ├── letter_grade.cc │ ├── makefile │ ├── pointer_example.cc │ └── write_args.cc ├── chapter11 │ ├── Student_info.cc │ ├── Student_info.h │ ├── Vec.h │ ├── analysis.cc │ ├── analysis.h │ ├── grade.cc │ ├── grade.h │ ├── grade_analysis.cc │ ├── grade_check.cc │ ├── main2.cc │ ├── main3.cc │ ├── makefile │ ├── median.h │ ├── urls.cc │ ├── urls.h │ └── urls_main.cc ├── chapter12 │ ├── Str.cc │ ├── Str.h │ ├── Student_info.cc │ ├── Student_info.h │ ├── analysis.cc │ ├── analysis.h │ ├── grade.cc │ ├── grade.h │ ├── grade_analysis.cc │ ├── grade_check.cc │ ├── main2.cc │ ├── main3.cc │ ├── makefile │ ├── urls.cc │ ├── urls.h │ └── urls_main.cc ├── chapter13 │ ├── Core.cc │ ├── Core.h │ ├── Student_info.cc │ ├── Student_info.h │ ├── grade.cc │ ├── grade.h │ ├── main_core.cc │ ├── main_core_ptrs.cc │ ├── main_grad.cc │ ├── main_orig.cc │ ├── makefile │ └── read_hw.cc ├── chapter14 │ ├── Core.cc │ ├── Core.h │ ├── Handle.h │ ├── Ptr.h │ ├── Ref_handle.h │ ├── Str.cc │ ├── Str.h │ ├── Student_info.cc │ ├── Student_info.h │ ├── Vec.h │ ├── Vec_clone.cc │ ├── grade.cc │ ├── grade.h │ ├── makefile │ ├── read_hw.cc │ ├── std_main1.cc │ ├── std_main2.cc │ ├── std_main3.cc │ ├── urls.cc │ ├── urls.h │ └── urls_main.cc ├── chapter15 │ ├── Pbase_pad │ ├── Pic.cc │ ├── Pic.h │ ├── main1.cc │ ├── main2.cc │ └── makefile ├── chapter16 │ ├── Core.cc │ ├── Core.h │ ├── Pic.cc │ ├── Pic.h │ ├── Ptr.h │ ├── Student_info.cc │ ├── Student_info.h │ ├── grade.cc │ ├── grade.h │ ├── histo.cc │ ├── makefile │ ├── median.h │ └── read_hw.cc ├── data │ ├── grades │ ├── grades_core │ ├── grades_grad │ ├── grammar_input │ ├── kids_grades │ ├── mixed_grades │ ├── name1 │ ├── name2 │ ├── numbers │ ├── orig_grammar_input │ ├── palins_input │ ├── single_grade │ └── urls.htm └── makefile ├── win-net-source ├── README.TXT ├── chapter00 │ ├── hello.cpp │ └── makefile ├── chapter01 │ ├── frame.cpp │ ├── greet.cpp │ └── makefile ├── chapter02 │ ├── frame.cpp │ └── makefile ├── chapter03 │ ├── avg.cpp │ ├── makefile │ └── med.cpp ├── chapter04 │ ├── Student_info.cpp │ ├── Student_info.h │ ├── grade.cpp │ ├── grade.h │ ├── grade1 │ ├── grade2 │ ├── main1.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── makefile │ ├── median.cpp │ ├── median.h │ └── read_hw ├── chapter05 │ ├── ext_main.cpp │ ├── fails_iters.cpp │ ├── fails_list.cpp │ ├── fails_vec1.cpp │ ├── fails_vec2.cpp │ ├── grade.cpp │ ├── grade.h │ ├── list_main.cpp │ ├── makefile │ ├── pics.cpp │ ├── pics.h │ ├── pics_main.cpp │ ├── split.cpp │ ├── split.h │ ├── split_main.cpp │ └── split_main2.cpp ├── chapter06 │ ├── analysis.cpp │ ├── analysis.h │ ├── ext_fails1.cpp │ ├── ext_fails2.cpp │ ├── grade.cpp │ ├── grade.h │ ├── grade_analysis.cpp │ ├── grade_check.cpp │ ├── makefile │ ├── palin.cpp │ ├── split.cpp │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter07 │ ├── grammar.cpp │ ├── makefile │ ├── wc.cpp │ └── xref.cpp ├── chapter08 │ ├── find2.h │ ├── makefile │ ├── median.h │ ├── my_max.cpp │ ├── split.h │ ├── split_main.cpp │ └── stl_algs.h ├── chapter09 │ ├── Student_info.cpp │ ├── Student_info.h │ ├── Student_info_read1 │ ├── grade.cpp │ ├── grade.h │ ├── grade_headers │ ├── grading_prog.cpp │ └── makefile ├── chapter10 │ ├── concat_files.cpp │ ├── copyfile.cpp │ ├── in │ ├── letter_grade.cpp │ ├── makefile │ ├── pointer_example.cpp │ └── write_args.cpp ├── chapter11 │ ├── Student_info.cpp │ ├── Student_info.h │ ├── Vec.h │ ├── analysis.cpp │ ├── analysis.h │ ├── grade.cpp │ ├── grade.h │ ├── grade_analysis.cpp │ ├── grade_check.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── makefile │ ├── median.h │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter12 │ ├── Str.cpp │ ├── Str.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── analysis.cpp │ ├── analysis.h │ ├── grade.cpp │ ├── grade.h │ ├── grade_analysis.cpp │ ├── grade_check.cpp │ ├── main2.cpp │ ├── main3.cpp │ ├── makefile │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter13 │ ├── Core.cpp │ ├── Core.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── grade.cpp │ ├── grade.h │ ├── main_core.cpp │ ├── main_core_ptrs.cpp │ ├── main_grad.cpp │ ├── main_orig.cpp │ ├── makefile │ └── read_hw.cpp ├── chapter14 │ ├── Core.cpp │ ├── Core.h │ ├── Handle.h │ ├── Ptr.h │ ├── Ref_handle.h │ ├── Str.cpp │ ├── Str.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── Vec.h │ ├── Vec_clone.cpp │ ├── grade.cpp │ ├── grade.h │ ├── makefile │ ├── read_hw.cpp │ ├── std_main1.cpp │ ├── std_main2.cpp │ ├── std_main3.cpp │ ├── urls.cpp │ ├── urls.h │ └── urls_main.cpp ├── chapter15 │ ├── Pbase_pad │ ├── Pic.cpp │ ├── Pic.h │ ├── main1.cpp │ ├── main2.cpp │ └── makefile ├── chapter16 │ ├── Core.cpp │ ├── Core.h │ ├── Pic.cpp │ ├── Pic.h │ ├── Ptr.h │ ├── Student_info.cpp │ ├── Student_info.h │ ├── grade.cpp │ ├── grade.h │ ├── histo.cpp │ ├── makefile │ ├── median.h │ └── read_hw.cpp └── data │ ├── grades │ ├── grades_core │ ├── grades_grad │ ├── grammar_input │ ├── kids_grades │ ├── mixed_grades │ ├── name1 │ ├── name2 │ ├── numbers │ ├── orig_grammar_input │ ├── palins_input │ ├── single_grade │ └── urls.htm └── win-source ├── README.TXT ├── chapter00 ├── hello.cpp └── makefile ├── chapter01 ├── frame.cpp ├── greet.cpp └── makefile ├── chapter02 ├── frame.cpp └── makefile ├── chapter03 ├── avg.cpp ├── makefile └── med.cpp ├── chapter04 ├── Student_info.cpp ├── Student_info.h ├── grade.cpp ├── grade.h ├── grade1 ├── grade2 ├── main1.cpp ├── main2.cpp ├── main3.cpp ├── makefile ├── median.cpp ├── median.h └── read_hw ├── chapter05 ├── ext_main.cpp ├── fails_iters.cpp ├── fails_list.cpp ├── fails_vec1.cpp ├── fails_vec2.cpp ├── grade.cpp ├── grade.h ├── list_main.cpp ├── makefile ├── pics.cpp ├── pics.h ├── pics_main.cpp ├── split.cpp ├── split.h ├── split_main.cpp └── split_main2.cpp ├── chapter06 ├── analysis.cpp ├── analysis.h ├── ext_fails1.cpp ├── ext_fails2.cpp ├── grade.cpp ├── grade.h ├── grade_analysis.cpp ├── grade_check.cpp ├── makefile ├── palin.cpp ├── split.cpp ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter07 ├── grammar.cpp ├── makefile ├── wc.cpp └── xref.cpp ├── chapter08 ├── find2.h ├── makefile ├── median.h ├── my_max.cpp ├── split.h ├── split_main.cpp └── stl_algs.h ├── chapter09 ├── Student_info.cpp ├── Student_info.h ├── Student_info_read1 ├── grade.cpp ├── grade.h ├── grade_headers ├── grading_prog.cpp └── makefile ├── chapter10 ├── concat_files.cpp ├── copyfile.cpp ├── in ├── letter_grade.cpp ├── makefile ├── pointer_example.cpp └── write_args.cpp ├── chapter11 ├── Student_info.cpp ├── Student_info.h ├── Vec.h ├── analysis.cpp ├── analysis.h ├── grade.cpp ├── grade.h ├── grade_analysis.cpp ├── grade_check.cpp ├── main2.cpp ├── main3.cpp ├── makefile ├── median.h ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter12 ├── Str.cpp ├── Str.h ├── Student_info.cpp ├── Student_info.h ├── analysis.cpp ├── analysis.h ├── grade.cpp ├── grade.h ├── grade_analysis.cpp ├── grade_check.cpp ├── main2.cpp ├── main3.cpp ├── makefile ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter13 ├── Core.cpp ├── Core.h ├── Student_info.cpp ├── Student_info.h ├── grade.cpp ├── grade.h ├── main_core.cpp ├── main_core_ptrs.cpp ├── main_grad.cpp ├── main_orig.cpp ├── makefile └── read_hw.cpp ├── chapter14 ├── Core.cpp ├── Core.h ├── Handle.h ├── Ptr.h ├── Ref_handle.h ├── Str.cpp ├── Str.h ├── Student_info.cpp ├── Student_info.h ├── Vec.h ├── Vec_clone.cpp ├── grade.cpp ├── grade.h ├── makefile ├── read_hw.cpp ├── std_main1.cpp ├── std_main2.cpp ├── std_main3.cpp ├── urls.cpp ├── urls.h └── urls_main.cpp ├── chapter15 ├── Pbase_pad ├── Pic.cpp ├── Pic.h ├── main1.cpp ├── main2.cpp └── makefile ├── chapter16 ├── Core.cpp ├── Core.h ├── Pic.cpp ├── Pic.h ├── Ptr.h ├── Student_info.cpp ├── Student_info.h ├── grade.cpp ├── grade.h ├── histo.cpp ├── makefile ├── median.h └── read_hw.cpp ├── data ├── grades ├── grades_core ├── grades_grad ├── grammar_input ├── kids_grades ├── mixed_grades ├── name1 ├── name2 ├── numbers ├── orig_grammar_input ├── palins_input ├── single_grade └── urls.htm ├── minmax.h └── msbugs.htm /Accelerated C++ Errata.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Joyounger/accelerated_cpp/41a057b754f0c8c9b7253a620d63e092c9d2f9ee/Accelerated C++ Errata.txt -------------------------------------------------------------------------------- /borland-source/chapter00/hello.cpp: -------------------------------------------------------------------------------- 1 | // a small C++ program 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "Hello, world!" << std::endl; 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /borland-source/chapter01/greet.cpp: -------------------------------------------------------------------------------- 1 | // ask for a person's name, and greet the person 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // ask for the person's name 8 | std::cout << "Please enter your first name: "; 9 | 10 | // read the name 11 | std::string name; // define `name' 12 | std::cin >> name; // read into `name' 13 | 14 | // write a greeting 15 | std::cout << "Hello, " << name << "!" << std::endl; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /borland-source/chapter04/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | std::vector homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, std::vector&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /borland-source/chapter04/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const std::vector&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /borland-source/chapter04/grade1: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades and homework grade 2 | double grade(double midterm, double final, double homework) 3 | { 4 | return 0.2 * midterm + 0.4 * final + 0.4 * homework; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /borland-source/chapter04/grade2: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades 2 | // and vector of homework grades. 3 | // this function does not copy its argument, because `median' does so for us. 4 | double grade(double midterm, double final, const vector& hw) 5 | { 6 | if (hw.size() == 0) 7 | throw domain_error("student has done no homework"); 8 | return grade(midterm, final, median(hw)); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /borland-source/chapter04/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | // `median.h'--final version 5 | #include 6 | double median(std::vector); 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /borland-source/chapter04/read_hw: -------------------------------------------------------------------------------- 1 | // read homework grades from an input stream into a `vector' 2 | istream& read_hw(istream& in, vector& hw) 3 | { 4 | if (in) { 5 | // get rid of previous contents 6 | hw.clear(); 7 | 8 | // read homework grades 9 | double x; 10 | while (in >> x) 11 | hw.push_back(x); 12 | 13 | // clear the stream so that input will work for the next student 14 | in.clear(); 15 | } 16 | return in; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /borland-source/chapter05/fails_iters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // version 3: iterators but no indexing; still potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | vector::iterator iter = students.begin(); 12 | 13 | while (iter != students.end()) { 14 | if (fgrade(*iter)) { 15 | fail.push_back(*iter); 16 | iter = students.erase(iter); 17 | } else 18 | ++iter; 19 | } 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /borland-source/chapter05/fails_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::list; 6 | // version 4: use `list' instead of `vector' 7 | list extract_fails(list& students) 8 | { 9 | list fail; 10 | list::iterator iter = students.begin(); 11 | 12 | while (iter != students.end()) { 13 | if (fgrade(*iter)) { 14 | fail.push_back(*iter); 15 | iter = students.erase(iter); 16 | } else 17 | ++iter; 18 | } 19 | return fail; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /borland-source/chapter05/fails_vec1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // separate passing and failing student records: first try 8 | vector extract_fails(vector& students) 9 | { 10 | vector pass, fail; 11 | 12 | for (vector::size_type i = 0; 13 | i != students.size(); ++i) 14 | if (fgrade(students[i])) 15 | fail.push_back(students[i]); 16 | else 17 | pass.push_back(students[i]); 18 | 19 | students = pass; 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /borland-source/chapter05/fails_vec2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // second try: correct but potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | vector::size_type i = 0; 12 | 13 | // invariant: elements `[0,' `i)' of `students' represent passing grades 14 | while (i != students.size()) { 15 | if (fgrade(students[i])) { 16 | fail.push_back(students[i]); 17 | students.erase(students.begin() + i); 18 | } else 19 | ++i; 20 | } 21 | return fail; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /borland-source/chapter05/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool fgrade(const Student_info&); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /borland-source/chapter05/pics.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_pics_h 2 | #define GUARD_pics_h 3 | 4 | #include 5 | #include 6 | 7 | std::string::size_type width(const std::vector& v); 8 | 9 | std::vector frame(const std::vector& v); 10 | 11 | std::vector vcat(const std::vector& top, 12 | const std::vector& bottom); 13 | 14 | std::vector hcat(const std::vector& left, 15 | const std::vector& right); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /borland-source/chapter05/split.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_split_h 2 | #define GUARD_split_h 3 | 4 | #include 5 | #include 6 | std::vector split(const std::string&); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /borland-source/chapter05/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "split.h" 7 | 8 | using std::cin; 9 | using std::cout; 10 | using std::endl; 11 | using std::getline; 12 | using std::string; 13 | using std::vector; 14 | 15 | using std::isspace; 16 | 17 | int main() 18 | { 19 | string s; 20 | 21 | // read and split each line of input 22 | while (getline(cin, s)) { 23 | vector v = split(s); 24 | 25 | // write each word in `v' 26 | for (vector::size_type i = 0; i != v.size(); ++i) 27 | cout << v[i] << endl; 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /borland-source/chapter05/split_main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s; 11 | while (cin >> s) 12 | cout << s << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /borland-source/chapter06/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const std::vector& students); 10 | double median_analysis(const std::vector& students); 11 | double optimistic_median_analysis(const std::vector& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const std::vector&), 14 | const std::vector& did, 15 | const std::vector& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /borland-source/chapter06/ext_fails1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Student_info.h" 6 | #include "grade.h" 7 | 8 | using std::back_inserter; 9 | using std::remove_copy_if; 10 | using std::remove_if; 11 | using std::vector; 12 | 13 | vector 14 | extract_fails(vector& students) { 15 | vector fail; 16 | remove_copy_if(students.begin(), students.end(), 17 | back_inserter(fail), pgrade); 18 | students.erase(remove_if(students.begin(), students.end(), 19 | fgrade), students.end()); 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /borland-source/chapter06/ext_fails2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Student_info.h" 5 | #include "grade.h" 6 | 7 | using std::stable_partition; 8 | using std::vector; 9 | 10 | vector 11 | extract_fails(vector& students) 12 | { 13 | vector::iterator iter = 14 | stable_partition(students.begin(), students.end(), pgrade); 15 | vector fail(iter, students.end()); 16 | students.erase(iter, students.end()); 17 | 18 | return fail; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /borland-source/chapter06/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool pgrade(const Student_info&); 12 | bool fgrade(const Student_info&); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /borland-source/chapter06/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter06/palin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cin; using std::cout; 7 | using std::endl; using std::equal; 8 | using std::string; using std::transform; 9 | 10 | using std::isspace; 11 | 12 | bool is_palindrome(const string& s) 13 | { 14 | return equal(s.begin(), s.end(), s.rbegin()); 15 | } 16 | 17 | int main() 18 | { 19 | string s; 20 | while (cin >> s) { 21 | if (is_palindrome(s)) 22 | cout << s << " is a palindrome" << endl; 23 | else 24 | cout << s << " is not a palindrome" << endl; 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /borland-source/chapter06/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include 5 | #include 6 | 7 | std::vector find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /borland-source/chapter06/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | using std::vector; 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | vector v = find_urls(s); 22 | for (vector::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /borland-source/chapter07/wc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cin; 6 | using std::cout; 7 | using std::endl; 8 | using std::map; 9 | using std::string; 10 | 11 | int main() 12 | { 13 | string s; 14 | map counters; // store each word and an associated counter 15 | 16 | // read the input, keeping track of each word and how often we see it 17 | while (cin >> s) 18 | ++counters[s]; 19 | 20 | // write the words and associated counts 21 | for (map::const_iterator it = counters.begin(); 22 | it != counters.end(); ++it) { 23 | cout << it->first << "\t" << it->second << endl; 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /borland-source/chapter08/find2.h: -------------------------------------------------------------------------------- 1 | // This file contains the implementation of the find2 function. 2 | // This function is not actually used elsewhere in the book. 3 | 4 | template In find(In begin, In end, const X& x) 5 | { 6 | if (begin == end || *begin == x) 7 | return begin; 8 | begin++; 9 | return find(begin, end, x); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter08/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | typedef typename vector::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty vector"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /borland-source/chapter08/my_max.cpp: -------------------------------------------------------------------------------- 1 | template 2 | T max(const T& left, const T& right) 3 | { 4 | return left > right ? left : right; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /borland-source/chapter08/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "split.h" 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::getline; 9 | using std::ostream_iterator; 10 | using std::string; 11 | 12 | int main() 13 | { 14 | string s; 15 | while (getline(cin, s)) 16 | split(s, ostream_iterator(cout, "\n")); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /borland-source/chapter09/Student_info_read1: -------------------------------------------------------------------------------- 1 | istream& Student_info::read(istream& in) 2 | { 3 | in >> name >> midterm >> final; 4 | read_hw(in, homework); 5 | return in; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /borland-source/chapter09/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter09/grade_headers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::domain_error; 5 | using std::vector; 6 | 7 | #include "grade.h" 8 | #include "median.h" 9 | 10 | -------------------------------------------------------------------------------- /borland-source/chapter10/concat_files.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cerr; 6 | using std::cout; 7 | using std::endl; 8 | using std::getline; 9 | using std::ifstream; 10 | using std::string; 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int fail_count = 0; 15 | // for each file in the input list 16 | for (int i = 1; i < argc; ++i) { 17 | ifstream in(argv[i]); 18 | 19 | // if it exists, write its contents, otherwise generate an error message 20 | if (in) { 21 | string s; 22 | while (getline(in, s)) 23 | cout << s << endl; 24 | } else { 25 | cerr << "cannot open file " << argv[i] << endl; 26 | ++fail_count; 27 | } 28 | } 29 | return fail_count; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /borland-source/chapter10/copyfile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::endl; 5 | using std::getline; 6 | using std::ifstream; 7 | using std::ofstream; 8 | using std::string; 9 | 10 | int main() 11 | { 12 | ifstream infile("in"); 13 | ofstream outfile("out"); 14 | 15 | string s; 16 | 17 | while (getline(infile, s)) 18 | outfile << s << endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /borland-source/chapter10/in: -------------------------------------------------------------------------------- 1 | This is a sample data file 2 | -------------------------------------------------------------------------------- /borland-source/chapter10/pointer_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int x = 5; 9 | 10 | // `p' points to `x' 11 | int* p = &x; 12 | cout << "x = " << x << endl; 13 | 14 | // change the value of `x' through `p' 15 | *p = 6; 16 | cout << "x = " << x << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /borland-source/chapter10/write_args.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | // if there are command-line arguments, write them 9 | if (argc > 1) { 10 | cout << argv[1]; // write the first argument 11 | 12 | // write each remaining argument with a space before it 13 | for (int i = 2; i != argc; ++i) 14 | cout << " " << argv[i]; // `argv[i]' is a `char*' 15 | } 16 | cout << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /borland-source/chapter11/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /borland-source/chapter11/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /borland-source/chapter11/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /borland-source/chapter11/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter11/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include "Vec.h" 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | 11 | template 12 | T median(Vec v) 13 | { 14 | typedef typename Vec::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty Vec"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /borland-source/chapter11/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include 6 | 7 | Vec find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /borland-source/chapter11/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /borland-source/chapter12/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include "Str.h" 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | Str name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /borland-source/chapter12/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include "Str.h" 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const Str& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /borland-source/chapter12/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /borland-source/chapter12/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter12/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /borland-source/chapter12/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /borland-source/chapter13/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter13/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /borland-source/chapter14/Student_info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | #include "Core.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | char ch; 11 | is >> ch; // get record type 12 | 13 | // allocate new object of the appropriate type 14 | // use `Handle(T*)' to build a `Handle' from the pointer to that object 15 | // call `Handle::operator=' to assign the `Handle' to the left-hand side 16 | if (ch == 'U') 17 | cp = new Core(is); 18 | else 19 | cp = new Grad(is); 20 | 21 | return is; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /borland-source/chapter14/Vec_clone.cpp: -------------------------------------------------------------------------------- 1 | #include "Vec.h" 2 | 3 | template T* clone(const T* tp); 4 | // the key to making `Ptr< Vec >' work 5 | template<> 6 | Vec* clone(const Vec* vp) 7 | { 8 | return new Vec(*vp); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /borland-source/chapter14/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter14/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /borland-source/chapter14/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /borland-source/chapter14/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /borland-source/chapter15/Pbase_pad: -------------------------------------------------------------------------------- 1 | static void pad(std::ostream& os, wd_sz beg, wd_sz end) { 2 | while (beg != end) { 3 | os << " "; 4 | ++beg; 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /borland-source/chapter15/main1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | Picture p(i); 20 | cout << p << endl; 21 | 22 | Picture q = frame(p); 23 | cout << q << endl; 24 | 25 | cout << frame(q) << endl; 26 | 27 | Picture r = hcat(p, q); 28 | cout << r << endl; 29 | 30 | Picture s = vcat(q, r); 31 | cout << s << endl << frame(s) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /borland-source/chapter15/main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | 20 | Picture p(i); 21 | cout << p << endl; 22 | Picture q = p; 23 | cout << q << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /borland-source/chapter16/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /borland-source/chapter16/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | typedef typename vector::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty vector"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /borland-source/chapter16/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /borland-source/data/grades: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | 23 | Fail1 45 55 65 80 90 70 65 60 24 | Fail2 55 55 65 50 55 60 65 60 25 | 26 | 27 | -------------------------------------------------------------------------------- /borland-source/data/grades_core: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /borland-source/data/grades_grad: -------------------------------------------------------------------------------- 1 | 2 | Moo 100 100 100 100 100 100 100 100 100 3 | Moore 75 85 77 59 0 85 75 89 75 4 | Norman 57 78 73 66 78 70 88 89 84 5 | Olson 89 86 70 90 55 73 80 84 99 6 | Peerson 47 70 82 73 50 87 73 71 84 7 | 8 | Russel 72 87 88 54 55 82 69 87 80 9 | Thomas 90 96 99 99 100 81 97 97 100 10 | Vaughn 81 97 99 67 40 90 70 96 76 11 | Westerly 43 98 96 79 100 82 97 96 96 12 | 13 | 14 | Baker 67 72 73 40 0 78 55 70 87 15 | Davis 77 70 82 65 70 77 83 81 80 16 | Edwards 77 72 73 80 90 93 75 90 92 17 | Franklin 47 70 82 73 50 87 73 71 72 18 | 19 | Jones 77 82 83 50 10 88 65 80 88 20 | Harris 97 90 92 95 100 87 93 91 94 21 | Smith 87 92 93 60 0 98 75 90 88 22 | Carpenter 47 90 92 73 100 87 93 91 85 23 | -------------------------------------------------------------------------------- /borland-source/data/grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /borland-source/data/kids_grades: -------------------------------------------------------------------------------- 1 | 2 | G Sarah 100 100 100 100 100 100 100 100 100 3 | U Lynn 75 85 77 59 0 85 75 89 4 | G James 57 78 73 66 78 70 88 89 84 5 | U Kevin 89 86 70 90 55 73 80 84 6 | U MaryKate 47 70 82 73 50 87 73 71 7 | 8 | U Pat 72 87 88 54 55 82 69 87 9 | G Paul 90 96 99 99 100 81 97 97 100 10 | U Rhia 81 97 99 67 40 90 70 96 11 | -------------------------------------------------------------------------------- /borland-source/data/mixed_grades: -------------------------------------------------------------------------------- 1 | G Moo 100 100 100 100 100 100 100 100 100 2 | U Moore 75 85 77 59 0 85 75 89 3 | G Norman 57 78 73 66 78 70 88 89 84 4 | U Olson 89 86 70 90 55 73 80 84 5 | U Peerson 47 70 82 73 50 87 73 71 6 | 7 | U Russel 72 87 88 54 55 82 69 87 8 | G Thomas 90 96 99 99 100 81 97 97 100 9 | U Vaughn 81 97 99 67 40 90 70 96 10 | U Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | G Baker 67 72 73 40 0 78 55 70 87 14 | G Davis 77 70 82 65 70 77 83 81 80 15 | G Edwards 77 72 73 80 90 93 75 90 92 16 | U Franklin 47 70 82 73 50 87 73 71 17 | 18 | U Jones 77 82 83 50 10 88 65 80 19 | G Harris 97 90 92 95 100 87 93 91 94 20 | U Smith 87 92 93 60 0 98 75 90 21 | U Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /borland-source/data/name1: -------------------------------------------------------------------------------- 1 | Vladimir 2 | -------------------------------------------------------------------------------- /borland-source/data/name2: -------------------------------------------------------------------------------- 1 | Estragon 2 | -------------------------------------------------------------------------------- /borland-source/data/numbers: -------------------------------------------------------------------------------- 1 | 22 23 5 -1 -2 0 1 2 3 4 2 | -------------------------------------------------------------------------------- /borland-source/data/orig_grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /borland-source/data/palins_input: -------------------------------------------------------------------------------- 1 | madam 2 | eye 3 | civic 4 | level 5 | rotor 6 | -------------------------------------------------------------------------------- /borland-source/data/single_grade: -------------------------------------------------------------------------------- 1 | harriet 85 95 88 87 93 45 76 99 2 | -------------------------------------------------------------------------------- /chapter00/hello.cpp: -------------------------------------------------------------------------------- 1 | // a small C++ program 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "Hello, world!" << std::endl; 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /chapter00/makefile: -------------------------------------------------------------------------------- 1 | all: hello.exe 2 | 3 | hello.exe: hello.obj 4 | 5 | test: all 6 | hello.exe 7 | 8 | 9 | 10 | # Say "nmake clobber" to get rid of files created by nmake 11 | clobber: 12 | del *.obj 13 | del *.exe 14 | 15 | # To convert a .cpp file to a .obj file, run the compiler with these options: 16 | # /nologo Don't print a copyright message when starting 17 | # /GX Enable exception handling 18 | # /c Compile only; don't link 19 | # $< Built-in macro that expands to the file being compiled 20 | .cpp.obj: 21 | cl /nologo /GX /c $< 22 | 23 | # To convert a collection of .obj files to a .exe file, 24 | # run the linker (same command as the compiler) with these options: 25 | # /nologo Don't print a copyright message when starting 26 | .obj.exe: 27 | cl /nologo $** 28 | -------------------------------------------------------------------------------- /chapter01/greet.cpp: -------------------------------------------------------------------------------- 1 | // ask for a person's name, and greet the person 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // ask for the person's name 8 | std::cout << "Please enter your first name: "; 9 | 10 | // read the name 11 | std::string name; // define `name' 12 | std::cin >> name; // read into `name' 13 | 14 | // write a greeting 15 | std::cout << "Hello, " << name << "!" << std::endl; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /chapter04/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | std::vector homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, std::vector&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /chapter04/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const std::vector&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /chapter04/grade1: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades and homework grade 2 | double grade(double midterm, double final, double homework) 3 | { 4 | return 0.2 * midterm + 0.4 * final + 0.4 * homework; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /chapter04/grade2: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades 2 | // and vector of homework grades. 3 | // this function does not copy its argument, because `median' does so for us. 4 | double grade(double midterm, double final, const vector& hw) 5 | { 6 | if (hw.size() == 0) 7 | throw domain_error("student has done no homework"); 8 | return grade(midterm, final, median(hw)); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /chapter04/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | // `median.h'--final version 5 | #include 6 | double median(std::vector); 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /chapter04/read_hw: -------------------------------------------------------------------------------- 1 | // read homework grades from an input stream into a `vector' 2 | istream& read_hw(istream& in, vector& hw) 3 | { 4 | if (in) { 5 | // get rid of previous contents 6 | hw.clear(); 7 | 8 | // read homework grades 9 | double x; 10 | while (in >> x) 11 | hw.push_back(x); 12 | 13 | // clear the stream so that input will work for the next student 14 | in.clear(); 15 | } 16 | return in; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /chapter05/fails_iters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // version 3: iterators but no indexing; still potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | vector::iterator iter = students.begin(); 12 | 13 | while (iter != students.end()) { 14 | if (fgrade(*iter)) { 15 | fail.push_back(*iter); 16 | iter = students.erase(iter); 17 | } else 18 | ++iter; 19 | } 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /chapter05/fails_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::list; 6 | // version 4: use `list' instead of `vector' 7 | list extract_fails(list& students) 8 | { 9 | list fail; 10 | list::iterator iter = students.begin(); 11 | 12 | while (iter != students.end()) { 13 | if (fgrade(*iter)) { 14 | fail.push_back(*iter); 15 | iter = students.erase(iter); 16 | } else 17 | ++iter; 18 | } 19 | return fail; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /chapter05/fails_vec1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // separate passing and failing student records: first try 8 | vector extract_fails(vector& students) 9 | { 10 | vector pass, fail; 11 | 12 | for (vector::size_type i = 0; 13 | i != students.size(); ++i) 14 | if (fgrade(students[i])) 15 | fail.push_back(students[i]); 16 | else 17 | pass.push_back(students[i]); 18 | 19 | students = pass; 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /chapter05/fails_vec2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // second try: correct but potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | vector::size_type i = 0; 12 | 13 | // invariant: elements `[0,' `i)' of `students' represent passing grades 14 | while (i != students.size()) { 15 | if (fgrade(students[i])) { 16 | fail.push_back(students[i]); 17 | students.erase(students.begin() + i); 18 | } else 19 | ++i; 20 | } 21 | return fail; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /chapter05/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool fgrade(const Student_info&); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /chapter05/pics.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_pics_h 2 | #define GUARD_pics_h 3 | 4 | #include 5 | #include 6 | 7 | std::string::size_type width(const std::vector& v); 8 | 9 | std::vector frame(const std::vector& v); 10 | 11 | std::vector vcat(const std::vector& top, 12 | const std::vector& bottom); 13 | 14 | std::vector hcat(const std::vector& left, 15 | const std::vector& right); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /chapter05/split.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_split_h 2 | #define GUARD_split_h 3 | 4 | #include 5 | #include 6 | std::vector split(const std::string&); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /chapter05/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "split.h" 7 | 8 | using std::cin; 9 | using std::cout; 10 | using std::endl; 11 | using std::getline; 12 | using std::string; 13 | using std::vector; 14 | 15 | using std::isspace; 16 | 17 | int main() 18 | { 19 | string s; 20 | 21 | // read and split each line of input 22 | while (getline(cin, s)) { 23 | vector v = split(s); 24 | 25 | // write each word in `v' 26 | for (vector::size_type i = 0; i != v.size(); ++i) 27 | cout << v[i] << endl; 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /chapter05/split_main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s; 11 | while (cin >> s) 12 | cout << s << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /chapter06/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const std::vector& students); 10 | double median_analysis(const std::vector& students); 11 | double optimistic_median_analysis(const std::vector& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const std::vector&), 14 | const std::vector& did, 15 | const std::vector& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /chapter06/ext_fails1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Student_info.h" 6 | #include "grade.h" 7 | 8 | using std::back_inserter; 9 | using std::remove_copy_if; 10 | using std::remove_if; 11 | using std::vector; 12 | 13 | vector 14 | extract_fails(vector& students) { 15 | vector fail; 16 | remove_copy_if(students.begin(), students.end(), 17 | back_inserter(fail), pgrade); 18 | students.erase(remove_if(students.begin(), students.end(), 19 | fgrade), students.end()); 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /chapter06/ext_fails2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Student_info.h" 5 | #include "grade.h" 6 | 7 | using std::stable_partition; 8 | using std::vector; 9 | 10 | vector 11 | extract_fails(vector& students) 12 | { 13 | vector::iterator iter = 14 | stable_partition(students.begin(), students.end(), pgrade); 15 | vector fail(iter, students.end()); 16 | students.erase(iter, students.end()); 17 | 18 | return fail; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /chapter06/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool pgrade(const Student_info&); 12 | bool fgrade(const Student_info&); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /chapter06/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /chapter06/palin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cin; using std::cout; 7 | using std::endl; using std::equal; 8 | using std::string; using std::transform; 9 | 10 | using std::isspace; 11 | 12 | bool is_palindrome(const string& s) 13 | { 14 | return equal(s.begin(), s.end(), s.rbegin()); 15 | } 16 | 17 | int main() 18 | { 19 | string s; 20 | while (cin >> s) { 21 | if (is_palindrome(s)) 22 | cout << s << " is a palindrome" << endl; 23 | else 24 | cout << s << " is not a palindrome" << endl; 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /chapter06/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include 5 | #include 6 | 7 | std::vector find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /chapter06/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | using std::vector; 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | vector v = find_urls(s); 22 | for (vector::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chapter07/wc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cin; 6 | using std::cout; 7 | using std::endl; 8 | using std::map; 9 | using std::string; 10 | 11 | int main() 12 | { 13 | string s; 14 | map counters; // store each word and an associated counter 15 | 16 | // read the input, keeping track of each word and how often we see it 17 | while (cin >> s) 18 | ++counters[s]; 19 | 20 | // write the words and associated counts 21 | for (map::const_iterator it = counters.begin(); 22 | it != counters.end(); ++it) { 23 | cout << it->first << "\t" << it->second << endl; 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /chapter08/find2.h: -------------------------------------------------------------------------------- 1 | // This file contains the implementation of the find2 function. 2 | // This function is not actually used elsewhere in the book. 3 | 4 | template In find(In begin, In end, const X& x) 5 | { 6 | if (begin == end || *begin == x) 7 | return begin; 8 | begin++; 9 | return find(begin, end, x); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /chapter08/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | typedef typename vector::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty vector"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /chapter08/my_max.cpp: -------------------------------------------------------------------------------- 1 | template 2 | T max(const T& left, const T& right) 3 | { 4 | return left > right ? left : right; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /chapter08/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "split.h" 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::getline; 9 | using std::ostream_iterator; 10 | using std::string; 11 | 12 | int main() 13 | { 14 | string s; 15 | while (getline(cin, s)) 16 | split(s, ostream_iterator(cout, "\n")); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /chapter09/Student_info_read1: -------------------------------------------------------------------------------- 1 | istream& Student_info::read(istream& in) 2 | { 3 | in >> name >> midterm >> final; 4 | read_hw(in, homework); 5 | return in; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /chapter09/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /chapter09/grade_headers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::domain_error; 5 | using std::vector; 6 | 7 | #include "grade.h" 8 | #include "median.h" 9 | 10 | -------------------------------------------------------------------------------- /chapter10/concat_files.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cerr; 6 | using std::cout; 7 | using std::endl; 8 | using std::getline; 9 | using std::ifstream; 10 | using std::string; 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int fail_count = 0; 15 | // for each file in the input list 16 | for (int i = 1; i < argc; ++i) { 17 | ifstream in(argv[i]); 18 | 19 | // if it exists, write its contents, otherwise generate an error message 20 | if (in) { 21 | string s; 22 | while (getline(in, s)) 23 | cout << s << endl; 24 | } else { 25 | cerr << "cannot open file " << argv[i] << endl; 26 | ++fail_count; 27 | } 28 | } 29 | return fail_count; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /chapter10/copyfile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::endl; 5 | using std::getline; 6 | using std::ifstream; 7 | using std::ofstream; 8 | using std::string; 9 | 10 | int main() 11 | { 12 | ifstream infile("in"); 13 | ofstream outfile("out"); 14 | 15 | string s; 16 | 17 | while (getline(infile, s)) 18 | outfile << s << endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /chapter10/in: -------------------------------------------------------------------------------- 1 | This is a sample data file 2 | -------------------------------------------------------------------------------- /chapter10/pointer_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int x = 5; 9 | 10 | // `p' points to `x' 11 | int* p = &x; 12 | cout << "x = " << x << endl; 13 | 14 | // change the value of `x' through `p' 15 | *p = 6; 16 | cout << "x = " << x << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /chapter10/write_args.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | // if there are command-line arguments, write them 9 | if (argc > 1) { 10 | cout << argv[1]; // write the first argument 11 | 12 | // write each remaining argument with a space before it 13 | for (int i = 2; i != argc; ++i) 14 | cout << " " << argv[i]; // `argv[i]' is a `char*' 15 | } 16 | cout << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /chapter11/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /chapter11/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /chapter11/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /chapter11/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /chapter11/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include "Vec.h" 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | 11 | template 12 | T median(Vec v) 13 | { 14 | typedef typename Vec::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty Vec"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /chapter11/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include 6 | 7 | Vec find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /chapter11/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chapter12/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include "Str.h" 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | Str name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /chapter12/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include "Str.h" 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const Str& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /chapter12/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /chapter12/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /chapter12/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /chapter12/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chapter13/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /chapter13/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /chapter14/Student_info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | #include "Core.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | char ch; 11 | is >> ch; // get record type 12 | 13 | // allocate new object of the appropriate type 14 | // use `Handle(T*)' to build a `Handle' from the pointer to that object 15 | // call `Handle::operator=' to assign the `Handle' to the left-hand side 16 | if (ch == 'U') 17 | cp = new Core(is); 18 | else 19 | cp = new Grad(is); 20 | 21 | return is; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /chapter14/Vec_clone.cpp: -------------------------------------------------------------------------------- 1 | #include "Vec.h" 2 | 3 | template T* clone(const T* tp); 4 | // the key to making `Ptr< Vec >' work 5 | template<> 6 | Vec* clone(const Vec* vp) 7 | { 8 | return new Vec(*vp); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /chapter14/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /chapter14/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /chapter14/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /chapter14/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chapter15/Pbase_pad: -------------------------------------------------------------------------------- 1 | static void pad(std::ostream& os, wd_sz beg, wd_sz end) { 2 | while (beg != end) { 3 | os << " "; 4 | ++beg; 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /chapter15/main1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | Picture p(i); 20 | cout << p << endl; 21 | 22 | Picture q = frame(p); 23 | cout << q << endl; 24 | 25 | cout << frame(q) << endl; 26 | 27 | Picture r = hcat(p, q); 28 | cout << r << endl; 29 | 30 | Picture s = vcat(q, r); 31 | cout << s << endl << frame(s) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /chapter15/main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | 20 | Picture p(i); 21 | cout << p << endl; 22 | Picture q = p; 23 | cout << q << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chapter16/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /chapter16/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | typedef typename vector::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty vector"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /chapter16/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /data/grades: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | 23 | Fail1 45 55 65 80 90 70 65 60 24 | Fail2 55 55 65 50 55 60 65 60 25 | 26 | 27 | -------------------------------------------------------------------------------- /data/grades_core: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /data/grades_grad: -------------------------------------------------------------------------------- 1 | 2 | Moo 100 100 100 100 100 100 100 100 100 3 | Moore 75 85 77 59 0 85 75 89 75 4 | Norman 57 78 73 66 78 70 88 89 84 5 | Olson 89 86 70 90 55 73 80 84 99 6 | Peerson 47 70 82 73 50 87 73 71 84 7 | 8 | Russel 72 87 88 54 55 82 69 87 80 9 | Thomas 90 96 99 99 100 81 97 97 100 10 | Vaughn 81 97 99 67 40 90 70 96 76 11 | Westerly 43 98 96 79 100 82 97 96 96 12 | 13 | 14 | Baker 67 72 73 40 0 78 55 70 87 15 | Davis 77 70 82 65 70 77 83 81 80 16 | Edwards 77 72 73 80 90 93 75 90 92 17 | Franklin 47 70 82 73 50 87 73 71 72 18 | 19 | Jones 77 82 83 50 10 88 65 80 88 20 | Harris 97 90 92 95 100 87 93 91 94 21 | Smith 87 92 93 60 0 98 75 90 88 22 | Carpenter 47 90 92 73 100 87 93 91 85 23 | -------------------------------------------------------------------------------- /data/grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /data/kids_grades: -------------------------------------------------------------------------------- 1 | 2 | G Sarah 100 100 100 100 100 100 100 100 100 3 | U Lynn 75 85 77 59 0 85 75 89 4 | G James 57 78 73 66 78 70 88 89 84 5 | U Kevin 89 86 70 90 55 73 80 84 6 | U MaryKate 47 70 82 73 50 87 73 71 7 | 8 | U Pat 72 87 88 54 55 82 69 87 9 | G Paul 90 96 99 99 100 81 97 97 100 10 | U Rhia 81 97 99 67 40 90 70 96 11 | -------------------------------------------------------------------------------- /data/mixed_grades: -------------------------------------------------------------------------------- 1 | G Moo 100 100 100 100 100 100 100 100 100 2 | U Moore 75 85 77 59 0 85 75 89 3 | G Norman 57 78 73 66 78 70 88 89 84 4 | U Olson 89 86 70 90 55 73 80 84 5 | U Peerson 47 70 82 73 50 87 73 71 6 | 7 | U Russel 72 87 88 54 55 82 69 87 8 | G Thomas 90 96 99 99 100 81 97 97 100 9 | U Vaughn 81 97 99 67 40 90 70 96 10 | U Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | G Baker 67 72 73 40 0 78 55 70 87 14 | G Davis 77 70 82 65 70 77 83 81 80 15 | G Edwards 77 72 73 80 90 93 75 90 92 16 | U Franklin 47 70 82 73 50 87 73 71 17 | 18 | U Jones 77 82 83 50 10 88 65 80 19 | G Harris 97 90 92 95 100 87 93 91 94 20 | U Smith 87 92 93 60 0 98 75 90 21 | U Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /data/name1: -------------------------------------------------------------------------------- 1 | Vladimir 2 | -------------------------------------------------------------------------------- /data/name2: -------------------------------------------------------------------------------- 1 | Estragon 2 | -------------------------------------------------------------------------------- /data/numbers: -------------------------------------------------------------------------------- 1 | 22 23 5 -1 -2 0 1 2 3 4 2 | -------------------------------------------------------------------------------- /data/orig_grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /data/palins_input: -------------------------------------------------------------------------------- 1 | madam 2 | eye 3 | civic 4 | level 5 | rotor 6 | -------------------------------------------------------------------------------- /data/single_grade: -------------------------------------------------------------------------------- 1 | harriet 85 95 88 87 93 45 76 99 2 | -------------------------------------------------------------------------------- /unix-source/chapter00/hello.cc: -------------------------------------------------------------------------------- 1 | // a small C++ program 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "Hello, world!" << std::endl; 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /unix-source/chapter00/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: hello 5 | 6 | test: all 7 | ./hello 8 | 9 | clobber: 10 | rm -f *.o core *.exe hello 11 | -------------------------------------------------------------------------------- /unix-source/chapter01/greet.cc: -------------------------------------------------------------------------------- 1 | // ask for a person's name, and greet the person 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // ask for the person's name 8 | std::cout << "Please enter your first name: "; 9 | 10 | // read the name 11 | std::string name; // define `name' 12 | std::cin >> name; // read into `name' 13 | 14 | // write a greeting 15 | std::cout << "Hello, " << name << "!" << std::endl; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /unix-source/chapter01/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: frame greet 5 | 6 | test: all 7 | ./greet <../data/name1 8 | ./frame <../data/name2 9 | 10 | clobber: 11 | rm -f *.o *.exe core frame greet 12 | -------------------------------------------------------------------------------- /unix-source/chapter02/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: frame 5 | 6 | test: all 7 | ./frame <../data/name1 8 | 9 | clobber: 10 | rm -f *.o *.exe core frame 11 | -------------------------------------------------------------------------------- /unix-source/chapter03/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: avg med 5 | 6 | test: all 7 | ./avg <../data/single_grade 8 | ./med <../data/single_grade 9 | 10 | clobber: 11 | rm -f *.o *.exe core avg med 12 | -------------------------------------------------------------------------------- /unix-source/chapter04/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | std::vector homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, std::vector&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /unix-source/chapter04/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const std::vector&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /unix-source/chapter04/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: main1 main2 main3 5 | 6 | Student_info.o: Student_info.cc Student_info.h 7 | 8 | grade.o: grade.cc grade.h median.h Student_info.h 9 | 10 | main3.o: main3.cc grade.h median.h Student_info.h 11 | 12 | median.o: median.cc median.h 13 | 14 | main3: main3.o grade.o median.o Student_info.o 15 | 16 | test: all 17 | ./main1 <../data/single_grade 18 | ./main2 <../data/single_grade 19 | ./main2 <../data/grades 20 | ./main3 <../data/grades 21 | 22 | clobber: 23 | rm -f *.o *.exe core main1 main2 main3 24 | -------------------------------------------------------------------------------- /unix-source/chapter04/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | // `median.h'--final version 5 | #include 6 | double median(std::vector); 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /unix-source/chapter05/fails_iters.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // version 3: iterators but no indexing; still potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | #ifdef _MSC_VER 12 | std::vector::iterator iter = students.begin(); 13 | #else 14 | vector::iterator iter = students.begin(); 15 | #endif 16 | 17 | while (iter != students.end()) { 18 | if (fgrade(*iter)) { 19 | fail.push_back(*iter); 20 | iter = students.erase(iter); 21 | } else 22 | ++iter; 23 | } 24 | return fail; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /unix-source/chapter05/fails_list.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::list; 6 | // version 4: use `list' instead of `vector' 7 | list extract_fails(list& students) 8 | { 9 | list fail; 10 | #ifdef _MSC_VER 11 | std::list::iterator iter = students.begin(); 12 | #else 13 | list::iterator iter = students.begin(); 14 | #endif 15 | 16 | while (iter != students.end()) { 17 | if (fgrade(*iter)) { 18 | fail.push_back(*iter); 19 | iter = students.erase(iter); 20 | } else 21 | ++iter; 22 | } 23 | return fail; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unix-source/chapter05/fails_vec1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // separate passing and failing student records: first try 8 | vector extract_fails(vector& students) 9 | { 10 | vector pass, fail; 11 | 12 | #ifdef _MSC_VER 13 | for (std::vector::size_type i = 0; 14 | #else 15 | for (vector::size_type i = 0; 16 | #endif 17 | i != students.size(); ++i) 18 | if (fgrade(students[i])) 19 | fail.push_back(students[i]); 20 | else 21 | pass.push_back(students[i]); 22 | 23 | students = pass; 24 | return fail; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /unix-source/chapter05/fails_vec2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // second try: correct but potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | #ifdef _MSC_VER 12 | std::vector::size_type i = 0; 13 | #else 14 | vector::size_type i = 0; 15 | #endif 16 | 17 | // invariant: elements `[0,' `i)' of `students' represent passing grades 18 | while (i != students.size()) { 19 | if (fgrade(students[i])) { 20 | fail.push_back(students[i]); 21 | students.erase(students.begin() + i); 22 | } else 23 | ++i; 24 | } 25 | return fail; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /unix-source/chapter05/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool fgrade(const Student_info&); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /unix-source/chapter05/pics.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_pics_h 2 | #define GUARD_pics_h 3 | 4 | #include 5 | #include 6 | 7 | std::string::size_type width(const std::vector& v); 8 | 9 | std::vector frame(const std::vector& v); 10 | 11 | std::vector vcat(const std::vector& top, 12 | const std::vector& bottom); 13 | 14 | std::vector hcat(const std::vector& left, 15 | const std::vector& right); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /unix-source/chapter05/split.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_split_h 2 | #define GUARD_split_h 3 | 4 | #include 5 | #include 6 | std::vector split(const std::string&); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /unix-source/chapter05/split_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "split.h" 7 | 8 | using std::cin; 9 | using std::cout; 10 | using std::endl; 11 | using std::getline; 12 | using std::string; 13 | using std::vector; 14 | 15 | #ifndef _MSC_VER 16 | using std::isspace; 17 | #endif 18 | 19 | int main() 20 | { 21 | string s; 22 | 23 | // read and split each line of input 24 | while (getline(cin, s)) { 25 | vector v = split(s); 26 | 27 | // write each word in `v' 28 | #ifdef _MSC_VER 29 | for (std::vector::size_type i = 0; i != v.size(); ++i) 30 | #else 31 | for (vector::size_type i = 0; i != v.size(); ++i) 32 | #endif 33 | cout << v[i] << endl; 34 | } 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /unix-source/chapter05/split_main2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s; 11 | while (cin >> s) 12 | cout << s << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /unix-source/chapter06/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const std::vector& students); 10 | double median_analysis(const std::vector& students); 11 | double optimistic_median_analysis(const std::vector& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const std::vector&), 14 | const std::vector& did, 15 | const std::vector& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /unix-source/chapter06/ext_fails1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Student_info.h" 6 | #include "grade.h" 7 | 8 | using std::back_inserter; 9 | using std::remove_copy_if; 10 | using std::remove_if; 11 | using std::vector; 12 | 13 | vector 14 | extract_fails(vector& students) { 15 | vector fail; 16 | remove_copy_if(students.begin(), students.end(), 17 | back_inserter(fail), pgrade); 18 | students.erase(remove_if(students.begin(), students.end(), 19 | fgrade), students.end()); 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /unix-source/chapter06/ext_fails2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Student_info.h" 5 | #include "grade.h" 6 | 7 | using std::stable_partition; 8 | using std::vector; 9 | 10 | vector 11 | extract_fails(vector& students) 12 | { 13 | #ifdef _MSC_VER 14 | std::vector::iterator iter = 15 | #else 16 | vector::iterator iter = 17 | #endif 18 | stable_partition(students.begin(), students.end(), pgrade); 19 | vector fail(iter, students.end()); 20 | students.erase(iter, students.end()); 21 | 22 | return fail; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /unix-source/chapter06/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool pgrade(const Student_info&); 12 | bool fgrade(const Student_info&); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /unix-source/chapter06/grade_check.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter06/palin.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cin; using std::cout; 7 | using std::endl; using std::equal; 8 | using std::string; using std::transform; 9 | 10 | #ifndef _MSC_VER 11 | using std::isspace; 12 | #endif 13 | 14 | bool is_palindrome(const string& s) 15 | { 16 | return equal(s.begin(), s.end(), s.rbegin()); 17 | } 18 | 19 | int main() 20 | { 21 | string s; 22 | while (cin >> s) { 23 | if (is_palindrome(s)) 24 | cout << s << " is a palindrome" << endl; 25 | else 26 | cout << s << " is not a palindrome" << endl; 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /unix-source/chapter06/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include 5 | #include 6 | 7 | std::vector find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /unix-source/chapter06/urls_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | using std::vector; 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | vector v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (std::vector::const_iterator i = v.begin(); 24 | #else 25 | for (vector::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /unix-source/chapter07/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | CXXFLAGS = -I../chapter05 4 | 5 | all: wc xref grammar 6 | 7 | wc.o: wc.cc 8 | 9 | wc: wc.o 10 | 11 | xref.o: xref.cc ../chapter05/split.h 12 | 13 | xref: xref.o split.o 14 | 15 | grammar.o: grammar.cc ../chapter05/split.h 16 | 17 | split.o: ../chapter06/split.cc ../chapter05/split.h 18 | $(CXX) $(CXXFLAGS) -c ../chapter06/split.cc 19 | 20 | grammar: grammar.o split.o 21 | 22 | test: all 23 | ./wc < wc.cc 24 | ./xref < wc.cc 25 | ./grammar <../data/grammar_input 26 | 27 | clobber: 28 | rm -f *.o *.exe core wc xref grammar 29 | -------------------------------------------------------------------------------- /unix-source/chapter07/wc.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cin; 6 | using std::cout; 7 | using std::endl; 8 | using std::map; 9 | using std::string; 10 | 11 | int main() 12 | { 13 | string s; 14 | map counters; // store each word and an associated counter 15 | 16 | // read the input, keeping track of each word and how often we see it 17 | while (cin >> s) 18 | ++counters[s]; 19 | 20 | // write the words and associated counts 21 | #ifdef _MSC_VER 22 | for (std::map::const_iterator it = counters.begin(); 23 | #else 24 | for (map::const_iterator it = counters.begin(); 25 | #endif 26 | it != counters.end(); ++it) { 27 | cout << it->first << "\t" << it->second << endl; 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /unix-source/chapter08/find2.h: -------------------------------------------------------------------------------- 1 | // This file contains the implementation of the find2 function. 2 | // This function is not actually used elsewhere in the book. 3 | 4 | template In find(In begin, In end, const X& x) 5 | { 6 | if (begin == end || *begin == x) 7 | return begin; 8 | begin++; 9 | return find(begin, end, x); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter08/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | #ifdef _MSC_VER 15 | typedef typename std::vector::size_type vec_sz; 16 | #else 17 | typedef typename vector::size_type vec_sz; 18 | #endif 19 | 20 | vec_sz size = v.size(); 21 | if (size == 0) 22 | throw domain_error("median of an empty vector"); 23 | 24 | sort(v.begin(), v.end()); 25 | 26 | vec_sz mid = size/2; 27 | 28 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /unix-source/chapter08/my_max.cc: -------------------------------------------------------------------------------- 1 | template 2 | T max(const T& left, const T& right) 3 | { 4 | return left > right ? left : right; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /unix-source/chapter08/split_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "split.h" 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::getline; 9 | using std::ostream_iterator; 10 | using std::string; 11 | 12 | int main() 13 | { 14 | string s; 15 | while (getline(cin, s)) 16 | split(s, ostream_iterator(cout, "\n")); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /unix-source/chapter09/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | #include 5 | #include 6 | 7 | class Student_info { 8 | public: 9 | Student_info(); // construct an empty `Student_info' object 10 | Student_info(std::istream&); // construct one by reading a stream 11 | std::string name() const { return n; } 12 | bool valid() const { return !homework.empty(); } 13 | 14 | // as defined in 9.2.1/157, and changed to read into `n' instead of `name' 15 | std::istream& read(std::istream&); 16 | 17 | double grade() const; // as defined in 9.2.1/158 18 | private: 19 | std::string n; 20 | double midterm, final; 21 | std::vector homework; 22 | }; 23 | 24 | bool compare(const Student_info&, const Student_info&); 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /unix-source/chapter09/Student_info_read1: -------------------------------------------------------------------------------- 1 | istream& Student_info::read(istream& in) 2 | { 3 | in >> name >> midterm >> final; 4 | read_hw(in, homework); 5 | return in; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /unix-source/chapter09/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter09/grade_headers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::domain_error; 5 | using std::vector; 6 | 7 | #include "grade.h" 8 | #include "median.h" 9 | 10 | -------------------------------------------------------------------------------- /unix-source/chapter09/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | CXXFLAGS = -I. -I../chapter08 4 | 5 | all: grading_prog 6 | 7 | Student_info.o: Student_info.cc Student_info.h 8 | 9 | grade.o: grade.cc ../chapter08/median.h Student_info.h 10 | 11 | grading_prog.o: grading_prog.cc grade.h ../chapter08/median.h Student_info.h 12 | 13 | grading_prog: grading_prog.o grade.o Student_info.o 14 | 15 | test: all 16 | ./grading_prog <../data/grades 17 | 18 | clobber: 19 | rm -f *.o *.exe core grading_prog 20 | -------------------------------------------------------------------------------- /unix-source/chapter10/concat_files.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cerr; 6 | using std::cout; 7 | using std::endl; 8 | using std::getline; 9 | using std::ifstream; 10 | using std::string; 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int fail_count = 0; 15 | // for each file in the input list 16 | for (int i = 1; i < argc; ++i) { 17 | ifstream in(argv[i]); 18 | 19 | // if it exists, write its contents, otherwise generate an error message 20 | if (in) { 21 | string s; 22 | while (getline(in, s)) 23 | cout << s << endl; 24 | } else { 25 | cerr << "cannot open file " << argv[i] << endl; 26 | ++fail_count; 27 | } 28 | } 29 | return fail_count; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /unix-source/chapter10/copyfile.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::endl; 5 | using std::getline; 6 | using std::ifstream; 7 | using std::ofstream; 8 | using std::string; 9 | 10 | int main() 11 | { 12 | ifstream infile("in"); 13 | ofstream outfile("out"); 14 | 15 | string s; 16 | 17 | while (getline(infile, s)) 18 | outfile << s << endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /unix-source/chapter10/in: -------------------------------------------------------------------------------- 1 | This is a sample data file 2 | -------------------------------------------------------------------------------- /unix-source/chapter10/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: concat_files copyfile letter_grade.o pointer_example write_args 5 | 6 | 7 | test: all 8 | ./concat_files ../data/name1 ../data/name2 9 | ./copyfile 10 | ./pointer_example 11 | ./write_args this is a test 12 | 13 | clobber: 14 | rm -f *.o *.exe out core concat_files copyfile pointer_example write_args 15 | -------------------------------------------------------------------------------- /unix-source/chapter10/pointer_example.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int x = 5; 9 | 10 | // `p' points to `x' 11 | int* p = &x; 12 | cout << "x = " << x << endl; 13 | 14 | // change the value of `x' through `p' 15 | *p = 6; 16 | cout << "x = " << x << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /unix-source/chapter10/write_args.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | // if there are command-line arguments, write them 9 | if (argc > 1) { 10 | cout << argv[1]; // write the first argument 11 | 12 | // write each remaining argument with a space before it 13 | for (int i = 2; i != argc; ++i) 14 | cout << " " << argv[i]; // `argv[i]' is a `char*' 15 | } 16 | cout << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /unix-source/chapter11/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /unix-source/chapter11/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /unix-source/chapter11/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /unix-source/chapter11/grade_check.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter11/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include "Vec.h" 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | 11 | template 12 | T median(Vec v) 13 | { 14 | #ifdef _MSC_VER 15 | typedef typename Vec::size_type vec_sz; 16 | #else 17 | typedef typename Vec::size_type vec_sz; 18 | #endif 19 | 20 | vec_sz size = v.size(); 21 | if (size == 0) 22 | throw domain_error("median of an empty Vec"); 23 | 24 | sort(v.begin(), v.end()); 25 | 26 | vec_sz mid = size/2; 27 | 28 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /unix-source/chapter11/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include 6 | 7 | Vec find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /unix-source/chapter11/urls_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (Vec::const_iterator i = v.begin(); 24 | #else 25 | for (Vec::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /unix-source/chapter12/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include "Str.h" 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | Str name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /unix-source/chapter12/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include "Str.h" 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const Str& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /unix-source/chapter12/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /unix-source/chapter12/grade_check.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter12/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /unix-source/chapter12/urls_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (Vec::const_iterator i = v.begin(); 24 | #else 25 | for (Vec::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /unix-source/chapter13/Student_info.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Core.h" 4 | #include "Student_info.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | delete cp; // delete previous object, if any 11 | 12 | char ch; 13 | is >> ch; // get record type 14 | 15 | if (ch == 'U') { 16 | cp = new Core(is); 17 | } else { 18 | cp = new Grad(is); 19 | } 20 | 21 | return is; 22 | } 23 | 24 | Student_info::Student_info(const Student_info& s): cp(0) 25 | { 26 | if (s.cp) cp = s.cp->clone(); 27 | } 28 | 29 | Student_info& Student_info::operator=(const Student_info& s) 30 | { 31 | if (&s != this) { 32 | delete cp; 33 | if (s.cp) 34 | cp = s.cp->clone(); 35 | else 36 | cp = 0; 37 | } 38 | return *this; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /unix-source/chapter13/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter13/read_hw.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unix-source/chapter14/Student_info.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | #include "Core.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | char ch; 11 | is >> ch; // get record type 12 | 13 | // allocate new object of the appropriate type 14 | // use `Handle(T*)' to build a `Handle' from the pointer to that object 15 | // call `Handle::operator=' to assign the `Handle' to the left-hand side 16 | if (ch == 'U') 17 | cp = new Core(is); 18 | else 19 | cp = new Grad(is); 20 | 21 | return is; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /unix-source/chapter14/Vec_clone.cc: -------------------------------------------------------------------------------- 1 | #include "Vec.h" 2 | 3 | template T* clone(const T* tp); 4 | // the key to making `Ptr< Vec >' work 5 | template<> 6 | Vec* clone(const Vec* vp) 7 | { 8 | return new Vec(*vp); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /unix-source/chapter14/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter14/read_hw.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unix-source/chapter14/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /unix-source/chapter14/urls_main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (Vec::const_iterator i = v.begin(); 24 | #else 25 | for (Vec::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /unix-source/chapter15/Pbase_pad: -------------------------------------------------------------------------------- 1 | static void pad(std::ostream& os, wd_sz beg, wd_sz end) { 2 | while (beg != end) { 3 | os << " "; 4 | ++beg; 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /unix-source/chapter15/main1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | Picture p(i); 20 | cout << p << endl; 21 | 22 | Picture q = frame(p); 23 | cout << q << endl; 24 | 25 | cout << frame(q) << endl; 26 | 27 | Picture r = hcat(p, q); 28 | cout << r << endl; 29 | 30 | Picture s = vcat(q, r); 31 | cout << s << endl << frame(s) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /unix-source/chapter15/main2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | 20 | Picture p(i); 21 | cout << p << endl; 22 | Picture q = p; 23 | cout << q << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /unix-source/chapter15/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | CXXFLAGS = -I. -I../chapter14 4 | 5 | all: main1 main2 6 | 7 | Pic.o: Pic.cc Pic.h 8 | 9 | main2.o: main2.cc Pic.h 10 | 11 | main2: main2.o Pic.o 12 | 13 | main1.o: main1.cc Pic.h 14 | 15 | main1: main1.o Pic.o 16 | 17 | 18 | test: all 19 | ./main1 20 | ./main2 21 | 22 | clobber: 23 | rm -f *.o *.exe core main1 main2 24 | -------------------------------------------------------------------------------- /unix-source/chapter16/Student_info.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Core.h" 4 | #include "Student_info.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | delete cp; // delete previous object, if any 11 | 12 | char ch; 13 | is >> ch; // get record type 14 | 15 | if (ch == 'U') { 16 | cp = new Core(is); 17 | } else { 18 | cp = new Grad(is); 19 | } 20 | 21 | return is; 22 | } 23 | 24 | Student_info::Student_info(const Student_info& s): cp(0) 25 | { 26 | if (s.cp) cp = s.cp->clone(); 27 | } 28 | 29 | Student_info& Student_info::operator=(const Student_info& s) 30 | { 31 | if (&s != this) { 32 | delete cp; 33 | if (s.cp) 34 | cp = s.cp->clone(); 35 | else 36 | cp = 0; 37 | } 38 | return *this; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /unix-source/chapter16/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /unix-source/chapter16/makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CC = g++ 3 | 4 | all: histo 5 | 6 | histo.o: histo.cc Pic.h Core.h median.h 7 | 8 | Pic.o: Pic.cc Pic.h 9 | $(CC) $(CXXFLAGS) -c Pic.cc 10 | 11 | Core.o: Core.cc Core.h 12 | $(CC) $(CXXFLAGS) -c Core.cc 13 | 14 | Student_info.o: Student_info.cc median.h Student_info.h Core.h 15 | $(CC) $(CXXFLAGS) -c Student_info.cc 16 | 17 | grade.o: grade.cc median.h Student_info.h 18 | $(CC) $(CXXFLAGS) -c grade.cc 19 | 20 | read_hw.o: read_hw.cc median.h Student_info.h 21 | $(CC) $(CXXFLAGS) -c read_hw.cc 22 | 23 | 24 | histo: histo.o Pic.o Core.o Student_info.o read_hw.o grade.o 25 | 26 | test: all 27 | ./histo <../data/kids_grades 28 | 29 | clobber: 30 | rm -f *.o *.exe core histo 31 | -------------------------------------------------------------------------------- /unix-source/chapter16/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | #ifdef _MSC_VER 15 | typedef typename std::vector::size_type vec_sz; 16 | #else 17 | typedef typename vector::size_type vec_sz; 18 | #endif 19 | 20 | vec_sz size = v.size(); 21 | if (size == 0) 22 | throw domain_error("median of an empty vector"); 23 | 24 | sort(v.begin(), v.end()); 25 | 26 | vec_sz mid = size/2; 27 | 28 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /unix-source/chapter16/read_hw.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /unix-source/data/grades: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | 23 | Fail1 45 55 65 80 90 70 65 60 24 | Fail2 55 55 65 50 55 60 65 60 25 | 26 | 27 | -------------------------------------------------------------------------------- /unix-source/data/grades_core: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /unix-source/data/grades_grad: -------------------------------------------------------------------------------- 1 | 2 | Moo 100 100 100 100 100 100 100 100 100 3 | Moore 75 85 77 59 0 85 75 89 75 4 | Norman 57 78 73 66 78 70 88 89 84 5 | Olson 89 86 70 90 55 73 80 84 99 6 | Peerson 47 70 82 73 50 87 73 71 84 7 | 8 | Russel 72 87 88 54 55 82 69 87 80 9 | Thomas 90 96 99 99 100 81 97 97 100 10 | Vaughn 81 97 99 67 40 90 70 96 76 11 | Westerly 43 98 96 79 100 82 97 96 96 12 | 13 | 14 | Baker 67 72 73 40 0 78 55 70 87 15 | Davis 77 70 82 65 70 77 83 81 80 16 | Edwards 77 72 73 80 90 93 75 90 92 17 | Franklin 47 70 82 73 50 87 73 71 72 18 | 19 | Jones 77 82 83 50 10 88 65 80 88 20 | Harris 97 90 92 95 100 87 93 91 94 21 | Smith 87 92 93 60 0 98 75 90 88 22 | Carpenter 47 90 92 73 100 87 93 91 85 23 | -------------------------------------------------------------------------------- /unix-source/data/grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /unix-source/data/kids_grades: -------------------------------------------------------------------------------- 1 | 2 | G Sarah 100 100 100 100 100 100 100 100 100 3 | U Lynn 75 85 77 59 0 85 75 89 4 | G James 57 78 73 66 78 70 88 89 84 5 | U Kevin 89 86 70 90 55 73 80 84 6 | U MaryKate 47 70 82 73 50 87 73 71 7 | 8 | U Pat 72 87 88 54 55 82 69 87 9 | G Paul 90 96 99 99 100 81 97 97 100 10 | U Rhia 81 97 99 67 40 90 70 96 11 | -------------------------------------------------------------------------------- /unix-source/data/mixed_grades: -------------------------------------------------------------------------------- 1 | G Moo 100 100 100 100 100 100 100 100 100 2 | U Moore 75 85 77 59 0 85 75 89 3 | G Norman 57 78 73 66 78 70 88 89 84 4 | U Olson 89 86 70 90 55 73 80 84 5 | U Peerson 47 70 82 73 50 87 73 71 6 | 7 | U Russel 72 87 88 54 55 82 69 87 8 | G Thomas 90 96 99 99 100 81 97 97 100 9 | U Vaughn 81 97 99 67 40 90 70 96 10 | U Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | G Baker 67 72 73 40 0 78 55 70 87 14 | G Davis 77 70 82 65 70 77 83 81 80 15 | G Edwards 77 72 73 80 90 93 75 90 92 16 | U Franklin 47 70 82 73 50 87 73 71 17 | 18 | U Jones 77 82 83 50 10 88 65 80 19 | G Harris 97 90 92 95 100 87 93 91 94 20 | U Smith 87 92 93 60 0 98 75 90 21 | U Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /unix-source/data/name1: -------------------------------------------------------------------------------- 1 | Vladimir 2 | -------------------------------------------------------------------------------- /unix-source/data/name2: -------------------------------------------------------------------------------- 1 | Estragon 2 | -------------------------------------------------------------------------------- /unix-source/data/numbers: -------------------------------------------------------------------------------- 1 | 22 23 5 -1 -2 0 1 2 3 4 2 | -------------------------------------------------------------------------------- /unix-source/data/orig_grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /unix-source/data/palins_input: -------------------------------------------------------------------------------- 1 | madam 2 | eye 3 | civic 4 | level 5 | rotor 6 | -------------------------------------------------------------------------------- /unix-source/data/single_grade: -------------------------------------------------------------------------------- 1 | harriet 85 95 88 87 93 45 76 99 2 | -------------------------------------------------------------------------------- /unix-source/makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/sh 2 | CXX = g++ 3 | CC = g++ 4 | 5 | SUBS = chapter00 chapter01 chapter02 chapter03 chapter04 \ 6 | chapter05 chapter06 chapter07 chapter08 chapter09 \ 7 | chapter10 chapter11 chapter12 chapter13 chapter14 \ 8 | chapter15 chapter16 9 | 10 | all: 11 | -for c in $(SUBS); do echo "=== $$c =="; (cd $$c; $(MAKE) "CXX=$(CXX)" "CC=$(CC)") done 12 | 13 | test: 14 | -for c in $(SUBS); do echo "=== $$c =="; (cd $$c; $(MAKE) "CXX=$(CXX)" "CC=$(CC)" test); done 15 | 16 | clobber: 17 | -for c in $(SUBS); do echo "=== $$c =="; (cd $$c; $(MAKE) "CXX=$(CXX)" "CC=$(CC)" clobber);done 18 | -------------------------------------------------------------------------------- /win-net-source/chapter00/hello.cpp: -------------------------------------------------------------------------------- 1 | // a small C++ program 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "Hello, world!" << std::endl; 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /win-net-source/chapter01/greet.cpp: -------------------------------------------------------------------------------- 1 | // ask for a person's name, and greet the person 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // ask for the person's name 8 | std::cout << "Please enter your first name: "; 9 | 10 | // read the name 11 | std::string name; // define `name' 12 | std::cin >> name; // read into `name' 13 | 14 | // write a greeting 15 | std::cout << "Hello, " << name << "!" << std::endl; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /win-net-source/chapter04/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | std::vector homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, std::vector&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /win-net-source/chapter04/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const std::vector&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /win-net-source/chapter04/grade1: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades and homework grade 2 | double grade(double midterm, double final, double homework) 3 | { 4 | return 0.2 * midterm + 0.4 * final + 0.4 * homework; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /win-net-source/chapter04/grade2: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades 2 | // and vector of homework grades. 3 | // this function does not copy its argument, because `median' does so for us. 4 | double grade(double midterm, double final, const vector& hw) 5 | { 6 | if (hw.size() == 0) 7 | throw domain_error("student has done no homework"); 8 | return grade(midterm, final, median(hw)); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /win-net-source/chapter04/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | // `median.h'--final version 5 | #include 6 | double median(std::vector); 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /win-net-source/chapter04/read_hw: -------------------------------------------------------------------------------- 1 | // read homework grades from an input stream into a `vector' 2 | istream& read_hw(istream& in, vector& hw) 3 | { 4 | if (in) { 5 | // get rid of previous contents 6 | hw.clear(); 7 | 8 | // read homework grades 9 | double x; 10 | while (in >> x) 11 | hw.push_back(x); 12 | 13 | // clear the stream so that input will work for the next student 14 | in.clear(); 15 | } 16 | return in; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /win-net-source/chapter05/fails_iters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // version 3: iterators but no indexing; still potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | vector::iterator iter = students.begin(); 12 | 13 | while (iter != students.end()) { 14 | if (fgrade(*iter)) { 15 | fail.push_back(*iter); 16 | iter = students.erase(iter); 17 | } else 18 | ++iter; 19 | } 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /win-net-source/chapter05/fails_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::list; 6 | // version 4: use `list' instead of `vector' 7 | list extract_fails(list& students) 8 | { 9 | list fail; 10 | list::iterator iter = students.begin(); 11 | 12 | while (iter != students.end()) { 13 | if (fgrade(*iter)) { 14 | fail.push_back(*iter); 15 | iter = students.erase(iter); 16 | } else 17 | ++iter; 18 | } 19 | return fail; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /win-net-source/chapter05/fails_vec1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // separate passing and failing student records: first try 8 | vector extract_fails(vector& students) 9 | { 10 | vector pass, fail; 11 | 12 | for (vector::size_type i = 0; 13 | i != students.size(); ++i) 14 | if (fgrade(students[i])) 15 | fail.push_back(students[i]); 16 | else 17 | pass.push_back(students[i]); 18 | 19 | students = pass; 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /win-net-source/chapter05/fails_vec2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // second try: correct but potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | vector::size_type i = 0; 12 | 13 | // invariant: elements `[0,' `i)' of `students' represent passing grades 14 | while (i != students.size()) { 15 | if (fgrade(students[i])) { 16 | fail.push_back(students[i]); 17 | students.erase(students.begin() + i); 18 | } else 19 | ++i; 20 | } 21 | return fail; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /win-net-source/chapter05/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool fgrade(const Student_info&); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /win-net-source/chapter05/pics.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_pics_h 2 | #define GUARD_pics_h 3 | 4 | #include 5 | #include 6 | 7 | std::string::size_type width(const std::vector& v); 8 | 9 | std::vector frame(const std::vector& v); 10 | 11 | std::vector vcat(const std::vector& top, 12 | const std::vector& bottom); 13 | 14 | std::vector hcat(const std::vector& left, 15 | const std::vector& right); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-net-source/chapter05/split.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_split_h 2 | #define GUARD_split_h 3 | 4 | #include 5 | #include 6 | std::vector split(const std::string&); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /win-net-source/chapter05/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "split.h" 7 | 8 | using std::cin; 9 | using std::cout; 10 | using std::endl; 11 | using std::getline; 12 | using std::string; 13 | using std::vector; 14 | 15 | using std::isspace; 16 | 17 | int main() 18 | { 19 | string s; 20 | 21 | // read and split each line of input 22 | while (getline(cin, s)) { 23 | vector v = split(s); 24 | 25 | // write each word in `v' 26 | for (vector::size_type i = 0; i != v.size(); ++i) 27 | cout << v[i] << endl; 28 | } 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /win-net-source/chapter05/split_main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s; 11 | while (cin >> s) 12 | cout << s << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /win-net-source/chapter06/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const std::vector& students); 10 | double median_analysis(const std::vector& students); 11 | double optimistic_median_analysis(const std::vector& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const std::vector&), 14 | const std::vector& did, 15 | const std::vector& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-net-source/chapter06/ext_fails1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Student_info.h" 6 | #include "grade.h" 7 | 8 | using std::back_inserter; 9 | using std::remove_copy_if; 10 | using std::remove_if; 11 | using std::vector; 12 | 13 | vector 14 | extract_fails(vector& students) { 15 | vector fail; 16 | remove_copy_if(students.begin(), students.end(), 17 | back_inserter(fail), pgrade); 18 | students.erase(remove_if(students.begin(), students.end(), 19 | fgrade), students.end()); 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /win-net-source/chapter06/ext_fails2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Student_info.h" 5 | #include "grade.h" 6 | 7 | using std::stable_partition; 8 | using std::vector; 9 | 10 | vector 11 | extract_fails(vector& students) 12 | { 13 | vector::iterator iter = 14 | stable_partition(students.begin(), students.end(), pgrade); 15 | vector fail(iter, students.end()); 16 | students.erase(iter, students.end()); 17 | 18 | return fail; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /win-net-source/chapter06/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool pgrade(const Student_info&); 12 | bool fgrade(const Student_info&); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /win-net-source/chapter06/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter06/palin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::cin; using std::cout; 7 | using std::endl; using std::equal; 8 | using std::string; using std::transform; 9 | 10 | using std::isspace; 11 | 12 | bool is_palindrome(const string& s) 13 | { 14 | return equal(s.begin(), s.end(), s.rbegin()); 15 | } 16 | 17 | int main() 18 | { 19 | string s; 20 | while (cin >> s) { 21 | if (is_palindrome(s)) 22 | cout << s << " is a palindrome" << endl; 23 | else 24 | cout << s << " is not a palindrome" << endl; 25 | } 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /win-net-source/chapter06/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include 5 | #include 6 | 7 | std::vector find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-net-source/chapter06/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | using std::vector; 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | vector v = find_urls(s); 22 | for (vector::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /win-net-source/chapter07/wc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cin; 6 | using std::cout; 7 | using std::endl; 8 | using std::map; 9 | using std::string; 10 | 11 | int main() 12 | { 13 | string s; 14 | map counters; // store each word and an associated counter 15 | 16 | // read the input, keeping track of each word and how often we see it 17 | while (cin >> s) 18 | ++counters[s]; 19 | 20 | // write the words and associated counts 21 | for (map::const_iterator it = counters.begin(); 22 | it != counters.end(); ++it) { 23 | cout << it->first << "\t" << it->second << endl; 24 | } 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /win-net-source/chapter08/find2.h: -------------------------------------------------------------------------------- 1 | // This file contains the implementation of the find2 function. 2 | // This function is not actually used elsewhere in the book. 3 | 4 | template In find(In begin, In end, const X& x) 5 | { 6 | if (begin == end || *begin == x) 7 | return begin; 8 | begin++; 9 | return find(begin, end, x); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter08/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | typedef typename vector::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty vector"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /win-net-source/chapter08/my_max.cpp: -------------------------------------------------------------------------------- 1 | template 2 | T max(const T& left, const T& right) 3 | { 4 | return left > right ? left : right; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /win-net-source/chapter08/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "split.h" 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::getline; 9 | using std::ostream_iterator; 10 | using std::string; 11 | 12 | int main() 13 | { 14 | string s; 15 | while (getline(cin, s)) 16 | split(s, ostream_iterator(cout, "\n")); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /win-net-source/chapter09/Student_info_read1: -------------------------------------------------------------------------------- 1 | istream& Student_info::read(istream& in) 2 | { 3 | in >> name >> midterm >> final; 4 | read_hw(in, homework); 5 | return in; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /win-net-source/chapter09/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter09/grade_headers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::domain_error; 5 | using std::vector; 6 | 7 | #include "grade.h" 8 | #include "median.h" 9 | 10 | -------------------------------------------------------------------------------- /win-net-source/chapter10/concat_files.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cerr; 6 | using std::cout; 7 | using std::endl; 8 | using std::getline; 9 | using std::ifstream; 10 | using std::string; 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int fail_count = 0; 15 | // for each file in the input list 16 | for (int i = 1; i < argc; ++i) { 17 | ifstream in(argv[i]); 18 | 19 | // if it exists, write its contents, otherwise generate an error message 20 | if (in) { 21 | string s; 22 | while (getline(in, s)) 23 | cout << s << endl; 24 | } else { 25 | cerr << "cannot open file " << argv[i] << endl; 26 | ++fail_count; 27 | } 28 | } 29 | return fail_count; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /win-net-source/chapter10/copyfile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::endl; 5 | using std::getline; 6 | using std::ifstream; 7 | using std::ofstream; 8 | using std::string; 9 | 10 | int main() 11 | { 12 | ifstream infile("in"); 13 | ofstream outfile("out"); 14 | 15 | string s; 16 | 17 | while (getline(infile, s)) 18 | outfile << s << endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /win-net-source/chapter10/in: -------------------------------------------------------------------------------- 1 | This is a sample data file 2 | -------------------------------------------------------------------------------- /win-net-source/chapter10/pointer_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int x = 5; 9 | 10 | // `p' points to `x' 11 | int* p = &x; 12 | cout << "x = " << x << endl; 13 | 14 | // change the value of `x' through `p' 15 | *p = 6; 16 | cout << "x = " << x << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /win-net-source/chapter10/write_args.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | // if there are command-line arguments, write them 9 | if (argc > 1) { 10 | cout << argv[1]; // write the first argument 11 | 12 | // write each remaining argument with a space before it 13 | for (int i = 2; i != argc; ++i) 14 | cout << " " << argv[i]; // `argv[i]' is a `char*' 15 | } 16 | cout << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /win-net-source/chapter11/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /win-net-source/chapter11/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-net-source/chapter11/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /win-net-source/chapter11/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter11/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include "Vec.h" 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | 11 | template 12 | T median(Vec v) 13 | { 14 | typedef typename Vec::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty Vec"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /win-net-source/chapter11/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include 6 | 7 | Vec find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-net-source/chapter11/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /win-net-source/chapter12/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include "Str.h" 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | Str name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /win-net-source/chapter12/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include "Str.h" 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const Str& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-net-source/chapter12/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /win-net-source/chapter12/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter12/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-net-source/chapter12/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /win-net-source/chapter13/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter13/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-net-source/chapter14/Student_info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | #include "Core.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | char ch; 11 | is >> ch; // get record type 12 | 13 | // allocate new object of the appropriate type 14 | // use `Handle(T*)' to build a `Handle' from the pointer to that object 15 | // call `Handle::operator=' to assign the `Handle' to the left-hand side 16 | if (ch == 'U') 17 | cp = new Core(is); 18 | else 19 | cp = new Grad(is); 20 | 21 | return is; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /win-net-source/chapter14/Vec_clone.cpp: -------------------------------------------------------------------------------- 1 | #include "Vec.h" 2 | 3 | template T* clone(const T* tp); 4 | // the key to making `Ptr< Vec >' work 5 | template<> 6 | Vec* clone(const Vec* vp) 7 | { 8 | return new Vec(*vp); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /win-net-source/chapter14/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter14/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-net-source/chapter14/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-net-source/chapter14/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | for (Vec::const_iterator i = v.begin(); 23 | i != v.end(); ++i) 24 | cout << *i << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /win-net-source/chapter15/Pbase_pad: -------------------------------------------------------------------------------- 1 | static void pad(std::ostream& os, wd_sz beg, wd_sz end) { 2 | while (beg != end) { 3 | os << " "; 4 | ++beg; 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /win-net-source/chapter15/main1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | Picture p(i); 20 | cout << p << endl; 21 | 22 | Picture q = frame(p); 23 | cout << q << endl; 24 | 25 | cout << frame(q) << endl; 26 | 27 | Picture r = hcat(p, q); 28 | cout << r << endl; 29 | 30 | Picture s = vcat(q, r); 31 | cout << s << endl << frame(s) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /win-net-source/chapter15/main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | 20 | Picture p(i); 21 | cout << p << endl; 22 | Picture q = p; 23 | cout << q << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /win-net-source/chapter16/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-net-source/chapter16/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | typedef typename vector::size_type vec_sz; 15 | 16 | vec_sz size = v.size(); 17 | if (size == 0) 18 | throw domain_error("median of an empty vector"); 19 | 20 | sort(v.begin(), v.end()); 21 | 22 | vec_sz mid = size/2; 23 | 24 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 25 | } 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /win-net-source/chapter16/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-net-source/data/grades: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | 23 | Fail1 45 55 65 80 90 70 65 60 24 | Fail2 55 55 65 50 55 60 65 60 25 | 26 | 27 | -------------------------------------------------------------------------------- /win-net-source/data/grades_core: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /win-net-source/data/grades_grad: -------------------------------------------------------------------------------- 1 | 2 | Moo 100 100 100 100 100 100 100 100 100 3 | Moore 75 85 77 59 0 85 75 89 75 4 | Norman 57 78 73 66 78 70 88 89 84 5 | Olson 89 86 70 90 55 73 80 84 99 6 | Peerson 47 70 82 73 50 87 73 71 84 7 | 8 | Russel 72 87 88 54 55 82 69 87 80 9 | Thomas 90 96 99 99 100 81 97 97 100 10 | Vaughn 81 97 99 67 40 90 70 96 76 11 | Westerly 43 98 96 79 100 82 97 96 96 12 | 13 | 14 | Baker 67 72 73 40 0 78 55 70 87 15 | Davis 77 70 82 65 70 77 83 81 80 16 | Edwards 77 72 73 80 90 93 75 90 92 17 | Franklin 47 70 82 73 50 87 73 71 72 18 | 19 | Jones 77 82 83 50 10 88 65 80 88 20 | Harris 97 90 92 95 100 87 93 91 94 21 | Smith 87 92 93 60 0 98 75 90 88 22 | Carpenter 47 90 92 73 100 87 93 91 85 23 | -------------------------------------------------------------------------------- /win-net-source/data/grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /win-net-source/data/kids_grades: -------------------------------------------------------------------------------- 1 | 2 | G Sarah 100 100 100 100 100 100 100 100 100 3 | U Lynn 75 85 77 59 0 85 75 89 4 | G James 57 78 73 66 78 70 88 89 84 5 | U Kevin 89 86 70 90 55 73 80 84 6 | U MaryKate 47 70 82 73 50 87 73 71 7 | 8 | U Pat 72 87 88 54 55 82 69 87 9 | G Paul 90 96 99 99 100 81 97 97 100 10 | U Rhia 81 97 99 67 40 90 70 96 11 | -------------------------------------------------------------------------------- /win-net-source/data/mixed_grades: -------------------------------------------------------------------------------- 1 | G Moo 100 100 100 100 100 100 100 100 100 2 | U Moore 75 85 77 59 0 85 75 89 3 | G Norman 57 78 73 66 78 70 88 89 84 4 | U Olson 89 86 70 90 55 73 80 84 5 | U Peerson 47 70 82 73 50 87 73 71 6 | 7 | U Russel 72 87 88 54 55 82 69 87 8 | G Thomas 90 96 99 99 100 81 97 97 100 9 | U Vaughn 81 97 99 67 40 90 70 96 10 | U Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | G Baker 67 72 73 40 0 78 55 70 87 14 | G Davis 77 70 82 65 70 77 83 81 80 15 | G Edwards 77 72 73 80 90 93 75 90 92 16 | U Franklin 47 70 82 73 50 87 73 71 17 | 18 | U Jones 77 82 83 50 10 88 65 80 19 | G Harris 97 90 92 95 100 87 93 91 94 20 | U Smith 87 92 93 60 0 98 75 90 21 | U Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /win-net-source/data/name1: -------------------------------------------------------------------------------- 1 | Vladimir 2 | -------------------------------------------------------------------------------- /win-net-source/data/name2: -------------------------------------------------------------------------------- 1 | Estragon 2 | -------------------------------------------------------------------------------- /win-net-source/data/numbers: -------------------------------------------------------------------------------- 1 | 22 23 5 -1 -2 0 1 2 3 4 2 | -------------------------------------------------------------------------------- /win-net-source/data/orig_grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /win-net-source/data/palins_input: -------------------------------------------------------------------------------- 1 | madam 2 | eye 3 | civic 4 | level 5 | rotor 6 | -------------------------------------------------------------------------------- /win-net-source/data/single_grade: -------------------------------------------------------------------------------- 1 | harriet 85 95 88 87 93 45 76 99 2 | -------------------------------------------------------------------------------- /win-source/chapter00/hello.cpp: -------------------------------------------------------------------------------- 1 | // a small C++ program 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "Hello, world!" << std::endl; 7 | return 0; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /win-source/chapter01/greet.cpp: -------------------------------------------------------------------------------- 1 | // ask for a person's name, and greet the person 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // ask for the person's name 8 | std::cout << "Please enter your first name: "; 9 | 10 | // read the name 11 | std::string name; // define `name' 12 | std::cin >> name; // read into `name' 13 | 14 | // write a greeting 15 | std::cout << "Hello, " << name << "!" << std::endl; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /win-source/chapter04/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | std::vector homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, std::vector&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /win-source/chapter04/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const std::vector&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /win-source/chapter04/grade1: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades and homework grade 2 | double grade(double midterm, double final, double homework) 3 | { 4 | return 0.2 * midterm + 0.4 * final + 0.4 * homework; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /win-source/chapter04/grade2: -------------------------------------------------------------------------------- 1 | // compute a student's overall grade from midterm and final exam grades 2 | // and vector of homework grades. 3 | // this function does not copy its argument, because `median' does so for us. 4 | double grade(double midterm, double final, const vector& hw) 5 | { 6 | if (hw.size() == 0) 7 | throw domain_error("student has done no homework"); 8 | return grade(midterm, final, median(hw)); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /win-source/chapter04/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | // `median.h'--final version 5 | #include 6 | double median(std::vector); 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /win-source/chapter04/read_hw: -------------------------------------------------------------------------------- 1 | // read homework grades from an input stream into a `vector' 2 | istream& read_hw(istream& in, vector& hw) 3 | { 4 | if (in) { 5 | // get rid of previous contents 6 | hw.clear(); 7 | 8 | // read homework grades 9 | double x; 10 | while (in >> x) 11 | hw.push_back(x); 12 | 13 | // clear the stream so that input will work for the next student 14 | in.clear(); 15 | } 16 | return in; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /win-source/chapter05/fails_iters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // version 3: iterators but no indexing; still potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | #ifdef _MSC_VER 12 | std::vector::iterator iter = students.begin(); 13 | #else 14 | vector::iterator iter = students.begin(); 15 | #endif 16 | 17 | while (iter != students.end()) { 18 | if (fgrade(*iter)) { 19 | fail.push_back(*iter); 20 | iter = students.erase(iter); 21 | } else 22 | ++iter; 23 | } 24 | return fail; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /win-source/chapter05/fails_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::list; 6 | // version 4: use `list' instead of `vector' 7 | list extract_fails(list& students) 8 | { 9 | list fail; 10 | #ifdef _MSC_VER 11 | std::list::iterator iter = students.begin(); 12 | #else 13 | list::iterator iter = students.begin(); 14 | #endif 15 | 16 | while (iter != students.end()) { 17 | if (fgrade(*iter)) { 18 | fail.push_back(*iter); 19 | iter = students.erase(iter); 20 | } else 21 | ++iter; 22 | } 23 | return fail; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-source/chapter05/fails_vec1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // separate passing and failing student records: first try 8 | vector extract_fails(vector& students) 9 | { 10 | vector pass, fail; 11 | 12 | #ifdef _MSC_VER 13 | for (std::vector::size_type i = 0; 14 | #else 15 | for (vector::size_type i = 0; 16 | #endif 17 | i != students.size(); ++i) 18 | if (fgrade(students[i])) 19 | fail.push_back(students[i]); 20 | else 21 | pass.push_back(students[i]); 22 | 23 | students = pass; 24 | return fail; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /win-source/chapter05/fails_vec2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Student_info.h" 3 | #include "grade.h" 4 | 5 | using std::vector; 6 | 7 | // second try: correct but potentially slow 8 | vector extract_fails(vector& students) 9 | { 10 | vector fail; 11 | #ifdef _MSC_VER 12 | std::vector::size_type i = 0; 13 | #else 14 | vector::size_type i = 0; 15 | #endif 16 | 17 | // invariant: elements `[0,' `i)' of `students' represent passing grades 18 | while (i != students.size()) { 19 | if (fgrade(students[i])) { 20 | fail.push_back(students[i]); 21 | students.erase(students.begin() + i); 22 | } else 23 | ++i; 24 | } 25 | return fail; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /win-source/chapter05/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool fgrade(const Student_info&); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /win-source/chapter05/pics.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_pics_h 2 | #define GUARD_pics_h 3 | 4 | #include 5 | #include 6 | 7 | std::string::size_type width(const std::vector& v); 8 | 9 | std::vector frame(const std::vector& v); 10 | 11 | std::vector vcat(const std::vector& top, 12 | const std::vector& bottom); 13 | 14 | std::vector hcat(const std::vector& left, 15 | const std::vector& right); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-source/chapter05/split.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_split_h 2 | #define GUARD_split_h 3 | 4 | #include 5 | #include 6 | std::vector split(const std::string&); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /win-source/chapter05/split_main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s; 11 | while (cin >> s) 12 | cout << s << endl; 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /win-source/chapter06/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const std::vector& students); 10 | double median_analysis(const std::vector& students); 11 | double optimistic_median_analysis(const std::vector& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const std::vector&), 14 | const std::vector& did, 15 | const std::vector& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-source/chapter06/ext_fails1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Student_info.h" 6 | #include "grade.h" 7 | 8 | using std::back_inserter; 9 | using std::remove_copy_if; 10 | using std::remove_if; 11 | using std::vector; 12 | 13 | vector 14 | extract_fails(vector& students) { 15 | vector fail; 16 | remove_copy_if(students.begin(), students.end(), 17 | back_inserter(fail), pgrade); 18 | students.erase(remove_if(students.begin(), students.end(), 19 | fgrade), students.end()); 20 | return fail; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /win-source/chapter06/ext_fails2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "Student_info.h" 5 | #include "grade.h" 6 | 7 | using std::stable_partition; 8 | using std::vector; 9 | 10 | vector 11 | extract_fails(vector& students) 12 | { 13 | #ifdef _MSC_VER 14 | std::vector::iterator iter = 15 | #else 16 | vector::iterator iter = 17 | #endif 18 | stable_partition(students.begin(), students.end(), pgrade); 19 | vector fail(iter, students.end()); 20 | students.erase(iter, students.end()); 21 | 22 | return fail; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /win-source/chapter06/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "Student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | bool pgrade(const Student_info&); 12 | bool fgrade(const Student_info&); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /win-source/chapter06/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter06/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include 5 | #include 6 | 7 | std::vector find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-source/chapter08/find2.h: -------------------------------------------------------------------------------- 1 | // This file contains the implementation of the find2 function. 2 | // This function is not actually used elsewhere in the book. 3 | 4 | template In find(In begin, In end, const X& x) 5 | { 6 | if (begin == end || *begin == x) 7 | return begin; 8 | begin++; 9 | return find(begin, end, x); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter08/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | #ifdef _MSC_VER 15 | typedef typename std::vector::size_type vec_sz; 16 | #else 17 | typedef typename vector::size_type vec_sz; 18 | #endif 19 | 20 | vec_sz size = v.size(); 21 | if (size == 0) 22 | throw domain_error("median of an empty vector"); 23 | 24 | sort(v.begin(), v.end()); 25 | 26 | vec_sz mid = size/2; 27 | 28 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /win-source/chapter08/my_max.cpp: -------------------------------------------------------------------------------- 1 | template 2 | T max(const T& left, const T& right) 3 | { 4 | return left > right ? left : right; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /win-source/chapter08/split_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "split.h" 5 | 6 | using std::cin; 7 | using std::cout; 8 | using std::getline; 9 | using std::ostream_iterator; 10 | using std::string; 11 | 12 | int main() 13 | { 14 | string s; 15 | while (getline(cin, s)) 16 | split(s, ostream_iterator(cout, "\n")); 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /win-source/chapter09/Student_info_read1: -------------------------------------------------------------------------------- 1 | istream& Student_info::read(istream& in) 2 | { 3 | in >> name >> midterm >> final; 4 | read_hw(in, homework); 5 | return in; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /win-source/chapter09/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter09/grade_headers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::domain_error; 5 | using std::vector; 6 | 7 | #include "grade.h" 8 | #include "median.h" 9 | 10 | -------------------------------------------------------------------------------- /win-source/chapter10/concat_files.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cerr; 6 | using std::cout; 7 | using std::endl; 8 | using std::getline; 9 | using std::ifstream; 10 | using std::string; 11 | 12 | int main(int argc, char **argv) 13 | { 14 | int fail_count = 0; 15 | // for each file in the input list 16 | for (int i = 1; i < argc; ++i) { 17 | ifstream in(argv[i]); 18 | 19 | // if it exists, write its contents, otherwise generate an error message 20 | if (in) { 21 | string s; 22 | while (getline(in, s)) 23 | cout << s << endl; 24 | } else { 25 | cerr << "cannot open file " << argv[i] << endl; 26 | ++fail_count; 27 | } 28 | } 29 | return fail_count; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /win-source/chapter10/copyfile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::endl; 5 | using std::getline; 6 | using std::ifstream; 7 | using std::ofstream; 8 | using std::string; 9 | 10 | int main() 11 | { 12 | ifstream infile("in"); 13 | ofstream outfile("out"); 14 | 15 | string s; 16 | 17 | while (getline(infile, s)) 18 | outfile << s << endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /win-source/chapter10/in: -------------------------------------------------------------------------------- 1 | This is a sample data file 2 | -------------------------------------------------------------------------------- /win-source/chapter10/pointer_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int x = 5; 9 | 10 | // `p' points to `x' 11 | int* p = &x; 12 | cout << "x = " << x << endl; 13 | 14 | // change the value of `x' through `p' 15 | *p = 6; 16 | cout << "x = " << x << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /win-source/chapter10/write_args.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | // if there are command-line arguments, write them 9 | if (argc > 1) { 10 | cout << argv[1]; // write the first argument 11 | 12 | // write each remaining argument with a space before it 13 | for (int i = 2; i != argc; ++i) 14 | cout << " " << argv[i]; // `argv[i]' is a `char*' 15 | } 16 | cout << endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /win-source/chapter11/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | std::string name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /win-source/chapter11/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const std::string& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-source/chapter11/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /win-source/chapter11/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter11/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include "Vec.h" 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | 11 | template 12 | T median(Vec v) 13 | { 14 | #ifdef _MSC_VER 15 | typedef typename Vec::size_type vec_sz; 16 | #else 17 | typedef typename Vec::size_type vec_sz; 18 | #endif 19 | 20 | vec_sz size = v.size(); 21 | if (size == 0) 22 | throw domain_error("median of an empty Vec"); 23 | 24 | sort(v.begin(), v.end()); 25 | 26 | vec_sz mid = size/2; 27 | 28 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /win-source/chapter11/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include 6 | 7 | Vec find_urls(const std::string& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-source/chapter11/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | using std::getline; 14 | using std::string; 15 | 16 | 17 | 18 | int main() { 19 | string s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (Vec::const_iterator i = v.begin(); 24 | #else 25 | for (Vec::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /win-source/chapter12/Student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | // `Student_info.h' header file 5 | #include 6 | #include "Str.h" 7 | #include "Vec.h" 8 | 9 | struct Student_info { 10 | Str name; 11 | double midterm, final; 12 | Vec homework; 13 | }; 14 | 15 | bool compare(const Student_info&, const Student_info&); 16 | std::istream& read(std::istream&, Student_info&); 17 | std::istream& read_hw(std::istream&, Vec&); 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /win-source/chapter12/analysis.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_analysis_h 2 | #define GUARD_analysis_h 3 | #include "Str.h" 4 | #include "Vec.h" 5 | 6 | #include "Student_info.h" 7 | 8 | bool did_all_hw(const Student_info& s); 9 | double average_analysis(const Vec& students); 10 | double median_analysis(const Vec& students); 11 | double optimistic_median_analysis(const Vec& students); 12 | void write_analysis(std::ostream& out, const Str& name, 13 | double analysis(const Vec&), 14 | const Vec& did, 15 | const Vec& didnt); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /win-source/chapter12/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include "Vec.h" 6 | #include "Student_info.h" 7 | 8 | double grade(double, double, double); 9 | double grade(double, double, const Vec&); 10 | double grade(const Student_info&); 11 | 12 | #endif 13 | 14 | -------------------------------------------------------------------------------- /win-source/chapter12/grade_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | 5 | using std::find; 6 | bool did_all_hw(const Student_info& s) 7 | { 8 | return ((find(s.homework.begin(), s.homework.end(), 0)) 9 | == s.homework.end()); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter12/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-source/chapter12/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (Vec::const_iterator i = v.begin(); 24 | #else 25 | for (Vec::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /win-source/chapter13/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter13/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-source/chapter14/Student_info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Student_info.h" 4 | #include "Core.h" 5 | 6 | using std::istream; 7 | 8 | istream& Student_info::read(istream& is) 9 | { 10 | char ch; 11 | is >> ch; // get record type 12 | 13 | // allocate new object of the appropriate type 14 | // use `Handle(T*)' to build a `Handle' from the pointer to that object 15 | // call `Handle::operator=' to assign the `Handle' to the left-hand side 16 | if (ch == 'U') 17 | cp = new Core(is); 18 | else 19 | cp = new Grad(is); 20 | 21 | return is; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /win-source/chapter14/Vec_clone.cpp: -------------------------------------------------------------------------------- 1 | #include "Vec.h" 2 | 3 | template T* clone(const T* tp); 4 | // the key to making `Ptr< Vec >' work 5 | template<> 6 | Vec* clone(const Vec* vp) 7 | { 8 | return new Vec(*vp); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /win-source/chapter14/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter14/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-source/chapter14/urls.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_urls_h 2 | #define GUARD_urls_h 3 | 4 | #include "Vec.h" 5 | #include "Str.h" 6 | 7 | Vec find_urls(const Str& s); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /win-source/chapter14/urls_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Str.h" 5 | #include "Vec.h" 6 | 7 | #include "urls.h" 8 | 9 | using std::cout; 10 | using std::cin; 11 | using std::endl; 12 | using std::find_if; 13 | 14 | 15 | 16 | 17 | 18 | int main() { 19 | Str s; 20 | while (getline(cin, s)) { 21 | Vec v = find_urls(s); 22 | #ifdef _MSC_VER 23 | for (Vec::const_iterator i = v.begin(); 24 | #else 25 | for (Vec::const_iterator i = v.begin(); 26 | #endif 27 | i != v.end(); ++i) 28 | cout << *i << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /win-source/chapter15/Pbase_pad: -------------------------------------------------------------------------------- 1 | static void pad(std::ostream& os, wd_sz beg, wd_sz end) { 2 | while (beg != end) { 3 | os << " "; 4 | ++beg; 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /win-source/chapter15/main1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | Picture p(i); 20 | cout << p << endl; 21 | 22 | Picture q = frame(p); 23 | cout << q << endl; 24 | 25 | cout << frame(q) << endl; 26 | 27 | Picture r = hcat(p, q); 28 | cout << r << endl; 29 | 30 | Picture s = vcat(q, r); 31 | cout << s << endl << frame(s) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /win-source/chapter15/main2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Pic.h" 6 | 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | 12 | char *init[] = { "Paris", "in the", "Spring" }; 13 | 14 | int main() 15 | { 16 | vector i; 17 | for (int j = 0; j < 3; ++j) 18 | i.push_back(init[j]); 19 | 20 | Picture p(i); 21 | cout << p << endl; 22 | Picture q = p; 23 | cout << q << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /win-source/chapter16/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | // `grade.h' 5 | #include 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | 10 | #endif 11 | 12 | -------------------------------------------------------------------------------- /win-source/chapter16/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::domain_error; 9 | using std::sort; 10 | using std::vector; 11 | template 12 | T median(vector v) 13 | { 14 | #ifdef _MSC_VER 15 | typedef typename std::vector::size_type vec_sz; 16 | #else 17 | typedef typename vector::size_type vec_sz; 18 | #endif 19 | 20 | vec_sz size = v.size(); 21 | if (size == 0) 22 | throw domain_error("median of an empty vector"); 23 | 24 | sort(v.begin(), v.end()); 25 | 26 | vec_sz mid = size/2; 27 | 28 | return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid]; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /win-source/chapter16/read_hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::istream; 5 | using std::vector; 6 | 7 | 8 | // read homework grades from an input stream into a `vector' 9 | istream& read_hw(istream& in, vector& hw) 10 | { 11 | if (in) { 12 | // get rid of previous contents 13 | hw.clear(); 14 | 15 | // read homework grades 16 | double x; 17 | while (in >> x) 18 | hw.push_back(x); 19 | 20 | // clear the stream so that input will work for the next student 21 | in.clear(); 22 | } 23 | return in; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /win-source/data/grades: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | 23 | Fail1 45 55 65 80 90 70 65 60 24 | Fail2 55 55 65 50 55 60 65 60 25 | 26 | 27 | -------------------------------------------------------------------------------- /win-source/data/grades_core: -------------------------------------------------------------------------------- 1 | Moo 100 100 100 100 100 100 100 100 2 | Moore 75 85 77 59 0 85 75 89 3 | Norman 57 78 73 66 78 70 88 89 4 | Olson 89 86 70 90 55 73 80 84 5 | Peerson 47 70 82 73 50 87 73 71 6 | 7 | Russel 72 87 88 54 55 82 69 87 8 | Thomas 90 96 99 99 100 81 97 97 9 | Vaughn 81 97 99 67 40 90 70 96 10 | Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | Baker 67 72 73 40 0 78 55 70 14 | Davis 77 70 82 65 70 77 83 81 15 | Edwards 77 72 73 80 90 93 75 90 16 | Franklin 47 70 82 73 50 87 73 71 17 | 18 | Jones 77 82 83 50 10 88 65 80 19 | Harris 97 90 92 95 100 87 93 91 20 | Smith 87 92 93 60 0 98 75 90 21 | Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /win-source/data/grades_grad: -------------------------------------------------------------------------------- 1 | 2 | Moo 100 100 100 100 100 100 100 100 100 3 | Moore 75 85 77 59 0 85 75 89 75 4 | Norman 57 78 73 66 78 70 88 89 84 5 | Olson 89 86 70 90 55 73 80 84 99 6 | Peerson 47 70 82 73 50 87 73 71 84 7 | 8 | Russel 72 87 88 54 55 82 69 87 80 9 | Thomas 90 96 99 99 100 81 97 97 100 10 | Vaughn 81 97 99 67 40 90 70 96 76 11 | Westerly 43 98 96 79 100 82 97 96 96 12 | 13 | 14 | Baker 67 72 73 40 0 78 55 70 87 15 | Davis 77 70 82 65 70 77 83 81 80 16 | Edwards 77 72 73 80 90 93 75 90 92 17 | Franklin 47 70 82 73 50 87 73 71 72 18 | 19 | Jones 77 82 83 50 10 88 65 80 88 20 | Harris 97 90 92 95 100 87 93 91 94 21 | Smith 87 92 93 60 0 98 75 90 88 22 | Carpenter 47 90 92 73 100 87 93 91 85 23 | -------------------------------------------------------------------------------- /win-source/data/grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /win-source/data/kids_grades: -------------------------------------------------------------------------------- 1 | 2 | G Sarah 100 100 100 100 100 100 100 100 100 3 | U Lynn 75 85 77 59 0 85 75 89 4 | G James 57 78 73 66 78 70 88 89 84 5 | U Kevin 89 86 70 90 55 73 80 84 6 | U MaryKate 47 70 82 73 50 87 73 71 7 | 8 | U Pat 72 87 88 54 55 82 69 87 9 | G Paul 90 96 99 99 100 81 97 97 100 10 | U Rhia 81 97 99 67 40 90 70 96 11 | -------------------------------------------------------------------------------- /win-source/data/mixed_grades: -------------------------------------------------------------------------------- 1 | G Moo 100 100 100 100 100 100 100 100 100 2 | U Moore 75 85 77 59 0 85 75 89 3 | G Norman 57 78 73 66 78 70 88 89 84 4 | U Olson 89 86 70 90 55 73 80 84 5 | U Peerson 47 70 82 73 50 87 73 71 6 | 7 | U Russel 72 87 88 54 55 82 69 87 8 | G Thomas 90 96 99 99 100 81 97 97 100 9 | U Vaughn 81 97 99 67 40 90 70 96 10 | U Westerly 43 98 96 79 100 82 97 96 11 | 12 | 13 | G Baker 67 72 73 40 0 78 55 70 87 14 | G Davis 77 70 82 65 70 77 83 81 80 15 | G Edwards 77 72 73 80 90 93 75 90 92 16 | U Franklin 47 70 82 73 50 87 73 71 17 | 18 | U Jones 77 82 83 50 10 88 65 80 19 | G Harris 97 90 92 95 100 87 93 91 94 20 | U Smith 87 92 93 60 0 98 75 90 21 | U Carpenter 47 90 92 73 100 87 93 91 22 | -------------------------------------------------------------------------------- /win-source/data/name1: -------------------------------------------------------------------------------- 1 | Vladimir 2 | -------------------------------------------------------------------------------- /win-source/data/name2: -------------------------------------------------------------------------------- 1 | Estragon 2 | -------------------------------------------------------------------------------- /win-source/data/numbers: -------------------------------------------------------------------------------- 1 | 22 23 5 -1 -2 0 1 2 3 4 2 | -------------------------------------------------------------------------------- /win-source/data/orig_grammar_input: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | -------------------------------------------------------------------------------- /win-source/data/palins_input: -------------------------------------------------------------------------------- 1 | madam 2 | eye 3 | civic 4 | level 5 | rotor 6 | -------------------------------------------------------------------------------- /win-source/data/single_grade: -------------------------------------------------------------------------------- 1 | harriet 85 95 88 87 93 45 76 99 2 | -------------------------------------------------------------------------------- /win-source/minmax.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_minmax_h 2 | #define GUARD_minmax_h 3 | 4 | #ifdef _MSC_VER 5 | // needed (only) to cope with bug in MS library: 6 | // it fails to define min/max 7 | 8 | template T max(const T& a, const T& b) 9 | { 10 | return (a > b) ? a : b; 11 | } 12 | 13 | template T min(const T& a, const T& b) 14 | { 15 | return (a < b) ? a : b;; 16 | } 17 | #endif 18 | 19 | #endif 20 | --------------------------------------------------------------------------------