特點
現狀
微軟在較新版本的Windows中不再集成QBASIC。不過Windows 98的用戶可以在光碟的\TOOLS\OLDMSDOS目錄中找到它,在Windows 95的光碟中,它存放在\OTHER\OLDMSDOS目錄中。微軟網站對它的技術支持只對
MS-DOS的授權用戶有效。
QBASIC擁有一個值得稱道的
集成開發環境和一個功能強大的集成調試器,這一切在那個時代讓人耳目一新。直到今天,QBASIC依然是許多面向初學者的編程書籍的主題。
語法
和Quick BASIC類似而又不同於微軟其他
BASIC的早期實現版本的是,QBASIC是一種結構化的程式語言。和
GW-BASIC相比,QBASIC的主要改進是:
擴充了變數和常量的類型
變數名長度:40個字元
提供了新的選擇結構
條件語句
IF <條件> THEN <語句組1> ELSE <語句組2>
多分支語句SELECT
WHILE <條件>
WEND
【DO循環】
DO
WHILE <條件> <循環體>
子程式和函式作為單獨的模組
不需要行號
代碼示例(賦值,求和運算,並顯示結果)
LET A=10LET B=20LET C=A+BPRINT C
在編寫程式時為了節省時間,可以不用寫LET;並且PRINT在編寫程式時直接用“?”代替。有一點非常重要,在參加競賽時,程式第一行必須是CLS!
快捷鍵
QBASIC 7.1快捷鍵:
F5:運行程式及繼續運行被中斷的程式;
Ctrl+Break:中斷正在運行的程式;
Shift+F5:從第一條語句開始重新運行程式;
F4:當程式中斷運行時,查看運行結果螢幕,再按一次F4則切換回代碼螢幕;
F1:獲得幫助。
F2:切換主程式或SUB、FUNCTION。
Alt+Enter:全螢幕。
Shift+F5:在程式終止時重新從頭執行。
Ctrl+Insert:選定文字後複製到
剪下板(Ctrl+C無效)
Shift+Insert:將剪貼簿中的文字貼上。(Ctrl+V無效)
範例
範例1:
菜場上一公斤香菇是7.5元,編一個程式,從鍵盤上輸入重量,計算機自動算出其總價
INPUT XSUM=7.5*XPRINT SUMEND
範例2:
賣西瓜,10千克以下每千克1.4元,10千克以上每千克0.7元,輸入要買多少千克(必須為自然數),輸出需要多少錢。
INPUT XIF X<>FIX(X) THEN ? "NO":GOTO 1IF X>10 THEN SUM=10*1.4+(X-10)*0.7 ELSE SUM=X*1.4PRINT SUMEND
範例3:
輸入10個字母,比較大小,輸出最大的一個
DIM A$(10)FOR I=1 TO 10 INPUT A$(I)NEXT IB$=A$(1)FOR I=2 TO 10 IF B$<A$(I) THEN B$=A$(I)NEXT IPRINT B$END
範例4:
一個遊戲:
CLS '清屏LOCATE 5, 38 '定位到5,38PRINT "Game" '顯示LOCATE 16, 34 '定位到16,34PRINT "Press Enter" '顯示DOLOOP WHILE INKEY$ = "" '暫停DIM Name1$, Name2$, HP1, HP2, Temp$CLS '清屏LOCATE 8 '定位至第8行PRINT " Input 1st player's name"; '輸入玩家1的名字INPUT Name1$PRINT " Input 2nd player's name"; '輸入玩家2的名字INPUT Name2$RANDOMIZE TIMERHP1 = INT(RND * 50) + 50 '隨機決定玩家1的HPHP2 = INT(RND * 50) + 50 '隨機決定玩家2的HPPRINT STRING$(12, " ") + Name1$ + "'s HP is " + STR$(HP1) '顯示玩家1的HPPRINT STRING$(12, " ") + Name2$ + "'s HP is " + STR$(HP2) '顯示玩家2的HPPRINT " Press any key to contiune"DOLOOP WHILE INKEY$ = "" '暫停DIM Row, Text$, Attack, IText$ = "n"Row = 0CLSDO '循環 PRINT PRINT Name1$ + "'s HP:" + STRING$(16 - LEN(Name1$), " "); PRINT STRING$(INT(HP1 / 2), 2) PRINT PRINT Name2$ + "'s HP:" + STRING$(16 - LEN(Name2$), " "); PRINT STRING$(INT(HP2 / 2), 2) '以上這幾句是用圖形來表示HP FOR I = 1 TO 4 PRINT NEXT '顯示空行 Row = Row + 1 '回合數+1 PRINT STRING$(11, " ") + STR$(Row) + "." PRINT STRING$(12, " ") + Name1$ + " now has HP " + STR$(HP1) PRINT STRING$(12, " ") + Name2$ + " now has HP " + STR$(HP2) '以上幾句顯示回合數和HP數 DO LOOP WHILE INKEY$ = "" '暫停 Attack = INT(RND * 20) '隨機決定攻擊點數 Text$ = STRING$(12, " ") + Name1$ Text$ = Text$ + " attack " + Name2$ + " " + STR$(Attack) '先想好要顯示的文字 HP2 = HP2 - Attack '用玩家2的HP減去攻擊點數 PRINT Text$ '顯示文字 DO LOOP WHILE INKEY$ = "" '暫停 Attack = INT(RND * 20) '隨機決定攻擊點數 Text$ = STRING$(12, " ") + Name2$ Text$ = Text$ + " attack " + Name1$ + " " + STR$(Attack) '先想好要顯示的文字 HP1 = HP1 - Attack '用玩家1的HP減去攻擊點數 PRINT Text$ '顯示文字 IF HP1 <= 0 OR HP2 <= 0 THEN EXIT DO 如果玩家1或2的HP小於等於0就退出循環 DO LOOP WHILE INKEY$ = "" '暫停 CLSLOOPIF HP1 <= 0 THEN PRINT STRING$(12, " ") + Name2$ + " win"IF HP2 <= 0 THEN PRINT STRING$(12, " ") + Name1$ + " win"IF HP1 <= 0 AND HP2 <= 0 THEN PRINT STRING$(12, " ") + "Double win!"
這裡有一個超長的畫圖程式:
DRAWTOOL.BAS
CLS
DO
SCREEN 1
SCREEN 0
PRINT TAB(16); "Draw tool"
PRINT STRING$(40, "*")
PRINT "Help:"
PRINT "Press highlighted letter."
FOR I = 1 TO 8
READ S$, S
GOTO PrintHLWord
PrintHLWordBack:
NEXT I
RESTORE
DATA "HorizontalBAR", 1, "UprightBAR", 1, "CrossBARs", 1, "BiasBAR1", 8
DATA "BiasBAR2", 8, "DiagonalBARs", 1, "DiffuseBARs", 2, "Exit", 1
ElseKey:
K$ = INPUT$(1)
SELECT CASE UCASE$(K$)
CASE "H"
GOTO HorizontalBAR
HorizontalBARBack:
CASE "U"
GOTO UprightBAR
UprightBARBack:
CASE "C"
GOTO CrossBARs
CrossBARsBack:
CASE "1"
GOTO BiasBAR1
BiasBAR1Back:
CASE "2"
GOTO BiasBAR2
BiasBAR2Back:
CASE "D"
GOTO DiagonalBARs
DiagonalBARsBack:
CASE "I"
GOTO DiffuseBARs
DiffuseBARsBack:
CASE "E"
END
CASE ELSE
GOTO ElseKey
END SELECT
LOOP
END
PrintHLWord:
IF S = 1 THEN
COLOR 15
PRINT LEFT$(S$, 1);
COLOR 7
PRINT MID$(S$, 2)
ELSE
COLOR 7
PRINT LEFT$(S$, S - 1);
COLOR 15
PRINT MID$(S$, S, 1);
COLOR 7
PRINT MID$(S$, S + 1)
END IF
GOTO PrintHLWordBack
HorizontalBAR:
SCREEN 1
FOR I = 1 TO 200
PSET (I, 100)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO HorizontalBARBack
UprightBAR:
SCREEN 1
FOR I = 1 TO 200
PSET (100, I)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO UprightBARBack
CrossBARs:
SCREEN 1
FOR I = 1 TO 200
PSET (I, 100)
PSET (100, I)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO CrossBARsBack
BiasBAR1:
SCREEN 1
FOR I = 1 TO 200
PSET (I, I)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO BiasBAR1Back
BiasBAR2:
SCREEN 1
FOR I = 1 TO 200
PSET (I, 200 - I)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO BiasBAR2Back
DiagonalBARs:
SCREEN 1
FOR I = 1 TO 200
PSET (I, I)
PSET (I, 200 - I)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO DiagonalBARsBack
DiffuseBARs:
SCREEN 1
FOR I = 1 TO 200
PSET (I, 100)
PSET (100, I)
PSET (I, I)
PSET (I, 200 - I)
DO: LOOP WHILE INKEY$ = ""
NEXT I
SCREEN 0
GOTO DiffuseBARsBack
路程圖:
CLS
輸入
計算
輸出
END