Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Pascal Programming Posted by StrudentTroub on 22 Apr 2005 at 3:09 PM
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.

Report
Re: Pascal Programming Posted by zibadian on 22 Apr 2005 at 9:20 PM
: 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.
Report
Re: Pascal Programming Posted by Gaashius on 23 Apr 2005 at 2:21 AM
: 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.
:
:
I Have Eagle Eyes!!! Found the problem, marked red. Delete that line. Cool code
****************
Any questions? Just ask!

GAASHIUS





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.