// See https://aka.ms/new-console-template for more information


using System.Text.RegularExpressions;

string str = getcontents();
Console.WriteLine(str);
double weight = 68.56;
double height = 172;
int age = 18;
int adc = 600;
int sex = 0;//1-男,0-女
var data = GetBodyfatResults(weight, height/100, age, adc, sex);
var data1 = GetBodyfatResultsV1(weight, height / 100, age, adc, sex);
Console.WriteLine($"阻抗{adc}");
Console.WriteLine($"BMI:{data.bmi},{data1.bmi}");
Console.WriteLine($"骨量,{data.bm},{data1.bm}");
Console.WriteLine($"肌肉率:{data.rom},{data1.rom}");
Console.WriteLine($"水分,{data.moi},{data1.moi}");
Console.WriteLine($"脂肪率,{data.bfr},{data1.bfr}");
Console.WriteLine($"皮下脂肪率,{data.sfr},{data1.sfr}");
Console.WriteLine($"骨骼肌率,{data.rosm},{data1.rosm}");
Console.WriteLine($"蛋白率,{data.pp},{data1.pp}");
Console.WriteLine($"内脏脂肪指数,{data.uvi},{data1.uvi}");
Console.WriteLine($"基础代谢,{data.bmr},{data1.bmr}");
Console.WriteLine($"身体年龄,{data.physicAge},{data1.physicAge}");
Console.WriteLine("____________________");
Console.ReadLine();

/// <summary>
/// 获取关键字
/// </summary>
static string getkeywords()
{
    string val = "签到结果:{{thing1.DATA}}\n签到次数:{{thing2.DATA}}\n签到奖励:{{thing3.DATA}}\n";
    var reg = new Regex(@"[\w.]+[DATA]");
    var mathes = reg.Matches(val).AsQueryable();
    string str = string.Empty;
    foreach(var item in mathes)
    {
        str += item.ToString().Replace(".DATA", "") + "|";
    }
    str = str.Substring(0, str.Length - 1);
    return str;
}
/// <summary>
/// 获取内容
/// </summary>
static string getcontents()
{
    string val = "签到结果:{{thing1.DATA}}\n签到次数:{{thing2.DATA}}\n签到奖励:{{thing3.DATA}}\n";
    var reg = new Regex(@"[\w]+[:{]");
    var mathes = reg.Matches(val).AsQueryable();
    string str = string.Empty;
    foreach (var item in mathes)
    {
        str += item.ToString().Replace(":", "") + "|";
    }
    str = str.Substring(0, str.Length - 1);
    return str;
}
/// <summary>
/// 计算算法
/// </summary>
/// <param name="weight"></param>
/// <param name="height"></param>
/// <param name="age"></param>
/// <param name="adc"></param>
/// <param name="sex"></param>
/// <returns></returns>
static BodyfatItem GetBodyfatResultsV1(double weight, double height, int age, int adc, int sex)
{
    BodyfatItem item = new BodyfatItem();
    double mheight = height;//米为单位的身高
    if ((weight <= 0.0) || (weight > 220.0) || (height <= 0.0) || (height > 270.0) || (age <= 0) || (age > 120) || (adc <= 0) || (adc > 0x3e8) || (sex < 0) || (sex > 1))
    {
        object[] arg = new object[] { weight, height, age, adc, sex };
        if (weight != 0 && height != 0)
            item.bmi = (((double)((int)((weight / (mheight * mheight)) * 10.0))) / 10.0).ToString("0.0");
        return item;
    }
    double bmi = (int)(weight / (mheight * mheight) * 10.0) / 10.0;
    double bm = 0.0;//骨量
    double rom = 0.0;//肌肉率
    double moi = 0.0;//水份
    double bfr = 0.0;//脂肪率
    double sfr = 0.0;//皮下脂肪率
    double rosm = 0.0;//骨骼肌率
    double pp = 0.0;//蛋白率
    double uvi = 0.0;//内脏脂肪指数
    double bmr = 0.0;//基础代谢率
    double physicAge = 0.0;//身体年龄
    if (sex == 1)
    {
        bm = (0.015 * weight) + (((2.0 - (0.00055 * adc)) * height) / 100.0) + 1.15;
        rom = (-((0.00115 * adc) + 0.01) * weight) + (((49.64 - (0.031 * adc)) * height) / 100.0) + (adc * 0.08) + (age * 0.04) + 15.4;
        moi = (0xf_4240 / (bmi * ((2.688 * adc) - 78.28))) - (0x274a / adc) - (0.22 * age) + 52.6;
        bfr = (-930_000.0 / bmi / ((1.966 * adc) - 58.46)) + (0x3378 / adc) - (0.06 * age) + 40.0;
        sfr = 0.898 * bfr;
        rosm = 0.895 * moi;
        pp = 0.8 * (((100.0 - bfr) - moi) - (bm / weight));
        uvi = (0.304 * weight) + (25.58 * height / 100.0) + (0.131 * age) + (0.005 * adc) - 0x16;
        //bmr = (((((9.0 + (0.0015 * adc)) * weight) + (((0x546 - (0.88 * adc)) * height) / 100.0)) + (0xbc / age)) + (0.748 * adc)) - 0x41d;
        bmr = 370 + 21.6 * weight * (1 - bfr / 100);
        physicAge = (age * (1.0 + (0.012 * (bmi - 1.0)))) - 0x15 + ((30 - age) * 0.35) + ((adc - 450) * 0.02) + 11.0;
    }
    else
    {
        bm = (2.2E-05 * adc * weight) + ((4.99 - (0.00284 * adc)) * height / 100.0) + (0.0012 * adc) + 1.15;
        rom = (-((0.00115 * adc) + 0.01) * weight) + (((49.64 - (0.031 * adc)) * height) / 100.0) + (adc * 0.08) + (age * 0.04) + 6.0;
        moi = (0xf_4240 / (bmi * ((2.467 * adc) - 75.37))) - (0x3787 / adc) - (0.034 * age) + 43.2;
        bfr = (-3030000.0 / (bmi + 20.0) / ((1.966 * adc) - 58.46)) + (28176 / adc) - (0.06 * age) + 51;
        sfr = (0.876 * bfr) + 1.66;
        rosm = (0.857 * moi) - 0.36;
        pp = 0.75 * ((100.0 - bfr) - moi - (bm / weight));
        uvi = (0.304 * weight) + (25.58 * height / 100.0) + (0.131 * age) + (0.005 * adc) - 0x16;
        bmr = 370 + 21.6 * weight * (1 - bfr / 100);
        //bmr = ((((((0.00307 * adc) + 1.5) * weight) + (((0x5b3 - (0.989 * adc)) * height) / 100.0)) + (age * 0.9)) + (0.923 * adc)) - 950.0;
        physicAge = (age * (0.95 + (0.02 * (bmi - 21.2)))) + ((adc - 500) * 0.02);
    }
    item.bmi = bmi.ToString("0.0");
    bm = (bm > (weight * 0.15)) ? (weight * 0.15) : bm;
    item.bm = ((bm < (weight * 0.02)) ? (weight * 0.02) : bm).ToString("0.0");
    rom = (rom > 0x4b) ? 0x4b : rom;
    item.rom = ((rom < 15.0) ? 15.0 : rom).ToString("0.0");
    moi = (moi > 70.0) ? 70.0 : moi;
    item.moi = ((moi < 20.0) ? 20.0 : moi).ToString("0.0");
    bfr = (bfr > 50.0) ? 50.0 : bfr;
    item.bfr = ((bfr < 5.0) ? 5.0 : bfr).ToString("0.0");
    item.sfr = sfr.ToString("0.0");
    item.rosm = rosm.ToString("0.0");
    pp = (pp > 50.0) ? 50.0 : pp;
    item.pp = ((pp < 10.0) ? 10.0 : pp).ToString("0.0");
    uvi = (uvi > 20.0) ? 20.0 : uvi;
    item.uvi = ((uvi < 1.0) ? 1.0 : uvi).ToString("0");
    item.bmr = bmr.ToString("0.0");
    if (age < 0x12)
    {
        physicAge = age;
    }
    else
    {
        physicAge = (physicAge > (age + 10)) ? age + 10 : physicAge;
        physicAge = (physicAge < (age - 10)) ? age - 10 : physicAge;
    }
    item.physicAge = physicAge.ToString("0");
    item.weight = weight;
    item.height = height;
    item.age = age;
    item.adc = adc;
    item.sex = sex;
    return item;
}


/// <summary>
/// 计算算法
/// </summary>
/// <param name="weight"></param>
/// <param name="height"></param>
/// <param name="age"></param>
/// <param name="adc"></param>
/// <param name="sex"></param>
/// <returns></returns>
static BodyfatItem GetBodyfatResults(double weight, double height, int age, int adc, int sex)
{
    BodyfatItem bodyfatItem = new BodyfatItem();
    double mheight = height;//米为单位的身高
    if ((weight <= 0.0) || (weight > 220.0) || (height <= 0.0) || (height > 270.0) || (age <= 0) || (age > 120) || (adc <= 0) || (adc > 0x3e8) || (sex < 0) || (sex > 1))
    {
        object[] arg = new object[] { weight, height, age, adc, sex };
        if (weight != 0 && height != 0)
            bodyfatItem.bmi = (((double)((int)((weight / (mheight * mheight)) * 10.0))) / 10.0).ToString("0.0");
        return bodyfatItem;
    }
    double num = (double)(int)(weight / (mheight * mheight) * 10.0) / 10.0;
    double num2 = 0.0;
    double num3 = 0.0;
    double num4 = 0.0;
    double num5 = 0.0;
    double num6 = 0.0;
    double num7 = 0.0;
    double num8 = 0.0;
    double num9 = 0.0;
    double num10 = 0.0;
    double num11 = 0.0;
    if (sex == 1)
    {
        num2 = 0.015 * weight + (2.0 - 0.00055 * (double)adc) * height / 100.0 - 1.15;
        num3 = (0.0 - (0.00115 * (double)adc + 0.01)) * weight + (49.64 - 0.031 * (double)adc) * height / 100.0 + (double)adc * 0.08 + (double)age * 0.04 + 15.4;
        num4 = 1000000.0 / (num * (2.688 * (double)adc - 78.28)) - (double)(10058 / adc) - 0.22 * (double)age + 52.6;
        num5 = -930000.0 / num / (1.966 * (double)adc - 58.46) + (double)(13176 / adc) - 0.06 * (double)age + 40.0;
        num6 = 0.898 * num5;
        num7 = 0.895 * num4;
        num8 = 0.8 * (100.0 - num5 - num4 - num2 / weight);
        num9 = 0.304 * weight - 25.58 * height / 100.0 + 0.131 * (double)age + 0.005 * (double)adc + 22.0;
        num10 = (9.0 + 0.0015 * (double)adc) * weight + (1350.0 - 0.88 * (double)adc) * height / 100.0 + (double)(188 / age) + 0.748 * (double)adc - 1053.0;
        num11 = (double)age * (1.0 + 0.012 * (num - 1.0)) - 21.0 + (double)(30 - age) * 0.35 + (double)(adc - 450) * 0.02 + 11.0;
    }
    else
    {
        num2 = 2.2E-05 * (double)adc * weight + (4.99 - 0.00284 * (double)adc) * height / 100.0 + 0.0012 * (double)adc - 4.45;
        num3 = (0.0 - (0.00115 * (double)adc + 0.01)) * weight + (49.64 - 0.031 * (double)adc) * height / 100.0 + (double)adc * 0.08 + (double)age * 0.04 + 6.0;
        num4 = 1000000.0 / (num * (2.467 * (double)adc - 75.37)) - (double)(14215 / adc) - 0.034 * (double)age + 43.2;
        num5 = -3030000.0 / (num + 20.0) / (1.966 * (double)adc - 58.46) + (double)(28176 / adc) - 0.06 * (double)age + 51.0;
        num6 = 0.876 * num5 + 1.66;
        num7 = 0.857 * num4 - 0.36;
        num8 = 0.75 * (100.0 - num5 - num4 - num2 / weight);
        num9 = 0.304 * weight - 25.58 * height / 100.0 + 0.131 * (double)age + 0.005 * (double)adc + 22.0;
        num10 = (0.00307 * (double)adc + 1.5) * weight + (1459.0 - 0.989 * (double)adc) * height / 100.0 + (double)age * 0.9 + 0.923 * (double)adc - 950.0;
        num11 = (double)age * (0.95 + 0.02 * (num - 21.2)) + (double)(adc - 500) * 0.02;
    }
    bodyfatItem.bmi = num.ToString("0.0");
    num2 = ((num2 > weight * 0.15) ? (weight * 0.15) : num2);
    bodyfatItem.bm = ((num2 < weight * 0.02) ? (weight * 0.02) : num2).ToString("0.0");
    num3 = ((num3 > 75.0) ? 75.0 : num3);
    bodyfatItem.rom = ((num3 < 15.0) ? 15.0 : num3).ToString("0.0");
    num4 = ((num4 > 70.0) ? 70.0 : num4);
    bodyfatItem.moi = ((num4 < 20.0) ? 20.0 : num4).ToString("0.0");
    num5 = ((num5 > 50.0) ? 50.0 : num5);
    bodyfatItem.bfr = ((num5 < 5.0) ? 5.0 : num5).ToString("0.0");
    bodyfatItem.sfr = num6 <= 0 ? "0" : num6.ToString("0.0");
    bodyfatItem.rosm = num7.ToString("0.0");
    num8 = ((num8 > 50.0) ? 50.0 : num8);
    bodyfatItem.pp = ((num8 < 10.0) ? 10.0 : num8).ToString("0.0");
    num9 = ((num9 > 20.0) ? 20.0 : num9);
    bodyfatItem.uvi = ((num9 < 1.0) ? 1.0 : num9).ToString("0");
    bodyfatItem.bmr = num10 <= 0 ? "0" : num10.ToString("0");
    if (age < 18)
    {
        num11 = age;
    }
    else
    {
        num11 = ((num11 > (double)(age + 10)) ? ((double)(age + 10)) : num11);
        num11 = ((num11 < (double)(age - 10)) ? ((double)(age - 10)) : num11);
    }
    bodyfatItem.physicAge = num11.ToString("0");
    bodyfatItem.weight = weight;
    bodyfatItem.height = height;
    bodyfatItem.age = age;
    bodyfatItem.adc = adc;
    bodyfatItem.sex = sex;
    return bodyfatItem;
}
/// <summary>
/// 算法返回结果定义
/// </summary>
class BodyfatItem
{
    /// <summary>
    /// bmi
    /// </summary>
    public string bmi;
    /// <summary>
    /// 骨量
    /// </summary>
    public string bm;
    /// <summary>
    /// 肌肉率
    /// </summary>
    public string rom;
    /// <summary>
    /// 水分
    /// </summary>
    public string moi;
    /// <summary>
    /// 脂肪率
    /// </summary>
    public string bfr;
    /// <summary>
    /// 皮下脂肪率
    /// </summary>
    public string sfr;
    /// <summary>
    /// 骨骼肌率
    /// </summary>
    public string rosm;
    /// <summary>
    /// 蛋白率
    /// </summary>
    public string pp;
    /// <summary>
    /// 内脏脂肪指数
    /// </summary>
    public string uvi;
    /// <summary>
    /// 基础代谢率
    /// </summary>
    public string bmr;
    /// <summary>
    /// 身体年龄
    /// </summary>
    public string physicAge;
    /// <summary>
    /// 体重
    /// </summary>
    public double weight;
    /// <summary>
    /// 身高
    /// </summary>
    public double height;
    /// <summary>
    /// 年龄
    /// </summary>
    public int age;
    /// <summary>
    /// 阻抗
    /// </summary>
    public int adc;
    /// <summary>
    /// 性别,0-女,1-男
    /// </summary>
    public int sex;
}