EasyManua.ls Logo

Commodore PET - Page 205

Commodore PET
447 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Step 3: Add the corresponding digits of the strings.
At
first glance. vou
might
assume
that
A$
and B$ can
now
be
added using the following statement:
C$=A$+B$
This
is
incorrect. When strings are added
with
a plus sign they are not added.
but are concatenated:
C$=A$+B$
C$=
[112131415161718IgI0111213/415161
+
1~]~I~I~!~M~I~Th17J914[3151*1
C$=
111213/41516/718Ig/0
/112j31415161~1~1~1~1~1~1~1~15171*1*1*1
We
want
to
add the
digits
in the strings.
not
concatenate the strings. To add
the contents of numeric strings. each digit must be extracted separately
from the string. converted into a numeric digit. then added to
one
digit from
the
other string. This
is
done
with
a routine using the
two
string functions
VAL
and MID$.
1020
FOR
I=LEN(A$)
TO
1
8TEP-l
1030
A=VAL(MID$(A$,I,l»
1050
B=VAL(MID$(B$,I,l»
11
f10
NE:X:T
1
where: A =
digit
extracted from
A$
B =
digit
extracted from
B$
''1''
is
a counter initialized
to
the length of the INPUT strings (either
A$
or B$
may
be
usedl.
With
each
FOR
...
NEXT loop iteration. the value of 1
is
decre-
mented by
1.
As 1decrements. it allows the string contents to
be
extracted one by
one. right
to
leH.
using the
MID$
function:
1
MID$(B$.1.1)
16
~~~~~~~~5794357~
15
~~~~~~~~579435.2
14
~~~~~~~~57943.72
13
~~~~~~~~5794.572
12
~~~~~~~~579.3572
11
~~~~~~~~57~43572
10
~~~~~~~~5.943572
9
~~~~~~~~.7943572
8
~~~~~~~.57943572
7
~~~~~~.~57943572
6
~~~~~.~~57943572
5
~~~~.~~~57943572
4
~~~.~~~~57943572
3
~~.~~~~~57943572
2
~.~~~~~~57943572
1
~~~~~~~~57943572
792

Table of Contents

Related product manuals