Salute e malattia
Se sei appassionato di salute e tecnologia, potresti essere interessato a calcolare il tuo Indice di Massa Corporea (BMI) usando codice C. Il BMI è calcolato usando il rapporto altezza-peso e a volte tiene conto di età e sesso. Secondo il Weight-control Information Network, un BMI alto può metterti a maggior rischio per certi tipi di cancro, malattie coronariche, malattie cistifellea, osteoartrite, complicazioni gravidanza. BMI 18,5-24,9 è considerato più sano, 25-29,9 e ≥30 sono sovrappeso e obeso.
Configura il tuo codice C++ BMI su Windows o Linux/Unix.
Vai su www.arachnoid.com per configurare il codice C++ BMI su Windows.
Vai alla pagina tutorial C++ di www.arachnoid.com per setup su Unix.
Esegui le righe di codice 1-59 sulla piattaforma appropriata:
01 using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Linq;
07 using System.Text;
08 using System.Windows.Forms;
09
10 namespace BMIApp
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void btnCalculate_Click(object sender, EventArgs e)
20 {
21 double weght = string.IsNullOrEmpty(txtWeight.Text) ? 1 : double.Parse(txtWeight.Text);
22 double height = string.IsNullOrEmpty(txtHeight.Text) ? 1 : double.Parse(txtHeight.Text);
23
24 if (weght == 0)
25 {
26 MessageBox.Show("Results will be inaccurate. Weight is not a valid number.");
27 }
28 if (height == 0)
29 {
30 MessageBox.Show("Results will be inaccurate. Height is not a valid number.");
31 }
32
33 double vmult = cboWeightUnits.SelectedItem.ToString() == "pounds" ? 2.204 : 1;
34 double hmult = cboHeightUnits.SelectedItem.ToString() == "inches" ? 0.0254 : 1;
35
36 double BMI = Math.Round(((weght / vmult) / ((height * hmult) * (height * hmult))) * 10) / 10;
37
38 string BMI_description = string.Empty;
39 if (BMI < 16.5)
40 BMI_description = "severely underweight";
41 else if (BMI >= 16.5 && BMI < 18.5)
42 BMI_description = "underweight";
43 else if (BMI >= 18.5 && BMI < 25)
44 BMI_description = "normal";
45 else if (BMI >= 25 && BMI <= 30)
46 BMI_description = "overweight";
47 else if (BMI > 30 && BMI <= 35)
48 BMI_description = "obese";
49 else if (BMI > 35 && BMI <= 40)
50 BMI_description = "clinically obese";
51 else
52 BMI_description = "morbidly obese";
53
54 txtResult.Text = string.Format("Your Body Mass Index (BMI) is: {0}. This would be considered {1}.", BMI, BMI_description);
55 }
56 }
57 }Prendi il tuo peso in libbre e inseriscilo nella casella peso del calcolatore BMI C++.
Misura la tua altezza in pollici e inseriscila nella casella altezza.
Determina se la tua classe di peso è sana, sovrappeso o obesa, basandoti sull'output del calcolatore.
Nota: Il codice mostrato è C# (Windows Forms), non C/C++. Per C++ standard, servirebbe main(), iostream, logica calcolo BMI = peso(kg)/altezza(m)². Esempio C++ moderno:
#include <iostream>
#include <cmath>
int main() {
double peso_kg, altezza_m;
std::cout << "Peso (kg): "; std::cin >> peso_kg;
std::cout << "Altezza (m): "; std::cin >> altezza_m;
double bmi = peso_kg / (altezza_m * altezza_m);
std::cout << "BMI: " << bmi << std::endl;
return 0;
}
Controllo del peso