PASCAL 中多维数组的总计
Totals from a multidimensional array in PASCAL
我有一个程序,可以获取用户输入的关于工厂生产的产品的信息,并将这些信息分成 2 table 部分。第一个 table 代表好产品,第二个代表坏产品。每个 table 表示每台机器(7台机器)生产了每种产品(10个产品)的数量。 Y 轴(每台机器生产的产品总数)和 X 轴(所有机器上特定产品的总数)应该有总计
我在 Y 轴上有总计,但结果总是不对。它默认显示 10 而不是 0,当你向它添加 1 时,它会变成 9 而不是 1。
代码如下:
program Production8;
uses crt;
var
machine: array of char;
machineindex: integer;
product: array of integer;
quality: array of integer;
min, number: integer;
i, j, k, extra, total: integer;
myTable: array[0..6,0..9,0..1] of integer;
procedure Header;
begin
writeln('Production Overview');
writeln();
end;
procedure Quit;
begin
writeln();
writeln('To Quit hit <ENTER>');
readln;
end;
procedure ShowTable(pQuality: integer; pTitle: string);
begin
writeln(pTitle);
writeln(' 0 1 2 3 4 5 6 7 8 9 T');
for i := 0 to 6 do
begin
total := 0;
write(CHR(65+i),' ');
for j := 0 to 9 do
begin
write(myTable[i,j,pQuality], ' ');
if myTable[machineindex,(product[number]),(quality[number])]
= myTable[i,j,pQuality] then
begin
total:= total + 1;
end;
if j = 9 then
begin
write (total);
end;
end;
writeln('');
end;
writeln ('T');
end;
begin
for i:= 0 to 6 do
for j:= 0 to 9 do
for k:= 0 to 1 do
myTable[i, j, k]:= 0;
i:= 0;
j:= 0;
number := 0;
min := 1;
extra := 1;
SETLENGTH( machine, min );
SETLENGTH( product, min );
SETLENGTH( quality, min );
Header;
writeln('Input Machine ID ( A, B, C, D, E, F or G ). Input "*" to stop');
readln(machine[number] );
while (machine[number] <> '*') do
begin
while ( ORD( machine[number] ) < 65 ) or ( ORD( machine[number] ) > 71 ) do
begin
writeln('Input Invalid. Please try again.');
readln(machine[number] );
end;
clrscr;
Header;
machineindex := (ORD(machine[number])-65);
writeln('Input Product Number ( 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9 ) ');
readln(product[number] );
while ( product[number] < 0 ) or ( product[number] > 9 ) do
begin
writeln('Input Invalid. Please try again.');
readln(product[number] );
end;
clrscr;
Header;
writeln('Quality Control Check. Input 0 for GOOD or 1 for BAD.');
readln(quality[number] );
while ( quality[number] <> 0 ) and ( quality[number] <> 1 ) do
begin
writeln('Input Invalid. Please try again.');
readln(quality[number] );
end;
clrscr;
Header;
myTable[machineindex,(product[number]),(quality[number])] :=
myTable[machineindex,(product[number]),(quality[number])] + 1;
number := number + 1;
if number = LENGTH(machine) then
begin
SETLENGTH(machine, LENGTH(machine)+extra);
SETLENGTH(product, LENGTH(product)+extra);
SETLENGTH(quality, LENGTH(quality)+extra)
end;
writeln('Input Machine ID ( A, B, C, D, E, F or G ). Input "*" to stop');
readln(machine[number] );
end;
clrscr;
Header;
ShowTable(0, 'Good Products');
writeln();
ShowTable(1, 'Bad Products');
Quit;
end.
起初我以为问题出在我的初始化上,因为我没有 "myTable" 初始化。现在我这样做了,问题仍然存在。我哪里错了?
此外,我不确定如何在 X 轴上实现总计。我该怎么做?
var "total" 的总和不正确。
我试图在不对您的代码进行许多设计更改的情况下使代码正常工作:
procedure ShowTable(pQuality: integer; pTitle: string);
var totalx: integer;
totaly: array[0..9] of integer;
begin
// initialize array totaly
for j:= 0 to 9 do
totaly[j] := 0;
// output header
writeln(pTitle);
writeln(' 0 1 2 3 4 5 6 7 8 9 T');
// iterate machines
for i := 0 to 6 do
begin
totalx := 0;
write(CHR(65+i),' ');
for j := 0 to 9 do
begin
write(myTable[i,j,pQuality], ' ');
if myTable[i,j,pQuality] = 1 then
begin
totalx:= totalx + 1;
totaly[j] := totaly[j] + 1;
end;
if j = 9 then
begin
write (totalx);
end;
end;
writeln('');
end;
write('T ');
total := 0;
for j := 0 to 9 do
begin
total := total + totaly[j];
write(totaly[j], ' ');
end;
write(total);
结束;
一般尽量减少全局变量的使用,多使用注释。
您还应该考虑面向对象的概念...
我有一个程序,可以获取用户输入的关于工厂生产的产品的信息,并将这些信息分成 2 table 部分。第一个 table 代表好产品,第二个代表坏产品。每个 table 表示每台机器(7台机器)生产了每种产品(10个产品)的数量。 Y 轴(每台机器生产的产品总数)和 X 轴(所有机器上特定产品的总数)应该有总计
我在 Y 轴上有总计,但结果总是不对。它默认显示 10 而不是 0,当你向它添加 1 时,它会变成 9 而不是 1。
代码如下:
program Production8;
uses crt;
var
machine: array of char;
machineindex: integer;
product: array of integer;
quality: array of integer;
min, number: integer;
i, j, k, extra, total: integer;
myTable: array[0..6,0..9,0..1] of integer;
procedure Header;
begin
writeln('Production Overview');
writeln();
end;
procedure Quit;
begin
writeln();
writeln('To Quit hit <ENTER>');
readln;
end;
procedure ShowTable(pQuality: integer; pTitle: string);
begin
writeln(pTitle);
writeln(' 0 1 2 3 4 5 6 7 8 9 T');
for i := 0 to 6 do
begin
total := 0;
write(CHR(65+i),' ');
for j := 0 to 9 do
begin
write(myTable[i,j,pQuality], ' ');
if myTable[machineindex,(product[number]),(quality[number])]
= myTable[i,j,pQuality] then
begin
total:= total + 1;
end;
if j = 9 then
begin
write (total);
end;
end;
writeln('');
end;
writeln ('T');
end;
begin
for i:= 0 to 6 do
for j:= 0 to 9 do
for k:= 0 to 1 do
myTable[i, j, k]:= 0;
i:= 0;
j:= 0;
number := 0;
min := 1;
extra := 1;
SETLENGTH( machine, min );
SETLENGTH( product, min );
SETLENGTH( quality, min );
Header;
writeln('Input Machine ID ( A, B, C, D, E, F or G ). Input "*" to stop');
readln(machine[number] );
while (machine[number] <> '*') do
begin
while ( ORD( machine[number] ) < 65 ) or ( ORD( machine[number] ) > 71 ) do
begin
writeln('Input Invalid. Please try again.');
readln(machine[number] );
end;
clrscr;
Header;
machineindex := (ORD(machine[number])-65);
writeln('Input Product Number ( 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9 ) ');
readln(product[number] );
while ( product[number] < 0 ) or ( product[number] > 9 ) do
begin
writeln('Input Invalid. Please try again.');
readln(product[number] );
end;
clrscr;
Header;
writeln('Quality Control Check. Input 0 for GOOD or 1 for BAD.');
readln(quality[number] );
while ( quality[number] <> 0 ) and ( quality[number] <> 1 ) do
begin
writeln('Input Invalid. Please try again.');
readln(quality[number] );
end;
clrscr;
Header;
myTable[machineindex,(product[number]),(quality[number])] :=
myTable[machineindex,(product[number]),(quality[number])] + 1;
number := number + 1;
if number = LENGTH(machine) then
begin
SETLENGTH(machine, LENGTH(machine)+extra);
SETLENGTH(product, LENGTH(product)+extra);
SETLENGTH(quality, LENGTH(quality)+extra)
end;
writeln('Input Machine ID ( A, B, C, D, E, F or G ). Input "*" to stop');
readln(machine[number] );
end;
clrscr;
Header;
ShowTable(0, 'Good Products');
writeln();
ShowTable(1, 'Bad Products');
Quit;
end.
起初我以为问题出在我的初始化上,因为我没有 "myTable" 初始化。现在我这样做了,问题仍然存在。我哪里错了?
此外,我不确定如何在 X 轴上实现总计。我该怎么做?
var "total" 的总和不正确。
我试图在不对您的代码进行许多设计更改的情况下使代码正常工作:
procedure ShowTable(pQuality: integer; pTitle: string);
var totalx: integer;
totaly: array[0..9] of integer;
begin
// initialize array totaly
for j:= 0 to 9 do
totaly[j] := 0;
// output header
writeln(pTitle);
writeln(' 0 1 2 3 4 5 6 7 8 9 T');
// iterate machines
for i := 0 to 6 do
begin
totalx := 0;
write(CHR(65+i),' ');
for j := 0 to 9 do
begin
write(myTable[i,j,pQuality], ' ');
if myTable[i,j,pQuality] = 1 then
begin
totalx:= totalx + 1;
totaly[j] := totaly[j] + 1;
end;
if j = 9 then
begin
write (totalx);
end;
end;
writeln('');
end;
write('T ');
total := 0;
for j := 0 to 9 do
begin
total := total + totaly[j];
write(totaly[j], ' ');
end;
write(total);
结束;
一般尽量减少全局变量的使用,多使用注释。 您还应该考虑面向对象的概念...