写一个Pascal程序(输入一个整数数组A,进行升序排列,也进行降序排列,最后输出数组A,升序排列后的数组A,还有降序排列后的数组A)
时间: 2024-10-18 07:19:47 浏览: 52
当然可以。下面是一个简单的Pascal程序示例,用于输入一个整数数组并进行升序和降序排序:
```pascal
program SortArray;
uses
SysUtils;
var
A: array of Integer;
Size: Integer;
I, J, Temp: Integer;
// 输入数组
procedure InputArray();
begin
Write('请输入数组元素的数量: ');
ReadLn(Size);
SetLength(A, Size);
for I := 0 to Size - 1 do
begin
Write('请输入第 ' + IntToStr(I+1) + ' 个元素: ');
Read(A[I]);
end;
end;
// 升序排序
procedure BubbleSortAscending(A: array of Integer);
var
swapped: Boolean;
begin
swapped := True;
repeat
swapped := False;
for I := 0 to High(A) - 1 do
if A[I] > A[I + 1] then
begin
Swap(A[I], A[I + 1]);
swapped := True;
end;
until not swapped;
end;
// 降序排序
procedure BubbleSortDescending(A: array of Integer);
var
swapped: Boolean;
begin
swapped := True;
repeat
swapped := False;
for I := 0 to High(A) - 1 do
if A[I] < A[I + 1] then
begin
Swap(A[I], A[I + 1]);
swapped := True;
end;
until not swapped;
end;
// 输出数组
procedure OutputArray(A: array of Integer; Descending: Boolean);
begin
if Descending then
Writeln('降序排列后的数组: ')
else
Writeln('升序排列后的数组: ');
for I := Low(A) to High(A) do
Writeln(IntToStr(A[I]));
Writeln('');
end;
begin
InputArray();
BubbleSortAscending(A);
OutputArray(A, False);
// 降序排列
BubbleSortDescending(A);
OutputArray(A, True);
end.
```
这个程序首先通过`InputArray`函数获取用户输入的整数数组,然后分别调用`BubbleSortAscending`和`BubbleSortDescending`进行升序和降序排序,最后使用`OutputArray`函数输出排序后的数组。
阅读全文
相关推荐













