差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
fltk:example-app-bmi_calc [2025/05/26 19:32] – Add source directory tree visual freemikanfltk:example-app-bmi_calc [2025/06/12 11:46] (現在) – [スクリーンショット] Remove imagebox freemikan
行 1: 行 1:
-====== サンプルアプリケーション: BMI計算機 ====== +====== : BMI計算機 ====== 
-{{:fltk:fltk_shadow.png?200|FLTK}}+[[fltk:|{{:fltk:fltk_shadow.png?200|}}]]
  
  
 ===== スクリーンショット ===== ===== スクリーンショット =====
 {{ :fltk:example-app-bmi_calc.jpg |BMI計算機}} {{ :fltk:example-app-bmi_calc.jpg |BMI計算機}}
- 
  
 ===== プロジェクトレイアウト ===== ===== プロジェクトレイアウト =====
行 48: 行 47:
 #include <cerrno> #include <cerrno>
 #include <cstdlib> #include <cstdlib>
 +#include <limits>
 #include <optional> #include <optional>
  
-Fl_Input *height_input+Fl_Input *height_input_
-Fl_Input *weight_input+Fl_Input *weight_input_
-Fl_Output *result_output;+Fl_Output *result_output_;
  
 std::optional<double> double_value(Fl_Input const *input) { std::optional<double> double_value(Fl_Input const *input) {
行 63: 行 63:
   errno = 0;   errno = 0;
  
-  // NOTE: We also can use std::stod with std::string instead of std::strtod.+  // NOTE: We can also use std::stod with std::string instead of std::strtod.
  
-  if (double value = std::strtod(value_str, &end_str);+  if (double const value = std::strtod(value_str, &end_str);
       *end_str == '\0' && errno == 0) {       *end_str == '\0' && errno == 0) {
     return value;  // Successfully converted to double value     return value;  // Successfully converted to double value
行 73: 行 73:
  
 void calc_button_cb(Fl_Widget *w, void *data) { void calc_button_cb(Fl_Widget *w, void *data) {
-  auto h_cm = double_value(height_input); +  auto h_cm = double_value(height_input_); 
-  auto w_kg = double_value(weight_input);+  auto w_kg = double_value(weight_input_);
  
   if (h_cm.has_value() && w_kg.has_value()) {   if (h_cm.has_value() && w_kg.has_value()) {
-    double h_m = *h_cm / 100; +    double const h_m = *h_cm / 100; 
-    double bmi = *w_kg / (h_m * h_m); + 
-    result_output->value(bmi); +    // Avoid zero divisions 
-    result_output->textcolor(FL_FOREGROUND_COLOR); +    if (double const h_m_2 = h_m * h_m
-  } else { +        h_m_2 > std::numeric_limits<double>::epsilon()) { 
-    result_output->value("Invalid input.")+      double const bmi = *w_kg / h_m_2
-    result_output->textcolor(FL_RED);+      result_output_->value(bmi); 
 +      result_output_->textcolor(FL_FOREGROUND_COLOR); 
 +      return
 +    }
   }   }
 +  result_output_->value("Invalid input.");
 +  result_output_->textcolor(FL_RED);
 } }
  
 int main(int argc, char **argv) { int main(int argc, char **argv) {
   auto window = new Fl_Window(230, 160, "BMI Calculator");   auto window = new Fl_Window(230, 160, "BMI Calculator");
-  height_input = new Fl_Input(100, 10, 100, 30, "Height (cm)"); +  height_input_ = new Fl_Input(100, 10, 100, 30, "Height (cm)"); 
-  weight_input = new Fl_Input(100, 40, 100, 30, "Weight (kg)");+  weight_input_ = new Fl_Input(100, 40, 100, 30, "Weight (kg)");
   auto calc_button = new Fl_Return_Button(20, 80, 180, 30, "Calculate");   auto calc_button = new Fl_Return_Button(20, 80, 180, 30, "Calculate");
-  result_output = new Fl_Output(100, 120, 100, 30, "BMI");+  result_output_ = new Fl_Output(100, 120, 100, 30, "BMI");
  
   calc_button->callback(calc_button_cb);   calc_button->callback(calc_button_cb);
文書の先頭へ