gpt4 book ai didi

C# 测量转换类

转载 作者:行者123 更新时间:2023-11-30 17:53:50 25 4
gpt4 key购买 nike

好吧,我一直在写一个厨房测量类,现在它只有体积或液体测量值,我这样调用它:

ktchnmsrmnts.VolumeMeasurements(number), current_measurement, to_measurement);

对于大多数人来说,从那里找出内部代码非常简单,只需要一些简单的数学运算。现在我的问题是我需要添加一个选项,上面写着‘嘿,他有 17.50 杯,让我们把它变成 1 加仑和 1 1/2 杯。

我正在考虑向“to_measurement”添加一个新案例检查数字是否大于最高测量值,比如 19 杯大于加仑,然后它变成 1 加仑并将剩余部分发送到生产线,但我从不在厨房里使用品脱,所以也许添加另一个选项转换它的内容......我不知道我被卡住了,是的,我知道代码是基本的但符合我的需要,无论如何......

以防万一你需要我的完整代码,给你:

class KitchenMeasurements
{
public double VolumeMeasurements(double number, string current_measurement, string to_measurement)
{
double mL = 0;
double tsp= 0;
double tbsp = 0;
double fluid_ounces = 0;
double cup = 0;
double pint = 0;
double quart = 0;
double gallon = 0;
double ounce = 0;
double gram = 0;
double pound = 0;
double liter = 0;

switch (current_measurement)
{
case "mL":
mL = number;
tsp = mL * 0.202884;
tbsp = mL * 0.067628;
fluid_ounces = mL * 0.033814;
cup = mL * 0.00422675;
pint = mL * 0.00211338;
quart = mL * 0.00105669;
gallon = mL * 0.000264172;
liter = mL * 0.001;
break;

case "tsb":
tsp = number;
mL = tsp * 4.92892;
tbsp = tsp * 0.333333;
fluid_ounces = tsp * 0.166667;
cup = tsp * 0.0208333;
pint = tsp * 0.0104167;
quart = tsp * 0.00520833;
gallon = tsp * 0.00130208;
liter = tsp * 0.00492892;
break;

case "tbsp":
tbsp = number;
mL = tbsp * 14.7868;
tsp = tbsp * 3;
fluid_ounces = tbsp * 0.5;
cup = tbsp * 0.0625;
pint = tbsp * 0.03125;
quart = tbsp * 0.015625;
gallon = tbsp * 0.00390625;
liter = tbsp * 0.0147868;
break;

case "fluid ounce":
fluid_ounces = number;
mL = fluid_ounces * 29.5735;
tsp = fluid_ounces * 6;
tbsp = fluid_ounces * 2;
cup = fluid_ounces * 0.125;
pint = fluid_ounces * 0.0625;
quart = fluid_ounces * 0.03125;
gallon = fluid_ounces * 0.0078125;
liter = fluid_ounces * 0.0295735;
break;

case "cup":
cup = number;
mL = cup * 236.588;
tsp = cup * 48;
tbsp = cup * 16;
fluid_ounces = cup * 8;
pint = cup * 0.5;
quart = cup * 0.25;
gallon = cup * 0.0625;
liter = cup * 0.236588;
break;

case "pint":
pint = number;
mL = pint * 473.176;
tsp = pint * 96;
tbsp = pint * 32;
fluid_ounces = pint * 16;
cup = pint * 2;
quart = pint * 0.5;
gallon = pint * 0.125;
liter = pint * 0.473176;
break;

case "quart":
quart = number;
mL = quart * 946.353;
tsp = quart * 192;
tbsp = quart * 64;
fluid_ounces = quart * 32;
cup = quart * 4;
pint = quart * 2;
gallon = quart * 0.25;
liter = quart * 0.946353;
break;

case "gallon":
gallon = number;
mL = gallon * 3785.41;
tsp = gallon * 768;
tbsp = gallon * 256;
fluid_ounces = gallon * 128;
cup = gallon * 16;
pint = gallon * 8;
quart = gallon * 4;
liter = gallon * 3.78541;
break;

case "liter":
liter = number;
mL = liter * 1000;
tsp = liter * 202.884;
tbsp = liter * 67.628;
fluid_ounces = liter * 33.814;
cup = liter * 4.22675;
pint = liter * 2.11338;
quart = liter * 1.05669;
gallon = liter * 0.264172;
break;
}

switch (to_measurement)
{
case "mL":
return mL;
case "tsb":
return tsp;
case "tbsp":
return tbsp;
case "fluid ounce":
return fluid_ounces;
case "cup":
return cup;
case "pint":
return pint;
case "quart":
return quart;
case "gallon":
return gallon;
case "ounce":
return ounce;
case "gram":
return gram;
case "pound":
return pound;
case "liter":
return liter;
}

}
}

最佳答案

很难准确地说出您的要求,但我确实看到了这一点:

Now my issue is I need to add a option that says 'hey he gots 17.50 cups, lets make it 1 gallon, and 1 1/2 cups.

您需要进行一组测量并循环遍历它们以重新计算余数。我强烈建议您将方法代码提取到类变量和辅助方法中。

下面是一些应该对您有所帮助的示例代码。您需要填写空白。

public string ConvertToDescription(double amount, string originalMeasurement, params string[] toMeasurements)
{
StringBuilder sb = new StringBuilder();
double valueToConvert = amount;
string priorMeasurement = originalMeasurement;
double displayAmount;
for (int i = 0; i < toMeasurements.Count; i++)
{
if (i > 0)
sb.Append(" ");

double convertedAmount = VolumeMeasurements(valueToConvert, priorMeasurement, toMeasurement[i]);

// Check if last item so we don't trim wanted decimals.
if (i < toMeasurements.Count - 1)
displayAmount = Math.Floor(convertedAmount)
else
displayAmount = convertedAmount;

valueToConvert = convertedAmount - displayAmount;
priorMeasurement = toMeasurements[i];

// You will need to add logic here to display fractions if needed.
sb.AppendFormat("{0} {1}", displayAmount, toMeasurements[i]);

if (fixedAmount == 0)
break;
}

return sb.ToString();
}

请记住,这些都没有经过测试,我真的希望这就是您所要求的。这应该让你走上正确的轨道。您将需要找到一种方法将最后一次转换中的小数转换为显示为分数并实现它。这至少会让你得到“1 加仑 1.5 杯”

关于C# 测量转换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16766908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com