You can email Donn at this address: donn_s_miller [AT] hotmail.com
Comments: Following is an anomaly, discovered in the course of debugging, which I cannot fathom. For some reason, a(205).e is changed from its proper value of CHR$(1) to a value of CHR$(255). Wha hoppen? For this test, only the MAIN routine and the SHOW subroutine are used. The program follows.
Best regards,
Donn S. Miller
Code: Select all
'This program is intended to facilitate computations with large polynomials.
'For the purpose of this program, a polynomial is deemed to be made up of a
'group of terms. The group is defined by referring to the index of its first
'and its last term. A term consists of a coefficient -- a long integer -- and
'a series of n exponents of variables -- 1-byte strings grouped together in a
'STRING * n -- taken in a certain order.
'
DECLARE SUB add (af%, al%, bf%, bl%, cf%, cl%)
DECLARE SUB create (first%, last%)
DECLARE SUB edit (first%, oldlast%, newlast%)
DECLARE SUB multiply (af%, al%, bf%, bl%, cf%, cl%)
DECLARE SUB show (first%, last%)
DECLARE SUB subtract (af%, al%, bf%, bl%, cf%, cl%)
CLS
n% = 1: GOTO main' TODO: After debugging, remove this line.
DO
INPUT "Enter number of variables. ", n%
LOOP WHILE ((n% < 0) OR (n% > 255))
TYPE term
c AS LONG
e AS STRING * n
END TYPE
DIM a(1999) AS term
main:
a(205).c = 1&
a(205).e = CHR$(1)
a(206).c = -1&
a(206).e = CHR$(0)
show 205, 205
END
SUB add (af%, al%, bf%, bl%, cf%, cl%)
SHARED a() AS term
ptr% = cf%
FOR ia% = af% TO al%
a(ptr%) = a(ia%)
ptr% = ptr% + 1
NEXT
FOR ib% = bf% TO bl%
a(ptr%) = a(ib%)
ptr% = ptr% + 1
NEXT
edit cf%, cf% + (al% - af% + 1) + (bl% - bf% + 1) - 1, d%
cl% = d%
END SUB
SUB create (first%, last%)
SHARED a() AS term, n%
pointer% = first%
INPUT "coefficient=", zc&
DO UNTIL (zc& = 0&)
a(pointer%).c = zc&
ze$ = ""
FOR i% = 1 TO n%
DO
PRINT "exponent"; STR$(i%); "=";
INPUT u%
LOOP UNTIL ((u% > -1) AND (u% < 256))
ze$ = ze$ + CHR$(u%)
NEXT
a(pointer%).e = ze$
pointer% = pointer% + 1
INPUT "coefficient=", zc&
LOOP
last% = pointer% - 1
END SUB
SUB edit (first%, oldlast%, newlast%)
SHARED a() AS term
FOR i% = oldlast% - 1 TO first% STEP -1
sw% = 0
FOR j% = first% TO i%
IF (a(j%).e < a(j% + 1).e) THEN
SWAP a(j%), a(j% + 1)
sw% = -1
END IF
NEXT
IF (NOT sw%) THEN EXIT FOR
NEXT
coefficient& = a(first%).c
exponent$ = a(first%).e
pointer% = first%
FOR i% = first% + 1 TO oldlast%
IF (a(i%).e = exponent$) THEN
coefficient& = coefficient& + a(i%).c
ELSE
IF (coefficient& <> 0&) THEN
a(pointer%).c = coefficient&
a(pointer%).e = exponent$
pointer% = pointer% + 1
END IF
coefficient& = a(i%).c
exponent$ = a(i%).e
END IF
NEXT
a(pointer%).c = coefficient&
a(pointer%).e = exponent$
newlast% = pointer%
END SUB
SUB multiply (af%, al%, bf%, bl%, cf%, cl%)
DIM wk AS term
SHARED a() AS term, n%
ptr% = cf%
FOR ia% = af% TO al%
wk = a(ia%)
FOR ib% = bf% TO bl%
a(ptr%).c = wk.c * a(ib%).c
FOR jb% = 1 TO n%
wke% = ASC(MID$(wk.e, jb%, 1))
aibe% = ASC(MID$(a(ib%).e, jb%, 1))
MID$(a(ptr%).e, jb%, 1) = CHR$((wke% + aibe%) MOD 256)
NEXT
ptr% = ptr% + 1
NEXT
NEXT
edit cf%, cf% + (al% - af% + 1) * (bl% - bf% + 1) - 1, d%
cl% = d%
END SUB
SUB show (first%, last%)
' TODO: After debugging, remove all COLOR statements.
SHARED a() AS term, n%
FOR i% = first% TO last%
COLOR 12: PRINT a(i%).c; "("; : COLOR 7
FOR j% = 1 TO n%
ae$ = a(i%).e
COLOR 12: PRINT ASC(MID$(ae$, j%, 1)); : COLOR 7
IF (j% < n%) THEN
COLOR 12: PRINT ","; : COLOR 7
END IF
NEXT
COLOR 12: PRINT ") "; : COLOR 7
NEXT
PRINT
END SUB
SUB subtract (af%, al%, bf%, bl%, cf%, cl%)
SHARED a() AS term
ptr% = cf%
FOR ia% = af% TO al%
a(ptr%).c = a(ia%).c
a(ptr%).e = a(ia%).e
ptr% = ptr% + 1
NEXT
FOR ib% = bf% TO bl%
a(ptr%).c = -a(ib%).c
a(ptr%).e = a(ib%).e
ptr% = ptr% + 1
NEXT
edit cf%, cf% + (al% - af% + 1) + (bl% - bf% + 1) - 1, d%
cl% = d%
END SUB