BASIC

来自MalacoKnowledge
Malacology留言 | 贡献2023年10月19日 (四) 09:07的版本 →‎矩阵

BASIC 有三代

  • Dartmouth
  • Structed
  • Object-oriented

解释器

Dartmouth: Legacy Basic

基础

开头一般写行号×10,因为可以补救

  • REM 注释
  • PRINT 输出: "" 里包含字符串 , 或者 ; 负责分割数值和字符串
  • END 结束

解释器

前面不加行号

  • RUN 解释器运行
  • STOP 解释器停止运行
  • LIST 显示代码,后面可以跟行号

赋值

单个

  • LET 赋值 声明变量

多个

  • READ 批量未赋值声明
  • DATA 批量赋值,多出来的不处理,这里的 8 9 10
410 READ A1, A2, A3
420 READ B1, B2, B3, B4
430 DATA 1, 2, 3, 4, 5
440 DATA 6, 7, 8, 9, 10
450 PRINT A1, A2, A3,
460 PRINT B1, B2, B3, B4

流程控制

IF

  • IF 行号
310 IF S < 0 THEN 350

GOTO

  • GOTO 行号

FOR & NEXT

  • FOR 循环范围,步数
  • NEXT 结束循环
250 LET S = 0
260 FOR N = 1 TO 3 STEP 2
270 LET S = S + N
280 NEXT N

GOSUB & RETURN

  • GOSUB
  • RETURN 回到 GOSUB 位置
10 LET X = 2
20 IF X<0 THEN 60
30 X = X - 1
40 PRINT "X (BEFORE) IS ", X
50 GOSUB 20
60 PRINT "X (AFTER) IS ", X
70 IF X<0 THEN 90
80 RETURN
90 PRINT "END"
100 END

计算

函数

  • SIN() P/6=30度 P/3=60度 P/2=90度
  • COS()
  • TAN()
  • ATN() tan 的倒数
  • EXP() e^()
  • LOG()
  • SQR() 平方根
  • ABS() 绝对值
  • RND随机数 ## 表达式
  • >
  • <
  • =
  • <> 不等于
  • >=
  • <=

矩阵

DIM 数组/矩阵

10 DIM A(10) 
20 FOR I = 0 TO 10
30 A(I) = I * 2  
40 NEXT I
50 FOR I = 0 TO 10
60 PRINT "A("; I; ") = "; A(I)
70 NEXT I
80 END

示例

10 REM INTRO TO BASIC PROGRAMMING: REM & PRINT
20 PRINT "HELLO, THIS IS BASED ON FIRST BASIC INSTUCTION MANUAL (1964)"
30 REM INTERGRES AND FLOAT: LET & EXPO & LOG & SQR & ABS
40 LET X=2
50 PRINT "X IS ", X
60 LET Y=(7+8)/3
70 PRINT "Y=(7+8)/3 IS ", Y
80 LET Z=2/3
90 PRINT "Z=2/3 IS ", Z
100 LET A = Y ^ X * 5
110 PRINT "A=Y^X IS ", A
120 LET B = 2* ( (P/6) + COS(P/3) )
130 PRINT "2(SIN30+COS60) IS ", B
140 LET C = EXP(0)
150 PRINT "EXPO(0) IS ", C, " WHICH MEANS E^0"
160 LET D = LOG(1)
170 PRINT "LOG(0) IS ", D
180 LET E = SQR(4)
190 PRINT "SQR(4) IS ", E
200 LET F = ABS(-12)
210 PRINT "ABS(-12) IS ", F
220 PRINT "THE CALCULATION RESULTS AVOBE ARE ", A, B, C, D, E, F
230 REM  FOR: FOR & NEXT
240 PRINT "FOR IS STARTING NOW"
250 LET S = 0
260 FOR N = 1 TO 3 STEP 2
270 LET S = S + N
280 NEXT N
290 PRINT S
300 REM GOTO, IF THEN. = > < (<> not equal) ALL ARE ALLOWED: GOO & IF & THEN
310 IF S < 0 THEN 350
320 LET S = S - 1
330 PRINT "NOW S IS ", S
340 GOTO 310
350 PRINT "IF IS END. S is "; S 
360 REM DEF: DEF
370 DEF FNC(G) = G^3 + 2.7*G
380 LET H = FNC(Y)
390 PRINT H
400 REM READ, DATA. LET BY BATCH: READ & DATA
410 READ A1, A2, A3
420 READ B1, B2, B3, B4
430 DATA 1, 2, 3, 4, 5
440 DATA 6, 7, 8, 9, 10
450 PRINT A1, A2, A3,
460 PRINT B1, B2, B3, B4
470 END