Примеры программ на языке QBASIC


REM Преобразование обычной дроби в десятичную
DECLARE FUNCTION FRAC2STR$ (M%, N%)
CLS : DEFINT A-Z
INPUT "Введите числитель и знаменатель дроби : ", M, N
PRINT M; "/"; N; "= "; FRAC2STR$(M, N)
END

DEFSNG A-Z
FUNCTION FRAC2STR$ (M AS INTEGER, N AS INTEGER)
DIM S AS STRING, REST(100) AS INTEGER
DEFINT A-Z
I = 3: S = "0."
DO
  Q = M * 10 \ N: P = M * 10 MOD N: REST(I) = P
  IF P = 0 THEN
     FRAC2STR = S + CHR$(Q + 48)
     EXIT FUNCTION
  END IF
  FOR J = 3 TO I - 1
    IF REST(J) = P THEN
       FRAC2STR = LEFT$(S, J - 1) + "(" + RIGHT$(S, LEN(S) - J + 1) + ")"
       EXIT FUNCTION
    END IF
  NEXT J
  S = S + CHR$(Q + 48): I = I + 1: M = P
LOOP UNTIL P = 0
END FUNCTION