Pascal编程帮助 [英] Pascal programming help

查看:174
本文介绍了Pascal编程帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴这早些时候,它得到关闭,因为我没有在编码它显示我的尝试,因此,这里的问题:

 段
$ 160个=第1
$ 220 =第2
$ 280 =第3
$ 350个=第4
$ 425 =第5
 

开发伪code,接受作为输入的假面具数目不详谁每人都支付了服装,每个已支付金额的全部费用的名称。

一个masquerader可能已经支付的任何五段在乐队的服装。该算法应确定在其中masquerader播放的基础上,他/她已经支付的服装的量的部分。该算法还应该确定谁已经支付了在每个部分的服装假面具的数目

者和名称为他们所付出应打印的部分。的部分,并登记在每个部分发挥者的总数目的列表也应印,以及在每个部分支付的总金额。

下面是我的尝试吧:*注意:这是编程帕斯卡,我需要帮助修复它,完成它。请大家帮帮忙,再次感谢。

 程序假面具;

用途
  WinCrt; {允许Writeln,Readln,光标移动等}

常量
   MAX = 5; {这个判断masquarader量进入}
类型
  LISTNAME =数组[1..MAX]串;
  listsect =数组[1..MAX]串;
变种
 名称:LISTNAME;
 部分:listsect;
 我,金额,TotalMas,TotalAmt,C1,C2,C3,C4,C5,AMT1,AMT2,AMT3,AMT4,AMT5:整数;

开始

 金额:= 1;
 而度<> 0做
 开始

      I:= I + 1;
      readln(名称[I]);
      readln(金额);

      如果(金额= 160),则
      开始

                C1:= C1 + 1; {数人的第1数}
                AMT1:= AMT1 +量; {累积量为第1}
                段[我]:='第1节';
      结束;

      如果(金额= 220),则
      开始

                C2:= C2 + 1; {数人的第1数}
                AMT2:= AMT2 +量; {累积量为第1}
                段[我]:='第2';
      结束; {结束IF为第2}

      如果(金额= 280),则
      开始

                C3:= C3 + 1; {数人的第1数}
                AMT3:= AMT3 +量; {累积量为第1}
                段[我]:='第3节';
      结束; {结束IF为第3}

      如果(金额= 350),则
      开始

               C4:= C 4 + 1;
               AMT4:= AMT4 +量;
               段[我]:='Section4';
      结束; {end如果为第4}

      如果(金额= 425),则
      开始

               C5:= C 5 + 1;
               AMT5:= AMT5 +量;
               段[我]:='Section5';


  结束; {结束while循环}

  TotalMas:= C1 + C2 + C3;
  TotalAmt:= AMT1 + AMT2 + AMT3;


  writeln(名科); {标题为输出}
  对于i:= 1到MAX做
  开始

       写(名称[I]);
       writeln('',节[I]);

  结束;


  writeln('第1节:');
  写(Masquader:'C1);
  写(额:'AMT1);



  writeln('Masquarader总人数:',TotalMas);
  writeln('总量masquarader支付:',TotalAmt);
 

结束;       结束。

在短,应该接受的人未定义数量并将其分配给基于他们输入货币量的相应部分,然后计算每个部分的人的数目。这是我的电流输出:

  

名称约翰·钱= 160第1

     

名称基思的钱= 220节约翰

这就是我想要的:

  

名称约翰·钱= 160 SECTION1

     

名称基思的钱= 220第2节

解决方案

我不是很熟悉,帕斯卡,所以我不能告诉你的如何的解决这一问题,但一个问题,我注意到的是,您的code似乎违反了干的规则:不要重复自己。里面每个code 如果AMT = XXX 块看起来几乎一模一样,所以有您可以编写code一次,然后调用一个方法你每次code有不同的参数?这样的东西,你不能重写相同的code五倍。

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

解决方案

I'm not really familiar with Pascal so I can't tell you how to fix this but one issue I notice is that your code seems to violate the "DRY" rule: Don't Repeat Yourself. The code inside each if amt = xxx block looks almost exactly the same, so is there a way you can write that code once, and then call your code with different parameters each time? Something so that you're not rewriting the same code five times.

这篇关于Pascal编程帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆