Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1.  
  2. const digits:array [0..1] of char = ('0','1');
  3.  
  4.  
  5. function tobin(x, a:longint):string;
  6. var res,s:string; d:0..1; i,j:integer;
  7. begin
  8. if x<0 then x:=x+trunc(2**a);
  9. i:=0;
  10. res:='';
  11. while (x<>0) do begin
  12. d:=x mod 2;
  13. res:=digits[d]+res;
  14. x:=x div 2;
  15. i:=i+1;
  16. end;
  17. s:='';
  18. for j:=i to a-1 do s:=s+'0';
  19. tobin:=s+res;
  20. end;
  21.  
  22. function todec(x:string):longint;
  23. const digits:array [0..1] of char = ('0','1');
  24. var res,ves:longint; i,j:byte;
  25. begin
  26. res:=0; ves:=1;
  27. for i:=length(x) downto 1 do begin
  28. j:=0;
  29. while (digits[j]<>x[i]) do inc(j);
  30. res:=res+ves*j;
  31. ves:=ves*2;
  32. end;
  33. todec:=res;
  34. end;
  35.  
  36. function tozndec(x:string; a:longint):longint;
  37. const digits:array [0..1] of char = ('0','1');
  38. var res,ves:longint; i,j:byte;
  39. begin
  40. res:=0; ves:=1;
  41. for i:=length(x) downto 1 do begin
  42. j:=0;
  43. while (digits[j]<>x[i]) do inc(j);
  44. res:=res+ves*j;
  45. ves:=ves*2;
  46. end;
  47. if res > trunc(2**(a-1)) then res:=res-trunc(2**a);
  48. tozndec:=res;
  49. end;
  50.  
  51. var
  52. a,b,c,d,e:longint;
  53. s,s1,s2,s3:string;
  54. f1,f2,f3,f4,f5,f6,f7,f8:byte;
  55.  
  56.  
  57. begin
  58. readln(a,b,c);
  59. d:=(b+c) mod trunc(2**a);
  60. e:=(b-c) mod trunc(2**a);
  61.  
  62. s:=tobin(b, a);
  63. s1:=tobin(c, a);
  64. s2:=tobin(d, a);
  65. s3:=tobin(e, a);
  66.  
  67. if todec(s2)=0 then f1:=1 else f1:=0;
  68. if todec(s3)=0 then f5:=1 else f5:=0;
  69. if tozndec(s2,a)<0 then f2:=1 else f2:=0;
  70. if tozndec(s3,a)<0 then f6:=1 else f6:=0;
  71. if ((b>=0) and (c>=0)) or (((b*c)<=0) and ((b+c)<0)) then f3:=0 else f3:=1;
  72. if ( ((b<0) and (c>=0)) or ((b>=c) and (c>=0)) or ((0>b) and (b>=c)) ) then f7:=0 else f7:=1;
  73. if ( ((b+c) >= (2**(a-1))*(-1)) and ((b+c) < (2**(a-1))) ) then f4:=0 else f4:=1;
  74. if ( ((b-c) >= (2**(a-1))*(-1)) and ((b-c) < (2**(a-1))) ) then f8:=0 else f8:=1;
  75.  
  76.  
  77. //writeln('+ ', s, ' ', s1, ' ', s2, ' ', todec(s2), ' ', tozndec(s2,a));
  78. //writeln('- ', s, ' ', s1, ' ', s3, ' ', todec(s3), ' ', tozndec(s3,a));
  79.  
  80. writeln('+ ', copy(s, length(s) - a, a), ' ', copy(s1, length(s1) - a, a), ' ', s2, ' ', todec(s2), ' ', tozndec(s2,a), ' ', f1,f2,f3,f4);
  81. writeln('- ', copy(s, length(s) - a, a), ' ', copy(s1, length(s1) - a, a), ' ', s3, ' ', todec(s3), ' ', tozndec(s3,a), ' ', f5,f6,f7,f8);
  82. writeln;
  83.  
  84.  
  85. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement