├── .gitignore ├── LICENSE ├── Project ├── Report.pdf ├── code │ ├── data_input │ │ └── data_input.cpp │ ├── data_prediction │ │ ├── Aotizhongxin_arg_prediction.csv │ │ ├── Aotizhongxin_linear_prediction.csv │ │ ├── Aotizhongxin_nn_prediction.csv │ │ ├── data_prediction_argumentssample.cpp │ │ ├── data_prediction_linear.cpp │ │ └── data_prediction_nn.cpp │ ├── data_selection │ │ └── data_selection.cpp │ ├── data_visualization │ │ ├── data_visualization.cbp │ │ ├── data_visualization.depend │ │ ├── data_visualization.layout │ │ └── main.cpp │ └── headfiles │ │ ├── GUI │ │ ├── Chart.hpp │ │ ├── GUI.cpp │ │ ├── GUI.h │ │ ├── Graph.cpp │ │ ├── Graph.h │ │ ├── Point.h │ │ ├── Simple_window.cpp │ │ ├── Simple_window.h │ │ ├── Window.cpp │ │ ├── Window.h │ │ └── std_lib_facilities.h │ │ ├── data_structure.hpp │ │ ├── exceptions.hpp │ │ ├── matrix.hpp │ │ ├── nn │ │ ├── data_manager.hpp │ │ └── nn.hpp │ │ ├── prediction.hpp │ │ ├── random.hpp │ │ └── tools.hpp ├── data │ ├── Aotizhongxin.dat │ ├── Aotizhongxin_pre.dat │ ├── PRSA_Data_Aotizhongxin_20130301-20170228.csv │ ├── PRSA_Data_Changping_20130301-20170228.csv │ ├── PRSA_Data_Dingling_20130301-20170228.csv │ ├── PRSA_Data_Dongsi_20130301-20170228.csv │ ├── PRSA_Data_Guanyuan_20130301-20170228.csv │ ├── PRSA_Data_Gucheng_20130301-20170228.csv │ ├── PRSA_Data_Huairou_20130301-20170228.csv │ ├── PRSA_Data_Nongzhanguan_20130301-20170228.csv │ ├── PRSA_Data_Shunyi_20130301-20170228.csv │ ├── PRSA_Data_Tiantan_20130301-20170228.csv │ ├── PRSA_Data_Wanliu_20130301-20170228.csv │ ├── PRSA_Data_Wanshouxigong_20130301-20170228.csv │ └── readme.txt ├── report │ ├── 1.jpg │ ├── Report.aux │ ├── Report.bbl │ ├── Report.blg │ ├── Report.log │ ├── Report.pdf │ ├── Report.synctex.gz │ ├── Report.tex │ ├── Report.tex.bak │ ├── Report.tex.sav │ ├── pic1.png │ ├── pic2.png │ ├── pic3.png │ └── pic4.png └── requirement │ └── Project.pptx ├── README.md └── Weekly-homework ├── calculator ├── exception.hpp ├── expression.hpp ├── main.cpp └── number.hpp ├── week01 ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week02 ├── Polynomial.hpp ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week03 ├── Gauss-Algorithm.cpp ├── LinearEquations.hpp ├── Newton-Algorithm.cpp ├── Polynomial.hpp ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week04 ├── Interpolation.hpp ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week05 ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week06 ├── NodeRecognition.hpp ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week07 ├── hw1 │ ├── GUI.cpp │ ├── GUI.h │ ├── Graph.cpp │ ├── Graph.h │ ├── Point.h │ ├── Simple_window.cpp │ ├── Simple_window.h │ ├── Window.cpp │ ├── Window.h │ ├── hw1.cbp │ ├── hw1.depend │ ├── hw1.layout │ ├── hyperelliptic.cpp │ ├── hyperelliptic.h │ ├── main.cpp │ └── std_lib_facilities.h ├── hw2 │ ├── GUI.cpp │ ├── GUI.h │ ├── Graph.cpp │ ├── Graph.h │ ├── Point.h │ ├── Simple_window.cpp │ ├── Simple_window.h │ ├── Window.cpp │ ├── Window.h │ ├── hw2.cbp │ ├── hw2.depend │ ├── hw2.layout │ ├── main.cpp │ ├── righttriangle.cpp │ ├── righttriangle.h │ └── std_lib_facilities.h ├── hw3 │ ├── Binary_tree.cpp │ ├── Binary_tree.h │ ├── GUI.cpp │ ├── GUI.h │ ├── Graph.cpp │ ├── Graph.h │ ├── Point.h │ ├── Simple_window.cpp │ ├── Simple_window.h │ ├── TriangleBinary_tree.cpp │ ├── TriangleBinary_tree.h │ ├── Window.cpp │ ├── Window.h │ ├── hw3.cbp │ ├── hw3.depend │ ├── hw3.layout │ ├── main.cpp │ └── std_lib_facilities.h └── solution.md ├── week08 ├── Code_Exercise_2.hpp ├── Token_Exercise_1.hpp ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week09 ├── hw1 │ ├── Austin.csv │ ├── BarChart.cpp │ ├── BarChart.h │ ├── GUI.cpp │ ├── GUI.h │ ├── Graph.cpp │ ├── Graph.h │ ├── NewYork.csv │ ├── Point.h │ ├── Simple_window.cpp │ ├── Simple_window.h │ ├── Window.cpp │ ├── Window.h │ ├── hw1.cbp │ ├── hw1.depend │ ├── hw1.layout │ ├── main.cpp │ └── std_lib_facilities.h ├── hw2 │ ├── AnalogClock.cpp │ ├── AnalogClock.h │ ├── GUI.cpp │ ├── GUI.h │ ├── Graph.cpp │ ├── Graph.h │ ├── Point.h │ ├── Simple_window.cpp │ ├── Simple_window.h │ ├── Window.cpp │ ├── Window.h │ ├── hw2.cbp │ ├── hw2.depend │ ├── hw2.layout │ ├── main.cpp │ └── std_lib_facilities.h └── solution.md ├── week10 ├── HuffmanCode.hpp ├── HuffmanWordCode.hpp ├── main.cpp ├── solution.md └── std_lib_facilities.h ├── week11 ├── main.cpp ├── solution.md └── std_lib_facilities.h └── week12 - Final └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /Project/Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Galaxies99/CS241-CourseData/24d5861afabe1a8580599e93d3ee2fa52bb4ea9b/Project/Report.pdf -------------------------------------------------------------------------------- /Project/code/data_input/data_input.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # include "../headfiles/data_structure.hpp" 6 | # include "../headfiles/exceptions.hpp" 7 | # include "../headfiles/tools.hpp" 8 | 9 | using namespace std; 10 | 11 | const int MAXN = 10000 + 5; 12 | 13 | string station_name, s; 14 | char *str; 15 | 16 | int main() { 17 | cout << "Please choose a station: \n"; 18 | for (int i = 0; i < 12; ++ i) cout << " " << (i + 1) << " - " << station_names[i] << endl; 19 | int id; cin >> id; 20 | if(id <= 0 || id > 12) { 21 | cout << "Input Error!\n"; 22 | return 1; 23 | } 24 | station_name = station_names[id - 1]; 25 | 26 | Pre_Record_List l(station_name); 27 | cout << "Reading data ...\n"; 28 | try { 29 | File_Oper f_in("../../data/PRSA_Data_" + station_name + "_20130301-20170228.csv", "r"); 30 | str = new char [MAXN]; 31 | // ignore the first line 32 | int line = 1; 33 | fscanf(f_in.fp, "%s", str); 34 | while(~ fscanf(f_in.fp, "%s", str)) 35 | l.rec.push_back(Pre_Record_Generator :: solver(static_cast (str))); 36 | delete []str; 37 | } catch(...) { 38 | cout << "Read file error! \n"; 39 | return 1; 40 | } 41 | cout << "Read data successfully!\n"; 42 | cout << "total count: " << l.rec.size() << " Pre_Record(s).\n"; 43 | cout << "\nStoring data ...\n"; 44 | int n = l.rec.size(); 45 | try { 46 | File_Oper f_out("../../data/" + station_name + "_pre.dat", "rb+"); 47 | f_out.writeFile(&n, 0, sizeof(int)); 48 | size_t offset = sizeof(int); 49 | for (int i = 0; i < n; ++ i) { 50 | f_out.writeFile(&l.rec[i], offset, sizeof(Pre_Record)); 51 | offset += sizeof(Pre_Record); 52 | } 53 | } catch(...) { 54 | cout << "Write file Error! \n"; 55 | return 1; 56 | } 57 | cout << "Store data as \"" << station_name << "_pre.dat\" successfully!\n"; 58 | cout << "total count: " << n << " Pre_Record(s).\n"; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Project/code/data_prediction/data_prediction_argumentssample.cpp: -------------------------------------------------------------------------------- 1 | # include "../headfiles/data_structure.hpp" 2 | # include "../headfiles/exceptions.hpp" 3 | # include "../headfiles/tools.hpp" 4 | # include "../headfiles/prediction.hpp" 5 | # include "../headfiles/matrix.hpp" 6 | 7 | # include 8 | # include 9 | # include 10 | # include 11 | # include 12 | 13 | using namespace std; 14 | 15 | string station_name; 16 | 17 | int main() { 18 | cout << "Please choose a station: \n"; 19 | for (int i = 0; i < 12; ++ i) cout << " " << (i + 1) << " - " << station_names[i] << endl; 20 | int id; cin >> id; 21 | if(id <= 0 || id > 12) { 22 | cout << "Input Error!\n"; 23 | return 1; 24 | } 25 | station_name = station_names[id - 1]; 26 | 27 | Record_List l(station_name); 28 | 29 | cout << "Reading data ...\n"; 30 | try { 31 | File_Oper f_in("../../data/" + station_name + ".dat", "rb+"); 32 | int n; f_in.readFile(&n, 0, sizeof(int)); 33 | size_t offset = sizeof(int); 34 | Record temp; 35 | for (int i = 0; i < n; ++ i) { 36 | f_in.readFile(&temp, offset, sizeof(Record)); 37 | l.rec.push_back(temp); 38 | offset += sizeof(Record); 39 | } 40 | } catch(...) { 41 | cout << "Read file error!\n"; 42 | return 1; 43 | } 44 | 45 | cout << "Read data from \"" << station_name << ".dat\" successfully!\n\n"; 46 | Data_Testing :: output_test(l); 47 | 48 | cout << "\n\nSelecting Training Data & Testing Data ...\n"; 49 | cout << " Randomly choose 90% of the data as training data, the rest are testing data.\n"; 50 | 51 | Record_List train_data(station_name), test_data(station_name); 52 | 53 | for (int i = 0; i < l.rec.size(); ++ i) { 54 | int rnd = rand() % 10; 55 | if(rnd != 0) train_data.rec.push_back(l.rec[i]); 56 | else test_data.rec.push_back(l.rec[i]); 57 | } 58 | 59 | cout << "Select successfully.\n"; 60 | cout << "Training data: "; 61 | Data_Testing :: output_test(train_data); 62 | cout << "Testing data: "; 63 | Data_Testing :: output_test(test_data); 64 | cout << "\n\n"; 65 | 66 | Data_Prediction_Model_ArgumentsSample :: solver(train_data, test_data); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Project/code/data_prediction/data_prediction_linear.cpp: -------------------------------------------------------------------------------- 1 | # include "../headfiles/data_structure.hpp" 2 | # include "../headfiles/exceptions.hpp" 3 | # include "../headfiles/tools.hpp" 4 | # include "../headfiles/prediction.hpp" 5 | # include "../headfiles/matrix.hpp" 6 | 7 | # include 8 | # include 9 | # include 10 | # include 11 | # include 12 | 13 | using namespace std; 14 | 15 | string station_name; 16 | 17 | int main() { 18 | cout << "Please choose a station: \n"; 19 | for (int i = 0; i < 12; ++ i) cout << " " << (i + 1) << " - " << station_names[i] << endl; 20 | int id; cin >> id; 21 | if(id <= 0 || id > 12) { 22 | cout << "Input Error!\n"; 23 | return 1; 24 | } 25 | station_name = station_names[id - 1]; 26 | 27 | Record_List l(station_name); 28 | 29 | cout << "Reading data ...\n"; 30 | try { 31 | File_Oper f_in("../../data/" + station_name + ".dat", "rb+"); 32 | int n; f_in.readFile(&n, 0, sizeof(int)); 33 | size_t offset = sizeof(int); 34 | Record temp; 35 | for (int i = 0; i < n; ++ i) { 36 | f_in.readFile(&temp, offset, sizeof(Record)); 37 | l.rec.push_back(temp); 38 | offset += sizeof(Record); 39 | } 40 | } catch(...) { 41 | cout << "Read file error!\n"; 42 | return 1; 43 | } 44 | 45 | cout << "Read data from \"" << station_name << ".dat\" successfully!\n\n"; 46 | Data_Testing :: output_test(l); 47 | 48 | cout << "\n\nSelecting Training Data & Testing Data ...\n"; 49 | cout << " Randomly choose 90% of the data as training data, the rest are testing data.\n"; 50 | 51 | Record_List train_data(station_name), test_data(station_name); 52 | 53 | for (int i = 0; i < l.rec.size(); ++ i) { 54 | int rnd = rand() % 10; 55 | if(rnd != 0) train_data.rec.push_back(l.rec[i]); 56 | else test_data.rec.push_back(l.rec[i]); 57 | } 58 | 59 | cout << "Select successfully.\n"; 60 | cout << "Training data: "; 61 | Data_Testing :: output_test(train_data); 62 | cout << "Testing data: "; 63 | Data_Testing :: output_test(test_data); 64 | cout << "\n\n"; 65 | 66 | Data_Prediction_LinearModel :: solver(train_data, test_data); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Project/code/data_selection/data_selection.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # include "../headfiles/data_structure.hpp" 6 | # include "../headfiles/exceptions.hpp" 7 | # include "../headfiles/tools.hpp" 8 | 9 | using namespace std; 10 | 11 | string station_name; 12 | 13 | int main() { 14 | cout << "Please choose a station: \n"; 15 | for (int i = 0; i < 12; ++ i) cout << " " << (i + 1) << " - " << station_names[i] << endl; 16 | int id; cin >> id; 17 | if(id <= 0 || id > 12) { 18 | cout << "Input Error!\n"; 19 | return 1; 20 | } 21 | station_name = station_names[id - 1]; 22 | 23 | cout << station_name << endl; 24 | 25 | Pre_Record_List pl(station_name); 26 | cout << "Reading data ...\n"; 27 | try { 28 | File_Oper f_in("../../data/" + station_name + "_pre.dat", "rb+"); 29 | int n; f_in.readFile(&n, 0, sizeof(int)); 30 | size_t offset = sizeof(int); 31 | Pre_Record temp; 32 | for (int i = 0; i < n; ++ i) { 33 | f_in.readFile(&temp, offset, sizeof(Pre_Record)); 34 | pl.rec.push_back(temp); 35 | offset += sizeof(Pre_Record); 36 | } 37 | } catch(...) { 38 | cout << "Read file error!\n"; 39 | return 1; 40 | } 41 | 42 | cout << "Read data from \"" << station_name << "_pre.dat\" successfully!\n\n"; 43 | Data_Testing :: output_test(pl); 44 | 45 | cout << "\nFiltering ...\n"; 46 | Record_List l(station_name); 47 | try { 48 | for (int i = 0; i < pl.rec.size(); ++ i) { 49 | pair r(Pre_Record_Filter :: filter(pl.rec[i])); 50 | if(r.second == true) l.rec.push_back(r.first); 51 | } 52 | } catch(...) { 53 | cout << "Filter error!\n"; 54 | return 1; 55 | } 56 | cout << "Filter successfully!\n\n"; 57 | 58 | Data_Testing :: output_test(l); 59 | 60 | cout << "\nStoring data ...\n"; 61 | int n = l.rec.size(); 62 | try { 63 | File_Oper f_out("../../data/" + station_name + ".dat", "rb+"); 64 | f_out.writeFile(&n, 0, sizeof(int)); 65 | size_t offset = sizeof(int); 66 | for (int i = 0; i < n; ++ i) { 67 | f_out.writeFile(&l.rec[i], offset, sizeof(Record)); 68 | offset += sizeof(Record); 69 | } 70 | } catch(...) { 71 | cout << "Write file Error! \n"; 72 | return 1; 73 | } 74 | cout << "Store data as \"" << station_name << ".dat\" Successfully!\n"; 75 | cout << "total count: " << n << " record(s).\n"; 76 | return 0; 77 | } 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Project/code/data_visualization/data_visualization.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 67 | 68 | -------------------------------------------------------------------------------- /Project/code/data_visualization/data_visualization.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Project/code/data_visualization/main.cpp: -------------------------------------------------------------------------------- 1 | # include "../headfiles/GUI/Simple_window.h" 2 | # include "../headfiles/GUI/Graph.h" 3 | # include "../headfiles/GUI/Chart.hpp" 4 | # include "../headfiles/GUI/std_lib_facilities.h" 5 | 6 | # include "../headfiles/data_structure.hpp" 7 | # include "../headfiles/exceptions.hpp" 8 | # include "../headfiles/tools.hpp" 9 | 10 | # include 11 | # include 12 | # include 13 | 14 | using namespace std; 15 | 16 | string station_name; 17 | 18 | int main() { 19 | cout << "Please choose a station: \n"; 20 | for (int i = 0; i < 12; ++ i) cout << " " << (i + 1) << " - " << station_names[i] << endl; 21 | int id; cin >> id; 22 | if(id <= 0 || id > 12) { 23 | cout << "Input Error!\n"; 24 | return 1; 25 | } 26 | station_name = station_names[id - 1]; 27 | 28 | Record_List l(station_name); 29 | 30 | cout << "Reading data ...\n"; 31 | try { 32 | File_Oper f_in("../../data/" + station_name + ".dat", "rb+"); 33 | int n; f_in.readFile(&n, 0, sizeof(int)); 34 | size_t offset = sizeof(int); 35 | Record temp; 36 | for (int i = 0; i < n; ++ i) { 37 | f_in.readFile(&temp, offset, sizeof(Record)); 38 | l.rec.push_back(temp); 39 | offset += sizeof(Record); 40 | } 41 | } catch(...) { 42 | cout << "Read file error!\n"; 43 | return 1; 44 | } 45 | 46 | cout << "Read data from \"" << station_name << ".dat\" successfully!\n\n"; 47 | Data_Testing :: output_test(l); 48 | 49 | int op; 50 | cout << endl << endl; 51 | try { 52 | cout << "Select data to visualize: \n"; 53 | for (int i = 1; i <= 12; ++ i) { 54 | cout << " " << i << " - " << op_to_string[i]; 55 | if(i == 11) cout << " (Pie Chart)"; 56 | else cout << " (Line Chart)"; 57 | cout << ";\n"; 58 | } 59 | for (int i = 1; i <= 12; ++ i) { 60 | cout << " " << i + 12 << " - " << op_to_string[i]; 61 | if(i == 11) cout << " (Radar Chart)"; 62 | else cout << " (Bar Chart)"; 63 | if(i == 12) cout << ".\n"; 64 | else cout << ";\n"; 65 | } 66 | cin >> op; 67 | if(op < 1 || op > 24) throw Option_Error(); 68 | } catch(...) { 69 | cout << "Option error!\n"; 70 | return 1; 71 | } 72 | 73 | if(op == 11 || op == 23) { 74 | cout << "Preparing & Constructing Data ... \n"; 75 | vData v; 76 | try { 77 | v = Visualization_Data_Constructing :: construct_wind_data(op, l); 78 | } catch(...) { 79 | cout << "Input Date Error or No Data!\n"; 80 | return 1; 81 | } 82 | 83 | int width = 700; 84 | Point tl(50, 50); 85 | Simple_window win(tl, width, 600, "Data Visualization"); 86 | 87 | Chart c(width, station_name, v); 88 | win.attach (c); 89 | 90 | win.wait_for_button(); 91 | } else { 92 | cout << "Preparing Data ... \n"; 93 | vector pv; 94 | try { 95 | pv = Visualization_Data_Constructing :: construct_pre_data(op, l); 96 | } catch(...) { 97 | cout << "Input Date Error!\n"; 98 | return 1; 99 | } 100 | 101 | vData v; 102 | cout << "Constructing figure ... \n"; 103 | try { 104 | v = Visualization_Data_Constructing :: construct_data(op, pv); 105 | } catch(...) { 106 | cout << "No Data!\n"; 107 | return 1; 108 | } 109 | 110 | cout << "\nFigure constructing completed! \n"; 111 | 112 | int width, height; 113 | if(op <= 12) width = max(static_cast (v.p.size()) * 3 + 200, 800); 114 | else width = 1100; 115 | Point tl(50, 50); 116 | Simple_window win(tl, width, 750, "Data Visualization"); 117 | 118 | Chart c(width, station_name, v); 119 | win.attach (c); 120 | 121 | win.wait_for_button(); 122 | } 123 | return 0; 124 | } 125 | -------------------------------------------------------------------------------- /Project/code/headfiles/GUI/GUI.cpp: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // This is a GUI support code to the chapters 12-16 of the book 4 | // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include "GUI.h" 11 | 12 | namespace Graph_lib { 13 | 14 | //------------------------------------------------------------------------------ 15 | 16 | void Button::attach(Window& win) 17 | { 18 | pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str()); 19 | pw->callback(reinterpret_cast(do_it), &win); // pass the window 20 | own = &win; 21 | } 22 | 23 | //------------------------------------------------------------------------------ 24 | 25 | int In_box::get_int() 26 | { 27 | Fl_Input& pi = reference_to(pw); 28 | // return atoi(pi.value()); 29 | const char* p = pi.value(); 30 | if (!isdigit(p[0])) return -999999; 31 | return atoi(p); 32 | } 33 | 34 | //------------------------------------------------------------------------------ 35 | 36 | void In_box::attach(Window& win) 37 | { 38 | pw = new Fl_Input(loc.x, loc.y, width, height, label.c_str()); 39 | own = &win; 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | 44 | void Out_box::put(const string& s) 45 | { 46 | reference_to(pw).value(s.c_str()); 47 | } 48 | 49 | //------------------------------------------------------------------------------ 50 | 51 | void Out_box::attach(Window& win) 52 | { 53 | pw = new Fl_Output(loc.x, loc.y, width, height, label.c_str()); 54 | own = &win; 55 | } 56 | 57 | //------------------------------------------------------------------------------ 58 | 59 | int Menu::attach(Button& b) 60 | { 61 | b.width = width; 62 | b.height = height; 63 | 64 | switch(k) { 65 | case horizontal: 66 | b.loc = Point(loc.x+offset,loc.y); 67 | offset+=b.width; 68 | break; 69 | case vertical: 70 | b.loc = Point(loc.x,loc.y+offset); 71 | offset+=b.height; 72 | break; 73 | } 74 | selection.push_back(b); // b is NOT OWNED: pass by reference 75 | return int(selection.size()-1); 76 | } 77 | 78 | //------------------------------------------------------------------------------ 79 | 80 | int Menu::attach(Button* p) 81 | { 82 | Button& b = *p; 83 | b.width = width; 84 | b.height = height; 85 | 86 | switch(k) { 87 | case horizontal: 88 | b.loc = Point(loc.x+offset,loc.y); 89 | offset+=b.width; 90 | break; 91 | case vertical: 92 | b.loc = Point(loc.x,loc.y+offset); 93 | offset+=b.height; 94 | break; 95 | } 96 | selection.push_back(&b); // b is OWNED: pass by pointer 97 | return int(selection.size()-1); 98 | } 99 | 100 | //------------------------------------------------------------------------------ 101 | 102 | }; // of namespace Graph_lib 103 | -------------------------------------------------------------------------------- /Project/code/headfiles/GUI/GUI.h: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // This is a GUI support code to the chapters 12-16 of the book 4 | // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup 5 | // 6 | 7 | #ifndef GUI_GUARD 8 | #define GUI_GUARD 9 | 10 | #include "Window.h" 11 | #include "Graph.h" 12 | 13 | namespace Graph_lib { 14 | 15 | //------------------------------------------------------------------------------ 16 | 17 | typedef void* Address; // Address is a synonym for void* 18 | typedef void(*Callback)(Address, Address); // FLTK's required function type for all callbacks 19 | 20 | //------------------------------------------------------------------------------ 21 | 22 | template W& reference_to(Address pw) 23 | // treat an address as a reference to a W 24 | { 25 | return *static_cast(pw); 26 | } 27 | 28 | //------------------------------------------------------------------------------ 29 | 30 | class Widget { 31 | // Widget is a handle to an Fl_widget - it is *not* an Fl_widget 32 | // We try to keep our interface classes at arm's length from FLTK 33 | public: 34 | Widget(Point xy, int w, int h, const string& s, Callback cb) 35 | : loc(xy), width(w), height(h), label(s), do_it(cb) 36 | {} 37 | 38 | virtual void move(int dx,int dy) { hide(); pw->position(loc.x+=dx, loc.y+=dy); show(); } 39 | virtual void hide() { pw->hide(); } 40 | virtual void show() { pw->show(); } 41 | virtual void attach(Window&) = 0; 42 | 43 | Point loc; 44 | int width; 45 | int height; 46 | string label; 47 | Callback do_it; 48 | 49 | virtual ~Widget() { } 50 | 51 | protected: 52 | Window* own; // every Widget belongs to a Window 53 | Fl_Widget* pw; // connection to the FLTK Widget 54 | private: 55 | Widget& operator=(const Widget&); // don't copy Widgets 56 | Widget(const Widget&); 57 | }; 58 | 59 | //------------------------------------------------------------------------------ 60 | 61 | struct Button : Widget { 62 | Button(Point xy, int w, int h, const string& label, Callback cb) 63 | : Widget(xy,w,h,label,cb) 64 | {} 65 | 66 | void attach(Window&); 67 | }; 68 | 69 | //------------------------------------------------------------------------------ 70 | 71 | struct In_box : Widget { 72 | In_box(Point xy, int w, int h, const string& s) 73 | :Widget(xy,w,h,s,0) { } 74 | int get_int(); 75 | string get_string(); 76 | 77 | void attach(Window& win); 78 | }; 79 | 80 | //------------------------------------------------------------------------------ 81 | 82 | struct Out_box : Widget { 83 | Out_box(Point xy, int w, int h, const string& s) 84 | :Widget(xy,w,h,s,0) { } 85 | void put(int); 86 | void put(const string&); 87 | 88 | void attach(Window& win); 89 | }; 90 | 91 | //------------------------------------------------------------------------------ 92 | 93 | struct Menu : Widget { 94 | enum Kind { horizontal, vertical }; 95 | Menu(Point xy, int w, int h, Kind kk, const string& label) 96 | : Widget(xy,w,h,label,0), k(kk), offset(0) 97 | {} 98 | 99 | Vector_ref