Программа перевода чисел из любой системы счисления в другую. Дробная часть учитывается — Pascal(Паскаль)

program notation;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

Var
  st: Set of Char;

Function F(a, b: Integer): Integer;
Var
  i: Integer;
Begin
  Result := a;

  For i := 1 To b - 1 Do
    Result := Result * a;

  IF b = 0 Then
    Result := 1;

End;

Function ConvertDec(n: ShortString; q: Integer): Integer;
Var
  p, j, i: Integer;
Begin
  j := Length(n) - 1;
  Result := 0;

  For i := 1 To Length(n) Do
  Begin

    IF n[i] in [#48 .. #58] Then
    Begin
      Result := Result + F(q, j) * StrToInt(n[i]);
      Dec(j);
    End

    Else IF n[i] in st Then
    Begin
      Result := Result + F(q, j) * (Ord(n[i]) - 55);
      Dec(j);
    End

  End;
End;

Function ConvertSS(d, q: Integer): ShortString;
Begin
  Result := '';

  While d <> 0 Do
  Begin

    IF d mod q > 9 Then
      Result := Chr(d mod q + 55) + Result
    Else
      Result := IntToStr(d mod q) + Result;

    d := d div q;
  End;
End;

Function ConvertEx(n: Extended; acc, q: Integer): ShortString;
Begin
  Result := ',';

  IF acc = 0 Then
  Begin
    Result := Result + '0';
    Exit;
  End;

  While Length(Result) + 1 Do
  Begin
    n := n * q;
    Result := Result + IntToStr(Trunc(n));
    n := n - Trunc(n);
  End;

End;

Var
  eps, i, tmp, qin, qout: Integer;
  t, nin: ShortString;
  buf: _Input_Record;
  n: Cardinal;
  r: Extended;

Label cont;

begin
  st := ['A' .. 'Z', 'a' .. 'z'];
cont:
  eps := 0;
  WriteLn('Input data{');
  WriteLn;
  Write('Enter N: ');
  ReadLn(nin);
  Write('Enter Q: ');
  ReadLn(qin);
  Write('Enter Q: ');
  ReadLn(qout);

  IF Pos(',', nin) <> 0 Then
  Begin
    Write('Enter accuracy: ');
    ReadLn(eps);
    t := '0,' + IntToStr(ConvertDec(Copy(nin, Pos(',', nin) + 1,
      Length(nin)), qin));
    r := StrToFloat(t);
  End;

  WriteLn('}');
  WriteLn('Output data{');
  Write('N=');

  IF qin = qout Then
    Write(nin)
  Else
  Begin
    IF eps <> 0 Then
      tmp := ConvertDec(Copy(nin, 1, Pos(',', nin) - 1), qin)
    Else
      tmp := ConvertDec(nin, qin);
    Write(ConvertSS(tmp, qout) + ConvertEx(r, eps, qout));
  End;

  WriteLn;
  WriteLn('}');
  WriteLn('Press ''q'' for quit or ''enter'' for continue...');
  WriteLn;

  Repeat
    ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), buf, 1, n);

    Case buf.Event.KeyEvent.AsciiChar of
      #81, #113:
        Break;
      #13:
        GoTo cont;
    End;

  Until False;

end.

Leave a Comment

+ 46 = 50