ユーザーが2進数を入力すると、10進数を取得する単純なコンソールプログラムを実行しようとしています。バイナリ文字列に0または1以外のものがあるかどうかを確認する必要はありません。すでに10進数から2進数に変換できましたが、それ以外の方法は実行できません。
SOとRedditで見つかったコードを試しましたが、ほとんどの場合、エラーI / O105が発生しました。
これがビンへのdecです:
program dectobin;
{$APPTYPE CONSOLE}
uses
SysUtils,
Crt32;
var
d,a :Integer;
str :String;
begin
str:='';
Readln(a);
while a>0 do begin
d:=a mod 2;
str:=concat(IntToStr(d),str);
a:=a div 2;
end;
Writeln(str);
Readln;
end.```
位置番号システム(PNS)の基本
重量は常にbase * weight of previous digit
(右から左の方向に)あることに注意してください
任意の位置記数法における一連の数の一般的な解釈
assign a variable 'result' = 0
assign a variable 'weight' = 1 (for (base)^0 )
repeat
read the rightmost (not yet read) digit from string
convert digit from character to integer
multiply it by weight and add to variable 'result'
multiply weight by base in prep. for next digit
until no more digits
前の後で、egIntToStr()
を使用して10進文字列に変換できます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加