July 24, 2016 : BMI Functions for the Medical Calculator
© 安岡喜晴 (JoyShine)

I added BMI functions to the Medical Calculator.

BMI is just weight divided by height squared. The formula itself is one line.
All the rest is machinery for calling that line safely.

There is an array that lists nothing but names and types, and the screen is built from it.
The calculating side holds nothing between calls, it just takes numbers and returns text.

static const CalcInput BMI_UN_CM_KG_INPUTS[] = {
    { L"Enter Height (centimeters)", INPUT_NUMBER, NULL },
    { L"Enter Weight (kilograms)",   INPUT_NUMBER, NULL },
};
inline void calc_bmi_universal_cm_kg(const double* in,
                                     wchar_t* disp, size_t dsz,
                                     wchar_t* pri,  size_t psz,
                                     wchar_t* det,  size_t esz) {
    double h = in[0], w = in[1];
    if (h <= 0 || w <= 0) {
        swprintf(disp, dsz, L"Error");
        swprintf(pri, psz, L"Height (cm) and weight (kg) must be > 0.");
        det[0] = 0; (void)esz; return;
    }
    double h2 = pow(h / 100.0, 2);
    double bmi  = (h2 > 0) ? w / h2 : 0;
    double bmip = bmi / 25.0;
    wchar_t b[32], bp[32];
    fmt_g(b, 32, bmi); fmt_g(bp, 32, bmip);
    swprintf(disp, dsz, L"%ls", b);
    swprintf(pri, psz,
        L"BMI       : %ls  (%ls)\r\n"
        L"BMI PRIME : %ls  (%ls)",
        b, bmi_zone_universal(bmi),
        bp, bmi_prime_zone(bmip));

    int zi;
    if      (bmi < 15.0) zi = 0;
    else if (bmi < 16.0) zi = 1;
    else if (bmi < 18.5) zi = 2;
    else if (bmi < 25.0) zi = 3;
    else if (bmi < 30.0) zi = 4;
    else if (bmi < 35.0) zi = 5;
    else if (bmi < 40.0) zi = 6;
    else                 zi = 7;
    const wchar_t* m[8]; for (int i = 0; i < 8; i++) m[i] = L""; m[zi] = HL;
    wchar_t z0[32], z1v[32], z2v[32], z3v[32], z4v[32], z5v[32], z6v[32], z7v[32];
    fmt_g(z0, 32, h2*15); fmt_g(z1v, 32, h2*16); fmt_g(z2v, 32, h2*18.5);
    fmt_g(z3v, 32, h2*25); fmt_g(z4v, 32, h2*30); fmt_g(z5v, 32, h2*35);
    fmt_g(z6v, 32, h2*40); fmt_g(z7v, 32, h2*40);
    swprintf(det, esz,
        L"%lsvery severely underweight zone < %lskg\r\n"
        L"%ls%lskg <= severely underweight zone < %lskg\r\n"
        L"%ls%lskg <= underweight zone < %lskg\r\n"
        L"%ls%lskg <= normal weight zone < %lskg\r\n"
        L"%ls%lskg <= overweight zone < %lskg\r\n"
        L"%ls%lskg <= moderately obese zone < %lskg\r\n"
        L"%ls%lskg <= severely obese zone < %lskg\r\n"
        L"%ls%lskg <= very severely obese zone",
        m[0], z0,
        m[1], z0, z1v,
        m[2], z1v, z2v,
        m[3], z2v, z3v,
        m[4], z3v, z4v,
        m[5], z4v, z5v,
        m[6], z5v, z6v,
        m[7], z7v);
}

Home  |  Profile  |  Software  |  Medicine  |  Diary  |  Photos  |  Contact