2

I posted this earlier and it got closed because I did not show my try at coding it, so here is the question:

SECTIONS
$160 = section 1
$220 = section 2
$280 = section 3
$350 = section 4
$425 = section 5

Develop pseudocode that accepts as input the name of an unspecified number of masqueraders who each have paid the full cost of their costume and the amount each has paid.

A masquerader may have paid for a costume in any of the five sections in the band. The algorithm should determine the section in which the masquerader plays, based on the amount he/she has paid for the costume. The algorithm should also determine the number of masqueraders who have paid for costumes in each section.

The names of persons and the section for which they have paid should be printed. A listing of the sections and the total number of persons registered to play in each section should also be printed, along with the total amount paid in each section.

Here is my try at it: *Note this is programmed in Pascal, and I need help fixing it up and finishing it. Please help and thanks again.

program Masqueraders;

uses
  WinCrt;  { Allows Writeln, Readln, cursor movement, etc. }

const
   MAX = 5; {this determine the amount of masquarader entered}
Type
  listname = Array[1..MAX] of string;
  listsect = Array[1..MAX] of string;
var
 names : listname;
 sections : listsect;
 i, amount, TotalMas, TotalAmt, c1, c2, c3, c4, c5, amt1, amt2, amt3, amt4, amt5 :     integer;

begin

 amount := 1;
 while amount <> 0 do
 begin

      i := i + 1;
      readln(names[i]);
      readln(amount);

      if(amount = 160) then
      begin

                c1 := c1 + 1;  {Count the number of persons for section 1}
                amt1 := amt1 + amount; {accumulate the amount for section 1}
                sections[i] := 'Section 1';
      end;

      if(amount = 220) then
      begin

                c2 := c2 + 1;  {Count the number of persons for section 1}
                amt2 := amt2 + amount; {accumulate the amount for section 1}
                sections[i] := 'Section 2';
      end; {end the IF for section 2}

      if(amount = 280) then
      begin

                c3 := c3 + 1;  {Count the number of persons for section 1}
                amt3 := amt3 + amount; {accumulate the amount for section 1}
                sections[i] := 'Section 3';
      end; {end the IF for section 3}

      if(amount = 350) then
      begin

               c4 := c4 + 1;
               amt4 := amt4 + amount;
               sections[i] := 'Section4';
      end; {end If for section 4}

      if (amount = 425) then
      begin

               c5 := c5 + 1;
               amt5 := amt5 + amount;
               sections[i] := 'Section5';


  end;{end the while loop}

  TotalMas := c1 + c2 + c3;
  TotalAmt := amt1 + amt2 + amt3;


  writeln('Name                    Section');  {Heading for the output}
  for i := 1 to MAX do
  begin

       write(names[i]);
       writeln('                    ',sections[i]);

  end;


  writeln('Section 1: ');
  write('Masquader: ', c1);
  write('Amount: ', amt1);



  writeln('Total Number of Masquarader: ', TotalMas);
  writeln('Total Amount Paid by masquarader: ', TotalAmt);

end; end.

In short, it should accept an undefined number of people and assign them to their respective sections based on the amount of money they entered, then calculate the number of people in each section. This is my current output:

Name John Money=160 Section 1

Name Keith Money=220 Section John

This is what I want:

Name John Money=160 Section1

Name Keith Money=220 Section2

4

4 回答 4

4

我对 Pascal 不是很熟悉,所以我不能告诉你如何解决这个问题,但我注意到的一个问题是你的代码似乎违反了“DRY”规则:不要重复自己。每个if amt = xxx块内的代码看起来几乎完全相同,那么有没有一种方法可以编写一次代码,然后每次使用不同的参数调用您的代码?这样您就不会五次重写相同的代码。

于 2010-03-01T05:37:50.107 回答
3

以下是一些改进代码的提示:

  • 您是否认为有一种方法可以在您的最终循环中打印“Section {i}”而不使用section[i] 查找它,从而完全消除对数组的需求?

  • 您如何构建它以便添加第 6 节不需要修改您的代码?

  • listname 和 listect 非常相似,您可以对您的代码进行哪些修改来消除两个相同定义的需要?

  • 如果用户在第三次提示时输入 0 会发生什么?

注意:这些提示之一应直接指出问题的根源。

于 2010-03-02T00:17:38.740 回答
3

以下是我看到的问题:

  1. 要求说“未指定数量的伪装者”,但您已将最大值设置为 5。如果伪装者可能超过 5 个,则不能将名称列表存储在固定大小的数组中。照原样,当用户进入第 6 个伪装者时,应用程序可能会崩溃或损坏内存(取决于您使用的 Pascal 版本)。 如果允许您在输入伪装者的姓名和部分后立即打印,那么您应该在用户输入姓名和金额后立即打印(而不是在输入 while 循环之后)。 但是,如果需要在所有输入后打印 Names 和 Sections 列表,则需要将 Name+Section 存储在可变长度数据结构中,例如链表,甚至更简单的字符串。

  2. 变量 c1 到 c5 和 amt1 到 amt5 最好声明为 1..MAX 的两个数组(c 和 amt),而不是 10 个变量。当用户输入金额时,使用它确定节号,然后可以将其用作 c 和 amt 数组的索引。

  3. 不确定您使用的是什么 Pascal 实现,但在使用之前初始化所有变量会更安全,否则它们可能包含不可预测的值。

  4. section 数组是不必要的。它最终包含的只是不需要计算或存储的部分的标题。

  5. 如果您使用重复/直到循环而不是 while 循环,则可以避免顶部稍微笨拙的“amount := 1”初始化进入 while 循环。重复/直到将直到 Amount = 0。

  6. TotalMas 只是将 c1 添加到 c3(c4 和 c5 呢)?

  7. TotalAmt 只是将 amt1 添加到 amt3(amt4 和 amt5 呢)?

  8. 打印名称和部分的 for 循环是完全错误的,请参见第 1 点和第 4 点。

希望这会有所帮助,如果您需要澄清任何问题,请告诉我。

于 2010-03-02T01:00:10.823 回答
0

我对这段代码的不满:

1)变量名很臭。几乎所有这些都太短了,他们不交流他们所做的事情。

2)这里使用了错误的循环控制结构。

3)我看到几乎相同的代码重复了 5 次。这简直就是在呼唤数组。

4)评论解释了你在做什么,而不是你为什么这样做。其中一半几乎完全没用,如果你要评论一个结局,你应该简单地用它的结尾的身份来评论它。结尾; //如果我只看到一条甚至试图解释原因的评论,而你把人与部分混为一谈,你就搞错了。

5)您的sections 数组没有任何用处。对值的分配始终是固定的,因此根本没有理由存储它们——如果您需要标签,您可以在打印时创建它们。

6) 你假设只有 5 个人。问题中没有给出 - 有 5 个部分

于 2010-03-02T01:55:19.847 回答