【问题标题】:Programming Pseudocode Nested if into Control Structure将嵌套的伪代码编程为控制结构
【发布时间】:2013-09-17 03:05:19
【问题描述】:

谁能帮我把这个嵌套的 if 改成控制用例,或者更有效的东西(只是不是循环)?

为以下问题设计一个解决方案,尽可能使用模块。用结构化流程图和相应的伪代码说明您的解决方案。

健康诊所对所提供的任何服务都有如下付款时间表:

一个。如果患者是没有保险的首次患者,则费用将在服务时全额支付。

b.如果患者是第一次有保险的患者,那么一半 服务费应在服务时支付,余额在 月结单。

c。如果患者不是第一次没有保险的患者,那么一半 费用的一部分将在服务时支付,余额将在 月结单。

d。如果患者不是第一次有保险的患者,那么所有 费用将在月结单上计费。

e。如果患者是没有保险的“首选”患者,则收取一半的费用 将支付服务时间和每月结算的余额 声明。

f。如果患者是有保险的“首选”患者,则所有费用都将在月结单上计费。


    Start
    Declare variables
    Input(first,ins,pre)
    if(first = 'T') then
        if(ins = 'T') then
            if(pre = 'T') then
                Print('Monthly Statement')
            else
                Print('Pay one-half and Billed one-half')
            endif
        else
            if(pre = 'T') then
                Print('Pay one-half and Billed one-half')
            else
                Print('Pay full')
            endif
        endif
    else
        if(ins = 'T') then
            if(pre = 'T') then
                Print('Monthly Statement')
            else
                Print('Monthly Statement')
            endif
        else
            if(pre = 'T') then
                Print('Pay one-half and Billed one-half')
            else
                Print('Pay one-half and Billed one-half')
            endif
        endif
    endif
    Stop

【问题讨论】:

    标签: pseudocode


    【解决方案1】:

    而不是您显示的 3 个布尔值,实际上似乎有两个变量:患者类型(3 个值)和保险状态(2 个值)。

    如果你想为一个人布置这个,你可能会画一张 3 x 2 的桌子。

    并且有 3 种可能的结果表示为字符串。

    所以在这种情况下,我会对二维数组做同样的事情:

    enum PatientType { FirstTime, Normal, Preferred };
    enum InsuranceStatus {  NotInsured, Insured };
    string HalfAndHalf = 'Pay one-half and Billed one-half';
    string InFull = 'Pay in full';
    string Payments = 'Monthly statement';
    
    string Result[PatientType][InsuranceStatus] = {
                         /* NotInsured */  /* Insured */
      /* FirstTime */ {  InFull,           HalfAndHalf }, 
      /* Normal    */ {  HalfAndHalf,      Payments    },
      /* Preferred */ {  HalfAndHalf,      Payments    }};
    

    现在您可以索引数组Result[patientType][insuranceStatus] 并打印此字符串。道德是数据表通常对于降低代码复杂性并使其在未来易于更改逻辑非常有用。

    如果我弄错了规范并且它实际上是 3 个布尔自变量,您仍然可以使用 2x2x2 数组。

    【讨论】:

      【解决方案2】:

      这是我发布到Code Review 的答案。我使用 Python 作为可执行伪代码。


      您必须为美国医疗保健机构工作。关于美国医疗保健的关键见解是,第一个问题始终是“你有保险吗?”事实上,如果你重新构造你的代码以首先提出这个问题,你会发现这种模式会更加明显。

      if insured:
          if first:
              if preferred:
                  print('Monthly Statement')
              else:
                  print('Pay one-half and Billed one-half')
          else:
              if preferred then:
                  print('Monthly Statement')
              else:
                  print('Monthly Statement')
      else:
          if first:
              if preferred:
                  print('Pay one-half and Billed one-half')
              else:
                  print('Pay full')
          else:
              if preferred:
                  print('Pay one-half and Billed one-half')
              else:
                  print('Pay one-half and Billed one-half')
      

      下一个目标是巩固以避免重复。您会注意到“首选”状态仅对首次使用的客户很重要,因此(first and not preferred) 是一个有用的常用表达方式;为了强调,我用括号括起来。

      答案:

      if (not insured) and (first and not preferred):
          pay_in_full()
      elif (not insured) or (first and not preferred):
          pay_half_bill_half()
      else:
          monthly_statement()
      

      从那里,您会了解到,一般来说,投保的患者每月收到账单,而未投保的患者预付一半。但是,成为(first and not preferred) 会受到一级可信度的惩罚。下面的代码实现了这个直观的分类过程。

      # Actions
      def pay_in_full():
          ...
      
      def pay_half_bill_half():
          ...
      
      def monthly_statement():
          ...
      
      # Determine trust level
      trust_levels = [pay_in_full, pay_half_bill_half, monthly_statement]
      trust = 2 if insured else 1            # insured -> monthly. uninsured -> pay half
      trust -= 1 if first and not preferred  # penalty for first-time unpreferred customers
      
      # Call the code corresponding to the trust level
      trust_levels[trust]()
      

      由您决定是基于真值表最小化还是基于直观分类的代码更好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多