From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog1/examples/ch01/Fig01_02.cpp | 30 +++ Bachelor/Prog1/examples/ch01/Fig01_04.cpp | 30 +++ Bachelor/Prog1/examples/ch01/Fig01_05.cpp | 29 +++ Bachelor/Prog1/examples/ch01/Fig01_06.cpp | 41 ++++ Bachelor/Prog1/examples/ch01/Fig01_14.cpp | 58 ++++++ Bachelor/Prog1/examples/ch02/Ex02_05.cpp | 43 ++++ Bachelor/Prog1/examples/ch02/Ex02_08.cpp | 55 +++++ Bachelor/Prog1/examples/ch02/Ex02_15.cpp | 44 ++++ Bachelor/Prog1/examples/ch02/Ex02_24.cpp | 39 ++++ Bachelor/Prog1/examples/ch02/Ex02_25.cpp | 47 +++++ Bachelor/Prog1/examples/ch02/Ex02_42.cpp | 47 +++++ Bachelor/Prog1/examples/ch02/Fig02_07.cpp | 54 +++++ Bachelor/Prog1/examples/ch02/Fig02_09.cpp | 76 +++++++ Bachelor/Prog1/examples/ch02/Fig02_11.cpp | 64 ++++++ Bachelor/Prog1/examples/ch02/Fig02_14.cpp | 44 ++++ Bachelor/Prog1/examples/ch02/Fig02_16.cpp | 38 ++++ Bachelor/Prog1/examples/ch02/Fig02_17.cpp | 36 ++++ Bachelor/Prog1/examples/ch02/Fig02_20.cpp | 38 ++++ Bachelor/Prog1/examples/ch02/Fig02_21.cpp | 60 ++++++ Bachelor/Prog1/examples/ch02/Fig02_22.cpp | 95 +++++++++ Bachelor/Prog1/examples/ch02/Fig02_24.cpp | 39 ++++ Bachelor/Prog1/examples/ch02/Fig02_26.cpp | 46 ++++ Bachelor/Prog1/examples/ch02/Fig02_27.cpp | 45 ++++ Bachelor/Prog1/examples/ch03/Ex03_02.cpp | 41 ++++ Bachelor/Prog1/examples/ch03/Ex03_03.cpp | 67 ++++++ Bachelor/Prog1/examples/ch03/Ex03_10.cpp | 53 +++++ Bachelor/Prog1/examples/ch03/Ex03_49.cpp | 36 ++++ Bachelor/Prog1/examples/ch03/Ex03_50.cpp | 51 +++++ Bachelor/Prog1/examples/ch03/Fig03_03.cpp | 45 ++++ Bachelor/Prog1/examples/ch03/Fig03_04.cpp | 60 ++++++ Bachelor/Prog1/examples/ch03/Fig03_07.cpp | 46 ++++ Bachelor/Prog1/examples/ch03/Fig03_08.cpp | 89 ++++++++ Bachelor/Prog1/examples/ch03/Fig03_09.cpp | 54 +++++ Bachelor/Prog1/examples/ch03/Fig03_10.cpp | 111 ++++++++++ Bachelor/Prog1/examples/ch03/Fig03_12.cpp | 96 +++++++++ Bachelor/Prog1/examples/ch03/Fig03_14.cpp | 52 +++++ Bachelor/Prog1/examples/ch03/Fig03_15.cpp | 56 +++++ Bachelor/Prog1/examples/ch03/Fig03_18.cpp | 50 +++++ Bachelor/Prog1/examples/ch03/Fig03_19.cpp | 50 +++++ Bachelor/Prog1/examples/ch03/Fig03_20.cpp | 61 ++++++ Bachelor/Prog1/examples/ch03/Fig03_21.cpp | 37 ++++ Bachelor/Prog1/examples/ch03/Fig03_22.cpp | 35 ++++ Bachelor/Prog1/examples/ch03/Fig03_23.cpp | 54 +++++ Bachelor/Prog1/examples/ch03/Fig03_24.cpp | 44 ++++ Bachelor/Prog1/examples/ch03/Fig03_25.cpp | 50 +++++ Bachelor/Prog1/examples/ch03/Fig03_26.cpp | 50 +++++ Bachelor/Prog1/examples/ch03/Fig03_27.cpp | 76 +++++++ Bachelor/Prog1/examples/ch04/Ex04_18.cpp | 49 +++++ Bachelor/Prog1/examples/ch04/Ex04_21.cpp | 47 +++++ Bachelor/Prog1/examples/ch04/Fig04_03.cpp | 44 ++++ Bachelor/Prog1/examples/ch04/Fig04_04.cpp | 42 ++++ Bachelor/Prog1/examples/ch04/Fig04_05.cpp | 46 ++++ Bachelor/Prog1/examples/ch04/Fig04_06.cpp | 35 ++++ Bachelor/Prog1/examples/ch04/Fig04_07.cpp | 29 +++ Bachelor/Prog1/examples/ch04/Fig04_08.cpp | 41 ++++ Bachelor/Prog1/examples/ch04/Fig04_09.cpp | 49 +++++ Bachelor/Prog1/examples/ch04/Fig04_10.cpp | 53 +++++ Bachelor/Prog1/examples/ch04/Fig04_11.cpp | 58 ++++++ Bachelor/Prog1/examples/ch04/Fig04_12.cpp | 49 +++++ Bachelor/Prog1/examples/ch04/Fig04_13.cpp | 82 ++++++++ Bachelor/Prog1/examples/ch04/Fig04_14.cpp | 87 ++++++++ Bachelor/Prog1/examples/ch04/Fig04_15.cpp | 46 ++++ Bachelor/Prog1/examples/ch04/Fig04_16.cpp | 65 ++++++ Bachelor/Prog1/examples/ch04/Fig04_17.cpp | 193 +++++++++++++++++ Bachelor/Prog1/examples/ch04/Fig04_19.cpp | 63 ++++++ Bachelor/Prog1/examples/ch04/Fig04_20.cpp | 142 +++++++++++++ Bachelor/Prog1/examples/ch04/Fig04_22.cpp | 57 +++++ Bachelor/Prog1/examples/ch04/Fig04_23.cpp | 134 ++++++++++++ Bachelor/Prog1/examples/ch05/Ex05_21.cpp | 49 +++++ Bachelor/Prog1/examples/ch05/Ex05_22.cpp | 48 +++++ Bachelor/Prog1/examples/ch05/Ex05_30.cpp | 49 +++++ Bachelor/Prog1/examples/ch05/Fig05_04.cpp | 43 ++++ Bachelor/Prog1/examples/ch05/Fig05_06.cpp | 45 ++++ Bachelor/Prog1/examples/ch05/Fig05_07.cpp | 46 ++++ Bachelor/Prog1/examples/ch05/Fig05_10.cpp | 53 +++++ Bachelor/Prog1/examples/ch05/Fig05_11.cpp | 45 ++++ Bachelor/Prog1/examples/ch05/Fig05_12.cpp | 38 ++++ Bachelor/Prog1/examples/ch05/Fig05_13.cpp | 34 +++ Bachelor/Prog1/examples/ch05/Fig05_14.cpp | 39 ++++ Bachelor/Prog1/examples/ch05/Fig05_15.cpp | 77 +++++++ Bachelor/Prog1/examples/ch05/Fig05_16.cpp | 45 ++++ Bachelor/Prog1/examples/ch05/Fig05_17.cpp | 55 +++++ Bachelor/Prog1/examples/ch05/Fig05_20.cpp | 60 ++++++ Bachelor/Prog1/examples/ch05/Fig05_21.cpp | 58 ++++++ Bachelor/Prog1/examples/ch05/Fig05_24.cpp | 103 +++++++++ Bachelor/Prog1/examples/ch05/Fig05_25.cpp | 115 ++++++++++ Bachelor/Prog1/examples/ch05/Fig05_26.cpp | 76 +++++++ Bachelor/Prog1/examples/ch05/Fig05_28.cpp | 44 ++++ Bachelor/Prog1/examples/ch05/Fig05_29.cpp | 50 +++++ Bachelor/Prog1/examples/ch05/Fig05_30.cpp | 50 +++++ Bachelor/Prog1/examples/ch05/Fig05_31.cpp | 47 +++++ Bachelor/Prog1/examples/ch05/Fig05_32.cpp | 40 ++++ Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp | 82 ++++++++ Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp | 114 ++++++++++ Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp | 58 ++++++ .../Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp | 57 +++++ Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp | 65 ++++++ Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h | 40 ++++ Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp | 38 ++++ Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp | 65 ++++++ Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h | 40 ++++ .../Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp | 32 +++ .../Prog1/examples/ch06/Fig06_09_11/Salesp.cpp | 86 ++++++++ Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h | 36 ++++ .../Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp | 63 ++++++ Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp | 65 ++++++ Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h | 40 ++++ .../Prog1/examples/ch06/Fig06_15_17/Create.cpp | 47 +++++ Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h | 34 +++ .../Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp | 68 ++++++ .../Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp | 82 ++++++++ Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp | 107 ++++++++++ Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h | 50 +++++ .../Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp | 53 +++++ Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp | 55 +++++ Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h | 40 ++++ Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp | 72 +++++++ .../Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp | 42 ++++ Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp | 107 ++++++++++ Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h | 49 +++++ Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp | 75 +++++++ Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp | 74 +++++++ Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp | 85 ++++++++ Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h | 39 ++++ .../Prog1/examples/ch07/Fig07_06_10/Employee1.cpp | 72 +++++++ .../Prog1/examples/ch07/Fig07_06_10/Employee1.h | 42 ++++ .../Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp | 40 ++++ Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp | 72 +++++++ Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp | 65 ++++++ Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp | 66 ++++++ .../Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp | 48 +++++ Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp | 114 ++++++++++ Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h | 51 +++++ .../Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp | 55 +++++ .../Prog1/examples/ch07/Fig07_17_19/employee2.cpp | 88 ++++++++ .../Prog1/examples/ch07/Fig07_17_19/employee2.h | 41 ++++ .../Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp | 39 ++++ .../examples/ch07/Fig07_20_23/implementation.h | 48 +++++ .../Prog1/examples/ch07/Fig07_20_23/interface.cpp | 48 +++++ .../Prog1/examples/ch07/Fig07_20_23/interface.h | 34 +++ Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp | 89 ++++++++ .../Prog1/examples/ch08/Fig08_04_06/Array1.cpp | 177 ++++++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h | 61 ++++++ .../Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp | 90 ++++++++ .../Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp | 101 +++++++++ .../Prog1/examples/ch08/Fig08_07_09/String1.cpp | 221 ++++++++++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h | 85 ++++++++ Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp | 137 ++++++++++++ Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h | 49 +++++ .../Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp | 54 +++++ Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp | 104 ++++++++++ Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp | 134 ++++++++++++ .../Prog1/examples/ch08/Fig08_15_17/Complex1.cpp | 54 +++++ .../Prog1/examples/ch08/Fig08_15_17/Complex1.h | 35 ++++ .../Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp | 57 +++++ .../Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp | 51 +++++ .../Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp | 121 +++++++++++ .../Prog1/examples/ch08/Fig08_18_20/Hugeint1.h | 47 +++++ .../Prog1/examples/elevator/ElevatorSimulation.cpp | 45 ++++ Bachelor/Prog1/examples/elevator/bell.cpp | 44 ++++ Bachelor/Prog1/examples/elevator/bell.h | 30 +++ Bachelor/Prog1/examples/elevator/building.cpp | 65 ++++++ Bachelor/Prog1/examples/elevator/building.h | 42 ++++ Bachelor/Prog1/examples/elevator/clock.cpp | 52 +++++ Bachelor/Prog1/examples/elevator/clock.h | 34 +++ Bachelor/Prog1/examples/elevator/door.cpp | 81 ++++++++ Bachelor/Prog1/examples/elevator/door.h | 40 ++++ Bachelor/Prog1/examples/elevator/elevator.cpp | 231 +++++++++++++++++++++ Bachelor/Prog1/examples/elevator/elevator.h | 75 +++++++ .../Prog1/examples/elevator/elevatorButton.cpp | 57 +++++ Bachelor/Prog1/examples/elevator/elevatorButton.h | 40 ++++ Bachelor/Prog1/examples/elevator/floor.cpp | 98 +++++++++ Bachelor/Prog1/examples/elevator/floor.h | 63 ++++++ Bachelor/Prog1/examples/elevator/floorButton.cpp | 62 ++++++ Bachelor/Prog1/examples/elevator/floorButton.h | 42 ++++ Bachelor/Prog1/examples/elevator/light.cpp | 65 ++++++ Bachelor/Prog1/examples/elevator/light.h | 36 ++++ Bachelor/Prog1/examples/elevator/person.cpp | 91 ++++++++ Bachelor/Prog1/examples/elevator/person.h | 42 ++++ Bachelor/Prog1/examples/elevator/scheduler.cpp | 134 ++++++++++++ Bachelor/Prog1/examples/elevator/scheduler.h | 53 +++++ 181 files changed, 11254 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch01/Fig01_02.cpp create mode 100644 Bachelor/Prog1/examples/ch01/Fig01_04.cpp create mode 100644 Bachelor/Prog1/examples/ch01/Fig01_05.cpp create mode 100644 Bachelor/Prog1/examples/ch01/Fig01_06.cpp create mode 100644 Bachelor/Prog1/examples/ch01/Fig01_14.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_05.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_08.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_15.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_24.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_25.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Ex02_42.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_07.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_09.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_11.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_14.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_16.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_17.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_20.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_21.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_22.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_24.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_26.cpp create mode 100644 Bachelor/Prog1/examples/ch02/Fig02_27.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_02.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_03.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_10.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_49.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Ex03_50.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_03.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_04.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_07.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_08.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_09.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_10.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_12.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_14.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_15.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_18.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_19.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_20.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_21.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_22.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_23.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_24.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_25.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_26.cpp create mode 100644 Bachelor/Prog1/examples/ch03/Fig03_27.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Ex04_18.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Ex04_21.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_03.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_04.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_05.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_06.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_07.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_08.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_09.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_10.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_11.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_12.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_13.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_14.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_15.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_16.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_17.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_19.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_20.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_22.cpp create mode 100644 Bachelor/Prog1/examples/ch04/Fig04_23.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Ex05_21.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Ex05_22.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Ex05_30.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_04.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_06.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_07.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_10.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_11.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_12.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_13.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_14.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_15.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_16.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_17.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_20.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_21.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_24.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_25.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_26.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_28.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_29.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_30.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_31.cpp create mode 100644 Bachelor/Prog1/examples/ch05/Fig05_32.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp create mode 100644 Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h create mode 100644 Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp create mode 100644 Bachelor/Prog1/examples/elevator/bell.cpp create mode 100644 Bachelor/Prog1/examples/elevator/bell.h create mode 100644 Bachelor/Prog1/examples/elevator/building.cpp create mode 100644 Bachelor/Prog1/examples/elevator/building.h create mode 100644 Bachelor/Prog1/examples/elevator/clock.cpp create mode 100644 Bachelor/Prog1/examples/elevator/clock.h create mode 100644 Bachelor/Prog1/examples/elevator/door.cpp create mode 100644 Bachelor/Prog1/examples/elevator/door.h create mode 100644 Bachelor/Prog1/examples/elevator/elevator.cpp create mode 100644 Bachelor/Prog1/examples/elevator/elevator.h create mode 100644 Bachelor/Prog1/examples/elevator/elevatorButton.cpp create mode 100644 Bachelor/Prog1/examples/elevator/elevatorButton.h create mode 100644 Bachelor/Prog1/examples/elevator/floor.cpp create mode 100644 Bachelor/Prog1/examples/elevator/floor.h create mode 100644 Bachelor/Prog1/examples/elevator/floorButton.cpp create mode 100644 Bachelor/Prog1/examples/elevator/floorButton.h create mode 100644 Bachelor/Prog1/examples/elevator/light.cpp create mode 100644 Bachelor/Prog1/examples/elevator/light.h create mode 100644 Bachelor/Prog1/examples/elevator/person.cpp create mode 100644 Bachelor/Prog1/examples/elevator/person.h create mode 100644 Bachelor/Prog1/examples/elevator/scheduler.cpp create mode 100644 Bachelor/Prog1/examples/elevator/scheduler.h (limited to 'Bachelor/Prog1/examples') diff --git a/Bachelor/Prog1/examples/ch01/Fig01_02.cpp b/Bachelor/Prog1/examples/ch01/Fig01_02.cpp new file mode 100644 index 0000000..4d6b3d4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch01/Fig01_02.cpp @@ -0,0 +1,30 @@ +// Fig. 1.2: fig01_02.cpp +// A first program in C++. +#include + +// function main begins program execution +int main() +{ + std::cout << "Welcome to C++!\n"; + + return 0; // indicate that program ended successfully + +} // end function main + + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch01/Fig01_04.cpp b/Bachelor/Prog1/examples/ch01/Fig01_04.cpp new file mode 100644 index 0000000..aaee89a --- /dev/null +++ b/Bachelor/Prog1/examples/ch01/Fig01_04.cpp @@ -0,0 +1,30 @@ +// Fig. 1.4: fig01_04.cpp +// Printing a line with multiple statements. +#include + +// function main begins program execution +int main() +{ + std::cout << "Welcome "; + std::cout << "to C++!\n"; + + return 0; // indicate that program ended successfully + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch01/Fig01_05.cpp b/Bachelor/Prog1/examples/ch01/Fig01_05.cpp new file mode 100644 index 0000000..6a8d719 --- /dev/null +++ b/Bachelor/Prog1/examples/ch01/Fig01_05.cpp @@ -0,0 +1,29 @@ +// Fig. 1.5: fig01_05.cpp +// Printing multiple lines with a single statement. +#include + +// function main begins program execution +int main() +{ + std::cout << "Welcome\nto\n\nC++!\n"; + + return 0; // indicate that program ended successfully + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch01/Fig01_06.cpp b/Bachelor/Prog1/examples/ch01/Fig01_06.cpp new file mode 100644 index 0000000..403121a --- /dev/null +++ b/Bachelor/Prog1/examples/ch01/Fig01_06.cpp @@ -0,0 +1,41 @@ +// Fig. 1.6: fig01_06.cpp +// Addition program. +#include + +// function main begins program execution +int main() +{ + int integer1; // first number to be input by user + int integer2; // second number to be input by user + int sum; // variable in which sum will be stored + + std::cout << "Enter first integer\n"; // prompt + std::cin >> integer1; // read an integer + + std::cout << "Enter second integer\n"; // prompt + std::cin >> integer2; // read an integer + + sum = integer1 + integer2; // assignment result to sum + + std::cout << "Sum is " << sum << std::endl; // print sum + + return 0; // indicate that program ended successfully + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch01/Fig01_14.cpp b/Bachelor/Prog1/examples/ch01/Fig01_14.cpp new file mode 100644 index 0000000..cb623fb --- /dev/null +++ b/Bachelor/Prog1/examples/ch01/Fig01_14.cpp @@ -0,0 +1,58 @@ +// Fig. 1.14: fig01_14.cpp +// Using if statements, relational +// operators, and equality operators. +#include + +using std::cout; // program uses cout +using std::cin; // program uses cin +using std::endl; // program uses endl + +// function main begins program execution +int main() +{ + int num1; // first number to be read from user + int num2; // second number to be read from user + + cout << "Enter two integers, and I will tell you\n" + << "the relationships they satisfy: "; + cin >> num1 >> num2; // read two integers + + if ( num1 == num2 ) + cout << num1 << " is equal to " << num2 << endl; + + if ( num1 != num2 ) + cout << num1 << " is not equal to " << num2 << endl; + + if ( num1 < num2 ) + cout << num1 << " is less than " << num2 << endl; + + if ( num1 > num2 ) + cout << num1 << " is greater than " << num2 << endl; + + if ( num1 <= num2 ) + cout << num1 << " is less than or equal to " + << num2 << endl; + + if ( num1 >= num2 ) + cout << num1 << " is greater than or equal to " + << num2 << endl; + + return 0; // indicate that program ended successfully + +} // end function main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Ex02_05.cpp b/Bachelor/Prog1/examples/ch02/Ex02_05.cpp new file mode 100644 index 0000000..1189989 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_05.cpp @@ -0,0 +1,43 @@ +// Ex. 2.5: ex02_05.cpp +// Calculate the sum of the integers from 1 to 10. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int sum; // stores sum of integers 1 to 10 + int x; // counter + + x = 1; // count from 1 + sum = 0; // initialize sum + + while ( x <= 10 ) { + sum += x; // add x to sum + ++x; // increment x + + } // end while + + cout << "The sum is: " << sum << endl; + + return 0; // indicate successful termination + +} // end function main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Ex02_08.cpp b/Bachelor/Prog1/examples/ch02/Ex02_08.cpp new file mode 100644 index 0000000..db9685a --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_08.cpp @@ -0,0 +1,55 @@ +// Ex. 2.8: ex02_08.cpp +// Raise x to the y power. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int x; // base + int y; // exponent + int i; // counts from 1 to y + int power; // used to calculate x raised to power y + + i = 1; // initialize i to begin counting from 1 + power = 1; // initialize power + + cout << "Enter base as an integer: "; // prompt for base + cin >> x; // input base + + // prompt for exponent + cout << "Enter exponent as an integer: "; + cin >> y; // input exponent + + // count from 1 to y and multiply power by x each time + while ( i <= y ) { + power *= x; + ++i; + + } // end while + + cout << power << endl; // display result + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Ex02_15.cpp b/Bachelor/Prog1/examples/ch02/Ex02_15.cpp new file mode 100644 index 0000000..63e8e14 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_15.cpp @@ -0,0 +1,44 @@ +// Ex. 2.15: ex02_15.cpp +// What does this program print? +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int y; // declare y + int x = 1; // initialize x + int total = 0; // initialize total + + while ( x <= 10 ) { // loop 10 times + y = x * x; // perform calculation + cout << y << endl; // output result + total += y; // add y to total + ++x; // increment counter x + + } // end while + + cout << "Total is " << total << endl; // display result + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Ex02_24.cpp b/Bachelor/Prog1/examples/ch02/Ex02_24.cpp new file mode 100644 index 0000000..3faf50c --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_24.cpp @@ -0,0 +1,39 @@ +// Ex. 2.24: ex02_24.cpp +// What does this program print? +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int count = 1; // initialize count + + while ( count <= 10 ) { // loop 10 times + + // output line of text + cout << ( count % 2 ? "****" : "++++++++" ) + << endl; + ++count; // increment count + } + + return 0; // indicate successful termination + +} // end function main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Ex02_25.cpp b/Bachelor/Prog1/examples/ch02/Ex02_25.cpp new file mode 100644 index 0000000..6d4a82a --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_25.cpp @@ -0,0 +1,47 @@ +// Ex. 2.25: ex_02_25.cpp +// What does this program print? +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int row = 10; // initialize row + int column; // declare column + + while ( row >= 1 ) { // loop until row < 1 + column = 1; // set column to 1 as iteration begins + + while ( column <= 10 ) { // loop 10 times + cout << ( row % 2 ? "<" : ">" ); // output + ++column; // increment column + + } // end inner while + + --row; // decrement row + cout << endl; // begin new output line + + } // end outer while + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Ex02_42.cpp b/Bachelor/Prog1/examples/ch02/Ex02_42.cpp new file mode 100644 index 0000000..3c3b514 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Ex02_42.cpp @@ -0,0 +1,47 @@ +// Ex. 2.42: ex02_42.cpp +// What does this program print? +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int x, // declare x + y; // declare y + + // prompt user for input + cout << "Enter two integers in the range 1-20: "; + cin >> x >> y; // read values for x and y + + for ( int i = 1; i <= y; i++ ) { // count from 1 to y + + for ( int j = 1; j <= x; j++ ) // count from 1 to x + cout << '@'; // output @ + + cout << endl; // begin new line + + } // end outer for + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_07.cpp b/Bachelor/Prog1/examples/ch02/Fig02_07.cpp new file mode 100644 index 0000000..eac7fce --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_07.cpp @@ -0,0 +1,54 @@ +// Fig. 2.7: fig02_07.cpp +// Class average program with counter-controlled repetition. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int total; // sum of grades input by user + int gradeCounter; // number of grade to be entered next + int grade; // grade value + int average; // average of grades + + // initialization phase + total = 0; // initialize total + gradeCounter = 1; // initialize loop counter + + // processing phase + while ( gradeCounter <= 10 ) { // loop 10 times + cout << "Enter grade: "; // prompt for input + cin >> grade; // read grade from user + total = total + grade; // add grade to total + gradeCounter = gradeCounter + 1; // increment counter + } + + // termination phase + average = total / 10; // integer division + + // display result + cout << "Class average is " << average << endl; + + return 0; // indicate program ended successfully + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_09.cpp b/Bachelor/Prog1/examples/ch02/Fig02_09.cpp new file mode 100644 index 0000000..a0c1065 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_09.cpp @@ -0,0 +1,76 @@ +// Fig. 2.9: fig02_09.cpp +// Class average program with sentinel-controlled repetition. +#include + +using std::cout; +using std::cin; +using std::endl; +using std::fixed; + +#include // parameterized stream manipulators + +using std::setprecision; // sets numeric output precision + +// function main begins program execution +int main() +{ + int total; // sum of grades + int gradeCounter; // number of grades entered + int grade; // grade value + + double average; // number with decimal point for average + + // initialization phase + total = 0; // initialize total + gradeCounter = 0; // initialize loop counter + + // processing phase + // get first grade from user + cout << "Enter grade, -1 to end: "; // prompt for input + cin >> grade; // read grade from user + + // loop until sentinel value read from user + while ( grade != -1 ) { + total = total + grade; // add grade to total + gradeCounter = gradeCounter + 1; // increment counter + + cout << "Enter grade, -1 to end: "; // prompt for input + cin >> grade; // read next grade + + } // end while + + // termination phase + // if user entered at least one grade ... + if ( gradeCounter != 0 ) { + + // calculate average of all grades entered + average = static_cast< double >( total ) / gradeCounter; + + // display average with two digits of precision + cout << "Class average is " << setprecision( 2 ) + << fixed << average << endl; + + } // end if part of if/else + + else // if no grades were entered, output appropriate message + cout << "No grades were entered" << endl; + + return 0; // indicate program ended successfully + +} // end function main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_11.cpp b/Bachelor/Prog1/examples/ch02/Fig02_11.cpp new file mode 100644 index 0000000..5cf6523 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_11.cpp @@ -0,0 +1,64 @@ +// Fig. 2.11: fig02_11.cpp +// Analysis of examination results. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + // initialize variables in declarations + int passes = 0; // number of passes + int failures = 0; // number of failures + int studentCounter = 1; // student counter + int result; // one exam result + + // process 10 students using counter-controlled loop + while ( studentCounter <= 10 ) { + + // prompt user for input and obtain value from user + cout << "Enter result (1 = pass, 2 = fail): "; + cin >> result; + + // if result 1, increment passes; if/else nested in while + if ( result == 1 ) // if/else nested in while + passes = passes + 1; + + else // if result not 1, increment failures + failures = failures + 1; + + // increment studentCounter so loop eventually terminates + studentCounter = studentCounter + 1; + + } // end while + + // termination phase; display number of passes and failures + cout << "Passed " << passes << endl; + cout << "Failed " << failures << endl; + + // if more than eight students passed, print "raise tuition" + if ( passes > 8 ) + cout << "Raise tuition " << endl; + + return 0; // successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_14.cpp b/Bachelor/Prog1/examples/ch02/Fig02_14.cpp new file mode 100644 index 0000000..b56633a --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_14.cpp @@ -0,0 +1,44 @@ +// Fig. 2.14: fig02_14.cpp +// Preincrementing and postincrementing. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int c; // declare variable + + // demonstrate postincrement + c = 5; // assign 5 to c + cout << c << endl; // print 5 + cout << c++ << endl; // print 5 then postincrement + cout << c << endl << endl; // print 6 + + // demonstrate preincrement + c = 5; // assign 5 to c + cout << c << endl; // print 5 + cout << ++c << endl; // preincrement then print 6 + cout << c << endl; // print 6 + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_16.cpp b/Bachelor/Prog1/examples/ch02/Fig02_16.cpp new file mode 100644 index 0000000..a1998b0 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_16.cpp @@ -0,0 +1,38 @@ +// Fig. 2.16: fig02_16.cpp +// Counter-controlled repetition. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int counter = 1; // initialization + + while ( counter <= 10 ) { // repetition condition + cout << counter << endl; // display counter + ++counter; // increment + + } // end while + + return 0; // successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_17.cpp b/Bachelor/Prog1/examples/ch02/Fig02_17.cpp new file mode 100644 index 0000000..26e9a9f --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_17.cpp @@ -0,0 +1,36 @@ +// Fig. 2.17: fig02_17.cpp +// Counter-controlled repetition with the for structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + // Initialization, repetition condition and incrementing + // are all included in the for structure header. + + for ( int counter = 1; counter <= 10; counter++ ) + cout << counter << endl; + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_20.cpp b/Bachelor/Prog1/examples/ch02/Fig02_20.cpp new file mode 100644 index 0000000..6a8081f --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_20.cpp @@ -0,0 +1,38 @@ +// Fig. 2.20: fig02_20.cpp +// Summation with for. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int sum = 0; // initialize sum + + // sum even integers from 2 through 100 + for ( int number = 2; number <= 100; number += 2 ) + sum += number; // add number to sum + + cout << "Sum is " << sum << endl; // output sum + return 0; // successful termination + +} // end function main + + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_21.cpp b/Bachelor/Prog1/examples/ch02/Fig02_21.cpp new file mode 100644 index 0000000..d27d6e8 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_21.cpp @@ -0,0 +1,60 @@ +// Fig. 2.21: fig02_21.cpp +// Calculating compound interest. +#include + +using std::cout; +using std::endl; +using std::ios; +using std::fixed; + +#include + +using std::setw; +using std::setprecision; + +#include // enables program to use function pow + +// function main begins program execution +int main() +{ + double amount; // amount on deposit + double principal = 1000.0; // starting principal + double rate = .05; // interest rate + + // output table column heads + cout << "Year" << setw( 21 ) << "Amount on deposit" << endl; + + // set floating-point number format + cout << fixed << setprecision( 2 ); + + // calculate amount on deposit for each of ten years + for ( int year = 1; year <= 10; year++ ) { + + // calculate new amount for specified year + amount = principal * pow( 1.0 + rate, year ); + + // output one table row + cout << setw( 4 ) << year + << setw( 21 ) << amount << endl; + + } // end for + + return 0; // indicate successful termination + +} // end function main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_22.cpp b/Bachelor/Prog1/examples/ch02/Fig02_22.cpp new file mode 100644 index 0000000..025f666 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_22.cpp @@ -0,0 +1,95 @@ +// Fig. 2.22: fig02_22.cpp +// Counting letter grades. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function main begins program execution +int main() +{ + int grade; // one grade + int aCount = 0; // number of As + int bCount = 0; // number of Bs + int cCount = 0; // number of Cs + int dCount = 0; // number of Ds + int fCount = 0; // number of Fs + + cout << "Enter the letter grades." << endl + << "Enter the EOF character to end input." << endl; + + // loop until user types end-of-file key sequence + while ( ( grade = cin.get() ) != EOF ) { + + // determine which grade was input + switch ( grade ) { // switch structure nested in while + + case 'A': // grade was uppercase A + case 'a': // or lowercase a + ++aCount; // increment aCount + break; // necessary to exit switch + + case 'B': // grade was uppercase B + case 'b': // or lowercase b + ++bCount; // increment bCount + break; // exit switch + + case 'C': // grade was uppercase C + case 'c': // or lowercase c + ++cCount; // increment cCount + break; // exit switch + + case 'D': // grade was uppercase D + case 'd': // or lowercase d + ++dCount; // increment dCount + break; // exit switch + + case 'F': // grade was uppercase F + case 'f': // or lowercase f + ++fCount; // increment fCount + break; // exit switch + + case '\n': // ignore newlines, + case '\t': // tabs, + case ' ': // and spaces in input + break; // exit switch + + default: // catch all other characters + cout << "Incorrect letter grade entered." + << " Enter a new grade." << endl; + break; // optional; will exit switch anyway + + } // end switch + + } // end while + + // output summary of results + cout << "\n\nTotals for each letter grade are:" + << "\nA: " << aCount // display number of A grades + << "\nB: " << bCount // display number of B grades + << "\nC: " << cCount // display number of C grades + << "\nD: " << dCount // display number of D grades + << "\nF: " << fCount // display number of F grades + << endl; + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_24.cpp b/Bachelor/Prog1/examples/ch02/Fig02_24.cpp new file mode 100644 index 0000000..23724b7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_24.cpp @@ -0,0 +1,39 @@ +// Fig. 2.24: fig02_24.cpp +// Using the do/while repetition structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + int counter = 1; // initialize counter + + do { + cout << counter << " "; // display counter + } while ( ++counter <= 10 ); // end do/while + + cout << endl; + + return 0; // indicate successful termination + +} // end function main + + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_26.cpp b/Bachelor/Prog1/examples/ch02/Fig02_26.cpp new file mode 100644 index 0000000..a17fd07 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_26.cpp @@ -0,0 +1,46 @@ +// Fig. 2.26: fig02_26.cpp +// Using the break statement in a for structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + + int x; // x declared here so it can be used after the loop + + // loop 10 times + for ( x = 1; x <= 10; x++ ) { + + // if x is 5, terminate loop + if ( x == 5 ) + break; // break loop only if x is 5 + + cout << x << " "; // display value of x + + } // end for + + cout << "\nBroke out of loop when x became " << x << endl; + + return 0; // indicate successful termination + +} // end function main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch02/Fig02_27.cpp b/Bachelor/Prog1/examples/ch02/Fig02_27.cpp new file mode 100644 index 0000000..9504968 --- /dev/null +++ b/Bachelor/Prog1/examples/ch02/Fig02_27.cpp @@ -0,0 +1,45 @@ +// Fig. 2.27: fig02_27.cpp +// Using the continue statement in a for structure. +#include + +using std::cout; +using std::endl; + +// function main begins program execution +int main() +{ + // loop 10 times + for ( int x = 1; x <= 10; x++ ) { + + // if x is 5, continue with next iteration of loop + if ( x == 5 ) + continue; // skip remaining code in loop body + + cout << x << " "; // display value of x + + } // end for structure + + cout << "\nUsed continue to skip printing the value 5" + << endl; + + return 0; // indicate successful termination + +} // end function main + + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_02.cpp b/Bachelor/Prog1/examples/ch03/Ex03_02.cpp new file mode 100644 index 0000000..cb1f501 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_02.cpp @@ -0,0 +1,41 @@ +// Exercise 3.2: ex03_02.cpp +#include + +using std::cout; +using std::endl; + +int cube( int y ); // function prototype + +int main() +{ + int x; + + // loop 10 times, calculate cube of x and output results + for ( x = 1; x <= 10; x++ ) + cout << cube( x ) << endl; + + return 0; // indicates successful termination + +} // end main + +// definition of function cube +int cube( int y ) +{ + return y * y * y; +} + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_03.cpp b/Bachelor/Prog1/examples/ch03/Ex03_03.cpp new file mode 100644 index 0000000..af9b320 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_03.cpp @@ -0,0 +1,67 @@ +// Exercise 3.3: ex03_03.cpp +// Testing the math library functions. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include + +int main() +{ + cout << fixed << setprecision( 1 ); + + cout << "sqrt(" << 900.0 << ") = " << sqrt( 900.0 ) + << "\nsqrt(" << 9.0 << ") = " << sqrt( 9.0 ); + cout << "\nexp(" << 1.0 << ") = " << setprecision( 6 ) + << exp( 1.0 ) << "\nexp(" << setprecision( 1 ) << 2.0 + << ") = " << setprecision( 6 ) << exp( 2.0 ); + cout << "\nlog(" << 2.718282 << ") = " << setprecision( 1 ) + << log( 2.718282 ) + << "\nlog(" << setprecision( 6 ) << 7.389056 << ") = " + << setprecision( 1 ) << log( 7.389056 ); + cout << "\nlog10(" << 1.0 << ") = " << log10( 1.0 ) + << "\nlog10(" << 10.0 << ") = " << log10( 10.0 ) + << "\nlog10(" << 100.0 << ") = " << log10( 100.0 ) ; + cout << "\nfabs(" << 13.5 << ") = " << fabs( 13.5 ) + << "\nfabs(" << 0.0 << ") = " << fabs( 0.0 ) + << "\nfabs(" << -13.5 << ") = " << fabs( -13.5 ); + cout << "\nceil(" << 9.2 << ") = " << ceil( 9.2 ) + << "\nceil(" << -9.8 << ") = " << ceil( -9.8 ); + cout << "\nfloor(" << 9.2 << ") = " << floor( 9.2 ) + << "\nfloor(" << -9.8 << ") = " << floor( -9.8 ); + cout << "\npow(" << 2.0 << ", " << 7.0 << ") = " + << pow( 2.0, 7.0 ) << "\npow(" << 9.0 << ", " + << 0.5 << ") = " << pow( 9.0, 0.5 ); + cout << setprecision(3) << "\nfmod(" + << 13.675 << ", " << 2.333 << ") = " + << fmod( 13.675, 2.333 ) << setprecision( 1 ); + cout << "\nsin(" << 0.0 << ") = " << sin( 0.0 ); + cout << "\ncos(" << 0.0 << ") = " << cos( 0.0 ); + cout << "\ntan(" << 0.0 << ") = " << tan( 0.0 ) << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_10.cpp b/Bachelor/Prog1/examples/ch03/Ex03_10.cpp new file mode 100644 index 0000000..466ad86 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_10.cpp @@ -0,0 +1,53 @@ +// Exercise 3.10: ex03_10.cpp +// Inline function that calculates the volume of a sphere. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +// define global constant PI +const double PI = 3.14159; + +// calculates volume of a sphere +inline double sphereVolume( const double radius ) +{ + return 4.0 / 3.0 * PI * pow( radius, 3 ); + +} // end inline function sphereVolume + +int main() +{ + double radiusValue; + + // prompt user for radius + cout << "Enter the length of the radius of your sphere: "; + cin >> radiusValue; // input radius + + // use radiusValue to calculate volume of sphere + // and display result + cout << "Volume of sphere with radius " << radiusValue + << " is " << sphereVolume( radiusValue ) << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_49.cpp b/Bachelor/Prog1/examples/ch03/Ex03_49.cpp new file mode 100644 index 0000000..faaf62f --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_49.cpp @@ -0,0 +1,36 @@ +// Exercise 3.49: ex03_49.cpp +// What is wrong with this program? +#include + +using std::cin; +using std::cout; + +int main() +{ + int c; + + if ( ( c = cin.get() ) != EOF ) { + main(); + cout << c; + } + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Ex03_50.cpp b/Bachelor/Prog1/examples/ch03/Ex03_50.cpp new file mode 100644 index 0000000..c887d3d --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Ex03_50.cpp @@ -0,0 +1,51 @@ +// Exercise 3.50: ex03_50.cpp +// What does this program do? +#include + +using std::cout; +using std::cin; +using std::endl; + +int mystery( int, int ); // function prototype + +int main() +{ + int x, y; + + cout << "Enter two integers: "; + cin >> x >> y; + cout << "The result is " << mystery( x, y ) << endl; + + return 0; // indicates successful termination + +} // end main + +// Parameter b must be a positive +// integer to prevent infinite recursion +int mystery( int a, int b ) +{ + // base case + if ( b == 1 ) + return a; + + // recursive step + else + return a + mystery( a, b - 1 ); + +} // end function mystery + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_03.cpp b/Bachelor/Prog1/examples/ch03/Fig03_03.cpp new file mode 100644 index 0000000..271802e --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_03.cpp @@ -0,0 +1,45 @@ +// Fig. 3.3: fig03_03.cpp +// Creating and using a programmer-defined function. +#include + +using std::cout; +using std::endl; + +int square( int ); // function prototype + +int main() +{ + // loop 10 times and calculate and output + // square of x each time + for ( int x = 1; x <= 10; x++ ) + cout << square( x ) << " "; // function call + + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// square function definition returns square of an integer +int square( int y ) // y is a copy of argument to function +{ + return y * y; // returns square of y as an int + +} // end function square + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_04.cpp b/Bachelor/Prog1/examples/ch03/Fig03_04.cpp new file mode 100644 index 0000000..d9e27f3 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_04.cpp @@ -0,0 +1,60 @@ +// Fig. 3.4: fig03_04.cpp +// Finding the maximum of three floating-point numbers. +#include + +using std::cout; +using std::cin; +using std::endl; + +double maximum( double, double, double ); // function prototype + +int main() +{ + double number1; + double number2; + double number3; + + cout << "Enter three floating-point numbers: "; + cin >> number1 >> number2 >> number3; + + // number1, number2 and number3 are arguments to + // the maximum function call + cout << "Maximum is: " + << maximum( number1, number2, number3 ) << endl; + + return 0; // indicates successful termination + +} // end main + +// function maximum definition; +// x, y and z are parameters +double maximum( double x, double y, double z ) +{ + double max = x; // assume x is largest + + if ( y > max ) // if y is larger, + max = y; // assign y to max + + if ( z > max ) // if z is larger, + max = z; // assign z to max + + return max; // max is largest value + +} // end function maximum + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_07.cpp b/Bachelor/Prog1/examples/ch03/Fig03_07.cpp new file mode 100644 index 0000000..4971cc4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_07.cpp @@ -0,0 +1,46 @@ +// Fig. 3.7: fig03_07.cpp +// Shifted, scaled integers produced by 1 + rand() % 6. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // contains function prototype for rand + +int main() +{ + // loop 20 times + for ( int counter = 1; counter <= 20; counter++ ) { + + // pick random number from 1 to 6 and output it + cout << setw( 10 ) << ( 1 + rand() % 6 ); + + // if counter divisible by 5, begin new line of output + if ( counter % 5 == 0 ) + cout << endl; + + } // end for structure + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch03/Fig03_08.cpp b/Bachelor/Prog1/examples/ch03/Fig03_08.cpp new file mode 100644 index 0000000..8bb489e --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_08.cpp @@ -0,0 +1,89 @@ +// Fig. 3.8: fig03_08.cpp +// Roll a six-sided die 6000 times. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // contains function prototype for rand + +int main() +{ + int frequency1 = 0; + int frequency2 = 0; + int frequency3 = 0; + int frequency4 = 0; + int frequency5 = 0; + int frequency6 = 0; + int face; // represents one roll of the die + + // loop 6000 times and summarize results + for ( int roll = 1; roll <= 6000; roll++ ) { + face = 1 + rand() % 6; // random number from 1 to 6 + + // determine face value and increment appropriate counter + switch ( face ) { + + case 1: // rolled 1 + ++frequency1; + break; + + case 2: // rolled 2 + ++frequency2; + break; + + case 3: // rolled 3 + ++frequency3; + break; + + case 4: // rolled 4 + ++frequency4; + break; + + case 5: // rolled 5 + ++frequency5; + break; + + case 6: // rolled 6 + ++frequency6; + break; + + default: // invalid value + cout << "Program should never get here!"; + + } // end switch + + } // end for + + // display results in tabular format + cout << "Face" << setw( 13 ) << "Frequency" + << "\n 1" << setw( 13 ) << frequency1 + << "\n 2" << setw( 13 ) << frequency2 + << "\n 3" << setw( 13 ) << frequency3 + << "\n 4" << setw( 13 ) << frequency4 + << "\n 5" << setw( 13 ) << frequency5 + << "\n 6" << setw( 13 ) << frequency6 << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_09.cpp b/Bachelor/Prog1/examples/ch03/Fig03_09.cpp new file mode 100644 index 0000000..5ea5fb6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_09.cpp @@ -0,0 +1,54 @@ +// Fig. 3.9: fig03_09.cpp +// Randomizing die-rolling program. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +// contains prototypes for functions srand and rand +#include + +int main() +{ + unsigned seed; + + cout << "Enter seed: "; + cin >> seed; + srand( seed ); // seed random number generator + + // loop 10 times + for ( int counter = 1; counter <= 10; counter++ ) { + + // pick random number from 1 to 6 and output it + cout << setw( 10 ) << ( 1 + rand() % 6 ); + + // if counter divisible by 5, begin new line of output + if ( counter % 5 == 0 ) + cout << endl; + + } // end for + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_10.cpp b/Bachelor/Prog1/examples/ch03/Fig03_10.cpp new file mode 100644 index 0000000..5ebbd59 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_10.cpp @@ -0,0 +1,111 @@ +// Fig. 3.10: fig03_10.cpp +// Craps. +#include + +using std::cout; +using std::endl; + +// contains function prototypes for functions srand and rand +#include + +#include // contains prototype for function time + +int rollDice( void ); // function prototype + +int main() +{ + // enumeration constants represent game status + enum Status { CONTINUE, WON, LOST }; + + int sum; + int myPoint; + + Status gameStatus; // can contain CONTINUE, WON or LOST + + // randomize random number generator using current time + srand( time( 0 ) ); + + sum = rollDice(); // first roll of the dice + + // determine game status and point based on sum of dice + switch ( sum ) { + + // win on first roll + case 7: + case 11: + gameStatus = WON; + break; + + // lose on first roll + case 2: + case 3: + case 12: + gameStatus = LOST; + break; + + // remember point + default: + gameStatus = CONTINUE; + myPoint = sum; + cout << "Point is " << myPoint << endl; + break; // optional + + } // end switch + + // while game not complete ... + while ( gameStatus == CONTINUE ) { + sum = rollDice(); // roll dice again + + // determine game status + if ( sum == myPoint ) // win by making point + gameStatus = WON; + else + if ( sum == 7 ) // lose by rolling 7 + gameStatus = LOST; + + } // end while + + // display won or lost message + if ( gameStatus == WON ) + cout << "Player wins" << endl; + else + cout << "Player loses" << endl; + + return 0; // indicates successful termination + +} // end main + +// roll dice, calculate sum and display results +int rollDice( void ) +{ + int die1; + int die2; + int workSum; + + die1 = 1 + rand() % 6; // pick random die1 value + die2 = 1 + rand() % 6; // pick random die2 value + workSum = die1 + die2; // sum die1 and die2 + + // display results of this roll + cout << "Player rolled " << die1 << " + " << die2 + << " = " << workSum << endl; + + return workSum; // return sum of dice + +} // end function rollDice + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_12.cpp b/Bachelor/Prog1/examples/ch03/Fig03_12.cpp new file mode 100644 index 0000000..593c27a --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_12.cpp @@ -0,0 +1,96 @@ +// Fig. 3.12: fig03_12.cpp +// A scoping example. +#include + +using std::cout; +using std::endl; + +void useLocal( void ); // function prototype +void useStaticLocal( void ); // function prototype +void useGlobal( void ); // function prototype + +int x = 1; // global variable + +int main() +{ + int x = 5; // local variable to main + + cout << "local x in main's outer scope is " << x << endl; + + { // start new scope + + int x = 7; + + cout << "local x in main's inner scope is " << x << endl; + + } // end new scope + + cout << "local x in main's outer scope is " << x << endl; + + useLocal(); // useLocal has local x + useStaticLocal(); // useStaticLocal has static local x + useGlobal(); // useGlobal uses global x + useLocal(); // useLocal reinitializes its local x + useStaticLocal(); // static local x retains its prior value + useGlobal(); // global x also retains its value + + cout << "\nlocal x in main is " << x << endl; + + return 0; // indicates successful termination + +} // end main + +// useLocal reinitializes local variable x during each call +void useLocal( void ) +{ + int x = 25; // initialized each time useLocal is called + + cout << endl << "local x is " << x + << " on entering useLocal" << endl; + ++x; + cout << "local x is " << x + << " on exiting useLocal" << endl; + +} // end function useLocal + +// useStaticLocal initializes static local variable x only the +// first time the function is called; value of x is saved +// between calls to this function +void useStaticLocal( void ) +{ + // initialized first time useStaticLocal is called. + static int x = 50; + + cout << endl << "local static x is " << x + << " on entering useStaticLocal" << endl; + ++x; + cout << "local static x is " << x + << " on exiting useStaticLocal" << endl; + +} // end function useStaticLocal + +// useGlobal modifies global variable x during each call +void useGlobal( void ) +{ + cout << endl << "global x is " << x + << " on entering useGlobal" << endl; + x *= 10; + cout << "global x is " << x + << " on exiting useGlobal" << endl; + +} // end function useGlobal + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_14.cpp b/Bachelor/Prog1/examples/ch03/Fig03_14.cpp new file mode 100644 index 0000000..ca4f128 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_14.cpp @@ -0,0 +1,52 @@ +// Fig. 3.14: fig03_14.cpp +// Recursive factorial function. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +unsigned long factorial( unsigned long ); // function prototype + +int main() +{ + // Loop 10 times. During each iteration, calculate + // factorial( i ) and display result. + for ( int i = 0; i <= 10; i++ ) + cout << setw( 2 ) << i << "! = " + << factorial( i ) << endl; + + return 0; // indicates successful termination + +} // end main + +// recursive definition of function factorial +unsigned long factorial( unsigned long number ) +{ + // base case + if ( number <= 1 ) + return 1; + + // recursive step + else + return number * factorial( number - 1 ); + +} // end function factorial + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_15.cpp b/Bachelor/Prog1/examples/ch03/Fig03_15.cpp new file mode 100644 index 0000000..3882d55 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_15.cpp @@ -0,0 +1,56 @@ +// Fig. 3.15: fig03_15.cpp +// Recursive fibonacci function. +#include + +using std::cout; +using std::cin; +using std::endl; + +unsigned long fibonacci( unsigned long ); // function prototype + +int main() +{ + unsigned long result, number; + + // obtain integer from user + cout << "Enter an integer: "; + cin >> number; + + // calculate fibonacci value for number input by user + result = fibonacci( number ); + + // display result + cout << "Fibonacci(" << number << ") = " << result << endl; + + return 0; // indicates successful termination + +} // end main + +// recursive definition of function fibonacci +unsigned long fibonacci( unsigned long n ) +{ + // base case + if ( n == 0 || n == 1 ) + return n; + + // recursive step + else + return fibonacci( n - 1 ) + fibonacci( n - 2 ); + +} // end function fibonacci + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_18.cpp b/Bachelor/Prog1/examples/ch03/Fig03_18.cpp new file mode 100644 index 0000000..9e5781c --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_18.cpp @@ -0,0 +1,50 @@ +// Fig. 3.18: fig03_18.cpp +// Functions that take no arguments. +#include + +using std::cout; +using std::endl; + +void function1(); // function prototype +void function2( void ); // function prototype + +int main() +{ + function1(); // call function1 with no arguments + function2(); // call function2 with no arguments + + return 0; // indicates successful termination + +} // end main + +// function1 uses an empty parameter list to specify that +// the function receives no arguments +void function1() +{ + cout << "function1 takes no arguments" << endl; + +} // end function1 + +// function2 uses a void parameter list to specify that +// the function receives no arguments +void function2( void ) +{ + cout << "function2 also takes no arguments" << endl; + +} // end function2 + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_19.cpp b/Bachelor/Prog1/examples/ch03/Fig03_19.cpp new file mode 100644 index 0000000..e785534 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_19.cpp @@ -0,0 +1,50 @@ +// Fig. 3.19: fig03_19.cpp +// Using an inline function to calculate. +// the volume of a cube. +#include + +using std::cout; +using std::cin; +using std::endl; + +// Definition of inline function cube. Definition of function +// appears before function is called, so a function prototype +// is not required. First line of function definition acts as +// the prototype. +inline double cube( const double side ) +{ + return side * side * side; // calculate cube + +} // end function cube + +int main() +{ + cout << "Enter the side length of your cube: "; + + double sideValue; + + cin >> sideValue; + + // calculate cube of sideValue and display result + cout << "Volume of cube with side " + << sideValue << " is " << cube( sideValue ) << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_20.cpp b/Bachelor/Prog1/examples/ch03/Fig03_20.cpp new file mode 100644 index 0000000..e94ca30 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_20.cpp @@ -0,0 +1,61 @@ +// Fig. 3.20: fig03_20.cpp +// Comparing pass-by-value and pass-by-reference +// with references. +#include + +using std::cout; +using std::endl; + +int squareByValue( int ); // function prototype +void squareByReference( int & ); // function prototype + +int main() +{ + int x = 2; + int z = 4; + + // demonstrate squareByValue + cout << "x = " << x << " before squareByValue\n"; + cout << "Value returned by squareByValue: " + << squareByValue( x ) << endl; + cout << "x = " << x << " after squareByValue\n" << endl; + + // demonstrate squareByReference + cout << "z = " << z << " before squareByReference" << endl; + squareByReference( z ); + cout << "z = " << z << " after squareByReference" << endl; + + return 0; // indicates successful termination +} // end main + +// squareByValue multiplies number by itself, stores the +// result in number and returns the new value of number +int squareByValue( int number ) +{ + return number *= number; // caller's argument not modified + +} // end function squareByValue + +// squareByReference multiplies numberRef by itself and +// stores the result in the variable to which numberRef +// refers in function main +void squareByReference( int &numberRef ) +{ + numberRef *= numberRef; // caller's argument modified + +} // end function squareByReference + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_21.cpp b/Bachelor/Prog1/examples/ch03/Fig03_21.cpp new file mode 100644 index 0000000..4e1af15 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_21.cpp @@ -0,0 +1,37 @@ +// Fig. 3.21: fig03_21.cpp +// References must be initialized. +#include + +using std::cout; +using std::endl; + +int main() +{ + int x = 3; + + // y refers to (is an alias for) x + int &y = x; + + cout << "x = " << x << endl << "y = " << y << endl; + y = 7; + cout << "x = " << x << endl << "y = " << y << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_22.cpp b/Bachelor/Prog1/examples/ch03/Fig03_22.cpp new file mode 100644 index 0000000..90ab6a9 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_22.cpp @@ -0,0 +1,35 @@ +// Fig. 3.22: fig03_22.cpp +// References must be initialized. +#include + +using std::cout; +using std::endl; + +int main() +{ + int x = 3; + int &y; // Error: y must be initialized + + cout << "x = " << x << endl << "y = " << y << endl; + y = 7; + cout << "x = " << x << endl << "y = " << y << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_23.cpp b/Bachelor/Prog1/examples/ch03/Fig03_23.cpp new file mode 100644 index 0000000..b3cffc5 --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_23.cpp @@ -0,0 +1,54 @@ +// Fig. 3.23: fig03_23.cpp +// Using default arguments. +#include + +using std::cout; +using std::endl; + +// function prototype that specifies default arguments +int boxVolume( int length = 1, int width = 1, int height = 1 ); + +int main() +{ + // no arguments--use default values for all dimensions + cout << "The default box volume is: " << boxVolume(); + + // specify length; default width and height + cout << "\n\nThe volume of a box with length 10,\n" + << "width 1 and height 1 is: " << boxVolume( 10 ); + + // specify length and width; default height + cout << "\n\nThe volume of a box with length 10,\n" + << "width 5 and height 1 is: " << boxVolume( 10, 5 ); + + // specify all arguments + cout << "\n\nThe volume of a box with length 10,\n" + << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) + << endl; + + return 0; // indicates successful termination + +} // end main + +// function boxVolume calculates the volume of a box +int boxVolume( int length, int width, int height ) +{ + return length * width * height; + +} // end function boxVolume + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_24.cpp b/Bachelor/Prog1/examples/ch03/Fig03_24.cpp new file mode 100644 index 0000000..b1fa28b --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_24.cpp @@ -0,0 +1,44 @@ +// Fig. 3.24: fig03_24.cpp +// Using the unary scope resolution operator. +#include + +using std::cout; +using std::endl; + +#include + +using std::setprecision; + +// define global constant PI +const double PI = 3.14159265358979; + +int main() +{ + // define local constant PI + const float PI = static_cast< float >( ::PI ); + + // display values of local and global PI constants + cout << setprecision( 20 ) + << " Local float value of PI = " << PI + << "\nGlobal double value of PI = " << ::PI << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_25.cpp b/Bachelor/Prog1/examples/ch03/Fig03_25.cpp new file mode 100644 index 0000000..2539aee --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_25.cpp @@ -0,0 +1,50 @@ +// Fig. 3.25: fig03_25.cpp +// Using overloaded functions. +#include + +using std::cout; +using std::endl; + +// function square for int values +int square( int x ) +{ + cout << "Called square with int argument: " << x << endl; + return x * x; + +} // end int version of function square + +// function square for double values +double square( double y ) +{ + cout << "Called square with double argument: " << y << endl; + return y * y; + +} // end double version of function square + +int main() +{ + int intResult = square( 7 ); // calls int version + double doubleResult = square( 7.5 ); // calls double version + + cout << "\nThe square of integer 7 is " << intResult + << "\nThe square of double 7.5 is " << doubleResult + << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_26.cpp b/Bachelor/Prog1/examples/ch03/Fig03_26.cpp new file mode 100644 index 0000000..5b2fdec --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_26.cpp @@ -0,0 +1,50 @@ +// Fig. 3.26: fig03_26.cpp +// Name mangling. + +// function square for int values +int square( int x ) +{ + return x * x; +} + +// function square for double values +double square( double y ) +{ + return y * y; +} + +// function that receives arguments of types +// int, float, char and int * +void nothing1( int a, float b, char c, int *d ) +{ + // empty function body +} + +// function that receives arguments of types +// char, int, float * and double * +char *nothing2( char a, int b, float *c, double *d ) +{ + return 0; +} + +int main() +{ + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch03/Fig03_27.cpp b/Bachelor/Prog1/examples/ch03/Fig03_27.cpp new file mode 100644 index 0000000..13a671f --- /dev/null +++ b/Bachelor/Prog1/examples/ch03/Fig03_27.cpp @@ -0,0 +1,76 @@ +// Fig. 3.27: fig03_27.cpp +// Using a function template. +#include + +using std::cout; +using std::cin; +using std::endl; + +// definition of function template maximum +template < class T > // or template< typename T > +T maximum( T value1, T value2, T value3 ) +{ + T max = value1; + + if ( value2 > max ) + max = value2; + + if ( value3 > max ) + max = value3; + + return max; + +} // end function template maximum + +int main() +{ + // demonstrate maximum with int values + int int1, int2, int3; + + cout << "Input three integer values: "; + cin >> int1 >> int2 >> int3; + + // invoke int version of maximum + cout << "The maximum integer value is: " + << maximum( int1, int2, int3 ); + + // demonstrate maximum with double values + double double1, double2, double3; + + cout << "\n\nInput three double values: "; + cin >> double1 >> double2 >> double3; + + // invoke double version of maximum + cout << "The maximum double value is: " + << maximum( double1, double2, double3 ); + + // demonstrate maximum with char values + char char1, char2, char3; + + cout << "\n\nInput three characters: "; + cin >> char1 >> char2 >> char3; + + // invoke char version of maximum + cout << "The maximum character value is: " + << maximum( char1, char2, char3 ) + << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Ex04_18.cpp b/Bachelor/Prog1/examples/ch04/Ex04_18.cpp new file mode 100644 index 0000000..9ccbc6e --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Ex04_18.cpp @@ -0,0 +1,49 @@ +// Ex. 4.18: ex04_18.cpp +// What does this program do? +#include + +using std::cout; +using std::endl; + +int whatIsThis( int [], int ); // function prototype + +int main() +{ + const int arraySize = 10; + int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + int result = whatIsThis( a, arraySize ); + + cout << "Result is " << result << endl; + + return 0; // indicates successful termination + +} // end main + +// What does this function do? +int whatIsThis( int b[], int size ) +{ + // base case + if ( size == 1 ) + return b[ 0 ]; + + // recursive step + else + return b[ size - 1 ] + whatIsThis( b, size - 1 ); + +} // end function whatIsThis + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Ex04_21.cpp b/Bachelor/Prog1/examples/ch04/Ex04_21.cpp new file mode 100644 index 0000000..06c3e49 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Ex04_21.cpp @@ -0,0 +1,47 @@ +// Ex. 4.21: ex04_21.cpp +// What does this program do? +#include + +using std::cout; +using std::endl; + +void someFunction( int [], int, int ); // function prototype + +int main() +{ + const int arraySize = 10; + int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + cout << "The values in the array are:" << endl; + someFunction( a, 0, arraySize ); + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// What does this function do? +void someFunction( int b[], int current, int size ) +{ + if ( current < size ) { + someFunction( b, current + 1, size ); + cout << b[ current ] << " "; + } + +} // end function someFunction + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_03.cpp b/Bachelor/Prog1/examples/ch04/Fig04_03.cpp new file mode 100644 index 0000000..9dffb95 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_03.cpp @@ -0,0 +1,44 @@ +// Fig. 4.3: fig04_03.cpp +// Initializing an array. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +int main() +{ + int n[ 10 ]; // n is an array of 10 integers + + // initialize elements of array n to 0 + for ( int i = 0; i < 10; i++ ) + n[ i ] = 0; // set element at location i to 0 + + cout << "Element" << setw( 13 ) << "Value" << endl; + + // output contents of array n in tabular format + for ( int j = 0; j < 10; j++ ) + cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_04.cpp b/Bachelor/Prog1/examples/ch04/Fig04_04.cpp new file mode 100644 index 0000000..6f6efb0 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_04.cpp @@ -0,0 +1,42 @@ +// Fig. 4.4: fig04_04.cpp +// Initializing an array with a declaration. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +int main() +{ + // use initializer list to initialize array n + int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; + + cout << "Element" << setw( 13 ) << "Value" << endl; + + // output contents of array n in tabular format + for ( int i = 0; i < 10; i++ ) + cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_05.cpp b/Bachelor/Prog1/examples/ch04/Fig04_05.cpp new file mode 100644 index 0000000..4905155 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_05.cpp @@ -0,0 +1,46 @@ +// Fig. 4.5: fig04_05.cpp +// Initialize array s to the even integers from 2 to 20. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +int main() +{ + // constant variable can be used to specify array size + const int arraySize = 10; + + int s[ arraySize ]; // array s has 10 elements + + for ( int i = 0; i < arraySize; i++ ) // set the values + s[ i ] = 2 + 2 * i; + + cout << "Element" << setw( 13 ) << "Value" << endl; + + // output contents of array s in tabular format + for ( int j = 0; j < arraySize; j++ ) + cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_06.cpp b/Bachelor/Prog1/examples/ch04/Fig04_06.cpp new file mode 100644 index 0000000..f62da85 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_06.cpp @@ -0,0 +1,35 @@ +// Fig. 4.6: fig04_06.cpp +// Using a properly initialized constant variable. +#include + +using std::cout; +using std::endl; + +int main() +{ + const int x = 7; // initialized constant variable + + cout << "The value of constant variable x is: " + << x << endl; + + return 0; // indicates successful termination + +} // end main + + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_07.cpp b/Bachelor/Prog1/examples/ch04/Fig04_07.cpp new file mode 100644 index 0000000..bc1cc0c --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_07.cpp @@ -0,0 +1,29 @@ +// Fig. 4.7: fig04_07.cpp +// A const object must be initialized. + +int main() +{ + const int x; // Error: x must be initialized + + x = 7; // Error: cannot modify a const variable + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_08.cpp b/Bachelor/Prog1/examples/ch04/Fig04_08.cpp new file mode 100644 index 0000000..bda7cc0 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_08.cpp @@ -0,0 +1,41 @@ +// Fig. 4.8: fig04_08.cpp +// Compute the sum of the elements of the array. +#include + +using std::cout; +using std::endl; + +int main() +{ + const int arraySize = 10; + + int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + int total = 0; + + // sum contents of array a + for ( int i = 0; i < arraySize; i++ ) + total += a[ i ]; + + cout << "Total of array element values is " << total << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_09.cpp b/Bachelor/Prog1/examples/ch04/Fig04_09.cpp new file mode 100644 index 0000000..1b722e9 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_09.cpp @@ -0,0 +1,49 @@ +// Fig. 4.9: fig04_09.cpp +// Histogram printing program. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +int main() +{ + const int arraySize = 10; + int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; + + cout << "Element" << setw( 13 ) << "Value" + << setw( 17 ) << "Histogram" << endl; + + // for each element of array n, output a bar in histogram + for ( int i = 0; i < arraySize; i++ ) { + cout << setw( 7 ) << i << setw( 13 ) + << n[ i ] << setw( 9 ); + + for ( int j = 0; j < n[ i ]; j++ ) // print one bar + cout << '*'; + + cout << endl; // start next line of output + + } // end outer for structure + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_10.cpp b/Bachelor/Prog1/examples/ch04/Fig04_10.cpp new file mode 100644 index 0000000..9984df6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_10.cpp @@ -0,0 +1,53 @@ +// Fig. 4.10: fig04_10.cpp +// Roll a six-sided die 6000 times. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include +#include + +int main() +{ + const int arraySize = 7; + int frequency[ arraySize ] = { 0 }; + + srand( time( 0 ) ); // seed random-number generator + + // roll die 6000 times + for ( int roll = 1; roll <= 6000; roll++ ) + ++frequency[ 1 + rand() % 6 ]; // replaces 20-line switch + // of Fig. 3.8 + + cout << "Face" << setw( 13 ) << "Frequency" << endl; + + // output frequency elements 1-6 in tabular format + for ( int face = 1; face < arraySize; face++ ) + cout << setw( 4 ) << face + << setw( 13 ) << frequency[ face ] << endl; + + return 0; // indicates successful termination + +} // end main + + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_11.cpp b/Bachelor/Prog1/examples/ch04/Fig04_11.cpp new file mode 100644 index 0000000..86eba62 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_11.cpp @@ -0,0 +1,58 @@ +// Fig. 4.11: fig04_11.cpp +// Student poll program. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +int main() +{ + // define array sizes + const int responseSize = 40; // size of array responses + const int frequencySize = 11; // size of array frequency + + // place survey responses in array responses + int responses[ responseSize ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, + 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, + 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; + + // initialize frequency counters to 0 + int frequency[ frequencySize ] = { 0 }; + + // for each answer, select value of an element of array + // responses and use that value as subscript in array + // frequency to determine element to increment + for ( int answer = 0; answer < responseSize; answer++ ) + ++frequency[ responses[answer] ]; + + // display results + cout << "Rating" << setw( 17 ) << "Frequency" << endl; + + // output frequencies in tabular format + for ( int rating = 1; rating < frequencySize; rating++ ) + cout << setw( 6 ) << rating + << setw( 17 ) << frequency[ rating ] << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_12.cpp b/Bachelor/Prog1/examples/ch04/Fig04_12.cpp new file mode 100644 index 0000000..115d033 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_12.cpp @@ -0,0 +1,49 @@ +// Fig. 4_12: fig04_12.cpp +// Treating character arrays as strings. +#include + +using std::cout; +using std::cin; +using std::endl; + +int main() +{ + char string1[ 20 ]; // reserves 20 characters + char string2[] = "string literal"; // reserves 15 characters + + // read string from user into array string2 + cout << "Enter the string \"hello there\": "; + cin >> string1; // reads "hello" [space terminates input] + + // output strings + cout << "string1 is: " << string1 + << "\nstring2 is: " << string2; + + cout << "\nstring1 with spaces between characters is:\n"; + + // output characters until null character is reached + for ( int i = 0; string1[ i ] != '\0'; i++ ) + cout << string1[ i ] << ' '; + + cin >> string1; // reads "there" + cout << "\nstring1 is: " << string1 << endl; + + return 0; // indicates successful termination + +} // end main + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_13.cpp b/Bachelor/Prog1/examples/ch04/Fig04_13.cpp new file mode 100644 index 0000000..ed21456 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_13.cpp @@ -0,0 +1,82 @@ +// Fig. 4.13: fig04_13.cpp +// Static arrays are initialized to zero. +#include + +using std::cout; +using std::endl; + +void staticArrayInit( void ); // function prototype +void automaticArrayInit( void ); // function prototype + +int main() +{ + cout << "First call to each function:\n"; + staticArrayInit(); + automaticArrayInit(); + + cout << "\n\nSecond call to each function:\n"; + staticArrayInit(); + automaticArrayInit(); + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// function to demonstrate a static local array +void staticArrayInit( void ) +{ + // initializes elements to 0 first time function is called + static int array1[ 3 ]; + + cout << "\nValues on entering staticArrayInit:\n"; + + // output contents of array1 + for ( int i = 0; i < 3; i++ ) + cout << "array1[" << i << "] = " << array1[ i ] << " "; + + cout << "\nValues on exiting staticArrayInit:\n"; + + // modify and output contents of array1 + for ( int j = 0; j < 3; j++ ) + cout << "array1[" << j << "] = " + << ( array1[ j ] += 5 ) << " "; + +} // end function staticArrayInit + +// function to demonstrate an automatic local array +void automaticArrayInit( void ) +{ + // initializes elements each time function is called + int array2[ 3 ] = { 1, 2, 3 }; + + cout << "\n\nValues on entering automaticArrayInit:\n"; + + // output contents of array2 + for ( int i = 0; i < 3; i++ ) + cout << "array2[" << i << "] = " << array2[ i ] << " "; + + cout << "\nValues on exiting automaticArrayInit:\n"; + + // modify and output contents of array2 + for ( int j = 0; j < 3; j++ ) + cout << "array2[" << j << "] = " + << ( array2[ j ] += 5 ) << " "; + +} // end function automaticArrayInit + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_14.cpp b/Bachelor/Prog1/examples/ch04/Fig04_14.cpp new file mode 100644 index 0000000..a1db3ea --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_14.cpp @@ -0,0 +1,87 @@ +// Fig. 4.14: fig04_14.cpp +// Passing arrays and individual array elements to functions. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +void modifyArray( int [], int ); // appears strange +void modifyElement( int ); + +int main() +{ + const int arraySize = 5; // size of array a + int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize a + + cout << "Effects of passing entire array by reference:" + << "\n\nThe values of the original array are:\n"; + + // output original array + for ( int i = 0; i < arraySize; i++ ) + cout << setw( 3 ) << a[ i ]; + + cout << endl; + + // pass array a to modifyArray by reference + modifyArray( a, arraySize ); + + cout << "The values of the modified array are:\n"; + + // output modified array + for ( int j = 0; j < arraySize; j++ ) + cout << setw( 3 ) << a[ j ]; + + // output value of a[ 3 ] + cout << "\n\n\n" + << "Effects of passing array element by value:" + << "\n\nThe value of a[3] is " << a[ 3 ] << '\n'; + + // pass array element a[ 3 ] by value + modifyElement( a[ 3 ] ); + + // output value of a[ 3 ] + cout << "The value of a[3] is " << a[ 3 ] << endl; + + return 0; // indicates successful termination + +} // end main + +// in function modifyArray, "b" points to +// the original array "a" in memory +void modifyArray( int b[], int sizeOfArray ) +{ + // multiply each array element by 2 + for ( int k = 0; k < sizeOfArray; k++ ) + b[ k ] *= 2; + +} // end function modifyArray + +// in function modifyElement, "e" is a local copy of +// array element a[ 3 ] passed from main +void modifyElement( int e ) +{ + // multiply parameter by 2 + cout << "Value in modifyElement is " + << ( e *= 2 ) << endl; + +} // end function modifyElement + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_15.cpp b/Bachelor/Prog1/examples/ch04/Fig04_15.cpp new file mode 100644 index 0000000..5d35b9d --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_15.cpp @@ -0,0 +1,46 @@ +// Fig. 4.15: fig04_15.cpp +// Demonstrating the const type qualifier. +#include + +using std::cout; +using std::endl; + +void tryToModifyArray( const int [] ); // function prototype + +int main() +{ + int a[] = { 10, 20, 30 }; + + tryToModifyArray( a ); + + cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n'; + + return 0; // indicates successful termination + +} // end main + +// In function tryToModifyArray, "b" cannot be used +// to modify the original array "a" in main. +void tryToModifyArray( const int b[] ) +{ + b[ 0 ] /= 2; // error + b[ 1 ] /= 2; // error + b[ 2 ] /= 2; // error + +} // end function tryToModifyArray + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_16.cpp b/Bachelor/Prog1/examples/ch04/Fig04_16.cpp new file mode 100644 index 0000000..e009daf --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_16.cpp @@ -0,0 +1,65 @@ +// Fig. 4.16: fig04_16.cpp +// This program sorts an array's values into ascending order. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +int main() +{ + const int arraySize = 10; // size of array a + int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; + int hold; // temporary location used to swap array elements + + cout << "Data items in original order\n"; + + // output original array + for ( int i = 0; i < arraySize; i++ ) + cout << setw( 4 ) << a[ i ]; + + // bubble sort + // loop to control number of passes + for ( int pass = 0; pass < arraySize - 1; pass++ ) + + // loop to control number of comparisons per pass + for ( int j = 0; j < arraySize - 1; j++ ) + + // compare side-by-side elements and swap them if + // first element is greater than second element + if ( a[ j ] > a[ j + 1 ] ) { + hold = a[ j ]; + a[ j ] = a[ j + 1 ]; + a[ j + 1 ] = hold; + + } // end if + + cout << "\nData items in ascending order\n"; + + // output sorted array + for ( int k = 0; k < arraySize; k++ ) + cout << setw( 4 ) << a[ k ]; + + cout << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_17.cpp b/Bachelor/Prog1/examples/ch04/Fig04_17.cpp new file mode 100644 index 0000000..c6442c1 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_17.cpp @@ -0,0 +1,193 @@ +// Fig. 4.17: fig04_17.cpp +// This program introduces the topic of survey data analysis. +// It computes the mean, median, and mode of the data. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setw; +using std::setprecision; + +void mean( const int [], int ); +void median( int [], int ); +void mode( int [], int [], int ); +void bubbleSort( int[], int ); +void printArray( const int[], int ); + +int main() +{ + const int responseSize = 99; // size of array responses + + int frequency[ 10 ] = { 0 }; // initialize array frequency + + // initialize array responses + int response[ responseSize ] = + { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, + 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, + 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, + 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, + 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, + 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, + 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, + 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, + 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, + 4, 5, 6, 1, 6, 5, 7, 8, 7 }; + + // process responses + mean( response, responseSize ); + median( response, responseSize ); + mode( frequency, response, responseSize ); + + return 0; // indicates successful termination + +} // end main + +// calculate average of all response values +void mean( const int answer[], int arraySize ) +{ + int total = 0; + + cout << "********\n Mean\n********\n"; + + // total response values + for ( int i = 0; i < arraySize; i++ ) + total += answer[ i ]; + + // format and output results + cout << fixed << setprecision( 4 ); + + cout << "The mean is the average value of the data\n" + << "items. The mean is equal to the total of\n" + << "all the data items divided by the number\n" + << "of data items (" << arraySize + << "). The mean value for\nthis run is: " + << total << " / " << arraySize << " = " + << static_cast< double >( total ) / arraySize + << "\n\n"; + +} // end function mean + +// sort array and determine median element's value +void median( int answer[], int size ) +{ + cout << "\n********\n Median\n********\n" + << "The unsorted array of responses is"; + + printArray( answer, size ); // output unsorted array + + bubbleSort( answer, size ); // sort array + + cout << "\n\nThe sorted array is"; + printArray( answer, size ); // output sorted array + + // display median element + cout << "\n\nThe median is element " << size / 2 + << " of\nthe sorted " << size + << " element array.\nFor this run the median is " + << answer[ size / 2 ] << "\n\n"; + +} // end function median + +// determine most frequent response +void mode( int freq[], int answer[], int size ) +{ + int largest = 0; // represents largest frequency + int modeValue = 0; // represents most frequent response + + cout << "\n********\n Mode\n********\n"; + + // initialize frequencies to 0 + for ( int i = 1; i <= 9; i++ ) + freq[ i ] = 0; + + // summarize frequencies + for ( int j = 0; j < size; j++ ) + ++freq[ answer[ j ] ]; + + // output headers for result columns + cout << "Response" << setw( 11 ) << "Frequency" + << setw( 19 ) << "Histogram\n\n" << setw( 55 ) + << "1 1 2 2\n" << setw( 56 ) + << "5 0 5 0 5\n\n"; + + // output results + for ( int rating = 1; rating <= 9; rating++ ) { + cout << setw( 8 ) << rating << setw( 11 ) + << freq[ rating ] << " "; + + // keep track of mode value and largest fequency value + if ( freq[ rating ] > largest ) { + largest = freq[ rating ]; + modeValue = rating; + + } // end if + + // output histogram bar representing frequency value + for ( int k = 1; k <= freq[ rating ]; k++ ) + cout << '*'; + + cout << '\n'; // begin new line of output + + } // end outer for + + // display the mode value + cout << "The mode is the most frequent value.\n" + << "For this run the mode is " << modeValue + << " which occurred " << largest << " times." << endl; + +} // end function mode + +// function that sorts an array with bubble sort algorithm +void bubbleSort( int a[], int size ) +{ + int hold; // temporary location used to swap elements + + // loop to control number of passes + for ( int pass = 1; pass < size; pass++ ) + + // loop to control number of comparisons per pass + for ( int j = 0; j < size - 1; j++ ) + + // swap elements if out of order + if ( a[ j ] > a[ j + 1 ] ) { + hold = a[ j ]; + a[ j ] = a[ j + 1 ]; + a[ j + 1 ] = hold; + + } // end if + +} // end function bubbleSort + +// output array contents (20 values per row) +void printArray( const int a[], int size ) +{ + for ( int i = 0; i < size; i++ ) { + + if ( i % 20 == 0 ) // begin new line every 20 values + cout << endl; + + cout << setw( 2 ) << a[ i ]; + + } // end for + +} // end function printArray + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_19.cpp b/Bachelor/Prog1/examples/ch04/Fig04_19.cpp new file mode 100644 index 0000000..0be6aa3 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_19.cpp @@ -0,0 +1,63 @@ +// Fig. 4.19: fig04_19.cpp +// Linear search of an array. +#include + +using std::cout; +using std::cin; +using std::endl; + +int linearSearch( const int [], int, int ); // prototype + +int main() +{ + const int arraySize = 100; // size of array a + int a[ arraySize ]; // create array a + int searchKey; // value to locate in a + + for ( int i = 0; i < arraySize; i++ ) // create some data + a[ i ] = 2 * i; + + cout << "Enter integer search key: "; + cin >> searchKey; + + // attempt to locate searchKey in array a + int element = linearSearch( a, searchKey, arraySize ); + + // display results + if ( element != -1 ) + cout << "Found value in element " << element << endl; + else + cout << "Value not found" << endl; + + return 0; // indicates successful termination + +} // end main + +// compare key to every element of array until location is +// found or until end of array is reached; return subscript of +// element if key or -1 if key not found +int linearSearch( const int array[], int key, int sizeOfArray ) +{ + for ( int j = 0; j < sizeOfArray; j++ ) + + if ( array[ j ] == key ) // if found, + return j; // return location of key + + return -1; // key not found + +} // end function linearSearch + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_20.cpp b/Bachelor/Prog1/examples/ch04/Fig04_20.cpp new file mode 100644 index 0000000..241c800 --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_20.cpp @@ -0,0 +1,142 @@ +// Fig. 4.20: fig04_20.cpp +// Binary search of an array. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +// function prototypes +int binarySearch( const int [], int, int, int, int ); +void printHeader( int ); +void printRow( const int [], int, int, int, int ); + +int main() +{ + const int arraySize = 15; // size of array a + int a[ arraySize ]; // create array a + int key; // value to locate in a + + for ( int i = 0; i < arraySize; i++ ) // create some data + a[ i ] = 2 * i; + + cout << "Enter a number between 0 and 28: "; + cin >> key; + + printHeader( arraySize ); + + // search for key in array a + int result = + binarySearch( a, key, 0, arraySize - 1, arraySize ); + + // display results + if ( result != -1 ) + cout << '\n' << key << " found in array element " + << result << endl; + else + cout << '\n' << key << " not found" << endl; + + return 0; // indicates successful termination + +} // end main + +// function to perform binary search of an array +int binarySearch( const int b[], int searchKey, int low, + int high, int size ) +{ + int middle; + + // loop until low subscript is greater than high subscript + while ( low <= high ) { + + // determine middle element of subarray being searched + middle = ( low + high ) / 2; + + // display subarray used in this loop iteration + printRow( b, low, middle, high, size ); + + // if searchKey matches middle element, return middle + if ( searchKey == b[ middle ] ) // match + return middle; + + else + + // if searchKey less than middle element, + // set new high element + if ( searchKey < b[ middle ] ) + high = middle - 1; // search low end of array + + // if searchKey greater than middle element, + // set new low element + else + low = middle + 1; // search high end of array + } + + return -1; // searchKey not found + +} // end function binarySearch + +// print header for output +void printHeader( int size ) +{ + cout << "\nSubscripts:\n"; + + // output column heads + for ( int j = 0; j < size; j++ ) + cout << setw( 3 ) << j << ' '; + + cout << '\n'; // start new line of output + + // output line of - characters + for ( int k = 1; k <= 4 * size; k++ ) + cout << '-'; + + cout << endl; // start new line of output + +} // end function printHeader + +// print one row of output showing the current +// part of the array being processed +void printRow( const int b[], int low, int mid, + int high, int size ) +{ + // loop through entire array + for ( int m = 0; m < size; m++ ) + + // display spaces if outside current subarray range + if ( m < low || m > high ) + cout << " "; + + // display middle element marked with a * + else + + if ( m == mid ) // mark middle value + cout << setw( 3 ) << b[ m ] << '*'; + + // display other elements in subarray + else + cout << setw( 3 ) << b[ m ] << ' '; + + cout << endl; // start new line of output + +} // end function printRow + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_22.cpp b/Bachelor/Prog1/examples/ch04/Fig04_22.cpp new file mode 100644 index 0000000..44c17da --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_22.cpp @@ -0,0 +1,57 @@ +// Fig. 4.22: fig04_22.cpp +// Initializing multidimensional arrays. +#include + +using std::cout; +using std::endl; + +void printArray( int [][ 3 ] ); + +int main() +{ + int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; + int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; + int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; + + cout << "Values in array1 by row are:" << endl; + printArray( array1 ); + + cout << "Values in array2 by row are:" << endl; + printArray( array2 ); + + cout << "Values in array3 by row are:" << endl; + printArray( array3 ); + + return 0; // indicates successful termination + +} // end main + +// function to output array with two rows and three columns +void printArray( int a[][ 3 ] ) +{ + for ( int i = 0; i < 2; i++ ) { // for each row + + for ( int j = 0; j < 3; j++ ) // output column values + cout << a[ i ][ j ] << ' '; + + cout << endl; // start new line of output + + } // end outer for structure + +} // end function printArray + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch04/Fig04_23.cpp b/Bachelor/Prog1/examples/ch04/Fig04_23.cpp new file mode 100644 index 0000000..7957c1e --- /dev/null +++ b/Bachelor/Prog1/examples/ch04/Fig04_23.cpp @@ -0,0 +1,134 @@ +// Fig. 4.23: fig04_23.cpp +// Double-subscripted array example. +#include + +using std::cout; +using std::endl; +using std::fixed; +using std::left; + +#include + +using std::setw; +using std::setprecision; + +const int students = 3; // number of students +const int exams = 4; // number of exams + +// function prototypes +int minimum( int [][ exams ], int, int ); +int maximum( int [][ exams ], int, int ); +double average( int [], int ); +void printArray( int [][ exams ], int, int ); + +int main() +{ + // initialize student grades for three students (rows) + int studentGrades[ students ][ exams ] = + { { 77, 68, 86, 73 }, + { 96, 87, 89, 78 }, + { 70, 90, 86, 81 } }; + + // output array studentGrades + cout << "The array is:\n"; + printArray( studentGrades, students, exams ); + + // determine smallest and largest grade values + cout << "\n\nLowest grade: " + << minimum( studentGrades, students, exams ) + << "\nHighest grade: " + << maximum( studentGrades, students, exams ) << '\n'; + + cout << fixed << setprecision( 2 ); + + // calculate average grade for each student + for ( int person = 0; person < students; person++ ) + cout << "The average grade for student " << person + << " is " + << average( studentGrades[ person ], exams ) + << endl; + + return 0; // indicates successful termination + +} // end main + +// find minimum grade +int minimum( int grades[][ exams ], int pupils, int tests ) +{ + int lowGrade = 100; // initialize to highest possible grade + + for ( int i = 0; i < pupils; i++ ) + + for ( int j = 0; j < tests; j++ ) + + if ( grades[ i ][ j ] < lowGrade ) + lowGrade = grades[ i ][ j ]; + + return lowGrade; + +} // end function minimum + +// find maximum grade +int maximum( int grades[][ exams ], int pupils, int tests ) +{ + int highGrade = 0; // initialize to lowest possible grade + + for ( int i = 0; i < pupils; i++ ) + + for ( int j = 0; j < tests; j++ ) + + if ( grades[ i ][ j ] > highGrade ) + highGrade = grades[ i ][ j ]; + + return highGrade; + +} // end function maximum + +// determine average grade for particular student +double average( int setOfGrades[], int tests ) +{ + int total = 0; + + // total all grades for one student + for ( int i = 0; i < tests; i++ ) + total += setOfGrades[ i ]; + + return static_cast< double >( total ) / tests; // average + +} // end function maximum + +// Print the array +void printArray( int grades[][ exams ], int pupils, int tests ) +{ + // set left justification and output column heads + cout << left << " [0] [1] [2] [3]"; + + // output grades in tabular format + for ( int i = 0; i < pupils; i++ ) { + + // output label for row + cout << "\nstudentGrades[" << i << "] "; + + // output one grades for one student + for ( int j = 0; j < tests; j++ ) + cout << setw( 5 ) << grades[ i ][ j ]; + + } // end outer for + +} // end function printArray + + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Ex05_21.cpp b/Bachelor/Prog1/examples/ch05/Ex05_21.cpp new file mode 100644 index 0000000..1fd9111 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Ex05_21.cpp @@ -0,0 +1,49 @@ +// Ex. 5.21: ex05_21.cpp +// What does this program do? +#include + +using std::cout; +using std::cin; +using std::endl; + +void mystery1( char *, const char * ); // prototype + +int main() +{ + char string1[ 80 ]; + char string2[ 80 ]; + + cout << "Enter two strings: "; + cin >> string1 >> string2; + mystery1( string1, string2 ); + cout << string1 << endl; + + return 0; // indicates successful termination + +} // end main + +// What does this function do? +void mystery1( char *s1, const char *s2 ) +{ + while ( *s1 != '\0' ) + ++s1; + + for ( ; *s1 = *s2; s1++, s2++ ) + ; // empty statement + +} // end function mystery1 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Ex05_22.cpp b/Bachelor/Prog1/examples/ch05/Ex05_22.cpp new file mode 100644 index 0000000..9299669 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Ex05_22.cpp @@ -0,0 +1,48 @@ +// Ex. 5.22: ex05_22.cpp +// What does this program do? +#include + +using std::cout; +using std::cin; +using std::endl; + +int mystery2( const char * ); // prototype + +int main() +{ + char string1[ 80 ]; + + cout << "Enter a string: "; + cin >> string1; + cout << mystery2( string1 ) << endl; + + return 0; // indicates successful termination + +} // end main + +// What does this function do? +int mystery2( const char *s ) +{ + int x; + + for ( x = 0; *s != '\0'; s++ ) + ++x; + + return x; + +} // end function mystery2 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Ex05_30.cpp b/Bachelor/Prog1/examples/ch05/Ex05_30.cpp new file mode 100644 index 0000000..88ede48 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Ex05_30.cpp @@ -0,0 +1,49 @@ +// Ex. 5.30: ex05_30.cpp +// What does this program do? +#include + +using std::cout; +using std::cin; +using std::endl; + +bool mystery3( const char *, const char * ); // prototype + +int main() +{ + char string1[ 80 ], string2[ 80 ]; + + cout << "Enter two strings: "; + cin >> string1 >> string2; + cout << "The result is " + << mystery3( string1, string2 ) << endl; + + return 0; // indicates successful termination + +} // end main + +// What does this function do? +bool mystery3( const char *s1, const char *s2 ) +{ + for ( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) + + if ( *s1 != *s2 ) + return false; + + return true; + +} // end function mystery3 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_04.cpp b/Bachelor/Prog1/examples/ch05/Fig05_04.cpp new file mode 100644 index 0000000..bcf52ff --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_04.cpp @@ -0,0 +1,43 @@ +// Fig. 5.4: fig05_04.cpp +// Using the & and * operators. +#include + +using std::cout; +using std::endl; + +int main() +{ + int a; // a is an integer + int *aPtr; // aPtr is a pointer to an integer + + a = 7; + aPtr = &a; // aPtr assigned address of a + + cout << "The address of a is " << &a + << "\nThe value of aPtr is " << aPtr; + + cout << "\n\nThe value of a is " << a + << "\nThe value of *aPtr is " << *aPtr; + + cout << "\n\nShowing that * and & are inverses of " + << "each other.\n&*aPtr = " << &*aPtr + << "\n*&aPtr = " << *&aPtr << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_06.cpp b/Bachelor/Prog1/examples/ch05/Fig05_06.cpp new file mode 100644 index 0000000..5aad3b7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_06.cpp @@ -0,0 +1,45 @@ +// Fig. 5.6: fig05_06.cpp +// Cube a variable using pass-by-value. +#include + +using std::cout; +using std::endl; + +int cubeByValue( int ); // prototype + +int main() +{ + int number = 5; + + cout << "The original value of number is " << number; + + // pass number by value to cubeByValue + number = cubeByValue( number ); + + cout << "\nThe new value of number is " << number << endl; + + return 0; // indicates successful termination + +} // end main + +// calculate and return cube of integer argument +int cubeByValue( int n ) +{ + return n * n * n; // cube local variable n and return result + +} // end function cubeByValue + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_07.cpp b/Bachelor/Prog1/examples/ch05/Fig05_07.cpp new file mode 100644 index 0000000..b4da523 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_07.cpp @@ -0,0 +1,46 @@ +// Fig. 5.7: fig05_07.cpp +// Cube a variable using pass-by-reference +// with a pointer argument. +#include + +using std::cout; +using std::endl; + +void cubeByReference( int * ); // prototype + +int main() +{ + int number = 5; + + cout << "The original value of number is " << number; + + // pass address of number to cubeByReference + cubeByReference( &number ); + + cout << "\nThe new value of number is " << number << endl; + + return 0; // indicates successful termination + +} // end main + +// calculate cube of *nPtr; modifies variable number in main +void cubeByReference( int *nPtr ) +{ + *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr + +} // end function cubeByReference + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_10.cpp b/Bachelor/Prog1/examples/ch05/Fig05_10.cpp new file mode 100644 index 0000000..cfd0fce --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_10.cpp @@ -0,0 +1,53 @@ +// Fig. 5.10: fig05_10.cpp +// Converting lowercase letters to uppercase letters +// using a non-constant pointer to non-constant data. +#include + +using std::cout; +using std::endl; + +#include // prototypes for islower and toupper + +void convertToUppercase( char * ); + +int main() +{ + char phrase[] = "characters and $32.98"; + + cout << "The phrase before conversion is: " << phrase; + convertToUppercase( phrase ); + cout << "\nThe phrase after conversion is: " + << phrase << endl; + + return 0; // indicates successful termination + +} // end main + +// convert string to uppercase letters +void convertToUppercase( char *sPtr ) +{ + while ( *sPtr != '\0' ) { // current character is not '\0' + + if ( islower( *sPtr ) ) // if character is lowercase, + *sPtr = toupper( *sPtr ); // convert to uppercase + + ++sPtr; // move sPtr to next character in string + + } // end while + +} // end function convertToUppercase + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_11.cpp b/Bachelor/Prog1/examples/ch05/Fig05_11.cpp new file mode 100644 index 0000000..6d92cef --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_11.cpp @@ -0,0 +1,45 @@ +// Fig. 5.11: fig05_11.cpp +// Printing a string one character at a time using +// a non-constant pointer to constant data. +#include + +using std::cout; +using std::endl; + +void printCharacters( const char * ); + +int main() +{ + char phrase[] = "print characters of a string"; + + cout << "The string is:\n"; + printCharacters( phrase ); + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// sPtr cannot modify the character to which it points, +// i.e., sPtr is a "read-only" pointer +void printCharacters( const char *sPtr ) +{ + for ( ; *sPtr != '\0'; sPtr++ ) // no initialization + cout << *sPtr; + +} // end function printCharacters + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_12.cpp b/Bachelor/Prog1/examples/ch05/Fig05_12.cpp new file mode 100644 index 0000000..c9f089a --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_12.cpp @@ -0,0 +1,38 @@ +// Fig. 5.12: fig05_12.cpp +// Attempting to modify data through a +// non-constant pointer to constant data. + +void f( const int * ); // prototype + +int main() +{ + int y; + + f( &y ); // f attempts illegal modification + + return 0; // indicates successful termination + +} // end main + +// xPtr cannot modify the value of the variable +// to which it points +void f( const int *xPtr ) +{ + *xPtr = 100; // error: cannot modify a const object + +} // end function f + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_13.cpp b/Bachelor/Prog1/examples/ch05/Fig05_13.cpp new file mode 100644 index 0000000..ac697ba --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_13.cpp @@ -0,0 +1,34 @@ +// Fig. 5.13: fig05_13.cpp +// Attempting to modify a constant pointer to +// non-constant data. + +int main() +{ + int x, y; + + // ptr is a constant pointer to an integer that can + // be modified through ptr, but ptr always points to the + // same memory location. + int * const ptr = &x; + + *ptr = 7; // allowed: *ptr is not const + ptr = &y; // error: ptr is const; cannot assign new address + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_14.cpp b/Bachelor/Prog1/examples/ch05/Fig05_14.cpp new file mode 100644 index 0000000..8193ad1 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_14.cpp @@ -0,0 +1,39 @@ +// Fig. 5.14: fig05_14.cpp +// Attempting to modify a constant pointer to constant data. +#include + +using std::cout; +using std::endl; + +int main() +{ + int x = 5, y; + + // ptr is a constant pointer to a constant integer. + // ptr always points to the same location; the integer + // at that location cannot be modified. + const int *const ptr = &x; + + cout << *ptr << endl; + + *ptr = 7; // error: *ptr is const; cannot assign new value + ptr = &y; // error: ptr is const; cannot assign new address + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_15.cpp b/Bachelor/Prog1/examples/ch05/Fig05_15.cpp new file mode 100644 index 0000000..bcdd13a --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_15.cpp @@ -0,0 +1,77 @@ +// Fig. 5.15: fig05_15.cpp +// This program puts values into an array, sorts the values into +// ascending order, and prints the resulting array. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +void bubbleSort( int *, const int ); // prototype +void swap( int * const, int * const ); // prototype + +int main() +{ + const int arraySize = 10; + int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; + + cout << "Data items in original order\n"; + + for ( int i = 0; i < arraySize; i++ ) + cout << setw( 4 ) << a[ i ]; + + bubbleSort( a, arraySize ); // sort the array + + cout << "\nData items in ascending order\n"; + + for ( int j = 0; j < arraySize; j++ ) + cout << setw( 4 ) << a[ j ]; + + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// sort an array of integers using bubble sort algorithm +void bubbleSort( int *array, const int size ) +{ + // loop to control passes + for ( int pass = 0; pass < size - 1; pass++ ) + + // loop to control comparisons during each pass + for ( int k = 0; k < size - 1; k++ ) + + // swap adjacent elements if they are out of order + if ( array[ k ] > array[ k + 1 ] ) + swap( &array[ k ], &array[ k + 1 ] ); + +} // end function bubbleSort + +// swap values at memory locations to which +// element1Ptr and element2Ptr point +void swap( int * const element1Ptr, int * const element2Ptr ) +{ + int hold = *element1Ptr; + *element1Ptr = *element2Ptr; + *element2Ptr = hold; + +} // end function swap + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_16.cpp b/Bachelor/Prog1/examples/ch05/Fig05_16.cpp new file mode 100644 index 0000000..921e8dd --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_16.cpp @@ -0,0 +1,45 @@ +// Fig. 5.16: fig05_16.cpp +// Sizeof operator when used on an array name +// returns the number of bytes in the array. +#include + +using std::cout; +using std::endl; + +size_t getSize( double * ); // prototype + +int main() +{ + double array[ 20 ]; + + cout << "The number of bytes in the array is " + << sizeof( array ); + + cout << "\nThe number of bytes returned by getSize is " + << getSize( array ) << endl; + + return 0; // indicates successful termination + +} // end main + +// return size of ptr +size_t getSize( double *ptr ) +{ + return sizeof( ptr ); + +} // end function getSize + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_17.cpp b/Bachelor/Prog1/examples/ch05/Fig05_17.cpp new file mode 100644 index 0000000..7091880 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_17.cpp @@ -0,0 +1,55 @@ +// Fig. 5.17: fig05_17.cpp +// Demonstrating the sizeof operator. +#include + +using std::cout; +using std::endl; + +int main() +{ + char c; + short s; + int i; + long l; + float f; + double d; + long double ld; + int array[ 20 ]; + int *ptr = array; + + cout << "sizeof c = " << sizeof c + << "\tsizeof(char) = " << sizeof( char ) + << "\nsizeof s = " << sizeof s + << "\tsizeof(short) = " << sizeof( short ) + << "\nsizeof i = " << sizeof i + << "\tsizeof(int) = " << sizeof( int ) + << "\nsizeof l = " << sizeof l + << "\tsizeof(long) = " << sizeof( long ) + << "\nsizeof f = " << sizeof f + << "\tsizeof(float) = " << sizeof( float ) + << "\nsizeof d = " << sizeof d + << "\tsizeof(double) = " << sizeof( double ) + << "\nsizeof ld = " << sizeof ld + << "\tsizeof(long double) = " << sizeof( long double ) + << "\nsizeof array = " << sizeof array + << "\nsizeof ptr = " << sizeof ptr + << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_20.cpp b/Bachelor/Prog1/examples/ch05/Fig05_20.cpp new file mode 100644 index 0000000..63bc2f7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_20.cpp @@ -0,0 +1,60 @@ +// Fig. 5.20: fig05_20.cpp +// Using subscripting and pointer notations with arrays. + +#include + +using std::cout; +using std::endl; + +int main() +{ + int b[] = { 10, 20, 30, 40 }; + int *bPtr = b; // set bPtr to point to array b + + // output array b using array subscript notation + cout << "Array b printed with:\n\n" + << "Array subscript notation\n"; + + for ( int i = 0; i < 4; i++ ) + cout << "b[" << i << "] = " << b[ i ] << '\n'; + + // output array b using the array name and + // pointer/offset notation + cout << "\nPointer/offset notation where " + << "the pointer is the array name\n"; + + for ( int offset1 = 0; offset1 < 4; offset1++ ) + cout << "*(b + " << offset1 << ") = " + << *( b + offset1 ) << '\n'; + + // output array b using bPtr and array subscript notation + cout << "\nPointer subscript notation\n"; + + for ( int j = 0; j < 4; j++ ) + cout << "bPtr[" << j << "] = " << bPtr[ j ] << '\n'; + + cout << "\nPointer/offset notation\n"; + + // output array b using bPtr and pointer/offset notation + for ( int offset2 = 0; offset2 < 4; offset2++ ) + cout << "*(bPtr + " << offset2 << ") = " + << *( bPtr + offset2 ) << '\n'; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_21.cpp b/Bachelor/Prog1/examples/ch05/Fig05_21.cpp new file mode 100644 index 0000000..e045aba --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_21.cpp @@ -0,0 +1,58 @@ +// Fig. 5.21: fig05_21.cpp +// Copying a string using array notation +// and pointer notation. +#include + +using std::cout; +using std::endl; + +void copy1( char *, const char * ); // prototype +void copy2( char *, const char * ); // prototype + +int main() +{ + char string1[ 10 ]; + char *string2 = "Hello"; + char string3[ 10 ]; + char string4[] = "Good Bye"; + + copy1( string1, string2 ); + cout << "string1 = " << string1 << endl; + + copy2( string3, string4 ); + cout << "string3 = " << string3 << endl; + + return 0; // indicates successful termination + +} // end main + +// copy s2 to s1 using array notation +void copy1( char *s1, const char *s2 ) +{ + for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) + ; // do nothing in body + +} // end function copy1 + +// copy s2 to s1 using pointer notation +void copy2( char *s1, const char *s2 ) +{ + for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) + ; // do nothing in body + +} // end function copy2 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_24.cpp b/Bachelor/Prog1/examples/ch05/Fig05_24.cpp new file mode 100644 index 0000000..746fd13 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_24.cpp @@ -0,0 +1,103 @@ +// Fig. 5.24: fig05_24.cpp +// Card shuffling dealing program. +#include + +using std::cout; +using std::left; +using std::right; + +#include + +using std::setw; + +#include // prototypes for rand and srand +#include // prototype for time + +// prototypes +void shuffle( int [][ 13 ] ); +void deal( const int [][ 13 ], const char *[], const char *[] ); + +int main() +{ + // initialize suit array + const char *suit[ 4 ] = + { "Hearts", "Diamonds", "Clubs", "Spades" }; + + // initialize face array + const char *face[ 13 ] = + { "Ace", "Deuce", "Three", "Four", + "Five", "Six", "Seven", "Eight", + "Nine", "Ten", "Jack", "Queen", "King" }; + + // initialize deck array + int deck[ 4 ][ 13 ] = { 0 }; + + srand( time( 0 ) ); // seed random number generator + + shuffle( deck ); + deal( deck, face, suit ); + + return 0; // indicates successful termination + +} // end main + +// shuffle cards in deck +void shuffle( int wDeck[][ 13 ] ) +{ + int row; + int column; + + // for each of the 52 cards, choose slot of deck randomly + for ( int card = 1; card <= 52; card++ ) { + + // choose new random location until unoccupied slot found + do { + row = rand() % 4; + column = rand() % 13; + } while( wDeck[ row ][ column ] != 0 ); // end do/while + + // place card number in chosen slot of deck + wDeck[ row ][ column ] = card; + + } // end for + +} // end function shuffle + +// deal cards in deck +void deal( const int wDeck[][ 13 ], const char *wFace[], + const char *wSuit[] ) +{ + // for each of the 52 cards + for ( int card = 1; card <= 52; card++ ) + + // loop through rows of wDeck + for ( int row = 0; row <= 3; row++ ) + + // loop through columns of wDeck for current row + for ( int column = 0; column <= 12; column++ ) + + // if slot contains current card, display card + if ( wDeck[ row ][ column ] == card ) { + cout << setw( 5 ) << right << wFace[ column ] + << " of " << setw( 8 ) << left + << wSuit[ row ] + << ( card % 2 == 0 ? '\n' : '\t' ); + + } // end if + +} // end function deal + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_25.cpp b/Bachelor/Prog1/examples/ch05/Fig05_25.cpp new file mode 100644 index 0000000..8073108 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_25.cpp @@ -0,0 +1,115 @@ +// Fig. 5.25: fig05_25.cpp +// Multipurpose sorting program using function pointers. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +// prototypes +void bubble( int [], const int, bool (*)( int, int ) ); +void swap( int * const, int * const ); +bool ascending( int, int ); +bool descending( int, int ); + +int main() +{ + const int arraySize = 10; + int order; + int counter; + int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; + + cout << "Enter 1 to sort in ascending order,\n" + << "Enter 2 to sort in descending order: "; + cin >> order; + cout << "\nData items in original order\n"; + + // output original array + for ( counter = 0; counter < arraySize; counter++ ) + cout << setw( 4 ) << a[ counter ]; + + // sort array in ascending order; pass function ascending + // as an argument to specify ascending sorting order + if ( order == 1 ) { + bubble( a, arraySize, ascending ); + cout << "\nData items in ascending order\n"; + } + + // sort array in descending order; pass function descending + // as an argument to specify descending sorting order + else { + bubble( a, arraySize, descending ); + cout << "\nData items in descending order\n"; + } + + // output sorted array + for ( counter = 0; counter < arraySize; counter++ ) + cout << setw( 4 ) << a[ counter ]; + + cout << endl; + + return 0; // indicates successful termination + +} // end main + +// multipurpose bubble sort; parameter compare is a pointer to +// the comparison function that determines sorting order +void bubble( int work[], const int size, + bool (*compare)( int, int ) ) +{ + // loop to control passes + for ( int pass = 1; pass < size; pass++ ) + + // loop to control number of comparisons per pass + for ( int count = 0; count < size - 1; count++ ) + + // if adjacent elements are out of order, swap them + if ( (*compare)( work[ count ], work[ count + 1 ] ) ) + swap( &work[ count ], &work[ count + 1 ] ); + +} // end function bubble + +// swap values at memory locations to which +// element1Ptr and element2Ptr point +void swap( int * const element1Ptr, int * const element2Ptr ) +{ + int hold = *element1Ptr; + *element1Ptr = *element2Ptr; + *element2Ptr = hold; + +} // end function swap + +// determine whether elements are out of order +// for an ascending order sort +bool ascending( int a, int b ) +{ + return b < a; // swap if b is less than a + +} // end function ascending + +// determine whether elements are out of order +// for a descending order sort +bool descending( int a, int b ) +{ + return b > a; // swap if b is greater than a + +} // end function descending + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_26.cpp b/Bachelor/Prog1/examples/ch05/Fig05_26.cpp new file mode 100644 index 0000000..916dbb9 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_26.cpp @@ -0,0 +1,76 @@ +// Fig. 5.26: fig05_26.cpp +// Demonstrating an array of pointers to functions. +#include + +using std::cout; +using std::cin; +using std::endl; + +// function prototypes +void function1( int ); +void function2( int ); +void function3( int ); + +int main() +{ + // initialize array of 3 pointers to functions that each + // take an int argument and return void + void (*f[ 3 ])( int ) = { function1, function2, function3 }; + + int choice; + + cout << "Enter a number between 0 and 2, 3 to end: "; + cin >> choice; + + // process user's choice + while ( choice >= 0 && choice < 3 ) { + + // invoke function at location choice in array f + // and pass choice as an argument + (*f[ choice ])( choice ); + + cout << "Enter a number between 0 and 2, 3 to end: "; + cin >> choice; + } + + cout << "Program execution completed." << endl; + + return 0; // indicates successful termination + +} // end main + +void function1( int a ) +{ + cout << "You entered " << a + << " so function1 was called\n\n"; + +} // end function1 + +void function2( int b ) +{ + cout << "You entered " << b + << " so function2 was called\n\n"; + +} // end function2 + +void function3( int c ) +{ + cout << "You entered " << c + << " so function3 was called\n\n"; + +} // end function3 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_28.cpp b/Bachelor/Prog1/examples/ch05/Fig05_28.cpp new file mode 100644 index 0000000..d973c37 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_28.cpp @@ -0,0 +1,44 @@ +// Fig. 5.28: fig05_28.cpp +// Using strcpy and strncpy. +#include + +using std::cout; +using std::endl; + +#include // prototypes for strcpy and strncpy + +int main() +{ + char x[] = "Happy Birthday to You"; + char y[ 25 ]; + char z[ 15 ]; + + strcpy( y, x ); // copy contents of x into y + + cout << "The string in array x is: " << x + << "\nThe string in array y is: " << y << '\n'; + + // copy first 14 characters of x into z + strncpy( z, x, 14 ); // does not copy null character + z[ 14 ] = '\0'; // append '\0' to z's contents + + cout << "The string in array z is: " << z << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_29.cpp b/Bachelor/Prog1/examples/ch05/Fig05_29.cpp new file mode 100644 index 0000000..daa58f6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_29.cpp @@ -0,0 +1,50 @@ +// Fig. 5.29: fig05_29.cpp +// Using strcat and strncat. +#include + +using std::cout; +using std::endl; + +#include // prototypes for strcat and strncat + +int main() +{ + char s1[ 20 ] = "Happy "; + char s2[] = "New Year "; + char s3[ 40 ] = ""; + + cout << "s1 = " << s1 << "\ns2 = " << s2; + + strcat( s1, s2 ); // concatenate s2 to s1 + + cout << "\n\nAfter strcat(s1, s2):\ns1 = " << s1 + << "\ns2 = " << s2; + + // concatenate first 6 characters of s1 to s3 + strncat( s3, s1, 6 ); // places '\0' after last character + + cout << "\n\nAfter strncat(s3, s1, 6):\ns1 = " << s1 + << "\ns3 = " << s3; + + strcat( s3, s1 ); // concatenate s1 to s3 + cout << "\n\nAfter strcat(s3, s1):\ns1 = " << s1 + << "\ns3 = " << s3 << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_30.cpp b/Bachelor/Prog1/examples/ch05/Fig05_30.cpp new file mode 100644 index 0000000..1f55555 --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_30.cpp @@ -0,0 +1,50 @@ +// Fig. 5.30: fig05_30.cpp +// Using strcmp and strncmp. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // prototypes for strcmp and strncmp + +int main() +{ + char *s1 = "Happy New Year"; + char *s2 = "Happy New Year"; + char *s3 = "Happy Holidays"; + + cout << "s1 = " << s1 << "\ns2 = " << s2 + << "\ns3 = " << s3 << "\n\nstrcmp(s1, s2) = " + << setw( 2 ) << strcmp( s1, s2 ) + << "\nstrcmp(s1, s3) = " << setw( 2 ) + << strcmp( s1, s3 ) << "\nstrcmp(s3, s1) = " + << setw( 2 ) << strcmp( s3, s1 ); + + cout << "\n\nstrncmp(s1, s3, 6) = " << setw( 2 ) + << strncmp( s1, s3, 6 ) << "\nstrncmp(s1, s3, 7) = " + << setw( 2 ) << strncmp( s1, s3, 7 ) + << "\nstrncmp(s3, s1, 7) = " + << setw( 2 ) << strncmp( s3, s1, 7 ) << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_31.cpp b/Bachelor/Prog1/examples/ch05/Fig05_31.cpp new file mode 100644 index 0000000..280bddc --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_31.cpp @@ -0,0 +1,47 @@ +// Fig. 5.31: fig05_31.cpp +// Using strtok. +#include + +using std::cout; +using std::endl; + +#include // prototype for strtok + +int main() +{ + char sentence[] = "This is a sentence with 7 tokens"; + char *tokenPtr; + + cout << "The string to be tokenized is:\n" << sentence + << "\n\nThe tokens are:\n\n"; + + // begin tokenization of sentence + tokenPtr = strtok( sentence, " " ); + + // continue tokenizing sentence until tokenPtr becomes NULL + while ( tokenPtr != NULL ) { + cout << tokenPtr << '\n'; + tokenPtr = strtok( NULL, " " ); // get next token + + } // end while + + cout << "\nAfter strtok, sentence = " << sentence << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch05/Fig05_32.cpp b/Bachelor/Prog1/examples/ch05/Fig05_32.cpp new file mode 100644 index 0000000..ae612dd --- /dev/null +++ b/Bachelor/Prog1/examples/ch05/Fig05_32.cpp @@ -0,0 +1,40 @@ +// Fig. 5.32: fig05_32.cpp +// Using strlen. +#include + +using std::cout; +using std::endl; + +#include // prototype for strlen + +int main() +{ + char *string1 = "abcdefghijklmnopqrstuvwxyz"; + char *string2 = "four"; + char *string3 = "Boston"; + + cout << "The length of \"" << string1 + << "\" is " << strlen( string1 ) + << "\nThe length of \"" << string2 + << "\" is " << strlen( string2 ) + << "\nThe length of \"" << string3 + << "\" is " << strlen( string3 ) << endl; + + return 0; // indicates successful termination + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp b/Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp new file mode 100644 index 0000000..0126a02 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp @@ -0,0 +1,82 @@ +// Fig. 6.1: fig06_01.cpp +// Create a structure, set its members, and print it. +#include + +using std::cout; +using std::endl; + +#include + +using std::setfill; +using std::setw; + +// structure definition +struct Time { + int hour; // 0-23 (24-hour clock format) + int minute; // 0-59 + int second; // 0-59 + +}; // end struct Time + +void printUniversal( const Time & ); // prototype +void printStandard( const Time & ); // prototype + +int main() +{ + Time dinnerTime; // variable of new type Time + + dinnerTime.hour = 18; // set hour member of dinnerTime + dinnerTime.minute = 30; // set minute member of dinnerTime + dinnerTime.second = 0; // set second member of dinnerTime + + cout << "Dinner will be held at "; + printUniversal( dinnerTime ); + cout << " universal time,\nwhich is "; + printStandard( dinnerTime ); + cout << " standard time.\n"; + + dinnerTime.hour = 29; // set hour to invalid value + dinnerTime.minute = 73; // set minute to invalid value + + cout << "\nTime with invalid values: "; + printUniversal( dinnerTime ); + cout << endl; + + return 0; + +} // end main + +// print time in universal-time format +void printUniversal( const Time &t ) +{ + cout << setfill( '0' ) << setw( 2 ) << t.hour << ":" + << setw( 2 ) << t.minute << ":" + << setw( 2 ) << t.second; + +} // end function printUniversal + +// print time in standard-time format +void printStandard( const Time &t ) +{ + cout << ( ( t.hour == 0 || t.hour == 12 ) ? + 12 : t.hour % 12 ) << ":" << setfill( '0' ) + << setw( 2 ) << t.minute << ":" + << setw( 2 ) << t.second + << ( t.hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp b/Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp new file mode 100644 index 0000000..3bb7a28 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp @@ -0,0 +1,114 @@ +// Fig. 6.3: fig06_03.cpp +// Time class. +#include + +using std::cout; +using std::endl; + +#include + +using std::setfill; +using std::setw; + +// Time abstract data type (ADT) definition +class Time { + +public: + Time(); // constructor + void setTime( int, int, int ); // set hour, minute, second + void printUniversal(); // print universal-time format + void printStandard(); // print standard-time format + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +// Time constructor initializes each data member to zero and +// ensures all Time objects start in a consistent state +Time::Time() +{ + hour = minute = second = 0; + +} // end constructor Time + +// set new Time value using universal time, perform validity +// checks on the data values and set invalid values to zero +void Time::setTime( int h, int m, int s ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + minute = ( m >= 0 && m < 60 ) ? m : 0; + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setTime + +// print Time in universal format +void Time::printUniversal() +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +int main() +{ + Time t; // instantiate object t of class Time + + // output Time object t's initial values + cout << "The initial universal time is "; + t.printUniversal(); // 00:00:00 + + cout << "\nThe initial standard time is "; + t.printStandard(); // 12:00:00 AM + + t.setTime( 13, 27, 6 ); // change time + + // output Time object t's new values + cout << "\n\nUniversal time after setTime is "; + t.printUniversal(); // 13:27:06 + + cout << "\nStandard time after setTime is "; + t.printStandard(); // 1:27:06 PM + + t.setTime( 99, 99, 99 ); // attempt invalid settings + + // output t's values after specifying invalid values + cout << "\n\nAfter attempting invalid settings:" + << "\nUniversal time: "; + t.printUniversal(); // 00:00:00 + + cout << "\nStandard time: "; + t.printStandard(); // 12:00:00 AM + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp b/Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp new file mode 100644 index 0000000..68c1be3 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp @@ -0,0 +1,58 @@ +// Fig. 6.4: fig06_04.cpp +// Demonstrating the class member access operators . and -> +// +// CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA! +#include + +using std::cout; +using std::endl; + +// class Count definition +class Count { + +public: + int x; + + void print() + { + cout << x << endl; + } + +}; // end class Count + +int main() +{ + Count counter; // create counter object + Count *counterPtr = &counter; // create pointer to counter + Count &counterRef = counter; // create reference to counter + + cout << "Assign 1 to x and print using the object's name: "; + counter.x = 1; // assign 1 to data member x + counter.print(); // call member function print + + cout << "Assign 2 to x and print using a reference: "; + counterRef.x = 2; // assign 2 to data member x + counterRef.print(); // call member function print + + cout << "Assign 3 to x and print using a pointer: "; + counterPtr->x = 3; // assign 3 to data member x + counterPtr->print(); // call member function print + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp new file mode 100644 index 0000000..85cca47 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp @@ -0,0 +1,57 @@ +// Fig. 6.7: fig06_07.cpp +// Program to test class Time. +// NOTE: This file must be compiled with time1.cpp. +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time1.h +#include "time1.h" + +int main() +{ + Time t; // instantiate object t of class Time + + // output Time object t's initial values + cout << "The initial universal time is "; + t.printUniversal(); // 00:00:00 + cout << "\nThe initial standard time is "; + t.printStandard(); // 12:00:00 AM + + t.setTime( 13, 27, 6 ); // change time + + // output Time object t's new values + cout << "\n\nUniversal time after setTime is "; + t.printUniversal(); // 13:27:06 + cout << "\nStandard time after setTime is "; + t.printStandard(); // 1:27:06 PM + + t.setTime( 99, 99, 99 ); // attempt invalid settings + + // output t's values after specifying invalid values + cout << "\n\nAfter attempting invalid settings:" + << "\nUniversal time: "; + t.printUniversal(); // 00:00:00 + cout << "\nStandard time: "; + t.printStandard(); // 12:00:00 AM + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp new file mode 100644 index 0000000..6ffea1c --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp @@ -0,0 +1,65 @@ +// Fig. 6.6: time1.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time1.h +#include "time1.h" + +// Time constructor initializes each data member to zero. +// Ensures all Time objects start in a consistent state. +Time::Time() +{ + hour = minute = second = 0; + +} // end Time constructor + +// Set new Time value using universal time. Perform validity +// checks on the data values. Set invalid values to zero. +void Time::setTime( int h, int m, int s ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + minute = ( m >= 0 && m < 60 ) ? m : 0; + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setTime + +// print Time in universal format +void Time::printUniversal() +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h new file mode 100644 index 0000000..9adaba7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h @@ -0,0 +1,40 @@ +// Fig. 6.5: time1.h +// Declaration of class Time. +// Member functions are defined in time1.cpp + +// prevent multiple inclusions of header file +#ifndef TIME1_H +#define TIME1_H + +// Time abstract data type definition +class Time { + +public: + Time(); // constructor + void setTime( int, int, int ); // set hour, minute, second + void printUniversal(); // print universal-time format + void printStandard(); // print standard-time format + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp b/Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp new file mode 100644 index 0000000..93cacc4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp @@ -0,0 +1,38 @@ +// Fig. 6.8: fig06_08.cpp +// Demonstrate errors resulting from attempts +// to access private class members. +#include + +using std::cout; + +// include definition of class Time from time1.h +#include "time1.h" + +int main() +{ + Time t; // create Time object + + + t.hour = 7; // error: 'Time::hour' is not accessible + + // error: 'Time::minute' is not accessible + cout << "minute = " << t.minute; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp new file mode 100644 index 0000000..0f53ccf --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp @@ -0,0 +1,65 @@ +// Fig. 6.6: time1.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time1.h +#include "time1.h" + +// Time constructor initializes each data member to zero. +// Ensures all Time objects start in a consistent state. +Time::Time() +{ + hour = minute = second = 0; + +} // end Time constructor + +// Set new Time value using universal time. Perform validity +// checks on the data values. Set invalid values to zero. +void Time::setTime( int h, int m, int s ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + minute = ( m >= 0 && m < 60 ) ? m : 0; + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setTime + +// print Time in universal format +void Time::printUniversal() +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h new file mode 100644 index 0000000..9adaba7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h @@ -0,0 +1,40 @@ +// Fig. 6.5: time1.h +// Declaration of class Time. +// Member functions are defined in time1.cpp + +// prevent multiple inclusions of header file +#ifndef TIME1_H +#define TIME1_H + +// Time abstract data type definition +class Time { + +public: + Time(); // constructor + void setTime( int, int, int ); // set hour, minute, second + void printUniversal(); // print universal-time format + void printStandard(); // print standard-time format + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp new file mode 100644 index 0000000..82787f6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp @@ -0,0 +1,32 @@ +// Fig. 6.11: fig06_11.cpp +// Demonstrating a utility function. +// Compile this program with salesp.cpp + +// include SalesPerson class definition from salesp.h +#include "salesp.h" + +int main() +{ + SalesPerson s; // create SalesPerson object s + + s.getSalesFromUser(); // note simple sequential code; no + s.printAnnualSales(); // control structures in main + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp new file mode 100644 index 0000000..4c05a6d --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp @@ -0,0 +1,86 @@ +// Fig. 6.10: salesp.cpp +// Member functions for class SalesPerson. +#include + +using std::cout; +using std::cin; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +// include SalesPerson class definition from salesp.h +#include "salesp.h" + +// initialize elements of array sales to 0.0 +SalesPerson::SalesPerson() +{ + for ( int i = 0; i < 12; i++ ) + sales[ i ] = 0.0; + +} // end SalesPerson constructor + +// get 12 sales figures from the user at the keyboard +void SalesPerson::getSalesFromUser() +{ + double salesFigure; + + for ( int i = 1; i <= 12; i++ ) { + cout << "Enter sales amount for month " << i << ": "; + cin >> salesFigure; + setSales( i, salesFigure ); + + } // end for + +} // end function getSalesFromUser + +// set one of the 12 monthly sales figures; function subtracts +// one from month value for proper subscript in sales array +void SalesPerson::setSales( int month, double amount ) +{ + // test for valid month and amount values + if ( month >= 1 && month <= 12 && amount > 0 ) + sales[ month - 1 ] = amount; // adjust for subscripts 0-11 + + else // invalid month or amount value + cout << "Invalid month or sales figure" << endl; + +} // end function setSales + +// print total annual sales (with the help of utility function) +void SalesPerson::printAnnualSales() +{ + cout << setprecision( 2 ) << fixed + << "\nThe total annual sales are: $" + << totalAnnualSales() << endl; // call utility function + +} // end function printAnnualSales + +// private utility function to total annual sales +double SalesPerson::totalAnnualSales() +{ + double total = 0.0; // initialize total + + for ( int i = 0; i < 12; i++ ) // summarize sales results + total += sales[ i ]; + + return total; + +} // end function totalAnnualSales + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h new file mode 100644 index 0000000..240e3e7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h @@ -0,0 +1,36 @@ +// Fig. 6.9: salesp.h +// SalesPerson class definition. +// Member functions defined in salesp.cpp. +#ifndef SALESP_H +#define SALESP_H + +class SalesPerson { + +public: + SalesPerson(); // constructor + void getSalesFromUser(); // input sales from keyboard + void setSales( int, double ); // set sales for a month + void printAnnualSales(); // summarize and print sales + +private: + double totalAnnualSales(); // utility function + double sales[ 12 ]; // 12 monthly sales figures + +}; // end class SalesPerson + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp new file mode 100644 index 0000000..8a7546a --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp @@ -0,0 +1,63 @@ +// Fig. 6.14: fig06_14.cpp +// Demonstrating a default constructor for class Time. +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time2.h +#include "time2.h" + +int main() +{ + Time t1; // all arguments defaulted + Time t2( 2 ); // minute and second defaulted + Time t3( 21, 34 ); // second defaulted + Time t4( 12, 25, 42 ); // all values specified + Time t5( 27, 74, 99 ); // all bad values specified + + cout << "Constructed with:\n\n" + << "all default arguments:\n "; + t1.printUniversal(); // 00:00:00 + cout << "\n "; + t1.printStandard(); // 12:00:00 AM + + cout << "\n\nhour specified; default minute and second:\n "; + t2.printUniversal(); // 02:00:00 + cout << "\n "; + t2.printStandard(); // 2:00:00 AM + + cout << "\n\nhour and minute specified; default second:\n "; + t3.printUniversal(); // 21:34:00 + cout << "\n "; + t3.printStandard(); // 9:34:00 PM + + cout << "\n\nhour, minute, and second specified:\n "; + t4.printUniversal(); // 12:25:42 + cout << "\n "; + t4.printStandard(); // 12:25:42 PM + + cout << "\n\nall invalid values specified:\n "; + t5.printUniversal(); // 00:00:00 + cout << "\n "; + t5.printStandard(); // 12:00:00 AM + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp new file mode 100644 index 0000000..5a5d42c --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp @@ -0,0 +1,65 @@ +// Fig. 6.13: time2.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time2.h +#include "time2.h" + +// Time constructor initializes each data member to zero; +// ensures all Time objects start in a consistent state +Time::Time( int hr, int min, int sec ) +{ + setTime( hr, min, sec ); // validate and set time + +} // end Time constructor + +// set new Time value using universal time, perform validity +// checks on the data values and set invalid values to zero +void Time::setTime( int h, int m, int s ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + minute = ( m >= 0 && m < 60 ) ? m : 0; + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setTime + +// print Time in universal format +void Time::printUniversal() +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h new file mode 100644 index 0000000..3b99b88 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h @@ -0,0 +1,40 @@ +// Fig. 6.12: time2.h +// Declaration of class Time. +// Member functions defined in time2.cpp. + +// prevent multiple inclusions of header file +#ifndef TIME2_H +#define TIME2_H + +// Time abstract data type definition +class Time { + +public: + Time( int = 0, int = 0, int = 0); // default constructor + void setTime( int, int, int ); // set hour, minute, second + void printUniversal(); // print universal-time format + void printStandard(); // print standard-time format + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp new file mode 100644 index 0000000..1f7614f --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp @@ -0,0 +1,47 @@ +// Fig. 6.16: create.cpp +// Member-function definitions for class CreateAndDestroy +#include + +using std::cout; +using std::endl; + +// include CreateAndDestroy class definition from create.h +#include "create.h" + +// constructor +CreateAndDestroy::CreateAndDestroy( + int objectNumber, char *messagePtr ) +{ + objectID = objectNumber; + message = messagePtr; + + cout << "Object " << objectID << " constructor runs " + << message << endl; + +} // end CreateAndDestroy constructor + +// destructor +CreateAndDestroy::~CreateAndDestroy() +{ + // the following line is for pedagogic purposes only + cout << ( objectID == 1 || objectID == 6 ? "\n" : "" ); + + cout << "Object " << objectID << " destructor runs " + << message << endl; + +} // end ~CreateAndDestroy destructor + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h new file mode 100644 index 0000000..f0a73cb --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h @@ -0,0 +1,34 @@ +// Fig. 6.15: create.h +// Definition of class CreateAndDestroy. +// Member functions defined in create.cpp. +#ifndef CREATE_H +#define CREATE_H + +class CreateAndDestroy { + +public: + CreateAndDestroy( int, char * ); // constructor + ~CreateAndDestroy(); // destructor + +private: + int objectID; + char *message; + +}; // end class CreateAndDestroy + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp new file mode 100644 index 0000000..3bd2906 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp @@ -0,0 +1,68 @@ +// Fig. 6.17: fig06_17.cpp +// Demonstrating the order in which constructors and +// destructors are called. +#include + +using std::cout; +using std::endl; + +// include CreateAndDestroy class definition from create.h +#include "create.h" + +void create( void ); // prototype + +// global object +CreateAndDestroy first( 1, "(global before main)" ); + +int main() +{ + cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl; + + CreateAndDestroy second( 2, "(local automatic in main)" ); + + static CreateAndDestroy third( + 3, "(local static in main)" ); + + create(); // call function to create objects + + cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl; + + CreateAndDestroy fourth( 4, "(local automatic in main)" ); + + cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl; + + return 0; + +} // end main + +// function to create objects +void create( void ) +{ + cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl; + + CreateAndDestroy fifth( 5, "(local automatic in create)" ); + + static CreateAndDestroy sixth( + 6, "(local static in create)" ); + + CreateAndDestroy seventh( + 7, "(local automatic in create)" ); + + cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl; + +} // end function create + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp new file mode 100644 index 0000000..58f5bb9 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp @@ -0,0 +1,82 @@ +// Fig. 6.20: fig06_20.cpp +// Demonstrating the Time class set and get functions +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time3.h +#include "time3.h" + +void incrementMinutes( Time &, const int ); // prototype + +int main() +{ + Time t; // create Time object + + // set time using individual set functions + t.setHour( 17 ); // set hour to valid value + t.setMinute( 34 ); // set minute to valid value + t.setSecond( 25 ); // set second to valid value + + // use get functions to obtain hour, miunute and second + cout << "Result of setting all valid values:\n" + << " Hour: " << t.getHour() + << " Minute: " << t.getMinute() + << " Second: " << t.getSecond(); + + // set time using individual set functions + t.setHour( 234 ); // invalid hour set to 0 + t.setMinute( 43 ); // set minute to valid value + t.setSecond( 6373 ); // invalid second set to 0 + + // display hour, minute and second after setting + // invalid hour and second values + cout << "\n\nResult of attempting to set invalid hour and" + << " second:\n Hour: " << t.getHour() + << " Minute: " << t.getMinute() + << " Second: " << t.getSecond() << "\n\n"; + + t.setTime( 11, 58, 0 ); // set time + incrementMinutes( t, 3 ); // increment t's minute by 3 + + return 0; + +} // end main + +// add specified number of minutes to a Time object +void incrementMinutes( Time &tt, const int count ) +{ + cout << "Incrementing minute " << count + << " times:\nStart time: "; + tt.printStandard(); + + for ( int i = 0; i < count; i++ ) { + tt.setMinute( ( tt.getMinute() + 1 ) % 60 ); + + if ( tt.getMinute() == 0 ) + tt.setHour( ( tt.getHour() + 1 ) % 24 ); + + cout << "\nminute + 1: "; + tt.printStandard(); + + } // end for + + cout << endl; + +} // end function incrementMinutes + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp new file mode 100644 index 0000000..7cf0176 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp @@ -0,0 +1,107 @@ +// Fig. 6.19: time3.cpp +// Member-function definitions for Time class. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time3.h +#include "time3.h" + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hr, int min, int sec ) +{ + setTime( hr, min, sec ); + +} // end Time constructor + +// set hour, minute and second values +void Time::setTime( int h, int m, int s ) +{ + setHour( h ); + setMinute( m ); + setSecond( s ); + +} // end function setTime + +// set hour value +void Time::setHour( int h ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + +} // end function setHour + +// set minute value +void Time::setMinute( int m ) +{ + minute = ( m >= 0 && m < 60 ) ? m : 0; + +} // end function setMinute + +// set second value +void Time::setSecond( int s ) +{ + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setSecond + +// return hour value +int Time::getHour() +{ + return hour; + +} // end function getHour + +// return minute value +int Time::getMinute() +{ + return minute; + +} // end function getMinute + +// return second value +int Time::getSecond() +{ + return second; + +} // end function getSecond + +// print Time in universal format +void Time::printUniversal() +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h new file mode 100644 index 0000000..2f68d31 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h @@ -0,0 +1,50 @@ +// Fig. 6.18: time3.h +// Declaration of class Time. +// Member functions defined in time3.cpp + +// prevent multiple inclusions of header file +#ifndef TIME3_H +#define TIME3_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); // default constructor + + // set functions + void setTime( int, int, int ); // set hour, minute, second + void setHour( int ); // set hour + void setMinute( int ); // set minute + void setSecond( int ); // set second + + // get functions + int getHour(); // return hour + int getMinute(); // return minute + int getSecond(); // return second + + void printUniversal(); // output universal-time format + void printStandard(); // output standard-time format + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end clas Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp new file mode 100644 index 0000000..a5b14b6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp @@ -0,0 +1,53 @@ +// Fig. 6.23: fig06_23.cpp +// Demonstrating a public member function that +// returns a reference to a private data member. +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time4.h +#include "time4.h" + +int main() +{ + Time t; + + // store in hourRef the reference returned by badSetHour + int &hourRef = t.badSetHour( 20 ); + + cout << "Hour before modification: " << hourRef; + + // use hourRef to set invalid value in Time object t + hourRef = 30; + + cout << "\nHour after modification: " << t.getHour(); + + // Dangerous: Function call that returns + // a reference can be used as an lvalue! + t.badSetHour( 12 ) = 74; + + cout << "\n\n*********************************\n" + << "POOR PROGRAMMING PRACTICE!!!!!!!!\n" + << "badSetHour as an lvalue, Hour: " + << t.getHour() + << "\n*********************************" << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp new file mode 100644 index 0000000..eef65f2 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp @@ -0,0 +1,55 @@ +// Fig. 6.22: time4.cpp +// Member-function definitions for Time class. + +// include definition of class Time from time4.h +#include "time4.h" + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hr, int min, int sec ) +{ + setTime( hr, min, sec ); + +} // end Time constructor + +// set values of hour, minute and second +void Time::setTime( int h, int m, int s ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + minute = ( m >= 0 && m < 60 ) ? m : 0; + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setTime + +// return hour value +int Time::getHour() +{ + return hour; + +} // end function getHour + +// POOR PROGRAMMING PRACTICE: +// Returning a reference to a private data member. +int &Time::badSetHour( int hh ) +{ + hour = ( hh >= 0 && hh < 24 ) ? hh : 0; + + return hour; // DANGEROUS reference return + +} // end function badSetHour + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h new file mode 100644 index 0000000..92a5d84 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h @@ -0,0 +1,40 @@ +// Fig. 6.21: time4.h +// Declaration of class Time. +// Member functions defined in time4.cpp + +// prevent multiple inclusions of header file +#ifndef TIME4_H +#define TIME4_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); + void setTime( int, int, int ); + int getHour(); + + int &badSetHour( int ); // DANGEROUS reference return + +private: + int hour; + int minute; + int second; + +}; // end class Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp b/Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp new file mode 100644 index 0000000..9df3198 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp @@ -0,0 +1,72 @@ +// Fig. 6.24: fig06_24.cpp +// Demonstrating that class objects can be assigned +// to each other using default memberwise assignment. +#include + +using std::cout; +using std::endl; + +// class Date definition +class Date { + +public: + Date( int = 1, int = 1, int = 1990 ); // default constructor + void print(); + +private: + int month; + int day; + int year; + +}; // end class Date + +// Date constructor with no range checking +Date::Date( int m, int d, int y ) +{ + month = m; + day = d; + year = y; + +} // end constructor Date + +// print Date in the format mm-dd-yyyy +void Date::print() +{ + cout << month << '-' << day << '-' << year; + +} // end function print + +int main() +{ + Date date1( 7, 4, 2002 ); + Date date2; // date2 defaults to 1/1/1990 + + cout << "date1 = "; + date1.print(); + cout << "\ndate2 = "; + date2.print(); + + date2 = date1; // default memberwise assignment + + cout << "\n\nAfter default memberwise assignment, date2 = "; + date2.print(); + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2002 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp new file mode 100644 index 0000000..e2f9ee2 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp @@ -0,0 +1,42 @@ +// Fig. 7.3: fig07_03.cpp +// Attempting to access a const object with +// non-const member functions. + +// include Time class definition from time5.h +#include "time5.h" + +int main() +{ + Time wakeUp( 6, 45, 0 ); // non-constant object + const Time noon( 12, 0, 0 ); // constant object + + // OBJECT MEMBER FUNCTION + wakeUp.setHour( 18 ); // non-const non-const + + noon.setHour( 12 ); // const non-const + + wakeUp.getHour(); // non-const const + + noon.getMinute(); // const const + noon.printUniversal(); // const const + + noon.printStandard(); // const non-const + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp new file mode 100644 index 0000000..a8e8a6b --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp @@ -0,0 +1,107 @@ +// Fig. 7.2: time5.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time5.h +#include "time5.h" + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hour, int minute, int second ) +{ + setTime( hour, minute, second ); + +} // end Time constructor + +// set hour, minute and second values +void Time::setTime( int hour, int minute, int second ) +{ + setHour( hour ); + setMinute( minute ); + setSecond( second ); + +} // end function setTime + +// set hour value +void Time::setHour( int h ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + +} // end function setHour + +// set minute value +void Time::setMinute( int m ) +{ + minute = ( m >= 0 && m < 60 ) ? m : 0; + +} // end function setMinute + +// set second value +void Time::setSecond( int s ) +{ + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setSecond + +// return hour value +int Time::getHour() const +{ + return hour; + +} // end function getHour + +// return minute value +int Time::getMinute() const +{ + return minute; + +} // end function getMinute + +// return second value +int Time::getSecond() const +{ + return second; + +} // end function getSecond + +// print Time in universal format +void Time::printUniversal() const +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h new file mode 100644 index 0000000..25b583e --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h @@ -0,0 +1,49 @@ +// Fig. 7.1: time5.h +// Definition of class Time. +// Member functions defined in time5.cpp. +#ifndef TIME5_H +#define TIME5_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); // default constructor + + // set functions + void setTime( int, int, int ); // set time + void setHour( int ); // set hour + void setMinute( int ); // set minute + void setSecond( int ); // set second + + // get functions (normally declared const) + int getHour() const; // return hour + int getMinute() const; // return minute + int getSecond() const; // return second + + // print functions (normally declared const) + void printUniversal() const; // print universal time + void printStandard(); // print standard time + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp b/Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp new file mode 100644 index 0000000..62b4464 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp @@ -0,0 +1,75 @@ +// Fig. 7.4: fig07_04.cpp +// Using a member initializer to initialize a +// constant of a built-in data type. +#include + +using std::cout; +using std::endl; + +class Increment { + +public: + Increment( int c = 0, int i = 1 ); // default constructor + + void addIncrement() + { + count += increment; + + } // end function addIncrement + + void print() const; // prints count and increment + +private: + int count; + const int increment; // const data member + +}; // end class Increment + +// constructor +Increment::Increment( int c, int i ) + : count( c ), // initializer for non-const member + increment( i ) // required initializer for const member +{ + // empty body + +} // end constructor Increment + +// print count and increment values +void Increment::print() const +{ + cout << "count = " << count + << ", increment = " << increment << endl; + +} // end function print + +int main() +{ + Increment value( 10, 5 ); + + cout << "Before incrementing: "; + value.print(); + + for ( int j = 0; j < 3; j++ ) { + value.addIncrement(); + cout << "After increment " << j + 1 << ": "; + value.print(); + } + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp b/Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp new file mode 100644 index 0000000..f7945b4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp @@ -0,0 +1,74 @@ +// Fig. 7.5: fig07_05.cpp +// Attempting to initialize a constant of +// a built-in data type with an assignment. +#include + +using std::cout; +using std::endl; + +class Increment { + +public: + Increment( int c = 0, int i = 1 ); // default constructor + + void addIncrement() + { + count += increment; + + } // end function addIncrement + + void print() const; // prints count and increment + +private: + int count; + const int increment; // const data member + +}; // end class Increment + +// constructor +Increment::Increment( int c, int i ) +{ // Constant member 'increment' is not initialized + count = c; // allowed because count is not constant + increment = i; // ERROR: Cannot modify a const object + +} // end constructor Increment + +// print count and increment values +void Increment::print() const +{ + cout << "count = " << count + << ", increment = " << increment << endl; + +} // end function print + +int main() +{ + Increment value( 10, 5 ); + + cout << "Before incrementing: "; + value.print(); + + for ( int j = 0; j < 3; j++ ) { + value.addIncrement(); + cout << "After increment " << j + 1 << ": "; + value.print(); + } + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp new file mode 100644 index 0000000..ef3818b --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp @@ -0,0 +1,85 @@ +// Fig. 7.7: date1.cpp +// Member-function definitions for class Date. +#include + +using std::cout; +using std::endl; + +// include Date class definition from date1.h +#include "date1.h" + +// constructor confirms proper value for month; calls +// utility function checkDay to confirm proper value for day +Date::Date( int mn, int dy, int yr ) +{ + if ( mn > 0 && mn <= 12 ) // validate the month + month = mn; + + else { // invalid month set to 1 + month = 1; + cout << "Month " << mn << " invalid. Set to month 1.\n"; + } + + year = yr; // should validate yr + day = checkDay( dy ); // validate the day + + // output Date object to show when its constructor is called + cout << "Date object constructor for date "; + print(); + cout << endl; + +} // end Date constructor + +// print Date object in form month/day/year +void Date::print() const +{ + cout << month << '/' << day << '/' << year; + +} // end function print + +// output Date object to show when its destructor is called +Date::~Date() +{ + cout << "Date object destructor for date "; + print(); + cout << endl; + +} // end ~Date destructor + +// utility function to confirm proper day value based on +// month and year; handles leap years, too +int Date::checkDay( int testDay ) const +{ + static const int daysPerMonth[ 13 ] = + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + // determine whether testDay is valid for specified month + if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) + return testDay; + + // February 29 check for leap year + if ( month == 2 && testDay == 29 && + ( year % 400 == 0 || + ( year % 4 == 0 && year % 100 != 0 ) ) ) + return testDay; + + cout << "Day " << testDay << " invalid. Set to day 1.\n"; + + return 1; // leave object in consistent state if bad value + +} // end function checkDay + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h new file mode 100644 index 0000000..5829c71 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h @@ -0,0 +1,39 @@ +// Fig. 7.6: date1.h +// Date class definition. +// Member functions defined in date1.cpp +#ifndef DATE1_H +#define DATE1_H + +class Date { + +public: + Date( int = 1, int = 1, int = 1900 ); // default constructor + void print() const; // print date in month/day/year format + ~Date(); // provided to confirm destruction order + +private: + int month; // 1-12 (January-December) + int day; // 1-31 based on month + int year; // any year + + // utility function to test proper day for month and year + int checkDay( int ) const; + +}; // end class Date + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp new file mode 100644 index 0000000..6cc742a --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp @@ -0,0 +1,72 @@ +// Fig. 7.9: employee1.cpp +// Member-function definitions for class Employee. +#include + +using std::cout; +using std::endl; + +#include // strcpy and strlen prototypes + +#include "employee1.h" // Employee class definition +#include "date1.h" // Date class definition + +// constructor uses member initializer list to pass initializer +// values to constructors of member objects birthDate and +// hireDate [Note: This invokes the so-called "default copy +// constructor" which the C++ compiler provides implicitly.] +Employee::Employee( const char *first, const char *last, + const Date &dateOfBirth, const Date &dateOfHire ) + : birthDate( dateOfBirth ), // initialize birthDate + hireDate( dateOfHire ) // initialize hireDateba +{ + // copy first into firstName and be sure that it fits + int length = strlen( first ); + length = ( length < 25 ? length : 24 ); + strncpy( firstName, first, length ); + firstName[ length ] = '\0'; + + // copy last into lastName and be sure that it fits + length = strlen( last ); + length = ( length < 25 ? length : 24 ); + strncpy( lastName, last, length ); + lastName[ length ] = '\0'; + + // output Employee object to show when constructor is called + cout << "Employee object constructor: " + << firstName << ' ' << lastName << endl; + +} // end Employee constructor + +// print Employee object +void Employee::print() const +{ + cout << lastName << ", " << firstName << "\nHired: "; + hireDate.print(); + cout << " Birth date: "; + birthDate.print(); + cout << endl; + +} // end function print + +// output Employee object to show when its destructor is called +Employee::~Employee() +{ + cout << "Employee object destructor: " + << lastName << ", " << firstName << endl; + +} // end ~Employee destructor + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h new file mode 100644 index 0000000..cfc294a --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h @@ -0,0 +1,42 @@ +// Fig. 7.8: employee1.h +// Employee class definition. +// Member functions defined in employee1.cpp. +#ifndef EMPLY1_H +#define EMPLY1_H + +// include Date class definition from date1.h +#include "date1.h" + +class Employee { + +public: + Employee( + const char *, const char *, const Date &, const Date & ); + + void print() const; + ~Employee(); // provided to confirm destruction order + +private: + char firstName[ 25 ]; + char lastName[ 25 ]; + const Date birthDate; // composition: member object + const Date hireDate; // composition: member object + +}; // end class Employee + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp new file mode 100644 index 0000000..90dfc9b --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp @@ -0,0 +1,40 @@ +// Fig. 7.10: fig07_10.cpp +// Demonstrating composition--an object with member objects. +#include + +using std::cout; +using std::endl; + +#include "employee1.h" // Employee class definition + +int main() +{ + Date birth( 7, 24, 1949 ); + Date hire( 3, 12, 1988 ); + Employee manager( "Bob", "Jones", birth, hire ); + + cout << '\n'; + manager.print(); + + cout << "\nTest Date constructor with invalid values:\n"; + Date lastDayOff( 14, 35, 1994 ); // invalid month and day + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp b/Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp new file mode 100644 index 0000000..995bc86 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp @@ -0,0 +1,72 @@ +// Fig. 7.11: fig07_11.cpp +// Friends can access private members of a class. +#include + +using std::cout; +using std::endl; + +// Count class definition +// (note that there is no friendship declaration) +class Count { + friend void setX( Count &, int ); // friend declaration + +public: + + // constructor + Count() + : x( 0 ) // initialize x to 0 + { + // empty body + + } // end constructor Count + + // output x + void print() const + { + cout << x << endl; + + } // end function print + +private: + int x; // data member + +}; // end class Count + +// function setX can modify private data of Count +// because setX is declared as a friend of Count +void setX( Count &c, int val ) +{ + c.x = val; // legal: setX is a friend of Count + +} // end function setX + +int main() +{ + Count counter; // create Count object + + cout << "counter.x after instantiation: "; + counter.print(); + + setX( counter, 8 ); // set x with a friend + + cout << "counter.x after call to setX friend function: "; + counter.print(); + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp b/Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp new file mode 100644 index 0000000..dbab9fc --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp @@ -0,0 +1,65 @@ +// Fig. 7.12: fig07_12.cpp +// Non-friend/non-member functions cannot access +// private data of a class. +#include + +using std::cout; +using std::endl; + +// Count class definition +class Count { + +public: + + // constructor + Count() + : x( 0 ) // initialize x to 0 + { + // empty body + + } // end constructor Count + + // output x + void print() const + { + cout << x << endl; + + } // end function print + +private: + int x; // data member + +}; // end class Count + +// function tries to modify private data of Count, +// but cannot because function is not a friend of Count +void cannotSetX( Count &c, int val ) +{ + c.x = val; // ERROR: cannot access private member in Count + +} // end function cannotSetX + +int main() +{ + Count counter; // create Count object + + cannotSetX( counter, 3 ); // cannotSetX is not a friend + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp b/Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp new file mode 100644 index 0000000..2e385a4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp @@ -0,0 +1,66 @@ +// Fig. 7.13: fig07_13.cpp +// Using the this pointer to refer to object members. +#include + +using std::cout; +using std::endl; + +class Test { + +public: + Test( int = 0 ); // default constructor + void print() const; + +private: + int x; + +}; // end class Test + +// constructor +Test::Test( int value ) + : x( value ) // initialize x to value +{ + // empty body + +} // end constructor Test + +// print x using implicit and explicit this pointers; +// parentheses around *this required +void Test::print() const +{ + // implicitly use this pointer to access member x + cout << " x = " << x; + + // explicitly use this pointer to access member x + cout << "\n this->x = " << this->x; + + // explicitly use dereferenced this pointer and + // the dot operator to access member x + cout << "\n(*this).x = " << ( *this ).x << endl; + +} // end function print + +int main() +{ + Test testObject( 12 ); + + testObject.print(); + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp new file mode 100644 index 0000000..70e7a64 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp @@ -0,0 +1,48 @@ +// Fig. 7.16: fig07_16.cpp +// Cascading member function calls with the this pointer. +#include + +using std::cout; +using std::endl; + +#include "time6.h" // Time class definition + +int main() +{ + Time t; + + // cascaded function calls + t.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); + + // output time in universal and standard formats + cout << "Universal time: "; + t.printUniversal(); + + cout << "\nStandard time: "; + t.printStandard(); + + cout << "\n\nNew standard time: "; + + // cascaded function calls + t.setTime( 20, 20, 20 ).printStandard(); + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp new file mode 100644 index 0000000..97205c0 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp @@ -0,0 +1,114 @@ +// Fig. 7.15: time6.cpp +// Member-function definitions for Time class. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +#include "time6.h" // Time class definition + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hr, int min, int sec ) +{ + setTime( hr, min, sec ); + +} // end Time constructor + +// set values of hour, minute, and second +Time &Time::setTime( int h, int m, int s ) +{ + setHour( h ); + setMinute( m ); + setSecond( s ); + + return *this; // enables cascading + +} // end function setTime + +// set hour value +Time &Time::setHour( int h ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + + return *this; // enables cascading + +} // end function setHour + +// set minute value +Time &Time::setMinute( int m ) +{ + minute = ( m >= 0 && m < 60 ) ? m : 0; + + return *this; // enables cascading + +} // end function setMinute + +// set second value +Time &Time::setSecond( int s ) +{ + second = ( s >= 0 && s < 60 ) ? s : 0; + + return *this; // enables cascading + +} // end function setSecond + +// get hour value +int Time::getHour() const +{ + return hour; + +} // end function getHour + +// get minute value +int Time::getMinute() const +{ + return minute; + +} // end function getMinute + +// get second value +int Time::getSecond() const +{ + return second; + +} // end function getSecond + +// print Time in universal format +void Time::printUniversal() const +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() const +{ + cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) + << ":" << setfill( '0' ) << setw( 2 ) << minute + << ":" << setw( 2 ) << second + << ( hour < 12 ? " AM" : " PM" ); + +} // end function printStandard + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h new file mode 100644 index 0000000..d9413ca --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h @@ -0,0 +1,51 @@ +// Fig. 7.14: time6.h +// Cascading member function calls. + +// Time class definition. +// Member functions defined in time6.cpp. +#ifndef TIME6_H +#define TIME6_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); // default constructor + + // set functions + Time &setTime( int, int, int ); // set hour, minute, second + Time &setHour( int ); // set hour + Time &setMinute( int ); // set minute + Time &setSecond( int ); // set second + + // get functions (normally declared const) + int getHour() const; // return hour + int getMinute() const; // return minute + int getSecond() const; // return second + + // print functions (normally declared const) + void printUniversal() const; // print universal time + void printStandard() const; // print standard time + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp b/Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp new file mode 100644 index 0000000..27ba9f7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp @@ -0,0 +1,55 @@ +// Fig. 7.19: fig07_19.cpp +// Driver to test class Employee. +#include + +using std::cout; +using std::endl; + +#include // C++ standard new operator + +#include "employee2.h" // Employee class definition + +int main() +{ + cout << "Number of employees before instantiation is " + << Employee::getCount() << endl; // use class name + + Employee *e1Ptr = new Employee( "Susan", "Baker" ); + Employee *e2Ptr = new Employee( "Robert", "Jones" ); + + cout << "Number of employees after instantiation is " + << e1Ptr->getCount(); + + cout << "\n\nEmployee 1: " + << e1Ptr->getFirstName() + << " " << e1Ptr->getLastName() + << "\nEmployee 2: " + << e2Ptr->getFirstName() + << " " << e2Ptr->getLastName() << "\n\n"; + + delete e1Ptr; // recapture memory + e1Ptr = 0; // disconnect pointer from free-store space + delete e2Ptr; // recapture memory + e2Ptr = 0; // disconnect pointer from free-store space + + cout << "Number of employees after deletion is " + << Employee::getCount() << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp new file mode 100644 index 0000000..540f367 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp @@ -0,0 +1,88 @@ +// Fig. 7.18: employee2.cpp +// Member-function definitions for class Employee. +#include + +using std::cout; +using std::endl; + +#include // C++ standard new operator +#include // strcpy and strlen prototypes + +#include "employee2.h" // Employee class definition + +// define and initialize static data member +int Employee::count = 0; + +// define static member function that returns number of +// Employee objects instantiated +int Employee::getCount() +{ + return count; + +} // end static function getCount + +// constructor dynamically allocates space for +// first and last name and uses strcpy to copy +// first and last names into the object +Employee::Employee( const char *first, const char *last ) +{ + firstName = new char[ strlen( first ) + 1 ]; + strcpy( firstName, first ); + + lastName = new char[ strlen( last ) + 1 ]; + strcpy( lastName, last ); + + ++count; // increment static count of employees + + cout << "Employee constructor for " << firstName + << ' ' << lastName << " called." << endl; + +} // end Employee constructor + +// destructor deallocates dynamically allocated memory +Employee::~Employee() +{ + cout << "~Employee() called for " << firstName + << ' ' << lastName << endl; + + delete [] firstName; // recapture memory + delete [] lastName; // recapture memory + + --count; // decrement static count of employees + +} // end ~Employee destructor + +// return first name of employee +const char *Employee::getFirstName() const +{ + // const before return type prevents client from modifying + // private data; client should copy returned string before + // destructor deletes storage to prevent undefined pointer + return firstName; + +} // end function getFirstName + +// return last name of employee +const char *Employee::getLastName() const +{ + // const before return type prevents client from modifying + // private data; client should copy returned string before + // destructor deletes storage to prevent undefined pointer + return lastName; + +} // end function getLastName + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h new file mode 100644 index 0000000..3010210 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h @@ -0,0 +1,41 @@ +// Fig. 7.17: employee2.h +// Employee class definition. +#ifndef EMPLOYEE2_H +#define EMPLOYEE2_H + +class Employee { + +public: + Employee( const char *, const char * ); // constructor + ~Employee(); // destructor + const char *getFirstName() const; // return first name + const char *getLastName() const; // return last name + + // static member function + static int getCount(); // return # objects instantiated + +private: + char *firstName; + char *lastName; + + // static data member + static int count; // number of objects instantiated + +}; // end class Employee + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp b/Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp new file mode 100644 index 0000000..a11a543 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp @@ -0,0 +1,39 @@ +// Fig. 7.23: fig07_23.cpp +// Hiding a class’s private data with a proxy class. +#include + +using std::cout; +using std::endl; + +#include "interface.h" // Interface class definition + +int main() +{ + Interface i( 5 ); + + cout << "Interface contains: " << i.getValue() + << " before setValue" << endl; + + i.setValue( 10 ); + + cout << "Interface contains: " << i.getValue() + << " after setValue" << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h b/Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h new file mode 100644 index 0000000..1630a50 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h @@ -0,0 +1,48 @@ +// Fig. 7.20: implementation.h +// Header file for class Implementation + +class Implementation { + +public: + + // constructor + Implementation( int v ) + : value( v ) // initialize value with v + { + // empty body + + } // end constructor Implementation + + // set value to v + void setValue( int v ) + { + value = v; // should validate v + + } // end function setValue + + // return value + int getValue() const + { + return value; + + } // end function getValue + +private: + int value; + +}; // end class Implementation + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp new file mode 100644 index 0000000..0f895fd --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp @@ -0,0 +1,48 @@ +// Fig. 7.22: interface.cpp +// Definition of class Interface +#include "interface.h" // Interface class definition +#include "implementation.h" // Implementation class definition + +// constructor +Interface::Interface( int v ) + : ptr ( new Implementation( v ) ) // initialize ptr +{ + // empty body + +} // end Interface constructor + +// call Implementation's setValue function +void Interface::setValue( int v ) +{ + ptr->setValue( v ); + +} // end function setValue + +// call Implementation's getValue function +int Interface::getValue() const +{ + return ptr->getValue(); + +} // end function getValue + +// destructor +Interface::~Interface() +{ + delete ptr; + +} // end ~Interface destructor + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h new file mode 100644 index 0000000..5d229e1 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h @@ -0,0 +1,34 @@ +// Fig. 7.21: interface.h +// Header file for interface.cpp + +class Implementation; // forward class declaration + +class Interface { + +public: + Interface( int ); + void setValue( int ); // same public interface as + int getValue() const; // class Implementation + ~Interface(); + +private: + + // requires previous forward declaration (line 4) + Implementation *ptr; + +}; // end class Interface + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp b/Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp new file mode 100644 index 0000000..ac5e434 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_03/Fig08_03.cpp @@ -0,0 +1,89 @@ +// Fig. 8.3: fig08_03.cpp +// Overloading the stream-insertion and +// stream-extraction operators. +#include + +using std::cout; +using std::cin; +using std::endl; +using std::ostream; +using std::istream; + +#include + +using std::setw; + +// PhoneNumber class definition +class PhoneNumber { + friend ostream &operator<<( ostream&, const PhoneNumber & ); + friend istream &operator>>( istream&, PhoneNumber & ); + +private: + char areaCode[ 4 ]; // 3-digit area code and null + char exchange[ 4 ]; // 3-digit exchange and null + char line[ 5 ]; // 4-digit line and null + +}; // end class PhoneNumber + +// overloaded stream-insertion operator; cannot be +// a member function if we would like to invoke it with +// cout << somePhoneNumber; +ostream &operator<<( ostream &output, const PhoneNumber &num ) +{ + output << "(" << num.areaCode << ") " + << num.exchange << "-" << num.line; + + return output; // enables cout << a << b << c; + +} // end function operator<< + +// overloaded stream-extraction operator; cannot be +// a member function if we would like to invoke it with +// cin >> somePhoneNumber; +istream &operator>>( istream &input, PhoneNumber &num ) +{ + input.ignore(); // skip ( + input >> setw( 4 ) >> num.areaCode; // input area code + input.ignore( 2 ); // skip ) and space + input >> setw( 4 ) >> num.exchange; // input exchange + input.ignore(); // skip dash (-) + input >> setw( 5 ) >> num.line; // input line + + return input; // enables cin >> a >> b >> c; + +} // end function operator>> + +int main() +{ + PhoneNumber phone; // create object phone + + cout << "Enter phone number in the form (123) 456-7890:\n"; + + // cin >> phone invokes operator>> by implicitly issuing + // the non-member function call operator>>( cin, phone ) + cin >> phone; + + cout << "The phone number entered was: "; + + // cout << phone invokes operator<< by implicitly issuing + // the non-member function call operator<<( cout, phone ) + cout << phone << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp new file mode 100644 index 0000000..817f7ec --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.cpp @@ -0,0 +1,177 @@ +// Fig 8.5: array1.cpp +// Member function definitions for class Array +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +#include // C++ standard "new" operator + +#include // exit function prototype + +#include "array1.h" // Array class definition + +// default constructor for class Array (default size 10) +Array::Array( int arraySize ) +{ + // validate arraySize + size = ( arraySize > 0 ? arraySize : 10 ); + + ptr = new int[ size ]; // create space for array + + for ( int i = 0; i < size; i++ ) + ptr[ i ] = 0; // initialize array + +} // end Array default constructor + +// copy constructor for class Array; +// must receive a reference to prevent infinite recursion +Array::Array( const Array &arrayToCopy ) + : size( arrayToCopy.size ) +{ + ptr = new int[ size ]; // create space for array + + for ( int i = 0; i < size; i++ ) + ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object + +} // end Array copy constructor + +// destructor for class Array +Array::~Array() +{ + delete [] ptr; // reclaim array space + +} // end destructor + +// return size of array +int Array::getSize() const +{ + return size; + +} // end function getSize + +// overloaded assignment operator; +// const return avoids: ( a1 = a2 ) = a3 +const Array &Array::operator=( const Array &right ) +{ + if ( &right != this ) { // check for self-assignment + + // for arrays of different sizes, deallocate original + // left-side array, then allocate new left-side array + if ( size != right.size ) { + delete [] ptr; // reclaim space + size = right.size; // resize this object + ptr = new int[ size ]; // create space for array copy + + } // end inner if + + for ( int i = 0; i < size; i++ ) + ptr[ i ] = right.ptr[ i ]; // copy array into object + + } // end outer if + + return *this; // enables x = y = z, for example + +} // end function operator= + +// determine if two arrays are equal and +// return true, otherwise return false +bool Array::operator==( const Array &right ) const +{ + if ( size != right.size ) + return false; // arrays of different sizes + + for ( int i = 0; i < size; i++ ) + + if ( ptr[ i ] != right.ptr[ i ] ) + return false; // arrays are not equal + + return true; // arrays are equal + +} // end function operator== + +// overloaded subscript operator for non-const Arrays +// reference return creates an lvalue +int &Array::operator[]( int subscript ) +{ + // check for subscript out of range error + if ( subscript < 0 || subscript >= size ) { + cout << "\nError: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program; subscript out of range + + } // end if + + return ptr[ subscript ]; // reference return + +} // end function operator[] + +// overloaded subscript operator for const Arrays +// const reference return creates an rvalue +const int &Array::operator[]( int subscript ) const +{ + // check for subscript out of range error + if ( subscript < 0 || subscript >= size ) { + cout << "\nError: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program; subscript out of range + + } // end if + + return ptr[ subscript ]; // const reference return + +} // end function operator[] + +// overloaded input operator for class Array; +// inputs values for entire array +istream &operator>>( istream &input, Array &a ) +{ + for ( int i = 0; i < a.size; i++ ) + input >> a.ptr[ i ]; + + return input; // enables cin >> x >> y; + +} // end function + +// overloaded output operator for class Array +ostream &operator<<( ostream &output, const Array &a ) +{ + int i; + + // output private ptr-based array + for ( i = 0; i < a.size; i++ ) { + output << setw( 12 ) << a.ptr[ i ]; + + if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output + output << endl; + + } // end for + + if ( i % 4 != 0 ) // end last line of output + output << endl; + + return output; // enables cout << x << y; + +} // end function operator<< + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h new file mode 100644 index 0000000..d84bc81 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Array1.h @@ -0,0 +1,61 @@ +// Fig. 8.4: array1.h +// Array class for storing arrays of integers. +#ifndef ARRAY1_H +#define ARRAY1_H + +#include + +using std::ostream; +using std::istream; + +class Array { + friend ostream &operator<<( ostream &, const Array & ); + friend istream &operator>>( istream &, Array & ); + +public: + Array( int = 10 ); // default constructor + Array( const Array & ); // copy constructor + ~Array(); // destructor + int getSize() const; // return size + + // assignment operator + const Array &operator=( const Array & ); + + // equality operator + bool operator==( const Array & ) const; + + // inequality operator; returns opposite of == operator + bool operator!=( const Array &right ) const + { + return ! ( *this == right ); // invokes Array::operator== + + } // end function operator!= + + // subscript operator for non-const objects returns lvalue + int &operator[]( int ); + + // subscript operator for const objects returns rvalue + const int &operator[]( int ) const; + +private: + int size; // array size + int *ptr; // pointer to first element of array + +}; // end class Array + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp new file mode 100644 index 0000000..29178eb --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_04_06/Fig08_06.cpp @@ -0,0 +1,90 @@ +// Fig. 8.6: fig08_06.cpp +// Array class test program. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "array1.h" + +int main() +{ + Array integers1( 7 ); // seven-element Array + Array integers2; // 10-element Array by default + + // print integers1 size and contents + cout << "Size of array integers1 is " + << integers1.getSize() + << "\nArray after initialization:\n" << integers1; + + // print integers2 size and contents + cout << "\nSize of array integers2 is " + << integers2.getSize() + << "\nArray after initialization:\n" << integers2; + + // input and print integers1 and integers2 + cout << "\nInput 17 integers:\n"; + cin >> integers1 >> integers2; + + cout << "\nAfter input, the arrays contain:\n" + << "integers1:\n" << integers1 + << "integers2:\n" << integers2; + + // use overloaded inequality (!=) operator + cout << "\nEvaluating: integers1 != integers2\n"; + + if ( integers1 != integers2 ) + cout << "integers1 and integers2 are not equal\n"; + + // create array integers3 using integers1 as an + // initializer; print size and contents + Array integers3( integers1 ); // calls copy constructor + + cout << "\nSize of array integers3 is " + << integers3.getSize() + << "\nArray after initialization:\n" << integers3; + + // use overloaded assignment (=) operator + cout << "\nAssigning integers2 to integers1:\n"; + integers1 = integers2; // note target is smaller + + cout << "integers1:\n" << integers1 + << "integers2:\n" << integers2; + + // use overloaded equality (==) operator + cout << "\nEvaluating: integers1 == integers2\n"; + + if ( integers1 == integers2 ) + cout << "integers1 and integers2 are equal\n"; + + // use overloaded subscript operator to create rvalue + cout << "\nintegers1[5] is " << integers1[ 5 ]; + + // use overloaded subscript operator to create lvalue + cout << "\n\nAssigning 1000 to integers1[5]\n"; + integers1[ 5 ] = 1000; + cout << "integers1:\n" << integers1; + + // attempt to use out-of-range subscript + cout << "\nAttempt to assign 1000 to integers1[15]" << endl; + integers1[ 15 ] = 1000; // ERROR: out of range + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp b/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp new file mode 100644 index 0000000..9a9d516 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/Fig08_09.cpp @@ -0,0 +1,101 @@ +// Fig. 8.9: fig08_09.cpp +// String class test program. +#include + +using std::cout; +using std::endl; + +#include "string1.h" + +int main() +{ + String s1( "happy" ); + String s2( " birthday" ); + String s3; + + // test overloaded equality and relational operators + cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 + << "\"; s3 is \"" << s3 << '\"' + << "\n\nThe results of comparing s2 and s1:" + << "\ns2 == s1 yields " + << ( s2 == s1 ? "true" : "false" ) + << "\ns2 != s1 yields " + << ( s2 != s1 ? "true" : "false" ) + << "\ns2 > s1 yields " + << ( s2 > s1 ? "true" : "false" ) + << "\ns2 < s1 yields " + << ( s2 < s1 ? "true" : "false" ) + << "\ns2 >= s1 yields " + << ( s2 >= s1 ? "true" : "false" ) + << "\ns2 <= s1 yields " + << ( s2 <= s1 ? "true" : "false" ); + + // test overloaded String empty (!) operator + cout << "\n\nTesting !s3:\n"; + + if ( !s3 ) { + cout << "s3 is empty; assigning s1 to s3;\n"; + s3 = s1; // test overloaded assignment + cout << "s3 is \"" << s3 << "\""; + } + + // test overloaded String concatenation operator + cout << "\n\ns1 += s2 yields s1 = "; + s1 += s2; // test overloaded concatenation + cout << s1; + + // test conversion constructor + cout << "\n\ns1 += \" to you\" yields\n"; + s1 += " to you"; // test conversion constructor + cout << "s1 = " << s1 << "\n\n"; + + // test overloaded function call operator () for substring + cout << "The substring of s1 starting at\n" + << "location 0 for 14 characters, s1(0, 14), is:\n" + << s1( 0, 14 ) << "\n\n"; + + // test substring "to-end-of-String" option + cout << "The substring of s1 starting at\n" + << "location 15, s1(15, 0), is: " + << s1( 15, 0 ) << "\n\n"; // 0 is "to end of string" + + // test copy constructor + String *s4Ptr = new String( s1 ); + cout << "\n*s4Ptr = " << *s4Ptr << "\n\n"; + + // test assignment (=) operator with self-assignment + cout << "assigning *s4Ptr to *s4Ptr\n"; + *s4Ptr = *s4Ptr; // test overloaded assignment + cout << "*s4Ptr = " << *s4Ptr << '\n'; + + // test destructor + delete s4Ptr; + + // test using subscript operator to create lvalue + s1[ 0 ] = 'H'; + s1[ 6 ] = 'B'; + cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " + << s1 << "\n\n"; + + // test subscript out of range + cout << "Attempt to assign 'd' to s1[30] yields:" << endl; + s1[ 30 ] = 'd'; // ERROR: subscript out of range + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp new file mode 100644 index 0000000..7cca6d7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.cpp @@ -0,0 +1,221 @@ +// Fig. 8.8: string1.cpp +// Member function definitions for class String. +#include + +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // C++ standard "new" operator + +#include // strcpy and strcat prototypes +#include // exit prototype + +#include "string1.h" // String class definition + +// conversion constructor converts char * to String +String::String( const char *s ) + : length( ( s != 0 ) ? strlen( s ) : 0 ) +{ + cout << "Conversion constructor: " << s << '\n'; + setString( s ); // call utility function + +} // end String conversion constructor + +// copy constructor +String::String( const String © ) + : length( copy.length ) +{ + cout << "Copy constructor: " << copy.sPtr << '\n'; + setString( copy.sPtr ); // call utility function + +} // end String copy constructor + +// Destructor +String::~String() +{ + cout << "Destructor: " << sPtr << '\n'; + delete [] sPtr; // reclaim string + +} // end ~String destructor + +// overloaded = operator; avoids self assignment +const String &String::operator=( const String &right ) +{ + cout << "operator= called\n"; + + if ( &right != this ) { // avoid self assignment + delete [] sPtr; // prevents memory leak + length = right.length; // new String length + setString( right.sPtr ); // call utility function + } + + else + cout << "Attempted assignment of a String to itself\n"; + + return *this; // enables cascaded assignments + +} // end function operator= + +// concatenate right operand to this object and +// store in this object. +const String &String::operator+=( const String &right ) +{ + size_t newLength = length + right.length; // new length + char *tempPtr = new char[ newLength + 1 ]; // create memory + + strcpy( tempPtr, sPtr ); // copy sPtr + strcpy( tempPtr + length, right.sPtr ); // copy right.sPtr + + delete [] sPtr; // reclaim old space + sPtr = tempPtr; // assign new array to sPtr + length = newLength; // assign new length to length + + return *this; // enables cascaded calls + +} // end function operator+= + +// is this String empty? +bool String::operator!() const +{ + return length == 0; + +} // end function operator! + +// Is this String equal to right String? +bool String::operator==( const String &right ) const +{ + return strcmp( sPtr, right.sPtr ) == 0; + +} // end function operator== + +// Is this String less than right String? +bool String::operator<( const String &right ) const +{ + return strcmp( sPtr, right.sPtr ) < 0; + +} // end function operator< + +// return reference to character in String as lvalue +char &String::operator[]( int subscript ) +{ + // test for subscript out of range + if( subscript < 0 || subscript >= length ) { + cout << "Error: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program + } + + return sPtr[ subscript ]; // creates lvalue + +} // end function operator[] + +// return reference to character in String as rvalue +const char &String::operator[]( int subscript ) const +{ + // test for subscript out of range + if( subscript < 0 || subscript >= length ) { + cout << "Error: Subscript " << subscript + << " out of range" << endl; + + exit( 1 ); // terminate program + } + + return sPtr[ subscript ]; // creates rvalue + +} // end function operator[] + +// return a substring beginning at index and +// of length subLength +String String::operator()( int index, int subLength ) +{ + // if index is out of range or substring length < 0, + // return an empty String object + if ( index < 0 || index >= length || subLength < 0 ) + return ""; // converted to a String object automatically + + // determine length of substring + int len; + + if ( ( subLength == 0 ) || ( index + subLength > length ) ) + len = length - index; + else + len = subLength; + + // allocate temporary array for substring and + // terminating null character + char *tempPtr = new char[ len + 1 ]; + + // copy substring into char array and terminate string + strncpy( tempPtr, &sPtr[ index ], len ); + tempPtr[ len ] = '\0'; + + // create temporary String object containing the substring + String tempString( tempPtr ); + delete [] tempPtr; // delete temporary array + + return tempString; // return copy of the temporary String + +} // end function operator() + +// return string length +int String::getLength() const +{ + return length; + +} // end function getLenth + +// utility function called by constructors and operator= +void String::setString( const char *string2 ) +{ + sPtr = new char[ length + 1 ]; // allocate memory + + // if string2 is not a null pointer, copy contents + if ( string2 != 0 ) + strcpy( sPtr, string2 ); // copy literal to object + + // if string2 is a null pointer, make this an empty string + else + sPtr[ 0 ] = '\0'; // empty string + +} // end function setString + +// overloaded output operator +ostream &operator<<( ostream &output, const String &s ) +{ + output << s.sPtr; + + return output; // enables cascading + +} // end function operator<< + +// overloaded input operator +istream &operator>>( istream &input, String &s ) +{ + char temp[ 100 ]; // buffer to store input + + input >> setw( 100 ) >> temp; + s = temp; // use String class assignment operator + + return input; // enables cascading + +} // end function operator>> + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h new file mode 100644 index 0000000..81012aa --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_07_09/String1.h @@ -0,0 +1,85 @@ +// Fig. 8.7: string1.h +// String class definition. +#ifndef STRING1_H +#define STRING1_H + +#include + +using std::ostream; +using std::istream; + +class String { + friend ostream &operator<<( ostream &, const String & ); + friend istream &operator>>( istream &, String & ); + +public: + String( const char * = "" ); // conversion/default ctor + String( const String & ); // copy constructor + ~String(); // destructor + + const String &operator=( const String & ); // assignment + const String &operator+=( const String & ); // concatenation + + bool operator!() const; // is String empty? + bool operator==( const String & ) const; // test s1 == s2 + bool operator<( const String & ) const; // test s1 < s2 + + // test s1 != s2 + bool operator!=( const String & right ) const + { + return !( *this == right ); + + } // end function operator!= + + // test s1 > s2 + bool operator>( const String &right ) const + { + return right < *this; + + } // end function operator> + + // test s1 <= s2 + bool operator<=( const String &right ) const + { + return !( right < *this ); + + } // end function operator <= + + // test s1 >= s2 + bool operator>=( const String &right ) const + { + return !( *this < right ); + + } // end function operator>= + + char &operator[]( int ); // subscript operator + const char &operator[]( int ) const; // subscript operator + + String operator()( int, int ); // return a substring + + int getLength() const; // return string length + +private: + int length; // string length + char *sPtr; // pointer to start of string + + void setString( const char * ); // utility function + +}; // end class String + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp new file mode 100644 index 0000000..0c96a95 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp @@ -0,0 +1,137 @@ +// Fig. 8.11: date1.cpp +// Date class member function definitions. +#include +#include "date1.h" + +// initialize static member at file scope; +// one class-wide copy +const int Date::days[] = + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + +// Date constructor +Date::Date( int m, int d, int y ) +{ + setDate( m, d, y ); + +} // end Date constructor + +// set month, day and year +void Date::setDate( int mm, int dd, int yy ) +{ + month = ( mm >= 1 && mm <= 12 ) ? mm : 1; + year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900; + + // test for a leap year + if ( month == 2 && leapYear( year ) ) + day = ( dd >= 1 && dd <= 29 ) ? dd : 1; + else + day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1; + +} // end function setDate + +// overloaded preincrement operator +Date &Date::operator++() +{ + helpIncrement(); + + return *this; // reference return to create an lvalue + +} // end function operator++ + +// overloaded postincrement operator; note that the dummy +// integer parameter does not have a parameter name +Date Date::operator++( int ) +{ + Date temp = *this; + helpIncrement(); // hold current state of object + + // return unincremented, saved, temporary object + return temp; // value return; not a reference return + +} // end function operator++ + +// add specified number of days to date +const Date &Date::operator+=( int additionalDays ) +{ + for ( int i = 0; i < additionalDays; i++ ) + helpIncrement(); + + return *this; // enables cascading + +} // end function operator+= + +// if the year is a leap year, return true; +// otherwise, return false +bool Date::leapYear( int testYear ) const +{ + if ( testYear % 400 == 0 || + ( testYear % 100 != 0 && testYear % 4 == 0 ) ) + return true; // a leap year + else + return false; // not a leap year + +} // end function leapYear + +// determine whether the day is the last day of the month +bool Date::endOfMonth( int testDay ) const +{ + if ( month == 2 && leapYear( year ) ) + return testDay == 29; // last day of Feb. in leap year + else + return testDay == days[ month ]; + +} // end function endOfMonth + +// function to help increment the date +void Date::helpIncrement() +{ + // day is not end of month + if ( !endOfMonth( day ) ) + ++day; + + else + + // day is end of month and month < 12 + if ( month < 12 ) { + ++month; + day = 1; + } + + // last day of year + else { + ++year; + month = 1; + day = 1; + } + +} // end function helpIncrement + +// overloaded output operator +ostream &operator<<( ostream &output, const Date &d ) +{ + static char *monthName[ 13 ] = { "", "January", + "February", "March", "April", "May", "June", + "July", "August", "September", "October", + "November", "December" }; + + output << monthName[ d.month ] << ' ' + << d.day << ", " << d.year; + + return output; // enables cascading + +} // end function operator<< + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h new file mode 100644 index 0000000..d1d6148 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h @@ -0,0 +1,49 @@ +// Fig. 8.10: date1.h +// Date class definition. +#ifndef DATE1_H +#define DATE1_H +#include + +using std::ostream; + +class Date { + friend ostream &operator<<( ostream &, const Date & ); + +public: + Date( int m = 1, int d = 1, int y = 1900 ); // constructor + void setDate( int, int, int ); // set the date + + Date &operator++(); // preincrement operator + Date operator++( int ); // postincrement operator + + const Date &operator+=( int ); // add days, modify object + + bool leapYear( int ) const; // is this a leap year? + bool endOfMonth( int ) const; // is this end of month? + +private: + int month; + int day; + int year; + + static const int days[]; // array of days per month + void helpIncrement(); // utility function + +}; // end class Date + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp new file mode 100644 index 0000000..0d4c996 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp @@ -0,0 +1,54 @@ +// Fig. 8.12: fig08_12.cpp +// Date class test program. +#include + +using std::cout; +using std::endl; + +#include "date1.h" // Date class definition + +int main() +{ + Date d1; // defaults to January 1, 1900 + Date d2( 12, 27, 1992 ); + Date d3( 0, 99, 8045 ); // invalid date + + cout << "d1 is " << d1 << "\nd2 is " << d2 + << "\nd3 is " << d3; + + cout << "\n\nd2 += 7 is " << ( d2 += 7 ); + + d3.setDate( 2, 28, 1992 ); + cout << "\n\n d3 is " << d3; + cout << "\n++d3 is " << ++d3; + + Date d4( 7, 13, 2002 ); + + cout << "\n\nTesting the preincrement operator:\n" + << " d4 is " << d4 << '\n'; + cout << "++d4 is " << ++d4 << '\n'; + cout << " d4 is " << d4; + + cout << "\n\nTesting the postincrement operator:\n" + << " d4 is " << d4 << '\n'; + cout << "d4++ is " << d4++ << '\n'; + cout << " d4 is " << d4 << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp b/Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp new file mode 100644 index 0000000..cd74972 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_13/Fig08_13.cpp @@ -0,0 +1,104 @@ +// Fig. 8.13: fig08_13.cpp +// Standard library string class test program. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string s1( "happy" ); + string s2( " birthday" ); + string s3; + + // test overloaded equality and relational operators + cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 + << "\"; s3 is \"" << s3 << '\"' + << "\n\nThe results of comparing s2 and s1:" + << "\ns2 == s1 yields " + << ( s2 == s1 ? "true" : "false" ) + << "\ns2 != s1 yields " + << ( s2 != s1 ? "true" : "false" ) + << "\ns2 > s1 yields " + << ( s2 > s1 ? "true" : "false" ) + << "\ns2 < s1 yields " + << ( s2 < s1 ? "true" : "false" ) + << "\ns2 >= s1 yields " + << ( s2 >= s1 ? "true" : "false" ) + << "\ns2 <= s1 yields " + << ( s2 <= s1 ? "true" : "false" ); + + // test string member function empty + cout << "\n\nTesting s3.empty():\n"; + + if ( s3.empty() ) { + cout << "s3 is empty; assigning s1 to s3;\n"; + s3 = s1; // assign s1 to s3 + cout << "s3 is \"" << s3 << "\""; + } + + // test overloaded string concatenation operator + cout << "\n\ns1 += s2 yields s1 = "; + s1 += s2; // test overloaded concatenation + cout << s1; + + // test overloaded string concatenation operator + // with C-style string + cout << "\n\ns1 += \" to you\" yields\n"; + s1 += " to you"; + cout << "s1 = " << s1 << "\n\n"; + + // test string member function substr + cout << "The substring of s1 starting at location 0 for\n" + << "14 characters, s1.substr(0, 14), is:\n" + << s1.substr( 0, 14 ) << "\n\n"; + + // test substr "to-end-of-string" option + cout << "The substring of s1 starting at\n" + << "location 15, s1.substr(15), is:\n" + << s1.substr( 15 ) << '\n'; + + // test copy constructor + string *s4Ptr = new string( s1 ); + cout << "\n*s4Ptr = " << *s4Ptr << "\n\n"; + + // test assignment (=) operator with self-assignment + cout << "assigning *s4Ptr to *s4Ptr\n"; + *s4Ptr = *s4Ptr; + cout << "*s4Ptr = " << *s4Ptr << '\n'; + + // test destructor + delete s4Ptr; + + // test using subscript operator to create lvalue + s1[ 0 ] = 'H'; + s1[ 6 ] = 'B'; + cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " + << s1 << "\n\n"; + + // test subscript out of range with string member function "at" + cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl; + s1.at( 30 ) = 'd'; // ERROR: subscript out of range + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp b/Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp new file mode 100644 index 0000000..7fda752 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_14/Fig08_14.cpp @@ -0,0 +1,134 @@ +// Fig. 8.14: fig08_14.cpp +// Demonstrating standard library class vector. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::setw; + +#include + +using std::vector; + +void outputVector( const vector< int > & ); +void inputVector( vector< int > & ); + +int main() +{ + vector< int > integers1( 7 ); // 7-element vector< int > + vector< int > integers2( 10 ); // 10-element vector< int > + + // print integers1 size and contents + cout << "Size of vector integers1 is " + << integers1.size() + << "\nvector after initialization:\n"; + outputVector( integers1 ); + + // print integers2 size and contents + cout << "\nSize of vector integers2 is " + << integers2.size() + << "\nvector after initialization:\n"; + outputVector( integers2 ); + + // input and print integers1 and integers2 + cout << "\nInput 17 integers:\n"; + inputVector( integers1 ); + inputVector( integers2 ); + + cout << "\nAfter input, the vectors contain:\n" + << "integers1:\n"; + outputVector( integers1 ); + cout << "integers2:\n"; + outputVector( integers2 ); + + // use overloaded inequality (!=) operator + cout << "\nEvaluating: integers1 != integers2\n"; + + if ( integers1 != integers2 ) + cout << "integers1 and integers2 are not equal\n"; + + // create vector integers3 using integers1 as an + // initializer; print size and contents + vector< int > integers3( integers1 ); // copy constructor + + cout << "\nSize of vector integers3 is " + << integers3.size() + << "\nvector after initialization:\n"; + outputVector( integers3 ); + + + // use overloaded assignment (=) operator + cout << "\nAssigning integers2 to integers1:\n"; + integers1 = integers2; + + cout << "integers1:\n"; + outputVector( integers1 ); + cout << "integers2:\n"; + outputVector( integers1 ); + + // use overloaded equality (==) operator + cout << "\nEvaluating: integers1 == integers2\n"; + + if ( integers1 == integers2 ) + cout << "integers1 and integers2 are equal\n"; + + // use overloaded subscript operator to create rvalue + cout << "\nintegers1[5] is " << integers1[ 5 ]; + + // use overloaded subscript operator to create lvalue + cout << "\n\nAssigning 1000 to integers1[5]\n"; + integers1[ 5 ] = 1000; + cout << "integers1:\n"; + outputVector( integers1 ); + + // attempt to use out of range subscript + cout << "\nAttempt to assign 1000 to integers1.at( 15 )" + << endl; + integers1.at( 15 ) = 1000; // ERROR: out of range + + return 0; + +} // end main + +// output vector contents +void outputVector( const vector< int > &array ) +{ + for ( int i = 0; i < array.size(); i++ ) { + cout << setw( 12 ) << array[ i ]; + + if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output + cout << endl; + + } // end for + + if ( i % 4 != 0 ) + cout << endl; + +} // end function outputVector + +// input vector contents +void inputVector( vector< int > &array ) +{ + for ( int i = 0; i < array.size(); i++ ) + cin >> array[ i ]; + +} // end function inputVector + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp new file mode 100644 index 0000000..130d39e --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.cpp @@ -0,0 +1,54 @@ +// Fig. 8.16: complex1.cpp +// Complex class member function definitions. +#include + +using std::cout; + +#include "complex1.h" // Complex class definition + +// Constructor +Complex::Complex( double realPart, double imaginaryPart ) + : real( realPart ), + imaginary( imaginaryPart ) +{ + // empty body + +} // end Complex constructor + +// addition operator +Complex Complex::operator+( const Complex &operand2 ) const +{ + return Complex( real + operand2.real, + imaginary + operand2.imaginary ); + +} // end function operator+ + +// subtraction operator +Complex Complex::operator-( const Complex &operand2 ) const +{ + return Complex( real - operand2.real, + imaginary - operand2.imaginary ); + +} // end function operator- + +// display a Complex object in the form: (a, b) +void Complex::print() const +{ + cout << '(' << real << ", " << imaginary << ')'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h new file mode 100644 index 0000000..934f2f2 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Complex1.h @@ -0,0 +1,35 @@ +// Fig. 8.15: complex1.h +// Complex class definition. +#ifndef COMPLEX1_H +#define COMPLEX1_H + +class Complex { + +public: + Complex( double = 0.0, double = 0.0 ); // constructor + Complex operator+( const Complex & ) const; // addition + Complex operator-( const Complex & ) const; // subtraction + void print() const; // output + +private: + double real; // real part + double imaginary; // imaginary part + +}; // end class Complex + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp new file mode 100644 index 0000000..d98f9bb --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_15_17/Fig08_17.cpp @@ -0,0 +1,57 @@ +// Fig. 8.17: fig08_17.cpp +// Complex class test program. +#include + +using std::cout; +using std::endl; + +#include "complex1.h" + +int main() +{ + Complex x; + Complex y( 4.3, 8.2 ); + Complex z( 3.3, 1.1 ); + + cout << "x: "; + x.print(); + cout << "\ny: "; + y.print(); + cout << "\nz: "; + z.print(); + + x = y + z; + cout << "\n\nx = y + z:\n"; + x.print(); + cout << " = "; + y.print(); + cout << " + "; + z.print(); + + x = y - z; + cout << "\n\nx = y - z:\n"; + x.print(); + cout << " = "; + y.print(); + cout << " - "; + z.print(); + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp new file mode 100644 index 0000000..f73bde7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Fig08_20.cpp @@ -0,0 +1,51 @@ +// Fig. 8.20: fig08_20.cpp +// HugeInt test program. +#include + +using std::cout; +using std::endl; + +#include "hugeint1.h" + +int main() +{ + HugeInt n1( 7654321 ); + HugeInt n2( 7891234 ); + HugeInt n3( "99999999999999999999999999999" ); + HugeInt n4( "1" ); + HugeInt n5; + + cout << "n1 is " << n1 << "\nn2 is " << n2 + << "\nn3 is " << n3 << "\nn4 is " << n4 + << "\nn5 is " << n5 << "\n\n"; + + n5 = n1 + n2; + cout << n1 << " + " << n2 << " = " << n5 << "\n\n"; + + cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 ) + << "\n\n"; + + n5 = n1 + 9; + cout << n1 << " + " << 9 << " = " << n5 << "\n\n"; + + n5 = n2 + "10000"; + cout << n2 << " + " << "10000" << " = " << n5 << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp new file mode 100644 index 0000000..6e1aff7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.cpp @@ -0,0 +1,121 @@ +// Fig. 8.19: hugeint1.cpp +// HugeInt member-function and friend-function definitions. + +#include // isdigit function prototype +#include // strlen function prototype + +#include "hugeint1.h" // HugeInt class definition + +// default constructor; conversion constructor that converts +// a long integer into a HugeInt object +HugeInt::HugeInt( long value ) +{ + // initialize array to zero + for ( int i = 0; i <= 29; i++ ) + integer[ i ] = 0; + + // place digits of argument into array + for ( int j = 29; value != 0 && j >= 0; j-- ) { + integer[ j ] = value % 10; + value /= 10; + + } // end for + +} // end HugeInt default/conversion constructor + +// conversion constructor that converts a character string +// representing a large integer into a HugeInt object +HugeInt::HugeInt( const char *string ) +{ + // initialize array to zero + for ( int i = 0; i <= 29; i++ ) + integer[ i ] = 0; + + // place digits of argument into array + int length = strlen( string ); + + for ( int j = 30 - length, k = 0; j <= 29; j++, k++ ) + + if ( isdigit( string[ k ] ) ) + integer[ j ] = string[ k ] - '0'; + +} // end HugeInt conversion constructor + +// addition operator; HugeInt + HugeInt +HugeInt HugeInt::operator+( const HugeInt &op2 ) +{ + HugeInt temp; // temporary result + int carry = 0; + + for ( int i = 29; i >= 0; i-- ) { + temp.integer[ i ] = + integer[ i ] + op2.integer[ i ] + carry; + + // determine whether to carry a 1 + if ( temp.integer[ i ] > 9 ) { + temp.integer[ i ] %= 10; // reduce to 0-9 + carry = 1; + + } // end if + + // no carry + else + carry = 0; + } + + return temp; // return copy of temporary object + +} // end function operator+ + +// addition operator; HugeInt + int +HugeInt HugeInt::operator+( int op2 ) +{ + // convert op2 to a HugeInt, then invoke + // operator+ for two HugeInt objects + return *this + HugeInt( op2 ); + +} // end function operator+ + +// addition operator; +// HugeInt + string that represents large integer value +HugeInt HugeInt::operator+( const char *op2 ) +{ + // convert op2 to a HugeInt, then invoke + // operator+ for two HugeInt objects + return *this + HugeInt( op2 ); + +} // end operator+ + +// overloaded output operator +ostream& operator<<( ostream &output, const HugeInt &num ) +{ + int i; + + for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ ) + ; // skip leading zeros + + if ( i == 30 ) + output << 0; + else + + for ( ; i <= 29; i++ ) + output << num.integer[ i ]; + + return output; + +} // end function operator<< + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h new file mode 100644 index 0000000..46dd1df --- /dev/null +++ b/Bachelor/Prog1/examples/ch08/Fig08_18_20/Hugeint1.h @@ -0,0 +1,47 @@ +// Fig. 8.18: hugeint1.h +// HugeInt class definition. +#ifndef HUGEINT1_H +#define HUGEINT1_H + +#include + +using std::ostream; + +class HugeInt { + friend ostream &operator<<( ostream &, const HugeInt & ); + +public: + HugeInt( long = 0 ); // conversion/default constructor + HugeInt( const char * ); // conversion constructor + + // addition operator; HugeInt + HugeInt + HugeInt operator+( const HugeInt & ); + + // addition operator; HugeInt + int + HugeInt operator+( int ); + + // addition operator; + // HugeInt + string that represents large integer value + HugeInt operator+( const char * ); + +private: + short integer[ 30 ]; + +}; // end class HugeInt + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp b/Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp new file mode 100644 index 0000000..6f9e648 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/ElevatorSimulation.cpp @@ -0,0 +1,45 @@ +// Fig. 7.24: elevatorSimulation.cpp +// Driver for the simulation. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "building.h" // Building class definition + +int main() +{ + int duration; // length of simulation in seconds + + cout << "Enter run time: "; + cin >> duration; + cin.ignore(); // ignore return char + + Building building; // create the building + + cout << endl << "*** ELEVATOR SIMULATION BEGINS ***" + << endl << endl; + + building.runSimulation( duration ); // start simulation + + cout << "*** ELEVATOR SIMULATION ENDS ***" << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/bell.cpp b/Bachelor/Prog1/examples/elevator/bell.cpp new file mode 100644 index 0000000..350b826 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/bell.cpp @@ -0,0 +1,44 @@ +// Fig. 7.32: bell.cpp +// Member-function definitions for class Bell. +#include + +using std::cout; +using std::endl; + +#include "bell.h" // Bell class definition + +// constructor +Bell::Bell() +{ + cout << "bell created" << endl; + +} // end Bell constructor + +// destructor +Bell::~Bell() +{ + cout << "bell destructed" << endl; + +} // end ~Bell destructor + +// ring bell +void Bell::ringBell() const +{ + cout << "elevator rings its bell" << endl; + +} // end function ringBell + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/bell.h b/Bachelor/Prog1/examples/elevator/bell.h new file mode 100644 index 0000000..a0ef0bc --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/bell.h @@ -0,0 +1,30 @@ +// Fig. 7.31: bell.h +// Bell class definition. +#ifndef BELL_H +#define BELL_H + +class Bell { + +public: + Bell(); // constructor + ~Bell(); // destructor + void ringBell() const; // ring the bell + +}; // end class Bell + +#endif // BELL_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/building.cpp b/Bachelor/Prog1/examples/elevator/building.cpp new file mode 100644 index 0000000..b2eaec3 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/building.cpp @@ -0,0 +1,65 @@ +// Fig. 7.26: building.cpp +// Member-function definitions for class Building. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "building.h" // Building class definition + +// constructor +Building::Building() + : floor1( Floor::FLOOR1, elevator ), + floor2( Floor::FLOOR2, elevator ), + elevator( floor1, floor2 ), + scheduler( floor1, floor2 ) +{ + cout << "building constructed" << endl; + +} // end Building constructor + +// destructor +Building::~Building() +{ + cout << "building destructed" << endl; + +} // end ~Building destructor + +// function to control simulation +void Building::runSimulation( int totalTime ) +{ + int currentTime = 0; + + while ( currentTime < totalTime ) { + clock.tick(); // increment time + currentTime = clock.getTime(); // get new time + cout << "TIME: " << currentTime << endl; + + // process person arrivals for currentTime + scheduler.processTime( currentTime ); + + // process elevator events for currentTime + elevator.processTime( currentTime ); + + // wait for Enter key press, so user can view output + cin.get(); + + } // end while + +} // end function runSimulation + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/building.h b/Bachelor/Prog1/examples/elevator/building.h new file mode 100644 index 0000000..e45e3cd --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/building.h @@ -0,0 +1,42 @@ +// Fig. 7.25: building.h +// Building class definition. +#ifndef BUILDING_H +#define BUILDING_H + +#include "elevator.h" // Elevator class definition +#include "floor.h" // Floor class definition +#include "clock.h" // Clock class definition +#include "scheduler.h" // Scheduler class definition + +class Building { + +public: + Building(); // constructor + ~Building(); // destructor + void runSimulation( int ); // controls simulation + +private: + Floor floor1; // floor1 object + Floor floor2; // floor2 object + Elevator elevator; // elevator object + Clock clock; // clock object + Scheduler scheduler; // scheduler object + +}; // end class Building + +#endif // BUILDING_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/clock.cpp b/Bachelor/Prog1/examples/elevator/clock.cpp new file mode 100644 index 0000000..466f392 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/clock.cpp @@ -0,0 +1,52 @@ +// Fig. 7.28: clock.cpp +// Member-function definitions for class Clock. +#include + +using std::cout; +using std::endl; + +#include "clock.h" // Clock class definition + +// constructor +Clock::Clock() + : time( 0 ) // initialize time to 0 +{ + cout << "clock constructed" << endl; + +} // end Clock constructor + +// destructor +Clock::~Clock() +{ + cout << "clock destructed" << endl; + +} // end ~Clock destructor + +// increment time by 1 +void Clock::tick() +{ + time++; + +} // end function tick + +// return current time +int Clock::getTime() const +{ + return time; + +} // end function getTime + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/clock.h b/Bachelor/Prog1/examples/elevator/clock.h new file mode 100644 index 0000000..29bb55a --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/clock.h @@ -0,0 +1,34 @@ +// Fig. 7.27: clock.h +// Clock class definition. +#ifndef CLOCK_H +#define CLOCK_H + +class Clock { + +public: + Clock(); // constructor + ~Clock(); // destructor + void tick(); // increment clock by one second + int getTime() const; // returns clock's current time + +private: + int time; // clock's time + +}; // end class Clock + +#endif // CLOCK_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/door.cpp b/Bachelor/Prog1/examples/elevator/door.cpp new file mode 100644 index 0000000..047fc1b --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/door.cpp @@ -0,0 +1,81 @@ +// Fig. 7.36: door.cpp +// Member-function definitions for class Door. +#include + +using std::cout; +using std::endl; + +#include "door.h" // Door class definition +#include "person.h" // Person class definition +#include "floor.h" // Floor class definition +#include "elevator.h" // Elevator class definition + +// constructor +Door::Door() + : open( false ) // initialize open to false +{ + cout << "door constructed" << endl; + +} // end Door constructor + +// destructor +Door::~Door() +{ + cout << "door destructed" << endl; + +} // end ~Door destructor + +// open the door +void Door::openDoor( Person * const passengerPtr, + Person * const nextPassengerPtr, Floor ¤tFloor, + Elevator &elevator ) +{ + if ( !open ) { // if door is not open, open door + open = true; + + cout << "elevator opens its door on floor " + << currentFloor.getNumber() << endl; + + // if passenger is in elevator, tell person to leave + if ( passengerPtr != 0 ) { + passengerPtr->exitElevator( currentFloor, elevator ); + delete passengerPtr; // passenger leaves simulation + + } // end if + + // if passenger waiting to enter elevator, + // tell passenger to enter + if ( nextPassengerPtr != 0 ) + nextPassengerPtr->enterElevator( + elevator, currentFloor ); + + } // end outer if + +} // end function openDoor + +// close the door +void Door::closeDoor( const Floor ¤tFloor ) +{ + if ( open ) { // if door is open, close door + open = false; + cout << "elevator closes its door on floor " + << currentFloor.getNumber() << endl; + + } // end if + +} // end function closeDoor + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/door.h b/Bachelor/Prog1/examples/elevator/door.h new file mode 100644 index 0000000..a15a5d1 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/door.h @@ -0,0 +1,40 @@ +// Fig. 7.35: door.h +// Door class definition. +#ifndef DOOR_H +#define DOOR_H + +class Person; // forward declaration +class Floor; // forward declaration +class Elevator; // forward declaration + +class Door { + +public: + Door(); // constructor + ~Door(); // destructor + + void openDoor( Person * const, // opens door + Person * const, Floor &, Elevator & ); + void closeDoor( const Floor & ); // closes door + +private: + bool open; // open or closed + +}; + +#endif // DOOR_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/elevator.cpp b/Bachelor/Prog1/examples/elevator/elevator.cpp new file mode 100644 index 0000000..1f271c1 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevator.cpp @@ -0,0 +1,231 @@ +// Fig. 7.42: elevator.cpp +// Member-function definitions for class Elevator. +#include + +using std::cout; +using std::endl; + +#include "elevator.h" // Elevator class definition +#include "person.h" // Person class definition +#include "floor.h" // Floor class definition + +// constants that represent time required to travel +// between floors and directions of the elevator +const int Elevator::ELEVATOR_TRAVEL_TIME = 5; +const int Elevator::UP = 0; +const int Elevator::DOWN = 1; + +// constructor +Elevator::Elevator( Floor &firstFloor, Floor &secondFloor ) + : elevatorButton( *this ), + currentBuildingClockTime( 0 ), + moving( false ), + direction( UP ), + currentFloor( Floor::FLOOR1 ), + arrivalTime( 0 ), + floor1NeedsService( false ), + floor2NeedsService( false ), + floor1Ref( firstFloor ), + floor2Ref( secondFloor ), + passengerPtr( 0 ) +{ + cout << "elevator constructed" << endl; + +} // end Elevator constructor + +// destructor +Elevator::~Elevator() +{ + delete passengerPtr; + cout << "elevator destructed" << endl; + +} // end ~Elevator destructor + +// give time to elevator +void Elevator::processTime( int time ) +{ + currentBuildingClockTime = time; + + if ( moving ) // elevator is moving + processPossibleArrival(); + + else // elevator is not moving + processPossibleDeparture(); + + if ( !moving ) + cout << "elevator at rest on floor " + << currentFloor << endl; + +} // end function processTime + +// when elevator is moving, determine if it should stop +void Elevator::processPossibleArrival() +{ + // if elevator arrives at destination floor + if ( currentBuildingClockTime == arrivalTime ) { + + currentFloor = // update current floor + ( currentFloor == Floor::FLOOR1 ? + Floor::FLOOR2 : Floor::FLOOR1 ); + + direction = // update direction + ( currentFloor == Floor::FLOOR1 ? UP : DOWN ); + + cout << "elevator arrives on floor " + << currentFloor << endl; + + // process arrival at currentFloor + arriveAtFloor( currentFloor == Floor::FLOOR1 ? + floor1Ref : floor2Ref ); + + return; + + } // end if + + // elevator still moving + cout << "elevator moving " + << ( direction == UP ? "up" : "down" ) << endl; + +} // end function processPossibleArrival + +// determine whether elevator should move +void Elevator::processPossibleDeparture() +{ + // this floor needs service? + bool currentFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor1NeedsService : floor2NeedsService; + + // other floor needs service? + bool otherFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor2NeedsService : floor1NeedsService; + + // service this floor (if needed) + if ( currentFloorNeedsService ) { + arriveAtFloor( currentFloor == Floor::FLOOR1 ? + floor1Ref : floor2Ref ); + + return; + } + + // service other floor (if needed) + if ( otherFloorNeedsService ) + prepareToLeave( true ); + +} // end function processPossibleDeparture + +// arrive at a particular floor +void Elevator::arriveAtFloor( Floor& arrivalFloor ) +{ + moving = false; // reset state + + cout << "elevator resets its button" << endl; + elevatorButton.resetButton(); + + bell.ringBell(); + + // notify floor that elevator has arrived + Person *floorPersonPtr = arrivalFloor.elevatorArrived(); + + door.openDoor( + passengerPtr, floorPersonPtr, arrivalFloor, *this ); + + // this floor needs service? + bool currentFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor1NeedsService : floor2NeedsService; + + // other floor needs service? + bool otherFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor2NeedsService : floor1NeedsService; + + // if this floor does not need service + // prepare to leave for the other floor + if ( !currentFloorNeedsService ) + prepareToLeave( otherFloorNeedsService ); + + else // otherwise, reset service flag + currentFloor == Floor::FLOOR1 ? + floor1NeedsService = false: floor2NeedsService = false; + +} // end function arriveAtFloor + +// request service from elevator +void Elevator::summonElevator( int floor ) +{ + // set appropriate servicing flag + floor == Floor::FLOOR1 ? + floor1NeedsService = true : floor2NeedsService = true; + +} // end function summonElevator + +// accept a passenger +void Elevator::passengerEnters( Person * const personPtr ) +{ + // board passenger + passengerPtr = personPtr; + + cout << "person " << passengerPtr->getID() + << " enters elevator from floor " + << currentFloor << endl; + +} // end function passengerEnters + +// notify elevator that passenger is exiting +void Elevator::passengerExits() +{ + passengerPtr = 0; + +} // end function passengerExits + +// prepare to leave a floor +void Elevator::prepareToLeave( bool leaving ) +{ + // get reference to current floor + Floor &thisFloor = + currentFloor == Floor::FLOOR1 ? floor1Ref : floor2Ref; + + // notify floor that elevator may be leaving + thisFloor.elevatorLeaving(); + + door.closeDoor( thisFloor ); + + if ( leaving ) // leave, if necessary + move(); + +} // end function prepareToLeave + +// go to other floor +void Elevator::move() +{ + moving = true; // change state + + // schedule arrival time + arrivalTime = currentBuildingClockTime + + ELEVATOR_TRAVEL_TIME; + + cout << "elevator begins moving " + << ( direction == DOWN ? "down " : "up " ) + << "to floor " + << ( direction == DOWN ? '1' : '2' ) + << " (arrives at time " << arrivalTime << ')' + << endl; + +} // end function move + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/elevator.h b/Bachelor/Prog1/examples/elevator/elevator.h new file mode 100644 index 0000000..e032607 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevator.h @@ -0,0 +1,75 @@ +// Fig. 7.41: elevator.h +// Elevator class definition. +#ifndef ELEVATOR_H +#define ELEVATOR_H + +#include "elevatorButton.h" +#include "door.h" +#include "bell.h" + +class Floor; // forward declaration +class Person; // forward declaration + +class Elevator { + +public: + Elevator( Floor &, Floor & ); // constructor + ~Elevator(); // destructor + void summonElevator( int ); // request to service floor + void prepareToLeave( bool ); // prepare to leave + void processTime( int ); // give current time to elevator + void passengerEnters( Person * const ); // board a passenger + void passengerExits(); // exit a passenger + + // public object accessible to client code with + // access to Elevator object + ElevatorButton elevatorButton; + +private: + + // utility functions + void processPossibleArrival(); + void processPossibleDeparture(); + void arriveAtFloor( Floor & ); + void move(); + + // static constants that represent time required to travel + // between floors and directions of the elevator + static const int ELEVATOR_TRAVEL_TIME; + static const int UP; + static const int DOWN; + + // data members + int currentBuildingClockTime; // current time + bool moving; // elevator state + int direction; // current direction + int currentFloor; // current location + int arrivalTime; // time to arrive at a floor + bool floor1NeedsService; // floor1 service flag + bool floor2NeedsService; // floor2 service flag + + Floor &floor1Ref; // reference to floor1 + Floor &floor2Ref; // reference to floor2 + Person *passengerPtr; // pointer to passenger + + Door door; // door object + Bell bell; // bell object + +}; // end class Elevator + +#endif // ELEVATOR_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/elevatorButton.cpp b/Bachelor/Prog1/examples/elevator/elevatorButton.cpp new file mode 100644 index 0000000..f1e3a51 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevatorButton.cpp @@ -0,0 +1,57 @@ +// Fig. 7.38: elevatorButton.cpp: +// Member-function definitions for class ElevatorButton. +#include + +using std::cout; +using std::endl; + +#include "elevatorButton.h" // ElevatorButton class definition +#include "elevator.h" // Elevator class definition + +// constructor +ElevatorButton::ElevatorButton( Elevator &elevatorHandle ) + : pressed( false ), + elevatorRef( elevatorHandle ) +{ + cout << "elevator button constructed" << endl; + +} // end ElevatorButton constructor + +// destructor +ElevatorButton::~ElevatorButton() +{ + cout << "elevator button destructed" << endl; + +} // end ~ElevatorButton destructor + +// press button and signal elevator to prepare to leave floor +void ElevatorButton::pressButton() +{ + pressed = true; + cout << "elevator button tells elevator to prepare to leave" + << endl; + elevatorRef.prepareToLeave( true ); + +} // end function pressButton + +// reset button +void ElevatorButton::resetButton() +{ + pressed = false; + +} // end function resetButton + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/elevatorButton.h b/Bachelor/Prog1/examples/elevator/elevatorButton.h new file mode 100644 index 0000000..7b2c5be --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/elevatorButton.h @@ -0,0 +1,40 @@ +// Fig. 7.37: elevatorButton.h +// ElevatorButton class definition. +#ifndef ELEVATORBUTTON_H +#define ELEVATORBUTTON_H + +class Elevator; // forward declaration + +class ElevatorButton { + +public: + ElevatorButton( Elevator & ); // constructor + ~ElevatorButton(); // destructor + + void pressButton(); // press the button + void resetButton(); // reset the button + +private: + bool pressed; // state of button + + // reference to elevator containing this button + Elevator &elevatorRef; + +}; // end class ElevatorButton + +#endif // ELEVATORBUTTON_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/floor.cpp b/Bachelor/Prog1/examples/elevator/floor.cpp new file mode 100644 index 0000000..c651095 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floor.cpp @@ -0,0 +1,98 @@ +// Fig. 7.44: floor.cpp +// Member-function definitions for class Floor. +#include + +using std::cout; +using std::endl; + +#include "floor.h" // Floor class definition +#include "person.h" // Person class definition +#include "elevator.h" // Elevator class definition +#include "door.h" // Door class definition + +// static constants that represent the floor numbers +const int Floor::FLOOR1 = 1; +const int Floor::FLOOR2 = 2; + +// constructor +Floor::Floor(int number, Elevator &elevatorHandle ) + : floorButton( number, elevatorHandle ), + floorNumber( number ), + elevatorRef( elevatorHandle ), + occupantPtr ( 0 ), + light( floorNumber ) +{ + cout << "floor " << floorNumber << " constructed" << endl; + +} // end Floor constructor + +// destructor +Floor::~Floor() +{ + delete occupantPtr; + cout << "floor " << floorNumber << " destructed" << endl; + +} // end ~Floor destructor + +// determine whether floor is occupied +bool Floor::isOccupied() const +{ + return ( occupantPtr != 0 ); + +} // end function isOccupied + +// return this floor's number +int Floor::getNumber() const +{ + return floorNumber; + +} // end function getNumber + +// person arrives on floor +void Floor::personArrives( Person * const personPtr ) +{ + occupantPtr = personPtr; + +} // end function personArrives + +// notify floor that elevator has arrived +Person *Floor::elevatorArrived() +{ + cout << "floor " << floorNumber + << " resets its button" << endl; + + floorButton.resetButton(); + light.turnOn(); + + return occupantPtr; + +} // end function elevatorArrived + +// tell floor that elevator is leaving +void Floor::elevatorLeaving() +{ + light.turnOff(); + +} // end function elevatorLeaving + +// notifies floor that person is leaving +void Floor::personBoardingElevator() +{ + occupantPtr = 0; // person no longer on floor + +} // end function personBoardingElevator + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/floor.h b/Bachelor/Prog1/examples/elevator/floor.h new file mode 100644 index 0000000..6ead42a --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floor.h @@ -0,0 +1,63 @@ +// Fig. 7.43: floor.h +// Floor class definition. +#ifndef FLOOR_H +#define FLOOR_H + +#include "floorButton.h" +#include "light.h" + +class Elevator; // forward declaration +class Person; // forward declaration + +class Floor { + +public: + Floor( int, Elevator & ); // constructor + ~Floor(); // destructor + bool isOccupied() const; // return true if floor occupied + int getNumber() const; // return floor's number + + // pass a handle to new person coming on floor + void personArrives( Person * const ); + + // notify floor that elevator has arrived + Person *elevatorArrived(); + + // notify floor that elevator is leaving + void elevatorLeaving(); + + // notify floor that person is leaving floor + void personBoardingElevator(); + + // static constants representing floor numbers + static const int FLOOR1; + static const int FLOOR2; + + // public FloorButton object accessible to + // any client code with access to a Floor + FloorButton floorButton; + +private: + const int floorNumber; // the floor's number + Elevator &elevatorRef; // reference to elevator + Person *occupantPtr; // pointer to person on floor + Light light; // light object + +}; // end class Floor + +#endif // FLOOR_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/floorButton.cpp b/Bachelor/Prog1/examples/elevator/floorButton.cpp new file mode 100644 index 0000000..6d19769 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floorButton.cpp @@ -0,0 +1,62 @@ +// Fig. 7.40: floorButton.cpp +// Member-function definitions for class FloorButton. +#include + +using std::cout; +using std::endl; + +#include "floorButton.h" +#include "elevator.h" + +// constructor +FloorButton::FloorButton( int floor, Elevator &elevatorHandle ) + : floorNumber( floor ), + pressed( false ), + elevatorRef( elevatorHandle ) +{ + cout << "floor " << floorNumber << " button constructed" + << endl; + +} // end FloorButton constructor + +// destructor +FloorButton::~FloorButton() +{ + cout << "floor " << floorNumber << " button destructed" + << endl; + +} // end ~FloorButton destructor + +// press the button +void FloorButton::pressButton() +{ + pressed = true; + cout << "floor " << floorNumber + << " button summons elevator" << endl; + + // call elevator to this floor + elevatorRef.summonElevator( floorNumber ); + +} // end function pressButton + +// reset button +void FloorButton::resetButton() +{ + pressed = false; + +} // end function resetButton + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/floorButton.h b/Bachelor/Prog1/examples/elevator/floorButton.h new file mode 100644 index 0000000..b09c6e5 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/floorButton.h @@ -0,0 +1,42 @@ +// Fig. 7.39: floorButton.h +// FloorButton class definition. +#ifndef FLOORBUTTON_H +#define FLOORBUTTON_H + +class Elevator; // forward declaration + +class FloorButton { + +public: + FloorButton( int, Elevator & ); // constructor + ~FloorButton(); // destructor + + void pressButton(); // press the button + void resetButton(); // reset the button + +private: + const int floorNumber; // button's floor number + bool pressed; // button state + + // reference to elevator used to summon + // elevator to floor + Elevator &elevatorRef; + +}; // end class FloorButton + +#endif // FLOORBUTTON_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/light.cpp b/Bachelor/Prog1/examples/elevator/light.cpp new file mode 100644 index 0000000..1a9eb79 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/light.cpp @@ -0,0 +1,65 @@ +// Fig. 7.34: light.cpp +// Member-function definitions for class Light. +#include + +using std::cout; +using std::endl; + +#include "light.h" // Light class definition + +// constructor +Light::Light( int number ) + : on( false ), + floorNumber( number ) +{ + cout << "floor " << floorNumber << " light constructed" + << endl; + +} // end Light constructor + +// destuctor +Light::~Light() +{ + cout << "floor " << floorNumber + << " light destructed" << endl; + +} // end ~Light destructor + +// turn light on +void Light::turnOn() +{ + if ( !on ) { // if light not on, turn it on + on = true; + cout << "floor " << floorNumber + << " light turns on" << endl; + + } // end if + +} // end function turnOn + +// turn light off +void Light::turnOff() +{ + if ( on ) { // if light is on, turn it off + on = false; + cout << "floor " << floorNumber + << " light turns off" << endl; + + } // end if + +} // end function turnOff + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/light.h b/Bachelor/Prog1/examples/elevator/light.h new file mode 100644 index 0000000..88e920d --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/light.h @@ -0,0 +1,36 @@ +// Fig. 7.33: light.h +// Light class definition. +#ifndef LIGHT_H +#define LIGHT_H + +class Light { + +public: + Light( int ); // constructor + ~Light(); // destructor + + void turnOn(); // turns light on + void turnOff(); // turns light off + +private: + bool on; // true if on; false if off + const int floorNumber; // floor number that contains light + +}; // end class Light + +#endif // LIGHT_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/person.cpp b/Bachelor/Prog1/examples/elevator/person.cpp new file mode 100644 index 0000000..72e070d --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/person.cpp @@ -0,0 +1,91 @@ +// Fig. 7.46: person.cpp +// Member-function definitions for class Person. +#include + +using std::cout; +using std::endl; + +#include "person.h" // Person class definition +#include "floor.h" // Floor class definition +#include "elevator.h" // Elevator class definition + +// initialize static member personCount +int Person::personCount = 0; + +// constructor +Person::Person( int destFloor ) + : ID( ++personCount ), + destinationFloor( destFloor ) +{ + cout << "person " << ID << " constructed" << endl; + +} // end Person constructor + +// destructor +Person::~Person() +{ + cout << "(person " << ID << " destructor invoked)" << endl; + +} // end ~Person destructor + +// return person's ID number +int Person::getID() const +{ + return ID; + +} // end function getID + +// person walks onto a floor +void Person::stepOntoFloor( Floor& floor ) +{ + // notify floor person is coming + cout << "person " << ID << " steps onto floor " + << floor.getNumber() << endl; + floor.personArrives( this ); + + // press button on floor + cout << "person " << ID + << " presses floor button on floor " + << floor.getNumber() << endl; + floor.floorButton.pressButton(); + +} // end function stepOntoFloor + +// person enters elevator +void Person::enterElevator( Elevator &elevator, Floor &floor ) +{ + floor.personBoardingElevator(); // person leaves floor + + elevator.passengerEnters( this ); // person enters elevator + + // press button on elevator + cout << "person " << ID + << " presses elevator button" << endl; + elevator.elevatorButton.pressButton(); + +} // end function enterElevator + +// person exits elevator +void Person::exitElevator( + const Floor &floor, Elevator &elevator ) const +{ + cout << "person " << ID << " exits elevator on floor " + << floor.getNumber() << endl; + elevator.passengerExits(); + +} // end function exitElevator + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/person.h b/Bachelor/Prog1/examples/elevator/person.h new file mode 100644 index 0000000..7a6f10c --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/person.h @@ -0,0 +1,42 @@ +// Fig. 7.45: person.h +// Person class definition. +#ifndef PERSON_H +#define PERSON_H + +class Floor; // forward declaration +class Elevator; // forward declaration + +class Person { + +public: + Person( int ); // constructor + ~Person(); // destructor + int getID() const; // returns person's ID + + void stepOntoFloor( Floor & ); + void enterElevator( Elevator &, Floor & ); + void exitElevator( const Floor &, Elevator & ) const; + +private: + static int personCount; // total number of people + const int ID; // person's unique ID # + const int destinationFloor; // destination floor # + +}; // end class Person + +#endif // PERSON_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/scheduler.cpp b/Bachelor/Prog1/examples/elevator/scheduler.cpp new file mode 100644 index 0000000..73bdebb --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/scheduler.cpp @@ -0,0 +1,134 @@ +// Fig. 7.30: scheduler.cpp +// Member-function definitions for class Scheduler. +#include + +using std::cout; +using std::endl; + +#include +#include +#include + +#include "scheduler.h" // Scheduler class definition +#include "floor.h" // Floor class definition +#include "person.h" // Person class definition + +// constructor +Scheduler::Scheduler( Floor &firstFloor, Floor &secondFloor ) + : currentClockTime( 0 ), + floor1Ref( firstFloor ), + floor2Ref( secondFloor ) +{ + srand( time( 0 ) ); // seed random number generator + cout << "scheduler constructed" << endl; + + // schedule first arrivals for floor 1 and floor 2 + scheduleTime( floor1Ref ); + scheduleTime( floor2Ref ); + +} // end Scheduler constructor + +// destructor +Scheduler::~Scheduler() +{ + cout << "scheduler destructed" << endl; + +} // end Scheduler destructor + +// schedule arrival on a floor +void Scheduler::scheduleTime( const Floor &floor ) +{ + int floorNumber = floor.getNumber(); + int arrivalTime = currentClockTime + ( 5 + rand() % 16 ); + + floorNumber == Floor::FLOOR1 ? + floor1ArrivalTime = arrivalTime : + floor2ArrivalTime = arrivalTime; + + cout << "(scheduler schedules next person for floor " + << floorNumber << " at time " << arrivalTime << ')' + << endl; + +} // end function scheduleTime + +// reschedule arrival on a floor +void Scheduler::delayTime( const Floor &floor ) +{ + int floorNumber = floor.getNumber(); + + int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? + ++floor1ArrivalTime : ++floor2ArrivalTime; + + cout << "(scheduler delays next person for floor " + << floorNumber << " until time " << arrivalTime << ')' + << endl; + +} // end function delayTime + +// give time to scheduler +void Scheduler::processTime( int time ) +{ + currentClockTime = time; // record time + + // handle arrivals on floor 1 + handleArrivals( floor1Ref, currentClockTime ); + + // handle arrivals on floor 2 + handleArrivals( floor2Ref, currentClockTime ); + +} // end function processTime + +// create new person and place it on specified floor +void Scheduler::createNewPerson( Floor &floor ) +{ + int destinationFloor = + floor.getNumber() == Floor::FLOOR1 ? + Floor::FLOOR2 : Floor::FLOOR1; + + // create new person + Person *newPersonPtr = new Person( destinationFloor ); + + cout << "scheduler creates person " + << newPersonPtr->getID() << endl; + + // place person on proper floor + newPersonPtr->stepOntoFloor( floor ); + + scheduleTime( floor ); // schedule next arrival + +} // end function createNewPerson + +// handle arrivals for a specified floor +void Scheduler::handleArrivals( Floor &floor, int time ) +{ + int floorNumber = floor.getNumber(); + + int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? + floor1ArrivalTime : floor2ArrivalTime; + + if ( arrivalTime == time ) { + + if ( floor.isOccupied() ) // if floor occupied, + delayTime( floor ); // delay arrival + + else // otherwise, + createNewPerson( floor ); // create new person + + } // end outer if + +} // end function handleArrivals + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/elevator/scheduler.h b/Bachelor/Prog1/examples/elevator/scheduler.h new file mode 100644 index 0000000..a035d08 --- /dev/null +++ b/Bachelor/Prog1/examples/elevator/scheduler.h @@ -0,0 +1,53 @@ +// Fig. 7.29: scheduler.h +// Scheduler class definition. +#ifndef SCHEDULER_H +#define SCHEDULER_H + +class Floor; // forward declaration + +class Scheduler { + +public: + Scheduler( Floor &, Floor & ); // constructor + ~Scheduler(); // destructor + void processTime( int ); // set scheduler's time + +private: + // schedule arrival to a floor + void scheduleTime( const Floor & ); + + // delay arrival to a floor + void delayTime( const Floor & ); + + // create new person; place on floor + void createNewPerson( Floor & ); + + // handle person arrival on a floor + void handleArrivals( Floor &, int ); + + int currentClockTime; + + Floor &floor1Ref; + Floor &floor2Ref; + + int floor1ArrivalTime; + int floor2ArrivalTime; + +}; // end class Scheduler + +#endif // SCHEDULER_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ -- cgit v1.2.3