1

これをネストされた 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
4

2 に答える 2

0

Here is the answer that I posted to Code Review. I'm using Python as executable pseudocode.


You must be working for American health care. The key insight about American healthcare is that the first question is always "Do you have insurance?" Indeed, if you restructure your code to ask that first, you'll find that the pattern will be more obvious.

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')

The next goal is to consolidate to avoid repetition. You'll notice that the "preferred" status only matters for first-time customers, so (first and not preferred) is a useful common expression; I've parenthesized them as such for emphasis.

Answer:

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()

From there, you gain the insight that in general, insured patients get monthly bills, and uninsured patients pay half up front. However, being (first and not preferred) carries a penalty of one degree of trustworthiness. The following code implements this intuitive classification process.

# 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]()

It's up to you to decide whether you think the code based on truth-table minimization or on the intuitive classification is better.

于 2013-09-17T08:34:58.120 に答える