Программа "Строковый калькулятор" - QBasic
Формулировка задачи:
решил попробовать написать программу производящую простые вычисления с использованием строк.
пока может только складывать, умножать и возводить в степень.
Решение задачи: «Программа "Строковый калькулятор"»
textual
Листинг программы
DECLARE SUB obrabotka (a$, b$, nznak!) DECLARE FUNCTION umnozhenie$ (a$, b$) DECLARE FUNCTION slozhenie$ (a$, b$) DECLARE FUNCTION vychitanie$ (a$, b$) DECLARE FUNCTION sravnenie! (a$, b$) DECLARE FUNCTION delenie$ (a$, b$, rezhim!) DECLARE FUNCTION prog1$ (a$, b$) DECLARE FUNCTION prog2$ (a$, b$) DECLARE FUNCTION prog3$ (a$, b$) DECLARE FUNCTION prog4$ (a$, b$, rezhim!) DECLARE FUNCTION prog5$ (a$, b$) DECLARE FUNCTION faktorial$ (a$) DECLARE FUNCTION perevodss$ (a$, qq$, pp$) DECLARE FUNCTION sinx$ (x$, rd!) DECLARE FUNCTION cosx$ (x$, rd!) PRINT "spisok dostupnyh operacij" PRINT " 1.slozhenie" PRINT " 2.vychitanie" PRINT " 3.umnozhenie" PRINT " 4.delenie" PRINT " 5.vozvedenie v stepen" PRINT " 6.celochislennoe delenie" PRINT " 7.ostatok ot delenija" PRINT " 8.izvlechenie kornja n stepeni" PRINT " 9.celaja chast chisla" PRINT " 10.faktorial" PRINT " 11.perevod ss q-p" PRINT " 12.sin(x)" PRINT " 13.cos(x)" INPUT "vybirete dejstvie"; k CLS SELECT CASE k CASE IS = 1 PRINT "a + b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ PRINT prog1$(a$, b$) CASE IS = 2 PRINT "a - b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ PRINT prog2$(a$, b$) CASE IS = 3 PRINT "a * b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ PRINT prog3$(a$, b$) CASE IS = 4 PRINT "a / b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ IF b$ = "0" THEN PRINT "osibka" ELSE PRINT prog4$(a$, b$, 0) CASE IS = 5 PRINT "a ^ b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ PRINT prog5$(a$, b$) CASE IS = 6 PRINT "a \ b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ PRINT prog4$(a$, b$, 1) CASE IS = 7 PRINT "a mod b" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ IF INSTR(a$, "-") <> 0 THEN a$ = RIGHT$(a$, LEN(a$) - 1): znak$ = CHR$(45) IF sravnenie(a$, b$) = 2 THEN PRINT znak$ + a$ ELSE PRINT prog4$(a$, b$, 2) END IF CASE IS = 8 PRINT "b^sqr(a)" INPUT "vvedite a"; a$: INPUT "vvedite b"; b$ PRINT prog3$(a$, "1/" + b$) CASE IS = 9 PRINT "int(a)" INPUT "vvedite a"; a$ PRINT LEFT$(a$, INSTR(a$, ".") - 1) CASE IS = 10 PRINT "a!" INPUT "vvedite"; a$ PRINT faktorial$(a$) CASE IS = 11 PRINT "q-p" INPUT "vvedite chislo"; a$: INPUT "vvedite q"; q$: INPUT "vvedite p"; p$ PRINT perevodss$(a$, q$, p$) CASE IS = 12 PRINT "sin(x)" INPUT "vyberite rad/deg[0/1]"; rd INPUT "vvedite x"; x$ PRINT sinx$(x$, rd) CASE IS = 13 PRINT "cos(x)" INPUT "vyberite rad/deg[0/1]"; rd INPUT "vvedite x"; x$ PRINT cosx$(x$, rd) END SELECT FUNCTION cosx$ (x$, rd) IF rd = 1 THEN rad$ = prog4$(x$, "180", 0) pi$ = "3.14159265" x$ = prog3$(rad$, pi$) IF x$ = "" THEN x$ = "0" END IF x0$ = x$ x$ = "1" FOR i = 1 TO 6 xy$ = LTRIM$(STR$(i * 2)) x1$ = prog5$(x0$, xy$) x2$ = faktorial$(xy$) x3$ = prog4$(x1$, x2$, 0) IF i MOD 2 <> 0 THEN x$ = prog2$(x$, x3$) ELSE x$ = prog1$(x$, x3$) END IF NEXT IF LEFT$(x$, 1) = "-" THEN sxs = 1 cosx$ = MID$(x$, 1, 5 + sxs) END FUNCTION FUNCTION delenie$ (a$, b$, rezhim) nachpozic = 1 ostatok$ = "" snos$ = "" rezultat4$ = "" FOR i = 1 TO LEN(a$) dlinasnosa = dlinasnosa + 1 snos$ = ostatok$ + MID$(a$, nachpozic, dlinasnosa) IF snos$ = CHR$(48) THEN rezultat4$ = rezultat4$ + CHR$(48) snos$ = "" nachpozic = nachpozic + 1 dlinasnosa = 0 END IF IF NOT (sravnenie(snos$, b$) = 2) THEN FOR j = 1 TO 9 jstr$ = LTRIM$(STR$(j)) podbor$ = umnozhenie$(b$, jstr$) IF sravnenie(snos$, podbor$) = 2 THEN EXIT FOR ELSE chislo4$ = jstr$ podbor0$ = podbor$ END IF NEXT rezultat4$ = rezultat4$ + chislo4$ ostatok$ = vychitanie$(snos$, podbor0$) nostat = nachpozic + dlinasnosa - 1 nachpozic = nachpozic + dlinasnosa dlinasnosa = 0 IF ostatok$ = "0" THEN ostatok$ = "" END IF NEXT IF nostat <> LEN(a$) THEN IF snos$ <> "" AND sravnenie(snos$, b$) = 2 THEN rezultat4$ = rezultat4$ + CHR$(48) ostatok$ = snos$ snos$ = "" END IF END IF SELECT CASE rezhim CASE IS = 1 IF rezultat4$ = "" THEN delenie$ = CHR$(48) ELSE delenie$ = rezultat4$ END IF EXIT FUNCTION CASE IS = 2 IF ostatok$ = "" THEN delenie$ = CHR$(48) ELSE delenie$ = ostatok$ END IF EXIT FUNCTION END SELECT IF ostatok$ = "" AND rezultat4$ <> "" THEN delenie$ = rezultat4$ EXIT FUNCTION END IF IF ostatok$ <> "" THEN rezultat4$ = rezultat4$ + CHR$(46) snos$ = ostatok$ i = 0 DO i = i + 1 snos$ = snos$ + CHR$(48) FOR j = 0 TO 9 jstrd$ = LTRIM$(STR$(j)) podbord$ = umnozhenie$(b$, jstrd$) IF sravnenie(snos$, podbord$) = 2 THEN EXIT FOR ELSE chislo4d$ = jstrd$ podbor0d$ = podbord$ END IF NEXT rezultat4$ = rezultat4$ + chislo4d$ ostatok$ = vychitanie$(snos$, podbor0d$) IF ostatok$ = CHR$(48) THEN EXIT DO ELSE snos$ = ostatok$ ostatok$ = "" END IF LOOP UNTIL i = 25 END IF IF rezultat4$ = "" THEN rezultat4$ = CHR$(48) delenie$ = rezultat4$ END FUNCTION FUNCTION faktorial$ (a$) fkt$ = "1" fqs$ = "0" DO UNTIL fqs$ = a$ fqs$ = slozhenie$(fqs$, "1") fkt$ = umnozhenie$(fkt$, fqs$) LOOP faktorial$ = fkt$ END FUNCTION SUB obrabotka (a$, b$, nznak) ato4ka = INSTR(a$, ".") bto4ka = INSTR(b$, ".") IF ato4ka > 0 THEN azn = LEN(a$) - ato4ka IF bto4ka > 0 THEN bzn = LEN(b$) - bto4ka IF azn > bzn THEN nznak = azn ELSE nznak = bzn IF azn > 0 THEN a$ = MID$(a$, 1, ato4ka - 1) + MID$(a$, ato4ka + 1, azn) END IF a$ = a$ + STRING$(nznak - azn, 48) IF bzn > 0 THEN b$ = MID$(b$, 1, bto4ka - 1) + MID$(b$, bto4ka + 1, bzn) END IF b$ = b$ + STRING$(nznak - bzn, 48) IF LEFT$(a$, 1) = CHR$(48) AND LEN(a$) <> 1 THEN FOR i = 1 TO LEN(a$) IF MID$(a$, i, 1) = CHR$(48) THEN vednola = vednola + 1 ELSE EXIT FOR END IF NEXT a$ = RIGHT$(a$, LEN(a$) - vednola) END IF IF LEFT$(b$, 1) = CHR$(48) AND LEN(b$) <> 1 THEN FOR i = 1 TO LEN(b$) IF MID$(b$, i, 1) = CHR$(48) THEN vednolb = vednolb + 1 ELSE EXIT FOR END IF NEXT b$ = RIGHT$(b$, LEN(b$) - vednolb) END IF END SUB FUNCTION perevodss$ (a$, qq$, pp$) u$ = "0123456789ABCDEF" k = INSTR(a$, ".") stepen$ = LTRIM$(STR$((LEN(a$) - k) * (k <> 0))) a$ = MID$(a$, 1, k - 1) + MID$(a$, k + 1, LEN(a$) - k) zx$ = "0" FOR i = LEN(a$) TO 1 STEP -1 j = INSTR(u$, UCASE$(MID$(a$, i, 1))) - 1 sj$ = LTRIM$(STR$(j)) stepen0$ = stepen$ x1$ = prog5$(qq$, stepen$) stepen$ = stepen0$ x2$ = prog3$(sj$, x1$) zx$ = prog1$(zx$, x2$) stepen$ = prog1$(stepen$, "1") NEXT dkj = INSTR(zx$, ".") IF dkj > 0 THEN z$ = LEFT$(zx$, dkj - 1) ds$ = RIGHT$(zx$, LEN(zx$) - dkj + 1) ELSE z$ = zx$ ds$ = "0" END IF DO IF sravnenie(z$, pp$) = 2 THEN ccc = VAL(z$) + 1 ELSE ccc = VAL(prog4$(z$, pp$, 2)) + 1 END IF cc$ = MID$(u$, ccc, 1) + cc$ z$ = prog4$(z$, pp$, 1) LOOP UNTIL z$ = "0" lll$ = "0" DO lll$ = slozhenie$(lll$, "1") pt$ = pp$ ds$ = prog3$(ds$, pp$) pp$ = pt$ yd = VAL(LEFT$(ds$, INSTR(ds$, ".") - 1)) + 1 ff$ = ff$ + MID$(u$, yd, 1) ds$ = RIGHT$(ds$, LEN(ds$) - INSTR(ds$, ".") + 1) LOOP UNTIL INSTR(ds$, ".") = 0 OR lll$ = "20" perevodss$ = cc$ + "." + ff$ END FUNCTION FUNCTION prog1$ (a$, b$) IF a$ = "0" THEN prog1$ = b$ EXIT FUNCTION END IF IF b$ = "0" THEN prog1$ = a$ EXIT FUNCTION END IF IF LEFT$(a$, 1) = "-" THEN flzna = 1 a$ = RIGHT$(a$, LEN(a$) - 1) END IF IF LEFT$(b$, 1) = "-" THEN flznb = 1 b$ = RIGHT$(b$, LEN(b$) - 1) END IF IF flzna = 1 AND flznb = 1 THEN znakm$ = "-" IF flzna = 1 XOR flznb = 1 THEN IF a$ = b$ THEN prog1$ = "0" EXIT FUNCTION END IF IF sravnenie(a$, b$) = 1 THEN IF flzna = 1 THEN prog1$ = prog2$(b$, a$) ELSE prog1$ = prog2$(a$, b$) END IF EXIT FUNCTION END IF IF sravnenie(a$, b$) = 2 THEN IF flzna = 1 THEN prog1$ = prog2$(b$, a$) ELSE prog1$ = prog2$(a$, b$) END IF EXIT FUNCTION END IF END IF nznak = 0 CALL obrabotka(a$, b$, nznak) process$ = slozhenie$(a$, b$) IF nznak > 0 THEN IF LEN(process$) - nznak > 0 THEN kcdz = LEN(process$) - nznak ELSE kcdz = 0 kvn = ABS(LEN(process$) - nznak) END IF process$ = MID$(process$, 1, kcdz) + "." + STRING$(kvn, 48) + MID$(process$, kcdz + 1, nznak) FOR y = LEN(process$) TO 1 STEP -1 vrp = ASC(MID$(process$, y, 1)) IF vrp = 46 OR vrp = 48 THEN ky = ky + 1 ELSE EXIT FOR END IF NEXT process$ = MID$(process$, 1, LEN(process$) - ky) END IF IF LEFT$(process$, 1) = "." THEN process$ = "0" + process$ prog1$ = znakm$ + process$ END FUNCTION FUNCTION prog2$ (a$, b$) IF LEFT$(a$, 1) = "-" THEN flzna2 = 1 a$ = RIGHT$(a$, LEN(a$) - 1) END IF IF LEFT$(b$, 1) = "-" THEN flznb2 = 1 b$ = RIGHT$(b$, LEN(b$) - 1) END IF IF flzna2 = 1 OR flznb2 = 1 THEN IF flzna2 = 1 AND flznb2 = 1 THEN prog2$ = prog1$("-" + a$, "-" + b$) EXIT FUNCTION END IF IF flzna2 = 1 THEN prog2$ = prog1$(CHR$(45) + a$, CHR$(45) + b$) EXIT FUNCTION END IF IF flznb2 = 1 THEN prog2$ = prog1$(a$, b$) EXIT FUNCTION END IF END IF nznak = 0 CALL obrabotka(a$, b$, nznak) process2$ = vychitanie$(a$, b$) IF LEFT$(process2$, 1) = "-" THEN znakm2$ = "-" process2$ = RIGHT$(process2$, LEN(process2$) - 1) END IF IF nznak > 0 THEN IF LEN(process2$) - nznak > 0 THEN kcdz2 = LEN(process2$) - nznak ELSE kcdz2 = 0 kvn2 = ABS(LEN(process2$) - nznak) END IF process2$ = MID$(process2$, 1, kcdz2) + "." + STRING$(kvn2, 48) + MID$(process2$, kcdz2 + 1, nznak) FOR y = LEN(process2$) TO 1 STEP -1 vrp2 = ASC(MID$(process2$, y, 1)) IF vrp2 = 46 OR vrp2 = 48 THEN ky2 = ky2 + 1 ELSE EXIT FOR END IF NEXT process2$ = MID$(process2$, 1, LEN(process2$) - ky2) END IF IF LEFT$(process2$, 1) = "." THEN process2$ = "0" + process2$ prog2$ = znakm2$ + process2$ END FUNCTION FUNCTION prog3$ (a$, b$) IF LEFT$(a$, 1) = "-" THEN flzna3 = 1 a$ = RIGHT$(a$, LEN(a$) - 1) END IF IF LEFT$(b$, 1) = "-" THEN flznb3 = 1 b$ = RIGHT$(b$, LEN(b$) - 1) END IF IF flzna3 = 1 XOR flznb3 = 1 THEN znakm3$ = "-" CALL obrabotka(a$, b$, nznak) process3$ = umnozhenie$(a$, b$) IF nznak > 0 THEN IF LEN(process3$) - 2 * nznak > 0 THEN kcdz3 = LEN(process3$) - 2 * nznak ELSE kcdz3 = 0 kvn3 = ABS(LEN(process3$) - 2 * nznak) END IF process3$ = MID$(process3$, 1, kcdz3) + "." + STRING$(kvn3, 48) + MID$(process3$, kcdz3 + 1, 2 * nznak) FOR y = LEN(process3$) TO 1 STEP -1 vrp3 = ASC(MID$(process3$, y, 1)) IF vrp3 = 46 OR vrp3 = 48 THEN ky3 = ky3 + 1 ELSE EXIT FOR END IF NEXT process3$ = MID$(process3$, 1, LEN(process3$) - ky3) END IF IF LEFT$(process3$, 1) = "." THEN process3$ = "0" + process3$ IF process3$ = "" THEN process3$ = "0" prog3$ = znakm3$ + process3$ END FUNCTION FUNCTION prog4$ (a$, b$, rezhim) IF LEFT$(a$, 1) = "-" THEN flzna4 = 1 a$ = RIGHT$(a$, LEN(a$) - 1) END IF IF LEFT$(b$, 1) = "-" THEN flznb4 = 1 b$ = RIGHT$(b$, LEN(b$) - 1) END IF IF flzna4 = 1 XOR flznb4 = 1 THEN znakm4$ = "-" ato4ka4 = INSTR(a$, ".") bto4ka4 = INSTR(b$, ".") IF ato4ka4 > 0 OR bto4ka4 > 0 THEN CALL obrabotka(a$, b$, nznak) END IF dl$ = delenie$(a$, b$, rezhim) IF LEFT$(d$, 1) = "." THEN dl$ = "0" + dl$ IF dl$ = "0" THEN prog4$ = "0" ELSE prog4$ = znakm4$ + dl$ END IF END FUNCTION FUNCTION prog5$ (a$, b$) IF LEFT$(a$, 1) = "-" THEN flzna5 = 1: a$ = RIGHT$(a$, LEN(a$) - 1) IF LEFT$(b$, 1) = "-" THEN nm3 = 1: b$ = RIGHT$(b$, LEN(b$) - 1) jh = INSTR(b$, "/") IF jh > 0 THEN b11$ = MID$(b$, 1, jh - 1) b22$ = MID$(b$, jh + 1, LEN(b$) - jh) b$ = prog4$(b11$, b22$, 0) END IF b22$ = RIGHT$(b22$, 1) IF (b22$ = "2" OR b22$ = "4" OR b22$ = "6" OR b22$ = "8") AND flzna5 = 1 THEN PRINT "oshibka, chetnaja stepen iz otricatelnogo chisla" EXIT FUNCTION END IF fs$ = "0" process5$ = "1" db = INSTR(b$, ".") IF db = 0 THEN nx$ = RIGHT$(b$, 1) IF nx$ = "1" OR nx$ = "3" OR nx$ = "5" OR nx$ = "7" OR nx$ = "9" THEN IF flzna5 = 1 THEN znakm5$ = "-" END IF END IF IF db > 0 THEN bd$ = MID$(b$, db, LEN(b$) - bd) b$ = MID$(b$, 1, db - 1) END IF IF b$ = "" THEN b$ = "0" DO UNTIL fs$ = b$ fs$ = slozhenie$(fs$, "1") aa$ = a$ process5$ = prog3$(process5$, aa$) LOOP IF db > 0 THEN hq$ = LTRIM$(STR$(EXP(VAL(bd$) * LOG(VAL(a$))))) ELSE hq$ = "1" END IF rezultat5$ = znakm5$ + prog3$(process5$, hq$) IF nm3 = 1 THEN prog5$ = prog4$("1", rezultat5$, 0) ELSE prog5$ = rezultat5$ END FUNCTION FUNCTION sinx$ (x$, rd) IF rd = 1 THEN rad$ = prog4$(x$, "180", 0) pi$ = "3.14159265" x$ = prog3$(rad$, pi$) IF x$ = "" THEN x$ = "0" END IF x0$ = x$ FOR i = 1 TO 6 xy$ = LTRIM$(STR$(i * 2 + 1)) x1$ = prog5$(x0$, xy$) x2$ = faktorial$(xy$) x3$ = prog4$(x1$, x2$, 0) IF i MOD 2 <> 0 THEN x$ = prog2$(x$, x3$) ELSE x$ = prog1$(x$, x3$) END IF NEXT IF LEFT$(x$, 1) = "-" THEN sxs = 1 sinx$ = MID$(x$, 1, 5 + sxs) END FUNCTION FUNCTION slozhenie$ (a$, b$) lena = LEN(a$) lenb = LEN(b$) perenos = 0 rezultat$ = "" DO i = i + 1 IF lena + 1 > i THEN achislo = ASC(MID$(a$, lena - i + 1, 1)) - 48 ELSE achislo = 0 END IF IF lenb + 1 > i THEN bchislo = ASC(MID$(b$, lenb - i + 1, 1)) - 48 ELSE bchislo = 0 END IF summa = achislo + bchislo + perenos IF summa >= 10 THEN perenos = summa \ 10 summa = summa MOD 10 ELSE perenos = 0 END IF rezultat$ = LTRIM$(STR$(summa)) + rezultat$ LOOP UNTIL i >= lena AND i >= lenb IF perenos > 0 THEN rezultat$ = LTRIM$(STR$(perenos)) + rezultat$ slozhenie$ = rezultat$ END FUNCTION FUNCTION sravnenie (a$, b$) IF LEN(a$) > LEN(b$) THEN sravnenie = 1 EXIT FUNCTION END IF IF LEN(a$) < LEN(b$) THEN sravnenie = 2 EXIT FUNCTION END IF IF LEN(a$) = LEN(b$) THEN FOR i = 1 TO LEN(a$) IF MID$(a$, i, 1) > MID$(b$, i, 1) THEN sravnenie = 1 EXIT FUNCTION END IF IF MID$(a$, i, 1) < MID$(b$, i, 1) THEN sravnenie = 2 EXIT FUNCTION END IF NEXT END IF sravnenie = 0 END FUNCTION FUNCTION umnozhenie$ (a$, b$) lena3 = LEN(a$) lenb3 = LEN(b$) IF lenb3 > lena3 THEN SWAP a$, b$ SWAP lena3, lenb3 END IF FOR i = 1 TO lenb3 proizv$ = "" perenos3 = 0 IF lenb3 + 1 > i THEN bchislo3 = ASC(MID$(b$, lenb3 - i + 1, 1)) - 48 ELSE bchislo3 = 0 END IF FOR j = 1 TO lena3 IF lena3 + 1 > j THEN achislo3 = ASC(MID$(a$, lena3 - j + 1, 1)) - 48 ELSE achislo3 = 0 END IF proizvedenie = achislo3 * bchislo3 + perenos3 IF proizvedenie >= 10 THEN perenos3 = proizvedenie \ 10 proizvedenie = proizvedenie MOD 10 ELSE perenos3 = 0 END IF proizv$ = LTRIM$(STR$(proizvedenie)) + proizv$ NEXT IF perenos3 > 0 THEN proizv$ = LTRIM$(STR$(perenos3)) + proizv$ rezultat3$ = slozhenie$(rezultat3$, proizv$ + STRING$(i - 1, 48)) NEXT IF LEFT$(rezultat3$, 1) = CHR$(48) THEN rezultat3$ = CHR$(48) umnozhenie$ = rezultat3$ END FUNCTION FUNCTION vychitanie$ (a$, b$) lena2 = LEN(a$) lenb2 = LEN(b$) IF sravnenie(a$, b$) = 2 THEN SWAP a$, b$ SWAP lena2, lenb2 znak2$ = "-" END IF zanimaem = 0 FOR i = 1 TO lena2 IF lena2 + 1 > i THEN achislo2 = ASC(MID$(a$, lena2 - i + 1, 1)) - 48 ELSE achislo2 = 0 END IF IF lenb2 + 1 > i THEN bchislo2 = ASC(MID$(b$, lenb2 - i + 1, 1)) - 48 ELSE bchislo2 = 0 END IF minus = achislo2 - bchislo2 - zanimaem IF i <> sa THEN IF minus < 0 THEN minus = minus + 10 zanimaem = 1 ELSE zanimaem = 0 END IF END IF IF minus >= 0 THEN rezultat2$ = LTRIM$(STR$(minus)) + rezultat2$ END IF NEXT FOR i = 1 TO LEN(rezultat2$) - 1 IF MID$(rezultat2$, i, 1) = CHR$(48) THEN vednul = vednul + 1 ELSE EXIT FOR END IF NEXT rezultat2$ = MID$(rezultat2$, 1 + vednul, LEN(rezultat2$) - vednul) vychitanie$ = znak2$ + rezultat2$ END FUNCTION
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д