Article View: comp.lang.pascal.borland
Article #444Suggestions on improving my code ?
From: Michael Weber
Date: Tue, 22 Sep 1998 00:00
Date: Tue, 22 Sep 1998 00:00
115 lines
3811 bytes
3811 bytes
Please help me to improve my PASCAL code :
It works (nearly ... you can use the convDB only once :-) .... damn it
! :-)
as advertised, but I would like to improve its
structure / performance / size AND firewalls against bad input
Any suggestions will be appreciated ..... thanks in advance . Mike
PROGRAM bitodec (INPUT,OUTPUT);
{is used to convert numbers from decimal to binary
as well as from binary to decimal}
USES
CRT;
VAR
choice , quit : CHAR;
{---------------------------------------------------------------------------}
PROCEDURE errormsg;
BEGIN
WRITELN;
WRITELN ('*** ERRONEUS INPUT ***');
WRITELN;
END;
{---------------------------------------------------------------------------}
PROCEDURE convBD;
{pre: user is propted to enter a number in base 2 representation
post: number is converted to decimal representation and printed to
screen}
var
binarry: ARRAY [1..16] OF INTEGER;
n , num , counter : INTEGER;
choice : CHAR;
BEGIN
WHILE n < 17 DO
binarry[n] := 0;
n := 0;
WRITE ('Enter the number in binary: ');
REPEAT
n := n + 1;
choice := READKEY;
WRITE (choice);
IF choice = '1' THEN
binarry[n] := 1
ELSE IF choice = '0' THEN
binarry[n] := 0
ELSE IF (choice <> #13) THEN BEGIN
errormsg;
EXIT;
END;
UNTIL NOT (choice IN ['0','1']);
WRITELN;
counter := 0;
num := 0;
REPEAT
counter := counter + 1;
num := (num * 2) + binarry[counter];
UNTIL counter = (n - 1);
WRITE ('equals to ',num,' decimal');
READLN;
END;
{---------------------------------------------------------------------------}
PROCEDURE convDB;
{pre: user is prompted for decimal number
post: converted to binary and printed to the screen}
VAR
I , Code , n : INTEGER;
decstr : STRING[5];
binout : STRING[16];
BEGIN
binout := '0000000000000000';{saves unnecessarry string operations
and keeps code short}
WRITE ('Enter the decimal to be converted : ');
READLN (decstr);
VAL (decstr , I , Code);
IF Code <> 0 THEN
WRITE ('error at position :',Code)
ELSE
FOR n := 16 DOWNTO 1 DO BEGIN
IF ODD(I) THEN
binout[n] := '1';
I := I SHR 1;{shift right one}
END;{FOR}
WRITE ('Equals to : ',binout);
END{convDB};
{============================================================================}
BEGIN
REPEAT
CLRSCR;
WRITELN ('options :');
WRITELN ('1 Convert binary to decimal');
WRITELN ('2 Convert decimal to binary');
WRITELN ('3 quit');
WRITELN;
choice := READKEY;
IF choice = '1' THEN convBD
ELSE IF choice = '2' THEN convDB
ELSE IF choice = '3' THEN HALT(1)
ELSE errormsg;
WRITELN;
WRITE ('Quit ? ');
READ (quit);
UNTIL (quit = 'y') OR (quit = 'Y');
END.{bitodec}
Technical Details
Message-ID:
<3606EF04.2B0227A5@inode.at>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10261c,beb0f8ef7fe1df95,start X-Google-Attributes: gid10261c,public X-Google-Thread: 10baee,beb0f8ef7fe1df95,start X-Google-Attributes: gid10baee,public X-Google-Thread: 110930,beb0f8ef7fe1df95,start X-Google-Attributes: gid110930,public X-Google-Thread: 111659,beb0f8ef7fe1df95,start X-Google-Attributes: gid111659,public X-Google-Thread: 108ebd,beb0f8ef7fe1df95,start X-Google-Attributes: gid108ebd,public From: Michael Weber <mikew@inode.at> Subject: Suggestions on improving my code ? Date: 1998/09/22 Message-ID: <3606EF04.2B0227A5@inode.at> X-Deja-AN: 393472055 Content-Transfer-Encoding: 7bit Organization: inode Internet Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: comp.lang.pascal.borland,borland.public.turbopascal,comp.lang.pascal.misc,comp.lang.pascal.delphi.misc,z-netz.sprachen.pascal