Пример программы, которая выполняет все арифметические действия с бинарными числами

Складывает, вычитает, умножает и делит бинарные числа.

code: #basic
DECLARE FUNCTION decTObin$ (number&)
DECLARE FUNCTION binTOdec! (st$)
 
DO
 
        DO
                CLS
                CLEAR
                bin = 1
                f = 0
                LINE INPUT "Enter first number : "; a$
                FOR i = 1 TO LEN(a$)
                        IF (ASC(MID$(a$, i, 1)) < 48 OR ASC(MID$(a$, i, 1)) > 57) AND ASC(MID$(a$, i, 1)) <> 45 THEN f = 1: EXIT FOR
                        IF ASC(MID$(a$, i, 1)) > 49 AND ASC(MID$(a$, i, 1)) < 58 THEN bin = 0
                NEXT i
 
                LINE INPUT "Enter second number: "; b$
                FOR i = 1 TO LEN(b$)
                        IF (ASC(MID$(b$, i, 1)) < 48 OR ASC(MID$(b$, i, 1)) > 57) AND ASC(MID$(a$, i, 1)) <> 45 THEN f = 1: EXIT FOR
                        IF ASC(MID$(b$, i, 1)) > 49 AND ASC(MID$(b$, i, 1)) < 58 THEN bin = 0
                NEXT i
        LOOP UNTIL f <> 1
 
        IF bin = 1 THEN
        a& = binTOdec(a$)
        b& = binTOdec(b$)
        ELSE
        a& = VAL(a$)
        b& = VAL(b$)
        END IF
 
        DO
                PRINT "Select action."
                LINE INPUT "Enter (a)ddition, (s)ubtraction, (m)ultiplication or (d)ivision: "; action$
                f = 0
                SELECT CASE LCASE$(action$)
                        CASE "a"
                                result! = a& + b&
                        CASE "s"
                                result! = a& - b&
                        CASE "m"
                                result! = a& * b&
                        CASE "d"
                                IF b& = 0 THEN
                                        result! = a&
                                        PRINT "Divizion by zero!"
                                        er = 1
                                ELSE
                                        result! = a& / b&
                                END IF
                        CASE ELSE
                CLS
                f = 1
                PRINT "Wrong!"
                END SELECT
        LOOP UNTIL f <> 1
 
        IF er <> 1 THEN
                PRINT
                PRINT "Result's:"
                IF bin = 0 THEN PRINT "Dec.:"; result!
                IF bin = 1 THEN PRINT "Bin.: "; decTObin(INT(result!))
        END IF
        PRINT
        LINE INPUT "Repeat? : "; choise$
        LOOP UNTIL LCASE$(choise$) <> "y" AND LCASE$(choise$) <> "yes"
 
FUNCTION binTOdec (st$)
f = 0
IF MID$(st$, 1, 1) = "-" THEN
    st$ = MID$(st$, 2, LEN(st$))
    f = 1
END IF
FOR i = 1 TO LEN(st$)
    ps = ps + VAL(MID$(st$, i, 1)) * 2 ^ (LEN(st$) - i)
NEXT i
IF f = 1 THEN ps = -ps
binTOdec = ps
END FUNCTION
 
FUNCTION decTObin$ (number&)
f = 0
IF number& = 0 THEN decTObin$ = "0": EXIT FUNCTION
IF number& < 0 THEN number& = ABS(number&): f = 1
DO WHILE number& > 1
    IF number& MOD 2 = 0 THEN
        number& = number& / 2
                temp$ = temp$ + "0"
    ELSE
                number& = (number& - 1) / 2
                temp$ = temp$ + "1"
    END IF
LOOP
temp$ = temp$ + "1"
FOR i = LEN(temp$) TO 1 STEP -1
    ps$ = ps$ + MID$(temp$, i, 1)
NEXT i
IF f = 1 THEN ps$ = "-" + ps$
decTObin$ = ps$
END FUNCTION
Поделиться:

Похожие статьи: