BINSUM.PAS 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. uses Graph,CRT;
  2. Var
  3. a,b : integer;
  4. aa,ab,res : String;
  5. {------------------------------------}
  6. Function GetValue(text:string;x1,y1,MinValue,MaxValue,dl1:integer):integer;
  7. Var
  8. p1 : pointer;
  9. s1 : word;
  10. x,y,StartX,StartY,xk : integer;
  11. k : real;
  12. length : integer;
  13. ch,tmpch : char;
  14. st : string;
  15. value : integer;
  16. code : integer;
  17. LastColor,LastBk : integer;
  18. {--------------------------------------}
  19. Procedure EnterVal;
  20. Var
  21. FillInfo :FillSettingsType;
  22. Begin
  23. GetFillSettings(FillInfo);
  24. SetFillStyle(CloseDotFill,LightBlue);
  25. Bar(Startx,starty,Startx+length,Starty+60);
  26. SetColor(White);
  27. Rectangle(Startx,starty,Startx+length,Starty+60);
  28. SetColor(LightGray);
  29. SetTextJustify(CenterText,TopText);
  30. OutTextXY(startx + length div 2, Starty+15,Text);
  31. SetColor(Yellow);
  32. SetTextJustify(LeftText,TopText);
  33. x := Startx+20;
  34. y := Starty+35;
  35. st := '';
  36. Repeat
  37. ch := readkey;
  38. if ch <> #13 then
  39. begin
  40. if ch = #8 then
  41. if ord(st[0]) <> 0 then
  42. begin
  43. delete(st,ord(st[0]),1);
  44. SetFillStyle(black,CloseDotFill);
  45. Bar(x-TextWidth(tmpch),y,x,y+TextHeight(tmpch));
  46. x := x - TextWidth(tmpch);
  47. SetFillStyle(FillInfo.color,FillInfo.pattern);
  48. End
  49. else
  50. else
  51. begin
  52. st := st + ch;
  53. OutTextXY(x,y,ch);
  54. x := x + TextWidth(ch);
  55. if x > startx+length-40 then x := x - TextWidth(ch);
  56. tmpch := ch;
  57. end;
  58. end;
  59. Until ch = #13;
  60. End;{EnterVal}
  61. {-----------------------------------}
  62. Procedure WriteBox(x,y:Integer);
  63. Begin
  64. SetColor(White);
  65. SetFillStyle(CloseDotFill,LightBlue);
  66. Bar(StartX+length div 2 - x,StartY+30-y,StartX+length div 2+x,StartY+30+y);
  67. Rectangle(StartX+length div 2 - x,StartY+30-y,StartX+length div 2+x,StartY+30+y);
  68. Delay(Dl1);
  69. End;{WriteBox}
  70. {-------------------------------------}
  71. Begin
  72. LastColor := GetColor;
  73. LastBK := GetBkColor;
  74. If MinValue > MaxValue then begin
  75. GetValue := 0;
  76. Exit;
  77. end;
  78. length := TextWidth(text) + 50;
  79. StartX := x1 - length div 2;
  80. StartY := y1 - 30;
  81. if length div 2 > x1 then startX := 2;
  82. if y1 - 30 < 2 then Starty := 1;
  83. if length div + x1 > 639 then Startx := 639 - length;
  84. if y1 + 30 > 479 then StartY := 419;
  85. s1 := ImageSize(Startx,Starty,startx+length,Starty+60);
  86. GetMem(p1,s1);
  87. GetImage(Startx,Starty,startx+length,Starty+60,p1^);
  88. value :=0 ;
  89. xk := 1;
  90. k :=60/length;
  91. for xk := 1 to length div 2 do WriteBox(xk,round(xk*k));
  92. Repeat
  93. EnterVal;
  94. val(st,value,code);
  95. Until (code = 0) and (value in [MinValue..MaxValue]);
  96. PutImage(Startx,Starty,p1^,NormalPut);
  97. GetValue := Value;
  98. SetColor(LastColor);
  99. SetBkColor(LastBk);
  100. End;{GetValue}
  101. Procedure InitG;
  102. var
  103. grDriver: Integer;
  104. grMode: Integer;
  105. ErrCode: Integer;
  106. i,j:word;
  107. begin
  108. grDriver := Detect;
  109. InitGraph(grDriver, grMode,'');
  110. ErrCode := GraphResult;
  111. if ErrCode <> grOk then
  112. begin
  113. Writeln('Graphics error:', GraphErrorMsg(ErrCode));
  114. Halt(1);
  115. end;
  116. SetColor(Blue);
  117. SetTextJustify(CenterText,CenterText);
  118. for j := 1 to 15 do
  119. begin
  120. Line(115+j*15,100,115+j*15+15,100);
  121. Line(115+j*15,100,115+j*15,115);
  122. Line(115+j*15,115,115+j*15+15,115);
  123. end;
  124. Line(115+16*15,100,115+16*15,115);
  125. SetColor(Red);
  126. Line(115,100,130,100);
  127. Line(115,100,115,115);
  128. Line(115,115,130,115);
  129. Line(130,100,130,115);
  130. SetColor(Blue);
  131. for j := 1 to 15 do
  132. begin
  133. Line(115+j*15,150,115+j*15+15,150);
  134. Line(115+j*15,150,115+j*15,165);
  135. Line(115+j*15,165,115+j*15+15,165);
  136. end;
  137. Line(115+16*15,150,115+16*15,165);
  138. SetColor(Red);
  139. Line(115,150,130,150);
  140. Line(115,150,115,165);
  141. Line(115,165,130,165);
  142. Line(130,150,130,165);
  143. End;{InitG}
  144. Function ToBinSimple(inp:integer):String;
  145. Const
  146. n : array [0..1] of string[1] = ('0','1');
  147. Var
  148. rc : string;
  149. a : integer;
  150. flag : boolean;
  151. Begin
  152. rc := '';
  153. if inp < 0 then flag := true else flag := false;
  154. a := abs(inp);
  155. repeat
  156. rc := rc + n[a mod 2];
  157. a := a div 2;
  158. until a < 2;
  159. rc := rc + n[a];
  160. for a := length(rc) to 15 do insert('0',rc,length(rc)+1);
  161. if flag then rc[16] := '1' else rc[16] := '0';
  162. ToBinSimple := rc;
  163. End;{ToBinSimple}
  164. {----------------------------}
  165. Procedure Invert(var inp:string);
  166. Var
  167. i: byte;
  168. Begin
  169. for i := 1 to 15 do if inp[i] = '0' then inp[i] := '1' else inp[i] :='0';
  170. End; {Invert}
  171. {---------------------------}
  172. Procedure WriteA(inp : string);
  173. Var i : byte;
  174. Begin
  175. setColor(White);
  176. SetTextJustify(1,1);
  177. for i := 1 to 16 do OutTextXY(362-i*15,108,inp[i]);
  178. End;{WriteA}
  179. {---------------------------}
  180. Procedure WriteB(inp : string);
  181. Var i : byte;
  182. Begin
  183. setColor(White);
  184. SetTextJustify(1,1);
  185. for i := 1 to 16 do OutTextXY(362-i*15,159,inp[i]);
  186. End;{WriteB}
  187. {---------------------------}
  188. Procedure WriteTextA(pos : byte; text:string);
  189. Begin
  190. SetTextJustify(1,1);
  191. SetColor(Yellow);
  192. OutTextXY(360-pos*15,90,'');
  193. OutTextXY(360-pos*15,80,text);
  194. End;{WriteTextA}
  195. {---------------------------}
  196. Procedure WriteTextB(pos : byte; text:string);
  197. Begin
  198. SetTextJustify(1,1);
  199. SetColor(Yellow);
  200. OutTextXY(360-pos*15,172,'');
  201. OutTextXY(360-pos*15,182,text);
  202. End;{WriteTextA}
  203. {---------------------------}
  204. Function BinSum(inA,inB : string):string;
  205. Var i,numA,numB,last,tm:byte;code : integer;rc:string;
  206. Begin
  207. i := 1;
  208. last := 0;
  209. fillChar(rc,sizeof(rc),'0');
  210. rc[0] := #17;
  211. while i <> 18 do begin
  212. val(inA[i],numA,code);
  213. val(inB[i],numB,code);
  214. tm := numA+numB+last;
  215. case tm of
  216. 3: begin
  217. rc[i] := '1';
  218. last := 1;
  219. end;
  220. 2: begin
  221. rc[i] := '0';
  222. last := 1;
  223. end;
  224. 1: begin
  225. rc[i] := '1';
  226. last := 0;
  227. end;
  228. 0: begin
  229. rc[i] := '0';
  230. last := 0;
  231. end;
  232. end;
  233. inc(i);
  234. end;
  235. BinSum := rc;
  236. End;{BinSum}
  237. {---------------------------}
  238. Procedure InAdd(var inp : string);
  239. Begin
  240. inp := BinSum(inp,'1000000000000000');
  241. End;{InAdd}
  242. {---------------------------}
  243. Procedure ClearNumA;
  244. Var i:byte;
  245. Begin
  246. SetColor(black);
  247. setfillstyle(SolidFill,Black);
  248. for i := 1 to 16 do Bar(116+i*15,101,116+i*15+13,114);
  249. End;{ClearNumA}
  250. {---------------------------}
  251. Procedure ClearNumB;
  252. Var i:byte;
  253. Begin
  254. SetColor(black);
  255. setfillstyle(SolidFill,Black);
  256. for i := 1 to 16 do Bar(116+i*15,151,116+i*15+13,164);
  257. End;{ClearNumB}
  258. {---------------------------}
  259. Procedure ClearText(pos:byte);
  260. Var i:byte;
  261. Begin
  262. SetColor(black);
  263. setfillstyle(SolidFill,black);
  264. if pos = 1 then
  265. Bar(1,75,640,92)
  266. else
  267. Bar(1,167,640,187);
  268. End;{ClearPos}
  269. {---------------------------}
  270. Procedure ClearResNum;
  271. Var j : integer;
  272. Begin
  273. SetColor(black);
  274. setfillstyle(SolidFill,Black);
  275. for j := 1 to 16 do Bar(116+j*15,181,116+j*15+13,194);
  276. End;
  277. {------------------------------------------}
  278. Procedure WriteResText(text : string;pos:integer);
  279. Begin
  280. SetColor(Yellow);
  281. OutTextXY(360-pos*15,202,'');
  282. OutTextXY(360-pos*15,212,text);
  283. End;
  284. {---------------------------------------}
  285. Procedure ClearResText;
  286. Begin
  287. SetFillStyle(SolidFill,Black);
  288. SetColor(Black);
  289. Bar(1,197,640,215);
  290. End;
  291. {----------------------------------------------------------}
  292. Procedure WriteResNum(var inp : string;ask : boolean);
  293. Var j : integer;
  294. Begin
  295. SetCOlor(Yellow);
  296. for j := 1 to 16 do begin
  297. OutTextXY(362-j*15,189,inp[j]);
  298. if ask then ReadKey;
  299. end;
  300. if inp[17] = '1' then
  301. begin
  302. setcolor(White);
  303. OutTextXY(362-17*15,189,inp[17]);
  304. WriteResText('‹¨è­¨©!',17);
  305. ReadKey;
  306. ClearResText;
  307. SetFillStyle(SolidFill,Black);
  308. SetColor(Black);
  309. Bar(1,181,110,194);
  310. delete(inp,17,1);
  311. end;
  312. End;
  313. Procedure InitRes;
  314. Var j : integer;
  315. Begin
  316. SetColor(Blue);
  317. for j := 1 to 15 do
  318. begin
  319. Line(115+j*15,180,115+j*15+15,180);
  320. Line(115+j*15,180,115+j*15,195);
  321. Line(115+j*15,195,115+j*15+15,195);
  322. end;
  323. Line(115+16*15,180,115+16*15,195);
  324. SetColor(Red);
  325. Line(115,180,130,180);
  326. Line(115,180,115,195);
  327. Line(115,195,130,195);
  328. Line(130,180,130,195);
  329. End;{InitRes}
  330. {---------------------------------}
  331. Function Step(inp : integer):integer;
  332. Var j : integer;rc:integer;
  333. Begin
  334. rc:=1;
  335. if inp = 0 then Step := 1 else
  336. begin
  337. for j := 1 to inp do rc:=rc*2;
  338. Step:=rc;
  339. end;
  340. End;{Step}
  341. {---------------------------------}
  342. Procedure ToInt(inp : string;var out : integer);
  343. Var j:integer;rc:integer;
  344. Begin
  345. rc := 0;
  346. for j := 1 to 15 do if inp[j] = '1' then inc(rc,Step(j-1));
  347. out := rc;
  348. End;{ToInt}
  349. {-------------------------------}
  350. Procedure WriteRes(var inp : string);
  351. Var j : integer;otr : boolean;res:integer;tm:string;
  352. Begin
  353. InitRes;
  354. SetCOlor(Yellow);
  355. WriteResNum(inp,true);
  356. if inp[16] = '1' then
  357. begin
  358. WriteResText('Žâà¨æ â¥«ì­®¥',16);
  359. ReadKey;
  360. ClearResText;
  361. WriteResText('ˆ­¢¥àâ¨àãî',16);
  362. ReadKey;
  363. Invert(inp);
  364. ClearResText;
  365. ClearResNum;
  366. WriteResNum(inp,false);
  367. WriteResText('+1',1);
  368. ReadKey;
  369. InAdd(inp);
  370. ClearResText;
  371. ClearResNum;
  372. WriteResNum(inp,false);
  373. WriteResText('‚á¥ ŽŠ',1);
  374. end
  375. else
  376. begin
  377. WriteResText('‚á¥ ŽŠ',1);
  378. end;
  379. if inp[16] = '1' then otr := true else otr := false;
  380. ToInt(inp,res);
  381. if otr then res := -res;
  382. str(res,tm);
  383. insert('�¥§ã«ìâ â: ',tm,1);
  384. SetColor(Green);
  385. OutTextXY(400,300,tm);
  386. ReadKey;
  387. End;{WriteRes}
  388. {----------------=======================---------------------------}
  389. Begin
  390. InitG;
  391. a := GetValue('‚¢¥¤¨â¥ ¯¥à¢®¥ ç¨á«®',320,240,-MaxInt,MaxInt,2);
  392. b := GetValue('‚¢¥¤¨â¥ ¢â®à®¥ ç¨á«®',320,240,-MaxInt,MaxInt,2);
  393. aa := ToBinSimple(a);
  394. WriteA(aa);
  395. ab := ToBinSimple(b);
  396. WriteB(ab);
  397. if aa[16] = '1' then
  398. begin
  399. WriteTextA(16,'Žâà¨æ â¥«ì­®¥');
  400. ReadKey;
  401. ClearText(1);
  402. WriteTextA(16,'ˆ­¢¥àâ¨àãî');
  403. ReadKey;
  404. Invert(aa);
  405. ClearNumA;
  406. WriteA(aa);
  407. ClearText(1);
  408. WriteTextA(16,'‚ ¤®¯. ¢¨¤');
  409. ReadKey;
  410. InAdd(aa);
  411. ClearNumA;
  412. WriteA(aa);
  413. ClearText(1);
  414. end;
  415. if ab[16] = '1' then
  416. begin
  417. WriteTextB(16,'Žâà¨æ â¥«ì­®¥');
  418. ReadKey;
  419. ClearText(2);
  420. WriteTextB(16,'ˆ­¢¥àâ¨àãî');
  421. ReadKey;
  422. Invert(ab);
  423. ClearNumB;
  424. WriteB(ab);
  425. ClearText(2);
  426. WriteTextB(16,'‚ ¤®¯. ¢¨¤');
  427. ReadKey;
  428. InAdd(ab);
  429. ClearNumB;
  430. WriteB(ab);
  431. ClearText(2);
  432. end;
  433. WriteTextA(1,'⇴ OK');
  434. WriteTextB(1,'⇴ OK');
  435. ReadKey;
  436. ClearText(1);
  437. ClearText(2);
  438. SetColor(Magenta);
  439. MoveTo(115,170);
  440. For a := 1 to 120 do
  441. begin
  442. LineRel(2,0);
  443. Delay(20);
  444. end;
  445. ReadKey;
  446. res := BinSum(aa,ab);
  447. WriteRes(Res);
  448. CloseGraph;
  449. End.