代码之家  ›  专栏  ›  技术社区  ›  Josh K

现金或信贷问题

  •  2
  • Josh K  · 技术社区  · 15 年前

    如果你去商店问“现金还是信用卡?”他们可能会简单地说“是的”。当你摆出 OR 陈述。 if(cash || credit)

    对于人类来说,他们可能会对这个问题同时回答“两个”或者“只有{现金{信用}”。有没有办法(或者操作员)强制A语句返回 TRUE 陈述的一部分?例如:

    boolean cash = true;
    boolean credit = true;
    boolean check = false;
    
    if(cash || credit || check)
    {
        // In here you would have an array with cash and credit in it because both of those are true
    }
    

    我想指出这是 我想解决的问题。这是我一直在想和想的事情 如果可能的话 . 我想不出 实际的 我会申请的。

    9 回复  |  直到 15 年前
        1
  •  10
  •   Mark Byers    15 年前

    在c中,可以通过设置flags属性的枚举执行与此非常类似的操作。

    [Flags]
    enum MethodOfPayment
    {
        None = 0,
        Cash = 1,
        Credit = 2,
        Check = 4
    }
    

    示例用法:

    void Run()
    {
        MethodOfPayment m = MethodOfPayment.Cash | MethodOfPayment.Credit;
        if (m != MethodOfPayment.None)
        {
            // You can now test m to see which values are selected.
    
            // If you really want the values in an array, you can do this:
            MethodOfPayment[] selected = getSelectedValues(m).ToArray();
            // selected now contains { Cash, Credit }
        }
    }
    
    // Helper method to get the selected values from the enum.
    IEnumerable<MethodOfPayment> getSelectedValues(MethodOfPayment m)
    {
        foreach (MethodOfPayment x in Enum.GetValues(typeof(MethodOfPayment)))
        {
            if ((m & x) != MethodOfPayment.None)
                yield return x;
        }
    }
    
        2
  •  5
  •   wheaties    15 年前

    在scala中,可以使用match语句编写以下内容

    def OneOrTheOther( _1:Boolean, _2:Boolean ) = {
        (_1, _2) match{
            case True, False => //do stuff
            case False, True => //do stuff
            case True, True =>  //do stuff
            case False, False =>//do stuff
         }
    }
    

    我喜欢搭配的表情。

        3
  •  3
  •   Ben Hoffstein    15 年前

    这就是使用位掩码的目的。下面是一个很好的概述(示例代码使用数据库权限作为示例)

    http://www.vipan.com/htdocs/bitwisehelp.html

        4
  •  3
  •   JasonTrue    15 年前

    我想这个有点人为的场景并不是你想要回答的,所以我要假设在其他情况下有一些价值。

    在具有函数构造的语言中,可以从与条件匹配的集合中“选择”项。

    红宝石:

    payment={cash=>true, credit=>true, check=>false}
    methods_used=payment.select{ |key,value| value==true}.map { |key,value| key}
    

    收益率[:现金,:信贷]

    在C 3中:

    var payment = new Dictionary<string, bool> 
       {{"cash", true}, {"credit",true}, {"check", false}};
    var items=payment.Where(x => x.Value == true).Select(x => x.Key);
    Console.WriteLine(String.Join(",",items.ToArray()));
    

    在我能想到的大多数语言中,如果不将变量添加到集合或字典中,就无法直接从变量绑定中执行所需的操作。可能有什么例外,但我想不出有什么例外。

    在haskell、f_和ocaml这样的语言中,模式匹配可能是一个更接近的方法,但是我仍然无法找到一种使它看起来像你暗示的那样的方法。

    在boo中,您可以更改编译器管道,用能满足您需要的东西替换if语句的语义。我可能会使用备用关键字,但您基本上可以提取所有子表达式,并将变量名添加到块的作用域,或者将“yes”值添加到一个集合,将“nos”添加到另一个集合,并按约定添加命名变量。为了有效地实现它,您可能必须打破短路评估的惯例,这将使大多数习惯于现代编程语言设计的人感到恼火。

        5
  •  2
  •   Yuval Adam    15 年前

    如果您的语言支持列表理解,则可以使用它(python中的示例):

    list = [True, False, True, True]
    print [x for x in list if x] # print only the True values
    

    或者,位掩码通常用于这样的任务(以及通常廉价且存在于大多数语言中):

    CASH = 1
    CREDIT = 2
    ELSE = 4
    
    myOption = CASH & ELSE
    

    这完全取决于你到底想做什么。

        6
  •  2
  •   FrustratedWithFormsDesigner    15 年前

    我认为你的目标是这样的(如果我错了就纠正我,因为我对这里不是百分之百的确定):

    if (operandA || operandB || operandC || (operandD && operandE))
    {
        //in here we have access to an environment-created array $TRUE_OPERANDS which stores the operands
        //that were TRUE in the most recently evaluated if-statement.
        //Maybe it contains the names of operands. suppose that A, C and E were TRUE
        //then $TRUE_OPERANDS = {'operandA', 'operandC', 'operandE'}
        //...but E was AND'd with D and D was false, so should E still be returned?
        //Are E and D considered separate operands or are the considered as a single "E && D" operand?
        //...and how would we implement nested if-statements?
        //Would there be only one global $TRUE_OPERANDS or would there be one
        //for the scope of every if-statement?
    }
    

    我不知道有什么语言能做到这一点(至少不像我在这里所描述的那样)。正如许多其他人所提到的,位掩码和枚举常常用来解决这个问题。其他一些人用ruby、python和c发布了一些例子 可以 接近你想要的。

        7
  •  1
  •   aioobe    15 年前

    人类语言有时使用“或”这个词来表示“异或”,这在许多语言中(包括C、C++和Java)都被表示为“异或”。 ^

    也就是说, cash ^ credit 计算为true,如果且仅当 cash credit 是真的。

        8
  •  1
  •   Claudiu    15 年前

    从评论中,您似乎在问:是否有任何语言会自动为任何得到求值的if语句创建该集合?

    答案是否定的。至少,不是任何现代语言(C,C,Java,Haskell,几乎任何东西)。原因是短路。假设这个if语句:

    if (a() || b() || c() || d()) {
    }
    

    也就是说,你调用一个函数来获取每个真值。如果 a() 求值为真,那么任何现代语言都不会求值任何其他函数,因为它们不会改变整个语句的真值。现在的代码依赖于这种短路,因此更改一种语言来计算其所有或操作数可能会导致大量代码中断。

    对于特殊情况,在测试用例中只有一组or和变量…这是一个非常特殊的情况,如果你想要一组真实的东西,最好使用这里提出的其他一些解决方案,而不是仅仅其中任何一个是真实的。