我目前正在尝试编写一个程序,该程序每季度概述用户定义的销售人员一年内所赚取的金额。
这是我目前的代码:
program Verkopers;
var month, day, sellernr:array[0..99] of integer;
sale:array[0..99] of real;
count, num: integer;
begin
num := 0;
writeln('Enter seller number. To stop enter "0"');
readln(sellernr[num] );
while (sellernr[num] <> 0) do
begin
writeln('Enter date in DD MM format');
readln(day[num] , month[num] );
writeln('Enter sale amount');
readln(sale[num] );
num := num + 1;
writeln('');
writeln('Enter seller number. To stop enter "0"');
readln(sellernr[num] );
end;
writeln('Seller Nr.':10, 'Date':14, 'Amount':16);
for count := 0 to num-1 do
begin
writeln(sellernr[count], day[count]:20,'/',month[count], sale[count]:14:2);
end;
writeln('');
writeln('ENTER to stop');
readln();
end.
如您所见,该程序会询问卖家编号,然后是DD MM格式的日期,然后是销售金额。
打印到屏幕上。
我接下来要做的是提供每季度的概述。我必须获取用户在数组卖家中定义的每个卖家编号,并将每季度的总销售额加起来。
我遇到问题的地方。如何获取存储在数组中的值并识别它们何时相等(即当sellernr [x]和sellernr [y]相同时)
假设用户输入的卖家号码是10,50,100。我必须只接受与第一个10对应的数组元素并打印输出,例如:
Seller Quarter1 Quarter2 Quarter3 Quarter4
10 $360.32 $567.21 $988.27 $1023.66
如何获取与特定用户定义值相对应的sellernr[x]
?
答案 0 :(得分:1)
您可以这样做:
program Verkopers;
var
SalesByQuarters:array[1..4]of real;//here you will store the sums
ReporSeller:integer; //this will be the nr. of the seller for report
begin
...//append your code with this
writeln('Write seller number for report: ');
readln(ReporSeller);
for count:=1 to 4 do
SalesByQuarters[count]:=0;//initialize the output variables for each quarter
for count:= Low(sellernr) to High(sellernr) do
begin
if sellernr[count]=ReporSeller then //count will store the proper index
SalesByQuarters[((month[count]-1) div 3) +1]:=
SalesByQuarters[((month[count]-1) div 3) +1]+sale[count];
end; //((month[count]-1) div 3) +1 gives us the number of quarter.
// You can get it in a different way (if/case/etc)
write('Report for seller ');
writeln(ReporSeller);
for count:=1 to 4 do
begin
write('Quarter ');
writeln(count);
writeln(SalesByQuarters[count]:14:2); //output the stored sums
end;
readln;
end.
您还可以通过以下方式美化输出:
writeln('Seller Quarter1 Quarter2 Quarter3 Quarter4');
writeln(ReporSeller:6,
' $', SalesByQuarters[1]:7:2,
' $', SalesByQuarters[2]:7:2,
' $', SalesByQuarters[3]:7:2,
' $', SalesByQuarters[4]:7:2);