所以我对QB64并不太熟悉,文档很少,所以我遇到了麻烦。
TYPE character
FName AS STRING * 12
LName AS STRING * 12
ID_Num AS INTEGER
Year AS STRING * 2
GPA AS DOUBLE
END TYPE
DIM LENGTH AS INTEGER
LENGTH = 11
REDIM chars(0) AS character
CLS
n$ = "names1.txt"
OPEN n$ FOR INPUT AS #1
k = -1
WHILE (NOT (EOF(1)))
k = k + 1
REDIM _PRESERVE chars(k) AS character
INPUT #1, chars(k).FName, chars(k).LName, chars(k).ID_Num, chars(k).Year,
chars(k).GPA
WEND
CLOSE #1
CALL sortArray(chars(), LENGTH)
SUB sortArray (score() AS INTEGER, SIZE AS INTEGER)
DIM x AS INTEGER
DIM y AS INTEGER
DIM COMPS AS INTEGER
x = 0
y = 0
COMPS = SIZE - 1
WHILE y < COMPS
x = 0
WHILE x < COMPS
IF score(x) > score(x + 1) THEN
CALL swap2(score(), x)
END IF
x = x + 1
WEND
y = y + 1
WEND
END SUB
SUB swap2 (score() AS INTEGER, x AS INTEGER)
DIM temp AS INTEGER
temp = score(x + 1)
score(x + 1) = score(x)
score(x) = temp
END SUB
我收到错误 - “在下一行传递给sub的数据类型不正确”:
CALL sortArray(chars(), LENGTH)
我假设自从我上面使用REDIM以来,sub没有正确处理它,但我不确定如何解决这个问题。最终我试图将文件读入数组,对该数组进行排序,然后将其打印给用户。目前我对如何对阵列进行实际排序感到困惑。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:2)
您传递的数组chars()
包含character
类型的元素,但您的子例程只接受包含INTEGER
类型元素的数组。
您需要的是score() AS character
(更好地命名为chars() AS character
),您可以根据自己的标准进行排序(例如IF score(x).GPA > score(x+1).GPA THEN ...
)。此外,有一个built-in SWAP
statement会为你换掉东西,所以没有必要自己编写(除非你应该这样做)。
答案 1 :(得分:0)
以下是您的代码(经过大量修改):
1)正确声明了UDT。 2)redim仅在声明$ dynamic时有效。 3)&#34;冒泡排序&#34;更短。
REM $DYNAMIC
TYPE character
FName AS STRING * 12
LName AS STRING * 12
ID_Num AS INTEGER
Year AS STRING * 2
GPA AS DOUBLE
END TYPE
DIM chars(0) AS character
n$ = "names1.txt"
OPEN n$ FOR INPUT AS #1
WHILE NOT EOF(1)
k = k + 1
REDIM _PRESERVE chars(k) AS character
INPUT #1, chars(k).FName, chars(k).LName, chars(k).ID_Num, chars(k).Year, chars(k).GPA
WEND
CLOSE #1
CALL SortArray(chars(), k)
END
SUB SortArray (score() AS character, SIZE AS INTEGER)
' bubble sort
FOR x = 1 TO SIZE
FOR y = x + 1 TO SIZE
IF score(x).GPA > score(y).GPA THEN
SWAP score(x), score(y)
END IF
NEXT
NEXT
END SUB