传递给子例程的数组类型不正确
Incorrect array type being passed to a subroutine
所以我对 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
我收到一个错误 - "Incorrect array type passed to sub" 在以下行:
CALL sortArray(chars(), LENGTH)
我假设因为我在上面使用了 REDIM,所以 sub 没有正确处理它,但我不确定如何解决这个问题。最终,我试图将文件读入数组,对该数组进行排序,然后将其打印给用户。目前我不知道如何对数组进行实际排序。
如有任何帮助,我们将不胜感激。谢谢!
您正在传递包含 character
类型元素的数组 chars()
,但您的子例程只接受包含 INTEGER
.
类型元素的数组
你需要的是 score() AS character
(更好地命名为 chars() AS character
),你可以根据你想要的任何标准进行排序(例如 IF score(x).GPA > score(x+1).GPA THEN ...
)。此外,还有一个 built-in SWAP
statement 可以为您交换东西,因此无需自己编写(除非您应该这样做)。
这是您的代码(经过大量修改),其中:
1)正确声明了UDT。
2) redim 仅在 $dynamic 声明时有效。
3) "bubble sort" 较短。
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
所以我对 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
我收到一个错误 - "Incorrect array type passed to sub" 在以下行:
CALL sortArray(chars(), LENGTH)
我假设因为我在上面使用了 REDIM,所以 sub 没有正确处理它,但我不确定如何解决这个问题。最终,我试图将文件读入数组,对该数组进行排序,然后将其打印给用户。目前我不知道如何对数组进行实际排序。
如有任何帮助,我们将不胜感激。谢谢!
您正在传递包含 character
类型元素的数组 chars()
,但您的子例程只接受包含 INTEGER
.
你需要的是 score() AS character
(更好地命名为 chars() AS character
),你可以根据你想要的任何标准进行排序(例如 IF score(x).GPA > score(x+1).GPA THEN ...
)。此外,还有一个 built-in SWAP
statement 可以为您交换东西,因此无需自己编写(除非您应该这样做)。
这是您的代码(经过大量修改),其中:
1)正确声明了UDT。 2) redim 仅在 $dynamic 声明时有效。 3) "bubble sort" 较短。
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