实现通过失败算法的类 [英] Class to implement pass fail algorithm

查看:85
本文介绍了实现通过失败算法的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我写代码来满足以下算法:

输入:MeasuredValue
高限
LowLimit

输出:基于以下逻辑的PASS/FAIL布尔值

如果测量值是< =上限且> =下限
返回通行证
否则返回失败.

我想在C#中的类中实现它,以便可重用.

谢谢,

Sophronis Mantoles

Can someone help me write code to satisfy the following algorithm:

input: MeasuredValue
HighLimit
LowLimit

output: PASS/ FAIL boolean based on the following logic

If the measured value is <= Highlimit and >= Low Limit
return PASS
else return FAIL.

I would like to implement this in C# in a class so it is reusable.

Thanks,

Sophronis Mantoles

推荐答案

这是非常基础的,您已经有了算法.您要使它变得多么复杂和/或强大取决于您自己.

我个人会使用这样的struct/class:
This is very basic and you already have the algorithm. How complex and/or robust you want to make it is up to you.

Personally I''d use a struct/class like this:
public struct MeasurementBoundsF
{
    public static readonly MeasurementBoundsF Empty = new MeasurementBoundsF();
    private float highLimit;
    private float lowLimit;
    public MeasurementBoundsF(float lowLimit, float highLimit)
    {
        if (highLimit > lowLimit)
        {
            this.lowLimit = lowLimit;
            this.highLimit = highLimit;
        }
        else
        {
            this.lowLimit = highLimit;
            this.highLimit = lowLimit;
        }
    }
    public float HighLimit
    {
        get { return highLimit; }
    }
    public float LowLimit
    {
        get { return lowLimit; }
    }
    public PassFail GetPassFail(float measuredValue)
    {
        return (measuredValue <= highLimit) && (measuredValue >= lowLimit) ? PassFail.Pass : PassFail.Fail;
    }
}

我用了一个枚举代替了bool,所以我可以在将来扩展可能的结果:

I''ve used an enum instead of a bool so I can extend the possible results in future:

public enum PassFail
{
    Fail = 0,
    Pass = 1
}

在使用中,您可以执行以下操作:

In use you could do something like this:

float measuredValue;
float highLimit;
float lowLimit;

Console.Write("Measured value? ");
if (float.TryParse(Console.ReadLine(), out measuredValue))
{
    Console.Write("High limit? ");
    if (float.TryParse(Console.ReadLine(), out highLimit))
    {
        Console.Write("Low limit? ");
        if (float.TryParse(Console.ReadLine(), out lowLimit))
        {
            MeasurementBoundsF measurementBoundsF = new MeasurementBoundsF(lowLimit, highLimit);
            Console.WriteLine(measurementBoundsF.GetPassFail(measuredValue));
        }
        else
            Console.WriteLine("Low limit was invalid");
    }
    else
        Console.WriteLine("High limit was invalid");
}
else
    Console.WriteLine("Measured value was invalid");
Console.ReadKey();

如果您不了解它,那么我建议您购买一本书或两本书并学习和/或学习一些在线初学者教程.

如果这是用于家庭作业(我怀疑),我强烈建议您不要复制并粘贴以上内容,因为您的导师可能会要求您解释一下,并且很可能会访问此网站(很多)并且会知道您被骗了.

If you don''t understand it then I suggest you purchase a book or two and study and/or study some online beginners tutorials.

If this is for homework (which I suspect), I would strongly suggest you don''t copy and paste the above as your tutor may ask you to explain it, and could quite possibly visit this site (many do) and will know you have cheated.


这篇关于实现通过失败算法的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆