Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Pascal Programming

StrudentTroubStrudentTroub Posts: 2Member
Help with this program.



program binarytodecimal;

uses crt,strings;

type
bstring = array[0..80] of Char;


var b1,b2:bstring;
op:char;


{[*************]}

function IntToStr(I: Longint): String;
{ Convert any integer type to a string }
var
S: string;
begin
Str(I, S);
IntToStr := S;
end;

function tobinary(pass_d:longint):string;
{converts the decimal value to a binary}
var tempbinary:string;
remainder: integer;

begin
remainder:=pass_d; {set the value of the remainder}
tempbinary:=''; {make the temp space for the binary empty}

while (remainder > 0) do
begin
{make the binary string}
tempbinary := inttostr(remainder mod 2)+tempbinary;
{get a new remainder}
remainder:=remainder div 2;
end;
tobinary:=tempbinary;
end;

function raisetopower(n,p:integer):integer;
{function is used to raise n to the power of p}
var
times,ans:integer;
begin
ans:=1;
for times:=1 to p do
ans:=ans*n;
raisetopower:=ans;
end;


function todecimal(pass_b:bstring):integer;
{function used to convert a binary string
to a decimal value}
var total, {total decimal value for string}
l, {lenght of the string}
x:integer; {used to traverse the string array}

begin
total:=0; {reset to 0}
l:=strlen(pass_b); {get the lenght}

for x:=0 to l-1 do {from the right side stat calc.}
begin
if(pass_b[x] ='0')or (pass_b[x] = '1') then
begin
if pass_b[x]='1' then
total:=total+raisetopower(2,l-1-x); {calculate the decimal
value}
end
else
begin
Writeln;
writeln('Invalid Binary String!!!');
writeln('Program halted');
readkey;
Halt;
end;

end;
todecimal:=total;
end;

begin
clrscr;
{sample main block}
write('Please enter the first binary number: ');
readln(b1);
write('Please enter the secon binary number: ');
readln(b2);
write('PRESS one of these keys [*] [/] [+] [-]');


'*':
op:=readkey;
writeln;
Write('ANS = ');
case op of
'+': writeln(tobinary(todecimal(b1)+todecimal(b2))); {addition}
'-': writeln(tobinary(todecimal(b1)-todecimal(b2))); {subraction}
'/': writeln(tobinary(todecimal(b1)div todecimal(b2))); {division using div}
'*': writeln(tobinary(todecimal(b1)*todecimal(b2))); {multiplication}
else
begin
writeln('Invald Input!! Program Halted !!');
readkey;
halt;
end;
end;


readkey;

end.

Comments

  • zibadianzibadian Posts: 6,349Member
    : Help with this program.
    :
    :
    :
    : program binarytodecimal;
    :
    : uses crt,strings;
    :
    : type
    : bstring = array[0..80] of Char;
    :
    :
    : var b1,b2:bstring;
    : op:char;
    :
    :
    : {[*************]}
    :
    : function IntToStr(I: Longint): String;
    : { Convert any integer type to a string }
    : var
    : S: string;
    : begin
    : Str(I, S);
    : IntToStr := S;
    : end;
    :
    : function tobinary(pass_d:longint):string;
    : {converts the decimal value to a binary}
    : var tempbinary:string;
    : remainder: integer;
    :
    : begin
    : remainder:=pass_d; {set the value of the remainder}
    : tempbinary:=''; {make the temp space for the binary empty}
    :
    : while (remainder > 0) do
    : begin
    : {make the binary string}
    : tempbinary := inttostr(remainder mod 2)+tempbinary;
    : {get a new remainder}
    : remainder:=remainder div 2;
    : end;
    : tobinary:=tempbinary;
    : end;
    :
    : function raisetopower(n,p:integer):integer;
    : {function is used to raise n to the power of p}
    : var
    : times,ans:integer;
    : begin
    : ans:=1;
    : for times:=1 to p do
    : ans:=ans*n;
    : raisetopower:=ans;
    : end;
    :
    :
    : function todecimal(pass_b:bstring):integer;
    : {function used to convert a binary string
    : to a decimal value}
    : var total, {total decimal value for string}
    : l, {lenght of the string}
    : x:integer; {used to traverse the string array}
    :
    : begin
    : total:=0; {reset to 0}
    : l:=strlen(pass_b); {get the lenght}
    :
    : for x:=0 to l-1 do {from the right side stat calc.}
    : begin
    : if(pass_b[x] ='0')or (pass_b[x] = '1') then
    : begin
    : if pass_b[x]='1' then
    : total:=total+raisetopower(2,l-1-x); {calculate the decimal
    : value}
    : end
    : else
    : begin
    : Writeln;
    : writeln('Invalid Binary String!!!');
    : writeln('Program halted');
    : readkey;
    : Halt;
    : end;
    :
    : end;
    : todecimal:=total;
    : end;
    :
    : begin
    : clrscr;
    : {sample main block}
    : write('Please enter the first binary number: ');
    : readln(b1);
    : write('Please enter the secon binary number: ');
    : readln(b2);
    : write('PRESS one of these keys [*] [/] [+] [-]');
    :
    :
    : '*':
    : op:=readkey;
    : writeln;
    : Write('ANS = ');
    : case op of
    : '+': writeln(tobinary(todecimal(b1)+todecimal(b2))); {addition}
    : '-': writeln(tobinary(todecimal(b1)-todecimal(b2))); {subraction}
    : '/': writeln(tobinary(todecimal(b1)div todecimal(b2))); {division using div}
    : '*': writeln(tobinary(todecimal(b1)*todecimal(b2))); {multiplication}
    : else
    : begin
    : writeln('Invald Input!! Program Halted !!');
    : readkey;
    : halt;
    : end;
    : end;
    :
    :
    : readkey;
    :
    : end.
    :
    :
    First what is wrong with it? Second use style codes (See below message input screen for instructions)! Third read Raymond's "How to ask questions the smart way" guide.
  • GaashiusGaashius Posts: 349Member
    : Help with this program.
    :
    :
    :
    : program binarytodecimal;
    :
    : uses crt,strings;
    :
    : type
    : bstring = array[0..80] of Char;
    :
    :
    : var b1,b2:bstring;
    : op:char;
    :
    :
    : {[*************]}
    :
    : function IntToStr(I: Longint): String;
    : { Convert any integer type to a string }
    : var
    : S: string;
    : begin
    : Str(I, S);
    : IntToStr := S;
    : end;
    :
    : function tobinary(pass_d:longint):string;
    : {converts the decimal value to a binary}
    : var tempbinary:string;
    : remainder: integer;
    :
    : begin
    : remainder:=pass_d; {set the value of the remainder}
    : tempbinary:=''; {make the temp space for the binary empty}
    :
    : while (remainder > 0) do
    : begin
    : {make the binary string}
    : tempbinary := inttostr(remainder mod 2)+tempbinary;
    : {get a new remainder}
    : remainder:=remainder div 2;
    : end;
    : tobinary:=tempbinary;
    : end;
    :
    : function raisetopower(n,p:integer):integer;
    : {function is used to raise n to the power of p}
    : var
    : times,ans:integer;
    : begin
    : ans:=1;
    : for times:=1 to p do
    : ans:=ans*n;
    : raisetopower:=ans;
    : end;
    :
    :
    : function todecimal(pass_b:bstring):integer;
    : {function used to convert a binary string
    : to a decimal value}
    : var total, {total decimal value for string}
    : l, {lenght of the string}
    : x:integer; {used to traverse the string array}
    :
    : begin
    : total:=0; {reset to 0}
    : l:=strlen(pass_b); {get the lenght}
    :
    : for x:=0 to l-1 do {from the right side stat calc.}
    : begin
    : if(pass_b[x] ='0')or (pass_b[x] = '1') then
    : begin
    : if pass_b[x]='1' then
    : total:=total+raisetopower(2,l-1-x); {calculate the decimal
    : value}
    : end
    : else
    : begin
    : Writeln;
    : writeln('Invalid Binary String!!!');
    : writeln('Program halted');
    : readkey;
    : Halt;
    : end;
    :
    : end;
    : todecimal:=total;
    : end;
    :
    : begin
    : clrscr;
    : {sample main block}
    : write('Please enter the first binary number: ');
    : readln(b1);
    : write('Please enter the secon binary number: ');
    : readln(b2);
    : write('PRESS one of these keys [*] [/] [+] [-]');
    :
    :
    : [red]'*': [/red]
    : op:=readkey;
    : writeln;
    : Write('ANS = ');
    : case op of
    : '+': writeln(tobinary(todecimal(b1)+todecimal(b2))); {addition}
    : '-': writeln(tobinary(todecimal(b1)-todecimal(b2))); {subraction}
    : '/': writeln(tobinary(todecimal(b1)div todecimal(b2))); {division using div}
    : '*': writeln(tobinary(todecimal(b1)*todecimal(b2))); {multiplication}
    : else
    : begin
    : writeln('Invald Input!! Program Halted !!');
    : readkey;
    : halt;
    : end;
    : end;
    :
    :
    : readkey;
    :
    : end.
    :
    :
    I Have Eagle Eyes!!! Found the problem, marked red. Delete that line. Cool code :-)
    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


Sign In or Register to comment.