Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code to display the steps when converting from decimal to
- // either binary or hexadecimal (class assignment)
- // Components used: frmMain, btnCalc, radToBin, radToHex, redDisplay
- procedure TfrmMain.btnCalcClick(Sender: TObject);
- var iNum, d, q, r: integer;
- s, sBin: string;
- begin
- redDisplay.Clear;
- if ((radToBin.Checked = False) AND (radToHex.Checked = False)) then
- begin
- ShowMessage('You need to select which conversion to perform.');
- Exit;
- end;
- iNum := StrToInt(InputBox('Input', 'Enter a number:' , ''));
- d := iNum;
- if (radToHex.Checked = True) then
- begin
- while (d > 0) do
- begin
- q := d Div 16;
- r := d Mod 16;
- s := IntToStr(d) + ' / 16 = ' + IntToStr(q) + ' rem ' + IntToStr(r);
- redDisplay.Lines.Add(s);
- d := q;
- end;
- if (d = 0) then
- begin
- redDisplay.Lines.Add('');
- s := 'Therefore, ' + IntToStr(iNum) + ' = 0x' + IntToHex(iNum, 1);
- redDisplay.Lines.Add(s);
- end;
- end
- else if (radToBin.checked = True) then
- begin
- sBin := '';
- while (d > 0) do
- begin
- q := d Div 2;
- r := d Mod 2;
- insert(IntToStr(r), sBin, 1); // create binary string
- s := IntToStr(d) + ' / 2 = ' + IntToStr(q) + ' rem ' + IntToStr(r);
- redDisplay.Lines.Add(s);
- d := q;
- end;
- if (d = 0) then
- begin
- redDisplay.Lines.Add('');
- s := 'Therefore, ' + IntToStr(iNum) + ' = 0b' + sBin;
- redDisplay.Lines.Add(s);
- end;
- end;
- end;
Advertisement