Step 4:
Whether
the final answer
is
to
be
negative or positive. the value of
A$
is
now larger than
B$.
We
can now perform simple subtraction. The subtrac-
tion subroutine
occurs at lines 1000 to 1080. The routine
is
taken directly from
lines 1020 to 1100. step 3 of the
"Addition
via Numeric Strings" program (page
192). because the digits are extracted from the strings in the same manner.
However. at line 1050. the carry variable 0
is
now
used
as
a
"borrow"
variable. If
(A-B)
<O.
then a tens
digit
must
be
borrowed from the direct left column to in-
crease the value of A by
10.
0
is
set to
-1
because a
"1"
is
being borrowed.
therefore decreasing the value of the
direct
left column. Once A
is
larger than
B.
B
is
subtracted from
A.
placing the result in
C:
1
0013
REt'l~~SUBTRACT
I
ON
ROUT
INE
••
113113
FOR
I~LEN(A$)
TO
1
8TEP-l
113213
A=VAL(MID$(A$,I,l»
1
(1313
A=A+
D:
D~0
10413
B=VAL(MID$(B$,I,l»
10513
IF
(A-B)<e
THEN
D=-lA=A+le
10613
C=A-B
A
-8
C
1
~
2
ÇJ
2
........
+~+~+10
3
3~
4)1l
516
7 8
005
7 9 4
3 3 9 8 8 4
+9
8"+?+~O
2
3 5 7 2
5 4 4 0
Step 5: Concatenate the answer into a one-string result. This function
is
taken directly from line 1090 of the
"Addition
via Numeric Strings" (page 194)
program. except that the N
is
replaced
with
a
1.
Since there
will
never
be
a final
carry
as
there
is
in addition. only one
digit
will
be
used in every iteration. In our
subtraction program. concatenation of the individual answers into one result oc-
curs at line 1070.
113713
C$=RIGHT$(STR$(C),l)+C$
Step 6: ln subtraction.
it
is
possible to pick up leading zeros in the final
answer.
We
eliminate these leading zeros before printing the answer. The
FOR
...
NEXT loop in lines 1090 to 1120 checks and eliminates allleading zeros, using
the VAL function and variable L
as
a counter.
10913
FOR
I~l
TO
LEN(C$)
11013
IF
VAL(MID$(C$,I,1»=0
THEN
L=L+i
11113
IF
VAL(LEFT$(C$,I»<>O
THEN
I=LEN(C$)
112(1
NE:>~T
1
The
FOR
...
NEXT loop.
which
iterates from 1 to the length of the answer
CS.
searches for leading zeros or blanks by extracting each
digit
from
C$
and com-
paring it to
zero.
It compares digits from left to right. If
it
identifies a zero or blank.
counter variable L
is
incremented by 1 (statement 1100). As soon
as
the first non-
zero or non-blank character
is
encountered. loop counter 1
is
set to the length of
the string so the program may drop out of the loop immediately.
205