😏1. 知识介绍
BMI
(Body Mass Index,身体质量指数),也称为体重指数,是一种常用的衡量成人人体肥胖程度的指标。它通过身高和体重之间的数值关系来评估一个人的体重是否适中。
BMI的计算公式如下:
BMI = 体重(kg)/ (身高(m) * 身高(m))
根据计算得到的BMI值,可以将人体的体重状况分为以下几个范围:
BMI < 18.5:体重过轻
18.5 <= BMI < 24:体重正常
24 <= BMI < 28:超重
BMI >= 28:肥胖
虽然BMI是一种常用的评估指标,但它仅仅是通过身高和体重的比值来进行估算,并没有考虑到体脂肪和肌肉的比例以及身体的其他组成。因此,在评估个体的体重状况时,还应考虑其他因素,如体脂肪百分比、腰围、腰臀比等。
😊2. c++终端程序
#include <iostream> using namespace std;
double calculateBMI(double weight, double height) {
// 身高单位为米,体重单位为千克
double bmi = weight / (height * height);
return bmi;
}void interpretBMI(double bmi) {
if (bmi < 18.5) {
cout << "体重过轻" << endl;
} else if (bmi >= 18.5 && bmi < 24) {
cout << "正常范围" << endl;
} else if (bmi >= 24 && bmi < 28) {
cout << "体重过重" << endl;
} else if (bmi >= 28 && bmi < 32) {
cout << "肥胖" << endl;
} else {
cout << "非常肥胖" << endl;
}
}int main() {
double weight, height;cout << "请输入体重(千克):"; cin >> weight; cout << "请输入身高(米):"; cin >> height; double bmi = calculateBMI(weight, height); cout << "你的BMI指数为:" << bmi << endl; interpretBMI(bmi); return 0;
}
😆3. qt界面程序
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QDebug>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();private slots:
/**
* @brief on_pb_calculate_clicked
* 自动创建的信号槽
*/
void on_pb_calculate_clicked();/** * @brief printBMI * 手动创建的信号槽 */ void printBMI(); /** * @brief calculateBMI * @param weight * @param height * @return double */ double calculateBMI(double weight, double height); /** * @brief interpretBMI * @param bmi */ void interpretBMI(double bmi);
private:
Ui::MainWindow *ui;double weight, height;
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);qDebug() << "start..."; /* 终端输出计算值 */ double mybmi = calculateBMI(80, 1.73); qDebug() << "terminal print: " << mybmi; connect(ui->pb_calculate_2, SIGNAL(clicked()), this, SLOT(printBMI()));
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pb_calculate_clicked()
{
QString text = "Hello, BMI Tool! Auto created!";
ui->tb_display->append(text);weight = ui->le_weight->text().toDouble(); height = ui->le_height->text().toDouble(); double bmi = weight / (height * height); QString strBMI = QVariant(bmi).toString(); ui->tb_display->append(strBMI); interpretBMI(bmi);
}
void MainWindow::printBMI()
{
QString text = "Hello, BMI Tool! Manual created!";
ui->tb_display->append(text);weight = ui->le_weight->text().toDouble(); height = ui->le_height->text().toDouble(); double bmi = calculateBMI(weight, height); QString strBMI = QVariant(bmi).toString(); ui->tb_display->append(strBMI); interpretBMI(bmi);
}
double MainWindow::calculateBMI(double weight, double height)
{
double bmi = weight / (height * height);
return bmi;
}
void MainWindow::interpretBMI(double bmi)
{
if (bmi < 18.5) {
ui->tb_display->append("体重过轻");
} else if (bmi >= 18.5 && bmi < 24) {
ui->tb_display->append("正常范围");
} else if (bmi >= 24 && bmi < 28) {
ui->tb_display->append("体重过重");
} else if (bmi >= 28 && bmi < 32) {
ui->tb_display->append("肥胖");
} else {
ui->tb_display->append("非常肥胖");
}
}