Advertisement
mehazava

Untitled

Dec 23rd, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.83 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, LCLType;
  9.  
  10. type
  11.  
  12. { TForm1 }
  13.  
  14. TForm1 = class(TForm)
  15.  
  16. ButtonBS: TButton;
  17. Button1: TButton;
  18. Button2: TButton;
  19. Button3: TButton;
  20. Button0: TButton;
  21. ButtonComma: TButton;
  22. ButtonPlusM: TButton;
  23. ButtonDivide: TButton;
  24. ButtonMult: TButton;
  25. ButtonMinus: TButton;
  26. ButtonPlus: TButton;
  27. ButtonCE: TButton;
  28. ButtonEquals: TButton;
  29. ButtonDiv1By: TButton;
  30. ButtonPercent: TButton;
  31. ButtonRoot: TButton;
  32. ButtonC: TButton;
  33. Button7: TButton;
  34. Button8: TButton;
  35. Button9: TButton;
  36. Button4: TButton;
  37. Button5: TButton;
  38. Button6: TButton;
  39. CurProc: TLabel;
  40. RepeatProc: TLabel;
  41. Error: TLabel;
  42. Sp: TEdit;
  43.  
  44. procedure Button0Click(Sender: TObject);
  45. procedure Button1Click(Sender: TObject);
  46. procedure Button2Click(Sender: TObject);
  47. procedure Button3Click(Sender: TObject);
  48. procedure Button4Click(Sender: TObject);
  49. procedure Button5Click(Sender: TObject);
  50. procedure Button6Click(Sender: TObject);
  51. procedure Button7Click(Sender: TObject);
  52. procedure Button8Click(Sender: TObject);
  53. procedure Button9Click(Sender: TObject);
  54. procedure ButtonBSClick(Sender: TObject);
  55. procedure ButtonCClick(Sender: TObject);
  56. procedure ButtonCEClick(Sender: TObject);
  57. procedure ButtonCommaClick(Sender: TObject);
  58. procedure ButtonDiv1ByClick(Sender: TObject);
  59. procedure ButtonDivideClick(Sender: TObject);
  60. procedure ButtonEqualsClick(Sender: TObject);
  61. procedure ButtonMinusClick(Sender: TObject);
  62. procedure ButtonMultClick(Sender: TObject);
  63. procedure ButtonPercentClick(Sender: TObject);
  64. procedure ButtonPlusClick(Sender: TObject);
  65. procedure ButtonPlusMClick(Sender: TObject);
  66. procedure ButtonRootClick(Sender: TObject);
  67. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  68. procedure FormKeyPress(Sender: TObject; var Key: char);
  69. procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
  70. Shift: TShiftState; X, Y: Integer);
  71.  
  72. private
  73. { private declarations }
  74. public
  75. { public declarations }
  76.  
  77. end;
  78. TInput = (Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Plus,Minus,Multiply,Divide,Comma,PlMi,BS,CE,C,Root,DivOneBy,Equal);
  79. TCalc = class
  80. private
  81. Upper,Lower,Normal: Extended;
  82. NZnaka,ErrorType: byte;
  83.  
  84. public
  85. //procedure ButtonPush(c: TInput);
  86. //procedure Recolor(a: TColor);
  87. //function IfStartAnew: boolean;
  88. //procedure UpdateInterface;
  89. end;
  90.  
  91.  
  92. var
  93. Form1: TForm1;
  94. Calc: TCalc;
  95. a,b: Extended;
  96. CurProcedure,PrevProcedure: word;
  97. EquationSuccesful: boolean;
  98. Znak: array[1..4] of char;
  99.  
  100. implementation
  101.  
  102. {$R *.lfm}
  103.  
  104. function IfStartAnew: boolean;
  105. begin
  106. Result:=false;
  107. if (Form1.Sp.Caption='0') then Result:=true;
  108. if EquationSuccesful then begin
  109. EquationSuccesful:=false;
  110. Result:=true;
  111. end;
  112. end;
  113.  
  114. procedure ButtonPush(c: TInput);
  115. begin
  116. Case ord(c) of
  117. 1:begin
  118. Form1.Error.Caption:='';
  119. Form1.RepeatProc.Caption:='';
  120. If IfStartAnew then
  121. Form1.Sp.Caption:='1'
  122. else Form1.Sp.Caption:=Form1.Sp.Caption+'1';
  123. end;
  124. 2:begin
  125. Form1.Error.Caption:='';
  126. Form1.RepeatProc.Caption:='';
  127. If IfStartAnew then
  128. Form1.Sp.Caption:='2'
  129. else Form1.Sp.Caption:=Form1.Sp.Caption+'2';
  130. end;
  131. 3:begin
  132. Form1.Error.Caption:='';
  133. Form1.RepeatProc.Caption:='';
  134. If IfStartAnew then
  135. Form1.Sp.Caption:='3'
  136. else Form1.Sp.Caption:=Form1.Sp.Caption+'3';
  137. end;
  138. 4:begin
  139. Form1.Error.Caption:='';
  140. Form1.RepeatProc.Caption:='';
  141. If IfStartAnew then
  142. Form1.Sp.Caption:='4'
  143. else Form1.Sp.Caption:=Form1.Sp.Caption+'4';
  144. end;
  145. 5:begin
  146. Form1.Error.Caption:='';
  147. Form1.RepeatProc.Caption:='';
  148. If IfStartAnew then
  149. Form1.Sp.Caption:='5'
  150. else Form1.Sp.Caption:=Form1.Sp.Caption+'5';
  151. end;
  152. 6:begin
  153. Form1.Error.Caption:='';
  154. Form1.RepeatProc.Caption:='';
  155. If IfStartAnew then
  156. Form1.Sp.Caption:='6'
  157. else Form1.Sp.Caption:=Form1.Sp.Caption+'6';
  158. end;
  159. 14:begin
  160. Form1.Error.Caption:='';
  161. Form1.RepeatProc.Caption:='';
  162. If IfStartAnew then
  163. Form1.Sp.Caption:='0,'
  164. else if (Pos(',',Form1.Sp.Caption)=0) then Form1.Sp.Caption:=Form1.Sp.Caption+',';
  165. end;
  166. 7:begin
  167. Form1.Error.Caption:='';
  168. Form1.RepeatProc.Caption:='';
  169. If IfStartAnew then
  170. Form1.Sp.Caption:='7'
  171. else Form1.Sp.Caption:=Form1.Sp.Caption+'7';
  172. end;
  173. 8:begin
  174. Form1.Error.Caption:='';
  175. Form1.RepeatProc.Caption:='';
  176. If IfStartAnew then
  177. Form1.Sp.Caption:='8'
  178. else Form1.Sp.Caption:=Form1.Sp.Caption+'8';
  179. end;
  180. 9:begin
  181. Form1.Error.Caption:='';
  182. Form1.RepeatProc.Caption:='';
  183. If IfStartAnew then
  184. Form1.Sp.Caption:='9'
  185. else Form1.Sp.Caption:=Form1.Sp.Caption+'9';
  186. end;
  187. 0:begin
  188. Form1.Error.Caption:='';
  189. Form1.RepeatProc.Caption:='';
  190. If IfStartAnew then
  191. Form1.Sp.Caption:='0'
  192. else Form1.Sp.Caption:=Form1.Sp.Caption+'0';
  193. end;
  194. 10:begin
  195. Form1.Error.Caption:='';
  196. Form1.RepeatProc.Caption:='';
  197. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  198. a:=StrToFloat(Form1.Sp.Caption);
  199. CurProcedure:=1;
  200. Form1.CurProc.Caption:=Form1.Sp.Caption+' + ';
  201. Form1.Sp.Caption:='0';
  202. end;
  203. 11:begin
  204. Form1.Error.Caption:='';
  205. Form1.RepeatProc.Caption:='';
  206. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  207. a:=StrToFloat(Form1.Sp.Caption);
  208. CurProcedure:=2;
  209. Form1.CurProc.Caption:=Form1.Sp.Caption+' - ';
  210. Form1.Sp.Caption:='0';
  211. end;
  212. 12:begin
  213. Form1.Error.Caption:='';
  214. Form1.RepeatProc.Caption:='';
  215. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  216. a:=StrToFloat(Form1.Sp.Caption);
  217. CurProcedure:=3;
  218. Form1.CurProc.Caption:=Form1.Sp.Caption+' * ';
  219. Form1.Sp.Caption:='0';
  220. end;
  221. 13:begin
  222. Form1.Error.Caption:='';
  223. Form1.RepeatProc.Caption:='';
  224. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  225. a:=StrToFloat(Form1.Sp.Caption);
  226. CurProcedure:=4;
  227. Form1.CurProc.Caption:=Form1.Sp.Caption+' / ';
  228. Form1.Sp.Caption:='0';
  229. end;
  230. 21:begin
  231. Form1.Error.Caption:='';
  232. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  233. If not((CurProcedure=4)and(Form1.Sp.Caption='0')) then begin
  234. If (CurProcedure=0) then begin
  235. Case PrevProcedure of
  236. 1:Form1.Sp.Caption:=FloatToStr(StrToFloat(Form1.Sp.Caption)+b);
  237. 2:Form1.Sp.Caption:=FloatToStr(StrToFloat(Form1.Sp.Caption)-b);
  238. 3:Form1.Sp.Caption:=FloatToStr(StrToFloat(Form1.Sp.Caption)*b);
  239. 4:Form1.Sp.Caption:=FloatToStr(StrToFloat(Form1.Sp.Caption)/b);
  240. end;
  241. end else begin
  242. b:=StrToFloat(Form1.Sp.Caption);
  243. Case CurProcedure of
  244. 1:Form1.Sp.Caption:=FloatToStr(a+StrToFloat(Form1.Sp.Caption));
  245. 2:Form1.Sp.Caption:=FloatToStr(a-StrToFloat(Form1.Sp.Caption));
  246. 3:Form1.Sp.Caption:=FloatToStr(a*StrToFloat(Form1.Sp.Caption));
  247. 4:Form1.Sp.Caption:=FloatToStr(a/StrToFloat(Form1.Sp.Caption));
  248. end;
  249. PrevProcedure:=CurProcedure;
  250. Form1.RepeatProc.Caption:=znak[CurProcedure]+' '+FloatToStr(b);
  251. CurProcedure:=0;
  252. Form1.CurProc.Caption:='';
  253. EquationSuccesful:=true;
  254. end;
  255. end else
  256. Form1.Error.Caption:='Error: can not divide by 0';
  257. end;
  258. 15:begin
  259. Form1.Error.Caption:='';
  260. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  261. Form1.Sp.Caption:=FloatToStr(-1*StrToFloat(Form1.Sp.Caption));
  262. end;
  263. 16:begin
  264. Form1.Error.Caption:='';
  265. Form1.RepeatProc.Caption:='';
  266. If not(IfStartAnew) then
  267. Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  268. If (Form1.Sp.Caption='') then Form1.Sp.Caption:='0';
  269. end;
  270. 17:begin
  271. Form1.Error.Caption:='';
  272. Form1.RepeatProc.Caption:='';
  273. Form1.Sp.Caption:='0';
  274. end;
  275. 18:begin
  276. Form1.Error.Caption:='';
  277. Form1.RepeatProc.Caption:='';
  278. Form1.Sp.Caption:='0';
  279. CurProcedure:=0;
  280. a:=0;
  281. Form1.CurProc.Caption:='';
  282. end;
  283. 19:begin
  284. Form1.Error.Caption:='';
  285. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  286. If (StrToFloat(Form1.Sp.Caption)>=0) then
  287. Form1.Sp.Caption:=FloatToStr(sqrt(StrToFloat(Form1.Sp.Caption)))
  288. else
  289. Form1.Error.Caption:='Error in counting root';
  290. end;
  291. 20:begin
  292. Form1.Error.Caption:='';
  293. If Copy(Form1.Sp.Caption,Length(Form1.Sp.Caption),1)=',' then Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  294. If not(Form1.Sp.Caption='0') then
  295. Form1.Sp.Caption:=FloatToStr(1/StrToFloat(Form1.Sp.Caption))
  296. else
  297. Form1.Error.Caption:='Error: can not divide by 0';
  298. end;
  299. end;
  300. end;
  301.  
  302.  
  303. procedure Recolor(a:TColor);
  304. begin
  305. Form1.Canvas.Brush.Color:=a;
  306. Form1.Canvas.Pen.Color:=a;
  307. Form1.Canvas.Rectangle(0,0,Form1.Width,Form1.Height);
  308. end;
  309.  
  310. { TForm1 }
  311.  
  312.  
  313. procedure TForm1.ButtonBSClick(Sender: TObject);
  314. begin
  315. ButtonPush(BS);
  316. end;
  317.  
  318. procedure TForm1.Button7Click(Sender: TObject);
  319. begin
  320. ButtonPush(Seven);
  321. end;
  322.  
  323. procedure TForm1.Button4Click(Sender: TObject);
  324. begin
  325. ButtonPush(Four);
  326. end;
  327.  
  328. procedure TForm1.Button1Click(Sender: TObject);
  329. begin
  330. ButtonPush(One);
  331. end;
  332.  
  333. procedure TForm1.Button0Click(Sender: TObject);
  334. begin
  335. ButtonPush(Zero);
  336. end;
  337.  
  338. procedure TForm1.Button2Click(Sender: TObject);
  339. begin
  340. ButtonPush(Two);
  341. end;
  342.  
  343. procedure TForm1.Button3Click(Sender: TObject);
  344. begin
  345. ButtonPush(Three);
  346. end;
  347.  
  348. procedure TForm1.Button5Click(Sender: TObject);
  349. begin
  350. ButtonPush(Five);
  351. end;
  352.  
  353. procedure TForm1.Button6Click(Sender: TObject);
  354. begin
  355. ButtonPush(Six);
  356. end;
  357.  
  358. procedure TForm1.Button8Click(Sender: TObject);
  359. begin
  360. ButtonPush(Eight);
  361. end;
  362.  
  363. procedure TForm1.Button9Click(Sender: TObject);
  364. begin
  365. ButtonPush(Nine);
  366. end;
  367.  
  368. procedure TForm1.ButtonCClick(Sender: TObject);
  369. begin
  370. ButtonPush(C);
  371. end;
  372.  
  373. procedure TForm1.ButtonCEClick(Sender: TObject);
  374. begin
  375. ButtonPush(CE);
  376. end;
  377.  
  378. procedure TForm1.ButtonCommaClick(Sender: TObject);
  379. begin
  380. ButtonPush(Comma);
  381. end;
  382.  
  383. procedure TForm1.ButtonDiv1ByClick(Sender: TObject);
  384. begin
  385. ButtonPush(DivOneBy);
  386. end;
  387.  
  388. procedure TForm1.ButtonDivideClick(Sender: TObject);
  389. begin
  390. ButtonPush(Divide);
  391. end;
  392.  
  393. procedure TForm1.ButtonEqualsClick(Sender: TObject);
  394. begin
  395. ButtonPush(Equal);
  396. end;
  397.  
  398. procedure TForm1.ButtonMinusClick(Sender: TObject);
  399. begin
  400. ButtonPush(Minus);
  401. end;
  402.  
  403. procedure TForm1.ButtonMultClick(Sender: TObject);
  404. begin
  405. ButtonPush(Multiply);
  406. end;
  407.  
  408. procedure TForm1.ButtonPercentClick(Sender: TObject);
  409. begin
  410.  
  411. end;
  412.  
  413. procedure TForm1.ButtonPlusClick(Sender: TObject);
  414. begin
  415. ButtonPush(Plus);
  416. end;
  417.  
  418. procedure TForm1.ButtonPlusMClick(Sender: TObject);
  419. begin
  420. ButtonPush(PlMi);
  421. end;
  422.  
  423. procedure TForm1.ButtonRootClick(Sender: TObject);
  424. begin
  425. ButtonPush(Root);
  426. end;
  427.  
  428. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  429. );
  430. begin
  431. Form1.Error.Caption:='';
  432. If ssShift in Shift then
  433. Case Key of
  434. VK_DOWN: Form1.Top:=Form1.Top+10;
  435. VK_UP: Form1.Top:=Form1.Top-10;
  436. VK_LEFT: Form1.Left:=Form1.Left-10;
  437. VK_RIGHT: Form1.Left:=Form1.Left+10;
  438. VK_0: Recolor(clBlack);
  439. VK_1: Recolor(clWhite);
  440. VK_2: Recolor(clLime);
  441. VK_3: Recolor(clDefault);
  442. VK_4: Recolor(clRed);
  443. VK_5: Recolor(clBlue);
  444. VK_6: Recolor(clYellow);
  445. VK_F1: begin
  446. Form1.Left:=random(1686);
  447. Form1.Top:=random(766);
  448. end;
  449. end else
  450. Case Key of
  451. VK_DOWN: Form1.Top:=Form1.Top+1;
  452. VK_UP: Form1.Top:=Form1.Top-1;
  453. VK_LEFT: Form1.Left:=Form1.Left-1;
  454. VK_RIGHT: Form1.Left:=Form1.Left+1;
  455. VK_BACK: begin
  456. Form1.Error.Caption:='';
  457. Form1.RepeatProc.Caption:='';
  458. If not(IfStartAnew) then
  459. Form1.Sp.Caption:=Copy(Form1.Sp.Caption,1,Length(Form1.Sp.Caption)-1);
  460. If (Form1.Sp.Caption='') then Form1.Sp.Caption:='0';
  461. end;
  462. end;
  463. end;
  464.  
  465. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  466. begin
  467. Form1.Error.Caption:='';
  468. Case Key of
  469. '1':ButtonPush(One);
  470. '2':ButtonPush(Two);
  471. '3':ButtonPush(Three);
  472. '4':ButtonPush(Four);
  473. '5':ButtonPush(Five);
  474. '6':ButtonPush(Six);
  475. '.':ButtonPush(Comma);
  476. '7':ButtonPush(Seven);
  477. '8':ButtonPush(Eight);
  478. '9':ButtonPush(Nine);
  479. '0':ButtonPush(Zero);
  480. '+':ButtonPush(Plus);
  481. '-':ButtonPush(Minus);
  482. '*':ButtonPush(Multiply);
  483. '/':ButtonPush(Divide);
  484. ' ':ButtonPush(Equal);
  485. end;
  486. end;
  487.  
  488. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  489. Shift: TShiftState; X, Y: Integer);
  490. begin
  491.  
  492. end;
  493.  
  494. Begin
  495. CurProcedure:=0;
  496. a:=0;
  497. znak[1]:='+';
  498. znak[2]:='-';
  499. znak[3]:='*';
  500. znak[4]:='/';
  501. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement