Sunday, July 28, 2019
HOW TO PROGRAM A CALCULATOR USING QBASIC
Using QBasic is not very common with most of the programmers. QBasic is a very good start for learner in programming. In this write up you'd see how to program a calculator that works perfectly. although it is just a simple calculator but it is a good start for start for new born programmer. I hope you find it very useful and visit this blog for more of it kind.
Before you start you should have installed the QBasic software but let me recommend QBasic 64 for you commonly known as qb64. After the installation you can get started.
CLS
SCREEN 13
PRINT "CALCULATOR-"
PRINT "ENTER YOUR EXPRESSION"
PRINT "EXAMPLE-58 * 20"
PRINT "THE TERMS SHOULD BE SEPARATED BY SPACE OR ERROR WILL OCCUR"
INPUT "ENTER YOUR EXPRESSION-"; EXP$
EXP$ = EXP$ + " "
INDEX = 1
DIM TERMS(3) AS INTEGER
FOR I = 1 TO LEN(EXP$)
CURRENTTOKEN$ = MID$(EXP$, I, 1)
IF CURRENTTOKEN$ = " " THEN
IF INDEX = 2 AND OPERATION$ = "" THEN
OPERATION$ = TERMCURRENT$
ELSE
TERMS(INDEX) = VAL(TERMCURRENT$)
INDEX = INDEX + 1
END IF
TERMCURRENT$ = ""
ELSE
TERMCURRENT$ = TERMCURRENT$ + CURRENTTOKEN$
END IF
NEXT I
SELECT CASE OPERATION$
CASE "+"
R = TERMS(1) + TERMS(2)
CASE "*"
R = TERMS(1) * TERMS(2)
CASE "-"
R = TERMS(1) - TERMS(2)
CASE "/"
R = TERMS(1) / TERMS(2)
CASE "%"
R = TERMS(1) MOD TERMS(2)
CASE "^"
R = TERMS(1) ^ TERMS(2)
END SELECT
PRINT TERMS(1); OPERATION$; TERMS(2); "="; R
Now you've finished coding you should now run your program.
Note : to run your program, you save it with .bas extension other wise it will not work.
Labels:
Technology
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment