voltage

longfpc

Oct 24th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.89 KB | None | 0 0
  1. {*
  2. * С клавиатуры вводится основание с/с (от 2 до 36, в случае >10 для ввода цифр используются буквы латинского алфавита)
  3. * и два числа длины не более 1000 цифр. Необходимо написать программу, которая будет выводить результат вычитания этих чисел.
  4. * В программе необходимо предусмотреть вывод чисел, как с цифрами >10 в виде букв, так и чисел
  5.  *}
  6.  
  7. const ErrorByte = $FF; { 255 }
  8.       BigIntegerMaxDigits = 1000;
  9.  
  10. type
  11.     BigInteger = record
  12.         n : word;
  13.         base : byte;
  14.         bytes : array[0 .. BigIntegerMaxDigits] of byte;
  15.     end;
  16.  
  17.  
  18. function char2byte(c : char) : byte;
  19. begin
  20.     c := lowerCase(c);
  21.  
  22.     if c in ['0'..'9'] then
  23.         char2byte := byte(ord(c) - ord('0'))
  24.     else if c in ['a'..'z'] then
  25.         char2byte := byte(ord(c) - ord('a') + 10)
  26.     else
  27.         char2byte := ErrorByte;
  28. end;
  29.  
  30.  
  31. function byte2char(b : byte) : char;
  32. begin
  33.     if b in [0..9] then
  34.         byte2char := char(ord('0') + b)
  35.     else if b in [10..35]then
  36.         byte2char := char(ord('A') + b - 10)
  37.     else
  38.         byte2char := '#';
  39. end;
  40.  
  41.  
  42. function readBase() : byte;
  43. var
  44.     i, n : integer;
  45.     base : string;
  46.     pow10, result : word;
  47.     sym : byte;
  48. begin
  49.     readln(base);
  50.     n := length(base);
  51.    
  52.    
  53.     pow10 := 1;
  54.     result := 0;
  55.    
  56.     for i := 1 to n do
  57.     begin
  58.         sym := char2byte(base[n - i + 1]);
  59.        
  60.         if sym = ErrorByte then
  61.         begin
  62.             readBase := ErrorByte;
  63.             Exit();
  64.         end;
  65.        
  66.         result := result + sym * pow10;
  67.         pow10 := pow10 * 10;
  68.     end;
  69.    
  70.     if (result >= 2) and (result <= 36) then
  71.         readBase := byte(result)
  72.     else
  73.         readBase := ErrorByte;
  74. end;
  75.    
  76.    
  77. function read_BigInteger_withBase(var number : BigInteger) : boolean;
  78. var
  79.     temp : byte;
  80.     c : char;
  81. begin
  82.     number.base := readBase();
  83.    
  84.     if number.base = ErrorByte then
  85.     begin
  86.         read_BigInteger_withBase := false;
  87.         Exit();
  88.     end;
  89.    
  90.     number.n := 0;
  91.     FillChar(number.bytes, BigIntegerMaxDigits, 0);
  92.    
  93.     while True do
  94.     begin
  95.         if number.n > BigIntegerMaxDigits then
  96.         begin
  97.             read_BigInteger_withBase := false;
  98.             Exit();
  99.         end;
  100.    
  101.         read(c);
  102.        
  103.         if (c = #10) or (c = #13) then
  104.             break;
  105.            
  106.         inc(number.n);
  107.         temp := char2byte(c);
  108.        
  109.         if (temp >= number.base) or (temp = ErrorByte) then
  110.         begin
  111.             read_BigInteger_withBase := false;
  112.             Exit();
  113.         end;
  114.        
  115.         number.bytes[number.n] := temp;
  116.     end;
  117.    
  118.     read_BigInteger_withBase := true;
  119. end;
  120.  
  121.  
  122. procedure write_BigInteger(number : BigInteger);
  123. var
  124.     i : word;
  125. begin
  126.     for i := 1 to number.n do
  127.         write(byte2char(number.bytes[i]));
  128.     writeln;
  129. end;
  130.  
  131.  
  132. function toBase_BigInteger(number : BigInteger; base : byte) : BigInteger;
  133. var
  134.     i, temp : word;
  135.     result : BigInteger;
  136. begin
  137.     //
  138. end;
  139.  
  140.  
  141. function substract_BigInteger(left, right : BigInteger) : BigInteger;
  142. begin
  143.     //
  144. end;
  145.  
  146.  
  147. var
  148.     left, right : BigInteger;
  149.     noError : boolean;
  150.    
  151. begin
  152.     noError := read_BigInteger_withBase(left);
  153.    
  154.     if not noError then
  155.     begin
  156.         writeln('Error reading first number');
  157.         Exit();
  158.     end;
  159.    
  160.     noError := read_BigInteger_withBase(right);
  161.    
  162.     if not noError then
  163.     begin
  164.         writeln('Error reading second number');
  165.         Exit();
  166.     end;
  167.    
  168.     write_BigInteger(left);
  169.     write_BigInteger(right);
  170.    
  171.     {write_BigInteger(substract_BigInteger(left, right));}    
  172. end.
Advertisement
Add Comment
Please, Sign In to add comment