Программа перевода чисел из одной системы счисления в другую
Категория: Basic
2011-10-03 16:48:10
code: #basic
DECLARE FUNCTION fromDec$ (number$, lenght%) DECLARE FUNCTION checkData! (number$, lenght%) DECLARE FUNCTION isFract! (number$, lenght%) DECLARE FUNCTION toDec (number$, lenght%, fract%) CLS DIM SHARED number$, q%, p%, valmas$(0 TO 15), res&(0 TO 10000) valmas$(0) = "0" valmas$(1) = "1" valmas$(2) = "2" valmas$(3) = "3" valmas$(4) = "4" valmas$(5) = "5" valmas$(6) = "6" valmas$(7) = "7" valmas$(8) = "8" valmas$(9) = "9" valmas$(10) = "A" valmas$(11) = "B" valmas$(12) = "C" valmas$(13) = "D" valmas$(14) = "E" valmas$(15) = "F" start: q% = 0 p% = 0 DIM SHARED wholenum$ DIM SHARED fract$ INPUT "Enter number: "; number$ number$ = UCASE$(number$) IF checkData(number$, LEN(number$)) <> 1 THEN PRINT PRINT "Incorrect data format. Try one more time! ;-)" PRINT GOTO start END IF osn: INPUT "Enter original system's notation: ", q% IF q% >= 2 AND q% <= 16 THEN ELSE GOTO osn END IF tsn: INPUT "Enter target system's notation: ", p% IF p% >= 2 AND p% <= 16 THEN ELSE GOTO tsn END IF count: lenght% = LEN(number$) fractional = 0 IF isFract(number$, lenght%) = 1 THEN fractional = 1 END IF IF q% = p% THEN GOTO answer ELSEIF q% = 10 THEN IF fractional = 1 THEN number$ = fromDec(wholenum$, LEN(wholenum$)) + "," + fromDec(fract$, LEN(fract$)) ELSE number$ = fromDec(number$, lenght%) END IF ELSEIF q% <> 10 THEN IF fractional = 1 THEN number$ = STR$(toDec(wholenum$, LEN(wholenum$), 1) + toDec(fract$, LEN(fract$), -1)) ELSE number$ = STR$(toDec(number$, lenght%, 1)) END IF q% = 10 GOTO count END IF answer: PRINT PRINT "OTBET: "; number$ PRINT continue: DO INPUT "Would U like to continue? (y/n)"; answ$ LOOP UNTIL LCASE$(answ$) = "y" OR LCASE$(answ$) = "n" IF LCASE$(answ$) = "y" THEN PRINT GOTO start ELSE PRINT PRINT "Exit programm..." END END IF FUNCTION checkData (number$, lenght%) FOR i = 1 TO lenght% inarray% = 0 num$ = MID$(number$, i, 1) FOR j = 0 TO 15 IF valmas$(j) = num$ OR num$ = "," OR num$ = "." THEN inarray% = 1 EXIT FOR END IF NEXT j IF inarray% <> 1 THEN EXIT FUNCTION END IF NEXT i checkData = inarray% END FUNCTION FUNCTION fromDec$ (number$, lenght%) DIM lres!(0 TO 1000) a$ = "" i = 0 number! = VAL(number$) WHILE number! > 0 lres!(i) = number! MOD p% number! = FIX(number! / p%) i = i + 1 WEND FOR j = i TO 0 STEP -1 IF lres!(j) > 9 THEN a$ = a$ + valmas$(lres!(j)) ELSE a$ = a$ + STR$(lres!(j)) END IF NEXT j a$ = RIGHT$(a$, LEN(a$) - 2) fromDec = a$ END FUNCTION FUNCTION isFract (number$, lenght%) isFract = 0 FOR i = 1 TO lenght% num$ = MID$(number$, i, 1) IF num$ = "," OR num$ = "." THEN wholenum$ = LEFT$(number$, i - 1) fract$ = RIGHT$(number$, lenght% - i) isFract = 1 END IF NEXT i END FUNCTION FUNCTION toDec (number$, lenght%, fract%) res! = 0 FOR i = 1 TO lenght% num$ = MID$(number$, i, 1) IF NOT num$ = "0" AND VAL(num$) = 0 THEN FOR k = 0 TO 15 STEP 1 IF num$ = valmas$(k) THEN num$ = STR$(k) END IF NEXT k END IF IF fract% = 1 THEN res! = res! + VAL(num$) * q% ^ (lenght% - i) ELSE res! = res! + VAL(num$) * q% ^ -i END IF NEXT i toDec = res! 'STR$(res!) END FUNCTION
автор: extrim
Поделиться: