RosettaCodeData/Task/Variables/Commodore-BASIC/variables-1.basic

123 lines
4.1 KiB
Plaintext

1 rem rosetta code
5 rem commodore basic variable demonstration
10 print chr$(147);chr$(14);:ti$="000000":rem see lines 420-460
15 rem numeric variables default to 0; strings default to empty
20 print a:print b%:print c$:print
25 :
30 rem no symbol after variable defaults to float.
35 let a=1.7
40 rem "let" is not required and rarely used.
45 b=2.42
50 print a:print b
55 rem % means integer type; digits after decimal are truncated
60 b%=1.7
65 print b%
70 rem $ means string type
75 c$="Commodore"
80 print c$:print
85 :
90 rem each type is unique, even when name is "same"
95 a=5.0
100 a%=9
105 a$="twenty-five"
110 print a:print a%:print a$:print
115 :
120 rem names unique only to two characters; extra ignored
125 li=10:lives=8:lights=64
130 print li:print lives:print lights:print
135 rem second character can be alphanumeric, but is not array
140 s1=100 : s2=200 : s3=300
145 print s1:print s2:print s3:print
150 gosub 5000
155 :
160 rem strings preserve all literal characters
165 rem numerics drop leading zeros and trailing zeros after decimal
170 n$="01276":print n$:rem 01276
175 o%=01276:print n%: rem 1276
180 p=4.900:print p: print: rem 4.9
185 :
190 rem string-numeric conversion functions
195 c$="05034"
200 c%=val(c$) : rem converts to the numeric value of 5034 (first zero dropped)
205 d=123.45600 : rem define a float
210 d$=str$(d) : rem converts above into a string
215 print c$:print c%:print d:print d$:print
218 :
220 rem strings can be ordered/compared > or < like numbers
225 input "Enter a string";x$:print
230 input "Enter another string";y$:print
235 if x$>y$ then print x$;" comes after ";y$
240 if x$<y$ then print x$;" comes before ";y$
245 if x$=y$ then print "You entered the same string twice!"
250 gosub 5000
255 :
260 rem numbers have a leading character for pos/neg sign
265 rem " " means positive
270 a=-52:b=124
275 print a:print b:print
280 :
285 rem variable operations
290 e$="endothermic":print e$
295 print left$(e$,3) : rem "end"
300 print right$(e$,3) : rem "mic"
305 print mid$(e$,4,5) : rem "other"
310 print
315 a=5:b=20:c=a+b:print a;"+";b;"=";c : rem addition
320 q=90:r=60:s=q-r:print s : rem subtraction
325 x=3:y=4:z=x*y:print x;"*";y;"=";z : rem 12 multiplication
330 l=12:m=16:n=l/m:print l;"/";m;"=";n :rem division
335 rem string concatenation
340 print
345 f$="John":l$="Jones":n$=l$+", "+f$:print f$:print l$:print n$
350 gosub 5000
355 :
360 rem arrays can be single or multidimensional
365 rem array index starts at 0
370 rem single dimenstion arrays of 11 elements or less
375 rem do not need to be DIMensioned
380 a$(0)="first":a$(1)="second":a$(3)="third":rem we skipped index 2
385 for i=0 to 3:print a$(i):next
390 gosub 5000
395 dim b(1,20) : rem 42 elements
400 for i=0 to 1:for j=0 to 20:b(i,j)=(i+1)*j:next j,i
405 for i=0 to 1:print chr$(19):for j=0 to 20:print tab(i*6);b(i,j):next j,i
410 gosub 5000
415 :
420 rem special variables - ti and st are technically functions,
425 rem but ti$ can be assigned a value similar to a string variable
430 t$=left$(ti$,2)+":"+mid$(ti$,3,2)+":"+right$(ti$,2)
435 print "Ticks since program started:":print ti
440 print "Elapsed time since program started:":print t$
445 print "I/O Status:";st : rem i/o status
450 print:print "Enter new time (HHMMSS): ";:gosub 5200
455 ti$=t$:gosub 5500
460 print
465 :
470 rem all variables can be cleared with the "clr" statement
475 rem however, this also clears the return address stack for subroutines
480 rem making "return" not possible.
485 print "Before CLR:":print a:print a$:print a$(0):print b:print c
490 clr:print:print "After CLR:"
495 print a:print a$:print a$(0):print b:print c
500 print
505 end
600 :
700 rem supporting subroutines
800 :
5000 rem screen pause routine
5005 print:print "press a key to continue"
5010 get k$:if k$="" then 5010
5015 print chr$(147):return
5020 :
5200 rem custom time input routine
5205 t$="":for d=1 to 6
5210 get k$:if k$<"0" or k$>"9" then 5210
5215 t$=t$+k$:print k$;:next
5220 return
5230 :
5500 rem display live clock
5505 print chr$(147):print:print "Press a key to continue."
5510 print chr$(19);"Time: "left$(ti$,2)":"mid$(ti$,3,2)":"right$(ti$,2);
5515 get k$:if k$="" then 5510
5520 print chr$(147);:return