program base_conversion_program; uses crt; function topower(base : integer; power : integer):longint; var total, temp : longint; i : integer; begin total := 1; for i := 1 to 32 do begin temp:=base*total; total:=total*base; if power = 0 then topower := 1; if i = power then topower := total; end; end; function decimal_binary(decimal : longint): string; var remain : array[1..32] of integer; binary,tempchar : string; count,i : integer; begin count := 0; binary := ''; while decimal <> 0 do begin count := count + 1; remain[count] := decimal mod 2; decimal := decimal div 2; end; for i := count downto 1 do begin str(remain[i],tempchar); binary := binary + tempchar; end; decimal_binary := binary; writeln; end; function binary_decimal(binary : string): longint; var decimal : longint; base,power,error,len,i : integer; num : array[1..32] of longint; begin decimal := 0; base := 2; power := 0; len := length(binary); for i := len downto 1 do begin val(binary[i],num[i],error); num[i] := num[i]*topower(base,power); decimal := decimal + num[i]; power := power + 1; end; binary_decimal := decimal; writeln; writeln; end; procedure readxy(var reply : longint; var binary : string; row,colour : integer; ok : boolean; max : integer); procedure centre_line(tempstr : string; row : integer); var column : integer; begin window(1,row,80,row); clrscr; window(1,1,80,25); column := 40-(length(tempstr) div 2); gotoxy(column,row); write(tempstr); end; var letter : char; count,error : integer; decimal : string; begin count := 1; binary := ''; str(reply,decimal); decimal := ''; textcolor(colour); repeat letter := readkey; if ok=false then begin if letter=#8 then begin count := count - 1; if count < 1 then count := 1; write(chr(8),' ',chr(8)); delete(binary,count,1); end else if count <= max then if (letter='0') or (letter='1') then begin count := count + 1; binary := binary + letter; end; centre_line(binary,row); end else if ok=true then begin if letter=#8 then begin count := count - 1; if count < 1 then count := 1; write(chr(8),' ',chr(8)); delete(decimal,count,1); end else if count <= max then if (letter>='0') and (letter<='9') then begin count := count + 1; decimal := decimal + letter; end; centre_line(decimal,row); end; until letter=#13; val(decimal,reply,error); end; procedure writexy(text1,text2,text3 : string; row,colour : integer); var len,column : integer; tempstr : string; begin tempstr := text1+text2+text3; len := length(tempstr); column := 40-(len div 2); textcolor(colour); gotoxy(column,row); write(tempstr); gotoxy(1,25); end; procedure menu(var option : integer; var row : integer); var pause : char; begin clrscr; repeat clrscr; writexy('Select a base conversion','','',10,4); writexy('Decimal To Binary','','',12,6); writexy('Binary To Decimal','','',13,6); writexy('To The Power','','',14,6); writexy('Exit','','',15,6); textcolor(4); gotoxy(29,row); writeln(chr(175)); gotoxy(51,row); writeln(chr(174)); gotoxy(1,25); pause := readkey; case pause of #72 : row := row - 1; #80 : row := row + 1; end; if row < 12 then row := 12; if row > 15 then row := 15; until pause = #13; option := row-11; clrscr; end; function commas(no : longint):string; var count : integer; len,i : integer; number : string; begin count := 2; str(no,number); len := length(number); for i := len downto 1 do if len-count = i then begin insert(',',number,i); count := count + 3; if number[1] = #44 then delete(number,1,1); end; commas := number; end; { Start of main program } var decimal : longint; binary : string; base,power : longint; option,row : integer; pause : char; begin textmode(80); row := 12; while option <> 4 do begin menu(option,row); case option of 1 : begin writexy('Enter a decimal number','','',10,6); readxy(decimal,binary,11,2,true,10); binary := decimal_binary(decimal); writexy(commas(decimal),' = ',binary,13,4); end; 2 : begin writexy('Enter a binary number','','',10,6); readxy(decimal,binary,11,2,false,31); decimal := binary_decimal(binary); writexy(binary,' = ',commas(decimal),13,4); end; 3 : begin writexy('Enter Base','','',10,6); readxy(base,binary,11,2,true,10); writexy('Enter Power','','',12,6); readxy(power,binary,13,2,true,10); writeln; writexy(commas(topower(base,power)),'','',15,4); end; end; if option <> 4 then pause := readkey; end; textcolor(7); clrscr; end. { End of main program }