Skip to content

Commit c873a50

Browse files
committed
added exercise09 to chapter13
1 parent 92bbfe4 commit c873a50

17 files changed

+2670
-0
lines changed

Chapter13/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,6 @@ Elaborate the bar graph class to allow labeling of the graph itself and its indi
138138

139139
## [Exercise 8](exercises/08)
140140
Here is a collection of heights in centimeters together with the number of people in a group of that height (rounded to the nearest 5cm): (170,7, (175,9), (180,23), (185,17), (190,6), 195,1). How would you graph that data? If you can't think of anything better, do a bar graph. Remember to provide axes and labels. Place the data in a file and read it from that file.
141+
142+
## [Exercise 9](exercises/09)
143+
Find another data set of heights (an inch is 2.54cm) and graph them with your program from the previous exercise. For example, search the Web for "height distribution" or "height of people in the United States" and ignore a lot of rubbish or ask your friends for their heights. Ideally, you don't have to change anything for the new data set. Calculating the scaling from the data is a key idea. Reading in labels from input also helps minimize changes when you want to reuse code.

Chapter13/exercises/09/CMakeLists.txt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(Programming_Qt VERSION 0.1 LANGUAGES CXX)
4+
5+
set(CMAKE_AUTOUIC ON)
6+
set(CMAKE_AUTOMOC ON)
7+
set(CMAKE_AUTORCC ON)
8+
9+
set(CMAKE_CXX_STANDARD 20)
10+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11+
12+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
13+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
14+
15+
set(PROJECT_SOURCES
16+
main.cpp
17+
PPP/Window.h
18+
PPP/Graph.h
19+
PPP/GUI.h
20+
PPP/Simple_window.h
21+
PPP/Point.h
22+
PPP/std_lib_facilities.h
23+
Window.cpp
24+
Graph.cpp
25+
PPP/Image_private.h
26+
GUI.cpp
27+
PPP/GUI_private.h
28+
Simple_window.cpp
29+
Colormap.cpp
30+
)
31+
32+
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
33+
qt_add_executable(Programming_Qt
34+
MANUAL_FINALIZATION
35+
${PROJECT_SOURCES}
36+
data.txt
37+
)
38+
39+
# Define target properties for Android with Qt 6 as:
40+
# set_property(TARGET Programming_Qt APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
41+
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
42+
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
43+
else()
44+
if(ANDROID)
45+
add_library(Programming_Qt SHARED
46+
${PROJECT_SOURCES}
47+
)
48+
# Define properties for Android with Qt 5 after find_package() calls as:
49+
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
50+
else()
51+
add_executable(Programming_Qt
52+
${PROJECT_SOURCES}
53+
)
54+
endif()
55+
endif()
56+
57+
target_link_libraries(Programming_Qt PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
58+
target_include_directories(Programming_Qt PRIVATE .)
59+
60+
set_target_properties(Programming_Qt PROPERTIES
61+
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
62+
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
63+
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
64+
MACOSX_BUNDLE TRUE
65+
WIN32_EXECUTABLE TRUE
66+
)
67+
68+
if(MSVC)
69+
target_compile_options(Programming_Qt PRIVATE /EHsc /W4 /WX)
70+
else()
71+
target_compile_options(Programming_Qt PRIVATE -Wall -Wextra -Wpedantic -Werror -O3)
72+
endif()
73+
74+
if (EMSCRIPTEN)
75+
target_link_options(Programming_Qt PUBLIC -sASYNCIFY -Os)
76+
endif()
77+
78+
install(TARGETS Programming_Qt
79+
BUNDLE DESTINATION .
80+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
81+
82+
if(QT_VERSION_MAJOR EQUAL 6)
83+
qt_finalize_executable(Programming_Qt)
84+
endif()

Chapter13/exercises/09/Colormap.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <QColor>
2+
#include <PPP/Image_private.h>
3+
4+
static QColor paletteColorMap[]
5+
= {{0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0}, {0, 0, 255},
6+
{255, 0, 255}, {0, 255, 255}, {255, 255, 255}, {85, 85, 85}, {198, 113, 113},
7+
{113, 198, 113}, {142, 142, 56}, {113, 113, 198}, {142, 56, 142}, {56, 142, 142},
8+
{0, 0, 128}, {168, 168, 152}, {232, 232, 216}, {104, 104, 88}, {152, 168, 168},
9+
{216, 232, 232}, {88, 104, 104}, {156, 156, 168}, {220, 220, 232}, {92, 92, 104},
10+
{156, 168, 156}, {220, 232, 220}, {92, 104, 92}, {144, 144, 144}, {192, 192, 192},
11+
{80, 80, 80}, {160, 160, 160}, {0, 0, 0}, {13, 13, 13}, {26, 26, 26},
12+
{38, 38, 38}, {49, 49, 49}, {61, 61, 61}, {72, 72, 72}, {85, 85, 85},
13+
{95, 95, 95}, {106, 106, 106}, {117, 117, 117}, {128, 128, 128}, {138, 138, 138},
14+
{149, 149, 149}, {160, 160, 160}, {170, 170, 170}, {181, 181, 181}, {192, 192, 192},
15+
{203, 203, 203}, {213, 213, 213}, {224, 224, 224}, {234, 234, 234}, {245, 245, 245},
16+
{255, 255, 255}, {0, 0, 0}, {0, 36, 0}, {0, 72, 0}, {0, 109, 0},
17+
{0, 145, 0}, {0, 182, 0}, {0, 218, 0}, {0, 255, 0}, {63, 0, 0},
18+
{63, 36, 0}, {63, 72, 0}, {63, 109, 0}, {63, 145, 0}, {63, 182, 0},
19+
{63, 218, 0}, {63, 255, 0}, {127, 0, 0}, {127, 36, 0}, {127, 72, 0},
20+
{127, 109, 0}, {127, 145, 0}, {127, 182, 0}, {127, 218, 0}, {127, 255, 0},
21+
{191, 0, 0}, {191, 36, 0}, {191, 72, 0}, {191, 109, 0}, {191, 145, 0},
22+
{191, 182, 0}, {191, 218, 0}, {191, 255, 0}, {255, 0, 0}, {255, 36, 0},
23+
{255, 72, 0}, {255, 109, 0}, {255, 145, 0}, {255, 182, 0}, {255, 218, 0},
24+
{255, 255, 0}, {0, 0, 63}, {0, 36, 63}, {0, 72, 63}, {0, 109, 63},
25+
{0, 145, 63}, {0, 182, 63}, {0, 218, 63}, {0, 255, 63}, {63, 0, 63},
26+
{63, 36, 63}, {63, 72, 63}, {63, 109, 63}, {63, 145, 63}, {63, 182, 63},
27+
{63, 218, 63}, {63, 255, 63}, {127, 0, 63}, {127, 36, 63}, {127, 72, 63},
28+
{127, 109, 63}, {127, 145, 63}, {127, 182, 63}, {127, 218, 63}, {127, 255, 63},
29+
{191, 0, 63}, {191, 36, 63}, {191, 72, 63}, {191, 109, 63}, {191, 145, 63},
30+
{191, 182, 63}, {191, 218, 63}, {191, 255, 63}, {255, 0, 63}, {255, 36, 63},
31+
{255, 72, 63}, {255, 109, 63}, {255, 145, 63}, {255, 182, 63}, {255, 218, 63},
32+
{255, 255, 63}, {0, 0, 127}, {0, 36, 127}, {0, 72, 127}, {0, 109, 127},
33+
{0, 145, 127}, {0, 182, 127}, {0, 218, 127}, {0, 255, 127}, {63, 0, 127},
34+
{63, 36, 127}, {63, 72, 127}, {63, 109, 127}, {63, 145, 127}, {63, 182, 127},
35+
{63, 218, 127}, {63, 255, 127}, {127, 0, 127}, {127, 36, 127}, {127, 72, 127},
36+
{127, 109, 127}, {127, 145, 127}, {127, 182, 127}, {127, 218, 127}, {127, 255, 127},
37+
{191, 0, 127}, {191, 36, 127}, {191, 72, 127}, {191, 109, 127}, {191, 145, 127},
38+
{191, 182, 127}, {191, 218, 127}, {191, 255, 127}, {255, 0, 127}, {255, 36, 127},
39+
{255, 72, 127}, {255, 109, 127}, {255, 145, 127}, {255, 182, 127}, {255, 218, 127},
40+
{255, 255, 127}, {0, 0, 191}, {0, 36, 191}, {0, 72, 191}, {0, 109, 191},
41+
{0, 145, 191}, {0, 182, 191}, {0, 218, 191}, {0, 255, 191}, {63, 0, 191},
42+
{63, 36, 191}, {63, 72, 191}, {63, 109, 191}, {63, 145, 191}, {63, 182, 191},
43+
{63, 218, 191}, {63, 255, 191}, {127, 0, 191}, {127, 36, 191}, {127, 72, 191},
44+
{127, 109, 191}, {127, 145, 191}, {127, 182, 191}, {127, 218, 191}, {127, 255, 191},
45+
{191, 0, 191}, {191, 36, 191}, {191, 72, 191}, {191, 109, 191}, {191, 145, 191},
46+
{191, 182, 191}, {191, 218, 191}, {191, 255, 191}, {255, 0, 191}, {255, 36, 191},
47+
{255, 72, 191}, {255, 109, 191}, {255, 145, 191}, {255, 182, 191}, {255, 218, 191},
48+
{255, 255, 191}, {0, 0, 255}, {0, 36, 255}, {0, 72, 255}, {0, 109, 255},
49+
{0, 145, 255}, {0, 182, 255}, {0, 218, 255}, {0, 255, 255}, {63, 0, 255},
50+
{63, 36, 255}, {63, 72, 255}, {63, 109, 255}, {63, 145, 255}, {63, 182, 255},
51+
{63, 218, 255}, {63, 255, 255}, {127, 0, 255}, {127, 36, 255}, {127, 72, 255},
52+
{127, 109, 255}, {127, 145, 255}, {127, 182, 255}, {127, 218, 255}, {127, 255, 255},
53+
{191, 0, 255}, {191, 36, 255}, {191, 72, 255}, {191, 109, 255}, {191, 145, 255},
54+
{191, 182, 255}, {191, 218, 255}, {191, 255, 255}, {255, 0, 255}, {255, 36, 255},
55+
{255, 72, 255}, {255, 109, 255}, {255, 145, 255}, {255, 182, 255}, {255, 218, 255},
56+
{255, 255, 255}};
57+
58+
namespace Graph_lib {
59+
QColor mapPaletteColor(int rawColor)
60+
{
61+
return paletteColorMap[rawColor % 256];
62+
}
63+
64+
} // namespace Graph_lib

0 commit comments

Comments
 (0)