我究竟做错了什么?试图创建一个表(行=每季度销售额,列数=销售人员)

时间:2015-12-02 17:58:15

标签: delphi pascal

在此计划中,您输入与每笔销售相关的三条信息:销售人员的销售数量,销售季度和销售额。如果您为同一个销售人员进行了多次销售,我的程序会生成一个额外的列,而不是将销售额添加到该销售人员的现有列中(对不起,我是一个解释这个和学习如何编程的菜鸟所以,如果我没有很好地解释一下,请告诉我。)最后,我想为每个销售人员制作一个只有一列的表,而不是每次销售(这就是我现在所看到的)。

例如,输入以下内容:

  • sNr:100,q:1,amt:50
  • sNr:200;问:2,amt:50
  • sNr:300,q:3,amt:50
  • sNr:100,q:1,amt:25

我希望看到三个列,每个销售人员编号(100,200,300),以及每个销售人员编号下每个季度的合并销售额(第一季度是销售人员的数字,第二季度,等等)。对于没有销售的季度,出现0。

此时,即使每个销售人员有多个条目,我也会为四个(或更多)条目中的每个条目分别设置一行。

以下是我运行程序时显示的内容。

Input sales number from sales person.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
200
Give sales quarter ( 1 2 3 or 4 ) .
2
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
300
Give sales quarter ( 1 2 3 or 4 ) .
3
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
25
Input sales number from sales person. Input 0 to quit.
0
  100  200  300  100
   50    0    0   25
    0   50    0    0
    0    0   50    0
    0    0    0    0

Press < ENTER > to stop the program.

我想要这个输出:

Input sales number from sales person.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
200
Give sales quarter ( 1 2 3 or 4 ) .
2
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
300
Give sales quarter ( 1 2 3 or 4 ) .
3
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
25
Input sales number from sales person. Input 0 to quit.
0
  100  200  300
   75    0    0
    0   50    0
    0    0   50
    0    0    0

Press < ENTER > to stop the program.

以下是我的代码。有人能帮我吗?

program Sales;

var arrayN: array of integer;
var arrayS: array of array of integer;
var max, row, column, sColumn, counter, nTotal, sNr, q, amt: integer;

begin
  max := 10;
  sColumn := 0;
  nTotal := 1;
  SETLENGTH( arrayN, max );
  SETLENGTH( arrayS, 5, max );
  writeln('Input sales number from sales person.');
  readln(sNr);
  arrayN[sColumn] := sNr;
  arrayS[0, sColumn] := sNr;
  while (sNr <> 0) do
  begin
    if nTotal = LENGTH( arrayN ) then
    begin
      SETLENGTH( arrayN, LENGTH( arrayN ) + 5 );
    end;
    writeln('Give sales quarter ( 1 2 3 or 4 ) .');
    readln(q);
    writeln('Give amount of sale.');
    readln(amt);
    arrayS[q, sColumn] := arrayS[q, sColumn] + amt;
    writeln('Input sales number from sales person. Input 0 to quit.');
    readln(sNr);
    if ( sNr < LOW( arrayN ) ) or ( sNr > HIGH( arrayN ) ) then
    begin
      sColumn := nTotal;
      nTotal := nTotal + 1;
    end
    else
    begin
      for counter := 0 to HIGH( arrayN ) do
      begin
        if sNr = arrayN[counter] then
        begin
          sColumn := counter;
        end;
      end;
    end;
    arrayN[sColumn] := sNr;
    arrayS[0, sColumn] := sNr;
  end;
  for row := 0 to 4 do
  begin
    for column := 0 to nTotal - 1 do
    begin
      write(arrayS[row, column] :5);
    end;
    writeln;
  end;
  writeln;
  writeln('Press < ENTER > to stop the program.');
  readln;
end.  

1 个答案:

答案 0 :(得分:1)

您获得的销售人员列多于输入不同销售人员编号的原因在于条件检查和紧随其后的begin - end

if ( sNr < LOW( arrayN ) ) or ( sNr > HIGH( arrayN ) ) then
begin
  sColumn := nTotal;
  nTotal := nTotal + 1;
end

由于您的销售人员编号与ArrayN的索引无关且使用100,200和300,因此条件始终为true。您实际上可以跳过上面的块(见下文)。

sNr输入后,您应该做的是检查该号码是否已经在&#39; ArrayN&#39;中。实际上,您在上述条件检查的else部分中执行此操作:

else
begin
  for counter := 0 to HIGH( arrayN ) do
  begin
    if sNr = arrayN[counter] then
    begin
      sColumn := counter;
    end;
  end;
end;

删除else和外begin - end块,只留下for循环。 如果找不到nTotal ,则需要修改此检查,以便在循环后增加sNr。在循环之前增加nTotal是容易的,如果找到匹配则减少它,例如,在您指定begin - end的同一sColumn块中。