[comp.lang.pascal] Brain Teaser

demon@thor.wright.edu (Brett Kottman) (10/05/89)

	Okay, time to exercise your brain as well as your fingertips:

	Write a routine that will convert from one base to another.

function convert(base : integer; input_num : string) : string;

is the function header.

	Keep the line count down!

jtw@wuee1.wustl.edu (Trent Wohlschlaeger) (10/06/89)

In article <727@thor.wright.EDU> demon@thor.wright.edu writes:
>	Write a routine that will convert from one base to another.
>function convert(base : integer; input_num : string) : string;
                  ^^^^
This is a fairly standard "problem."  However, I think your definition is
under-determined.  Is 'base' the current base of the number, or the base
to which it is to be converted?

Might I suggest:

function convert(inbase, outbase : integer; input_num : string) : string;

I would be more impressed with code that does this directly, rather
than converting to base 10 (or 2, or 16, or...) first.

For example, convert 10110101 base 4 to x base 7.

x:= convert (4, 7, "10110101");

Trent

karlth@rhi.hi.is (Karl Thoroddsen) (10/16/89)

In article <1989Oct5.213548.13466@wuee1.wustl.edu>, jtw@wuee1.wustl.edu (Trent Wohlschlaeger) writes:
> In article <727@thor.wright.EDU> demon@thor.wright.edu writes:
> >	Write a routine that will convert from one base to another.
> >function convert(base : integer; input_num : string) : string;
>                   ^^^^
> > Might I suggest:
> 
> function convert(inbase, outbase : integer; input_num : string) : string;
> 
> I would be more impressed with code that does this directly, rather
> than converting to base 10 (or 2, or 16, or...) first.
> 
> For example, convert 10110101 base 4 to x base 7.
> 
> x:= convert (4, 7, "10110101");
> 
> Trent

A solution to the problem of converting from one base to another is  
 fairly straightforward (solution follows).

But as Trent says it would be much more impressive if the routine would
convert directly, can anyone program a routine which does that?

-----------------------CONVERT ROUTINE TP 5.0------------------
{FOR BASE >1 and <17}

function power(x,y:longint):longint;
var pow : longint;
begin pow:=1;
while y>0 do begin
  pow:=pow*x;dec(y);end;
power:=pow;
end;

function convert(in_base,out_base:integer;in_num:string):string;
const num_char = '0123456789ABCDEF';
var count,sum,num:longint;Out_num:string;
begin
sum:=0;count:=1;num:=0;Out_num:='';
while not(count>length(in_num)) do begin
  num:=pos(copy(in_num,count,1),num_char)-1;
  sum:=num*power(in_base,length(in_num)-count)+sum;
  inc(count);{ count:=count+1}
  end;       { Of convert to 10 base}
while sum>0 do begin
  out_num:=concat(copy(num_char,(sum mod out_base)+1,1),out_num);
  sum:=sum div out_base;
  end;
 
convert:=out_num;
end;   
       

R1TMARG%AKRONVM.BITNET@cornellc.cit.cornell.edu (Tim Margush) (10/19/89)

Included is a routine to convert from one base to another.  This
submission does not first convert to base ten.  It also is not limited
by the size of an integer variable.  I wrote this in VS-Pascal and
converted it to turbo syntax (strings are different) so there may be
a few problems. I am not sure how turbo handles concatenation of strings
and characters for example.

-----------------------------cut here---------------------------------


program con;
 {written by Tim Margush, University of Akron, 10/18/1989}
 {converts base b1 to base b2}
 { 1 < b1,b2 < 37, length of numbers limited to string lengths}
 {legal digits are 0..9, A..Z (up to limit of b1-1)}

var a,b,c:string;
    b1,b2:integer;

function c2int(x:char):integer;
 begin {returns integer for character digit}
  if (x>='0') and (x<='9')
    then c2int:=ord(x)-ord('0')
    else c2int:=ord(x)-ord('A')+10
 end;

function int2c(x:integer):string;
 begin  {returns character digit for integer}
  if x<10
    then int2c:=str(chr(ord('0')+x))
    else int2c:=str(chr(ord('A')+x-10))
 end;

function sum(x:string;y,b:integer):string;
 {computes the sum of x and y in base b}
var
 carry,digit,s:integer;
 sm:string;
begin
 carry:=y;sm:='';
 for digit:=length(x) downto 1 do begin
  s:=c2int(x[digit]) + carry;
  sm:=concat(int2c(s mod b),sm);
  carry:=s div b
 end;
 while carry>0 do begin
  sm:=concat(int2c(carry mod b),sm);
  carry:=carry div b
 end;
 sum:=sm
end;

function product(x:string;y,b:integer):string;
{computes the product of x and y in base b}
var
 carry,digit,p:integer;
 prod:string;
begin
 carry:=0;prod:='';
 for digit:=length(x) downto 1 do begin
  p:=c2int(x[digit]) * y + carry;
  prod:=concat(int2c(p mod b),prod);
  carry:=p div b
 end;
 while carry>0 do begin
  prod:=concat(int2c(carry mod b),prod);
  carry:=carry div b
 end;
 product:=prod
end;

function convert(in_base,out_base:integer;in_num:string):string;
{convert base in_base numeral to out_base numeral using the
 definition of place value representation. All arithmetic is done
 in base out_base.  In_num is processed from left to right.}
var
 digit:integer;
 out_num:string;
begin
 out_num:='0';
 for digit:=1 to length(in_num) do
  out_num:=sum(product(out_num,in_base,out_base),
               c2int(in_num[digit]),out_base);
 convert:=out_num
end;

begin {test driver}
repeat
 writeln('enter oldbase newbase numeral');
 readln(b1,b2,a);
 writeln('original:',a);
 c:=convert(b1,b2,a);
 writeln('converted to base ',b2:1,':',c)
until a='0'
end.


-----------------------------and here--------------------------------
Tim Margush                                    R1TMARG@AKRONVM.BITNET
Department of Mathematical Sciences         R1TMARG@VM1.CC.UAKRON.EDU
University Of Akron                        R1TMARG@AKRONVM.UAKRON.EDU
Akron, OH 44325                                        (216) 375-7109