我正在学习C ++类,我们已经找到了指针。我们得到的分配基本上是通过将指针作为各种函数的参数传递来对从文本文件中读取的数组进行冒泡排序。我认为我有一个不错的设置可以输出所需的内容,但是对于特定的操作,当数组中没有写入任何内容时,我将得到零作为元素。
Sub Test2()
' Test2 Macro
Dim FileName As String
FileName = ""
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select File"
.Filters.Add "Excel File", "*.xls?"
.AllowMultiSelect = False
If .Show Then
FileName = .SelectedItems(1)
End If
End With
If Len(FileName) < 4 Then Exit Sub 'No file selected
Dim TempWorkbook As Workbook, CurrentSheet As Worksheet
Set CurrentSheet = ActiveSheet 'Store the ActiveSheet, it will change
Set TempWorkbook = Workbooks.Open(FileName, ReadOnly:=True)
CurrentSheet.Range("A8").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(16, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A9").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(16, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A10").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(16, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A11").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(16, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A134").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(214, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A135").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(214, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A136").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(214, 3).Address(True, True, xlR1C1, True)
CurrentSheet.Range("A137").FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells(214, 3).Address(True, True, xlR1C1, True)
TempWorkbook.Close SaveChanges:=False
Set TempWorkbook = Nothing
Set CurrentSheet = Nothing
End Sub
我的输出如下:
原始数组数据: [8 4 7 2 9 5 6 1 3] 经过简单的气泡排序: [0 1 2 3 4 5 6 7 8] 现在下降: [9 8 7 6 5 4 3 2 1]
关于为什么我的第二种排序为零的任何想法吗?谢谢!
答案 0 :(得分:1)
您必须执行此更改是在bubble_sort函数中
for (j = 1; j < last - i; j++)
{
//Use the function pointer to determine which logic to use
if (ptr(arr[j-1], arr[j]))
{
swap(arr[j-1], arr[j]);
swapped = true;
}
}