program calculator; uses crt; {add exponents + memory slots} var spotfull,mm2,n,c,what,b,deccount,decmult:integer; c2,c3,answer:real; x,sign,sign2,gg:char; quit,dec,neg,sounds_on:boolean; input:string; procedure clear_variables; begin {----------------clears all variables---------------------------------------} input := ' '; c2 := 0; c3 := 0; spotfull := 0; what := 0; n := 1; dec := false; neg := false; sign := ' '; sign2 := ' '; quit := false; end; procedure border; var x:integer; {----------------Puts a border around the screen----------------------------} begin textcolor (3); gotoxy (1,1); write (#201); for x := 1 to 78 do write (#205); write (#187); gotoxy (1,24); write (#200); for x := 1 to 78 do write (#205); write (#188); for x := 2 to 23 do begin gotoxy (80,x); write (#186); gotoxy (1,x); write (#186); end; gotoxy (1,11); write (#199); for x := 1 to 78 do write (#196); write (#182); end; procedure writing; begin {----------------TITLE AND INTRO--------------------------------------------} textcolor(green); gotoxy (21,3); writeln ('~~~~~~~~~~~~The Calculator~~~~~~~~~~~'); writeln; textcolor(7); writeln (' Welcome to the calculator. Press ''h'' for help, but everything works'); writeln (' pretty much just like a normal pocket calculator. Basically you just'); writeln (' type in a number, a symbol, another number, and then an equal sign.'); writeln (' To make a number negative, press ''n'' before you enter in the number.'); writeln (' Pressing ''c'' clears everything and pressing ''q'' quits. Have fun'); end; procedure buttons; var y:integer; y2:char; begin {----------------WRITING NUMBER BUTTONS-------------------------------------} gotoxy (2,15); write (' ') ; for y := 0 to 9 do begin case y of 0 : y2 := '0'; 1 : y2 := '1'; 2 : y2 := '2'; 3 : y2 := '3'; 4 : y2 := '4'; 5 : y2 := '5'; 6 : y2 := '6'; 7 : y2 := '7'; 8 : y2 := '8'; 9 : y2 := '9'; end; textcolor(blue); if x = y2 then textcolor(red); write ('[',y,'] '); end; textcolor(blue); if x = '.' then textcolor(red); write ('[.] '); textcolor(blue); if (x = 'n') or (x = 'N') then textcolor(red); write ('[+/-] '); {---------------WRITING SYMBOL BUTTONS--------------------------------------} gotoxy (2,17); write (' ') ; textcolor(blue); if x = '+' then textcolor(red); write ('[+] '); textcolor(blue); if x = '-' then textcolor(red); write ('[-] '); textcolor(blue); if x = '*' then textcolor(red); write ('[*] '); textcolor(blue); if x = '/' then textcolor(red); write ('[/] '); textcolor(blue); if x = '=' then textcolor(red); write ('[=] '); textcolor(blue); if (x = 'c') or (x = 'C') then textcolor(red); write ('[C] '); textcolor(11); end; procedure sounds; begin if sounds_on then begin if x = '=' then begin sound(750); delay(1000); nosound; end else if (x = 'Q') or (x = 'C') then begin sound(300); delay(1000); nosound; end else begin sound(500); delay(850); nosound; end; end; end; procedure equation; begin {----------------writing user input and answer------------------------------} gotoxy (33,20); gg := ' '; if b <> 5 then write(input) else write (answer:0:mm2); b := 0; {----------------input main variable : x------------------------------------} gotoxy (80,25); x := upcase(readkey); {----------------sorting out actions determined by x------------------------} case x of '0','1','2','3','4','5','6','7','8','9' : begin if dec = true then if what = 5 then n := n + 1; sounds; input := input + x; what := 5; end; '+','-','*','/','=' : begin if what <> 5 then x := ' ' else begin sounds; spotfull := spotfull + 1; input := input + x; neg := false; dec := false; end; n := 1; what := 6; end; '.' : begin if dec = true then x := ' '; if dec = false then begin sounds; input := input + x; what := 7; end; dec := true; end; 'N' : begin if (what <> 5) and (what <> 7) and (neg = false) then begin sounds; neg := true; input := input + '-'; what := 8; end else x := ' '; end; 'C' : begin clear_variables; sounds; end; #27,'Q' : begin sounds; quit := true; x := ' '; end else if x <> 'H' then x := ' '; end; {----------------COMPUTATION------------------------------------------------} if (x <> ' ') and (x <> 'C') and (x <> 'Q') and (x <> 'H') then begin {----------------getting variable x into sign holder or integer-------------} if x = '=' then sign2 := sign; if (what = 6) and (spotfull = 2) then sign2 := sign; case x of '0' : c := 0; '1' : c := 1; '2' : c := 2; '3' : c := 3; '4' : c := 4; '5' : c := 5; '6' : c := 6; '7' : c := 7; '8' : c := 8; '9' : c := 9; else if (what <> 8) and (x <> '.') then sign := x; end; {----------------putting together negitive whole numbers--------------------} if (what = 5) and (dec = false) and (neg = true) then if sign = ' ' then c2 := c2 * 10 - c else c3 := c3 * 10 - c; {----------------putting together decimal numbers (pos and neg)-------------} if (what = 5) and (dec = true) then begin decmult := 1; deccount := 0; repeat decmult := decmult * 10; deccount := deccount + 1; until deccount = n; if (sign = ' ') and (neg = false) then c2 := c2 + c / decmult else if (sign <> ' ') and (neg = false) then c3 := c3 + c / decmult else if (sign = ' ') and (neg = true) then c2 := c2 - c / decmult else if (sign <> ' ') and (neg = true) then c3 := c3 - c / decmult; end; {-----------------putting together positive whole numbers-------------------} if (what = 5) and (dec = false) and (neg = false) then begin if sign = ' ' then c2 := c2 * 10 + c else c3 := c3 * 10 + c; end; {-----------------more than 2 number answer (2+2+2)-------------------------} if (spotfull = 2) and (what = 6) and (sign <> '=') then begin case sign2 of '+' : c2 := c2 + c3; '-' : c2 := c2 - c3; '*' : c2 := c2 * c3; '/' : begin if c3 <> 0 then c2 := c2 / c3 else begin clear_variables; x := ' '; gotoxy (30,18); writeln ('Can''t divide by 0'); gotoxy (80,25); gg := readkey; end; end; end; c3 := 0; spotfull := 1; end; {------------------final = sign answer-------------------------------------} if (what = 6) and (sign = '=') then begin if spotfull = 2 then begin case sign2 of '+' : answer := c2 + c3; '-' : answer := c2 - c3; '*' : answer := c2 * c3; '/' : begin if c3 <> 0 then answer := c2 / c3 else begin clear_variables; x := ' '; gotoxy (30,18); writeln ('Can''t divide by 0'); gotoxy (80,25); gg := readkey; gg := '}'; end; end; end; end else answer := c2; if gg <> '}' then b := 5; clear_variables; end; end; end; procedure decimal; begin {----------------changing number of decimal places in answer----------------} textcolor (green); writeln (' How many decimal places should be displayed in the answer?'); write (' (0 through 9)'); gg := readkey; case gg of '0' : mm2 := 0; '1' : mm2 := 1; '2' : mm2 := 2; '3' : mm2 := 3; '4' : mm2 := 4; '5' : mm2 := 5; '6' : mm2 := 6; '7' : mm2 := 7; '8' : mm2 := 8; '9' : mm2 := 9; else begin writeln;writeln; textcolor (red); writeln ('A number between 1 and 9, please'); writeln; decimal; end; end; end; procedure help; begin {----------------HELP SECTION-----------------------------------------------} clrscr; textcolor(green); writeln; writeln ('Despite my master pascal programming skills, this program probably has a'); writeln ('couple of bugs, but for you to find one, you pretty much have to go looking'); writeln ('for them. However, I''ve done my best to clear things up and anything that'); writeln ('you do find won''t be too horrible.'); writeln ('One major thing is that this program won''t do order of operations. For'); writeln ('example:'); writeln; textcolor(red); writeln ('Real world : 5 + 2 * 5 = 15'); writeln ('My program : 5 + 2 * 5 = 35'); writeln; textcolor(green); writeln ('But that doesn''t really matter, you just gotta put everything in the right'); writeln ('order from the start, for example:'); writeln; textcolor(red); writeln ('Right way : 2 * 5 + 5 = 15'); writeln; textcolor(blue); writeln ('--More--'); gg := readkey; textcolor(green); writeln; writeln ('The Calculator can also do negative numbers, but there are also a couple of'); writeln ('catches here. For one thing, you gotta press ''n'' before you enter in the'); writeln ('number, not at the end or somewhere in the middle. Secondly, pressing ''n'''); writeln ('again won''t turn turn the number back to positive, if you mess up, you gotta'); writeln ('just clear everything.'); writeln; writeln ('This program also plays sounds when keys are pressed, nothing big, just'); writeln ('internal speaker stuff, but they do kind of slow things down a tiny bit. If'); writeln ('you wanna toggle them on and off for some reason, then press ''s'' right now.'); writeln; writeln ('I''ve also set the default number of decimal places that the answer will'); writeln ('have to 2, but if you want more or less, then you can set it yourself by'); writeln ('pressing ''d'' right now.'); writeln; textcolor(blue); writeln ('Press any key to continue, or ''d'' to configure decimal places, or ''s'''); writeln ('to toggle sounds'); gotoxy (80,25); textcolor(green); gg := upcase(readkey); if gg = 'D' then decimal; if gg = 'S' then begin textcolor(green); writeln; writeln ('Do you want the program to play sounds?'); write ('(press Y or N)'); repeat gg := upcase(readkey); until (gg = 'Y') or (gg = 'N'); if gg = 'Y' then sounds_on := true else sounds_on := false; end; end; begin {----------------SETUP AND MAIN PROCEDURE STARTUP---------------------------} x := ' '; mm2 := 2; sounds_on := false; clear_variables; repeat clrscr; writing; border; buttons; equation; if x = 'H' then help; until quit; clrscr; end.