一级棒(Eachfun)
一级棒 - 教学经纬 - 奥赛辅导 - 三十三课:二进制到十进制的转化
RSS订阅
三十三课:二进制到十进制的转化
发表时间:2002-12-26 00:00:00 关键词:奥赛,PASCAL,编程,源码

program btod2;
var
  b:string;
  str1:char;
  i,p,leftofdot,rightofdot:integer;dot:boolean;
  a1,a2:array [1..20] of integer;
  d,y:real;
begin
  write('Input the Binary Number:');readln(b);
  p:=pos('.',b);
  if p=0 then b:=b+'.';
  if p=1 then b:='0'+b;
  dot:=false;
  for i:=1 to length(b) do
    begin
      if b[i]='.' then
                    begin
                      dot:=true;
                      leftofdot:=i-1
                    end
                  else
                    begin
                      if dot then
                                      begin
                                        str1:=b[i];
                                        a2[i-leftofdot-1]:=ord(str1)-48;
                                      end
                                    else
                                      begin
                                        str1:=b[i];
                                        a1[i]:=ord(str1)-48;
                                      end;
                    end;
    end;
  for i:=1 to leftofdot do write(a1[i]);
  write('   .   ');
  for i:=1 to length(b)-leftofdot-1 do write(a2[i]);
  writeln;
  y:=1;d:=a1[leftofdot];
  for i:=leftofdot-1 downto 1 do
    begin
      y:=y*2;
      d:=d+a1[i]*y;
    end;
  y:=1;
  for i:=1 to length(b)-leftofdot-1 do
    begin
      y:=y/2;
      d:=d+a2[i]*y;
    end;
  writeln(d:10:5)
end.

本站特约顾问律师常州东晟律师事务所朱立律师(电话13915029670,QQ646146109)提醒您:
本站文章皆为作者原创,其它媒体(包括但不限于报刊、杂志、网站、电视、电台)未经作者书面许可严禁转载(或部分摘录)!
发表评论
称呼:
QQ:
邮箱:
链接:
内容:
搜索: 百度搜索 Google搜索
Copyright©2000 - 2008 Eachfun.Com, All Rights Reserved 一级棒网络
苏ICP备05080156号
一级棒建站系统 http://www.eachfun.com 一级棒版权所有,未经许可不得商用!