【问题标题】:How to end this loop / logically end pseudocode如何结束这个循环/逻辑结束伪代码
【发布时间】:2014-07-01 05:05:22
【问题描述】:

我正在编写一个伪代码程序来读取客户记录,确定他们的帐户类型,然后在最后输出他们的姓名和欠款金额。我写了一个程序(几乎完成)我只是不确定如何循环它,直到没有更多的记录。你们能帮帮我吗?我最后的输出应该是客户名称和欠款。谢谢。

read_customer_record 
get num_of_records
    get customer_name
get account_type
get num_basic_channels
get num_premium_channels
calculate_rate (calcR) 
calculate_totals (calcT)
output(outp)


END

Calculate_rate (calcR)
IF account_type = personal
Basic_rateP = $5
Pre_rateP = $10
    ELSE
        Basic_rateB = $7.50
        Pre_rateB = $12.50
END

Calculate_totals (calcT, calcR)
IF account_type = personal
total = (num_basic_channels * Basic_rateP) + (num_premium_channels * Pre_rateP)
    ELSE
total = (num_basic_channels * Basic_rateB) + (num_premium_channels * Pre_rateB)
END

output (

【问题讨论】:

    标签: loops logic pseudocode do-while records


    【解决方案1】:

    你可以这样做:

    >>> 打印客户姓名和欠款金额,直到客户姓名不等于空。

    如果我用 C++ 编写代码,它会看起来像这样。

    for(i=0;CustomerName[i]!=NULL;i++)  //Loop till there is some Customer Name exists
    {
    cout<<CustomerName[i]<<"\t"<<AmountOwed[i];  //Printing out the Data
    }
    

    当没有 Customer Remaining 时它将停止,当没有 Customer 时,显然没有 Amount Owed。

    【讨论】:

    • 那么我在哪里可以启动类似的 for 循环?我的其余逻辑有意义吗?
    • 我写了循环打印结果。如果你想对记录做一些计算,你可以简单地使用数组。比如total[i]。是的,您的逻辑确实有意义,但请确保在处理 If Else 语句时小心使用括号 }。
    【解决方案2】:

    我认为你只需要在你的主程序周围包裹一个 WHILE 语句。执行此操作的方法取决于您从哪里读取客户记录。

    案例 1:在许多情况下,您只能通过尝试读取结尾来判断您已完成输入流的处理。在这种情况下,您的伪代码可能如下所示:

    read_customer_record 
    while the previous read succeeded
        get num_of_records
        get customer_name
        get account_type
        get num_basic_channels
        get num_premium_channels
        calculate_rate (calcR) 
        calculate_totals (calcT)
        output(outp)
        read_customer_record
    end
    

    案例 2:在其他情况下,您可以在读取之前判断输入流中是否还有要读取的内容。在这种情况下,您的伪代码可能如下所示:

    while there are more customer records to process
        read_customer_record
        get num_of_records
        get customer_name
        get account_type
        get num_basic_channels
        get num_premium_channels
        calculate_rate (calcR) 
        calculate_totals (calcT)
        output(outp)
    end
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多