Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.89 KB | None | 0 0
  1. unit UnitMain;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IBX.IBDatabaseInfo,
  8. Vcl.Menus;
  9.  
  10. type
  11. TFormMain = class(TForm)
  12. BtnAdd: TButton;
  13. BtnEdit: TButton;
  14. BtnDel: TButton;
  15. BtnView: TButton;
  16. MainMenu: TMainMenu;
  17. Help: TMenuItem;
  18. ButtonLoad: TButton;
  19. ButtonSave: TButton;
  20. OpenDialogMain: TOpenDialog;
  21. SaveDialogMain: TSaveDialog;
  22. procedure BtnAddClick(Sender: TObject);
  23. procedure FormCreate(Sender: TObject);
  24. procedure BtnDelClick(Sender: TObject);
  25. procedure BtnViewClick(Sender: TObject);
  26. procedure ButtonLoadClick(Sender: TObject);
  27. procedure ButtonSaveClick(Sender: TObject);
  28. procedure HelpClick(Sender: TObject);
  29. procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  30. private
  31. { Private declarations }
  32. public
  33. { Public declarations }
  34. end;
  35. TStats = record
  36. GroupNum: Integer;
  37. Surname: string[15];
  38. Math: Integer;
  39. Physics: Integer;
  40. Programming: Integer;
  41. EGraphics: Integer;
  42. end;
  43. TStatsArr = array of TStats;
  44. TGroupInfo = record
  45. GroupNum: Integer;
  46. FirstIndex: Integer;
  47. LastIndex: Integer;
  48. HiMark: Integer;
  49. LowMark: Integer;
  50. end;
  51. TGroupArr = array of TGroupInfo;
  52. TCompareFunc = function(x1, x2: TStats): Boolean;
  53.  
  54. var
  55. FormMain: TFormMain;
  56. isSaved: Boolean;
  57. isChanged: Boolean;
  58. GroupArr: TGroupArr;
  59.  
  60. implementation
  61.  
  62. {$R *.dfm}
  63.  
  64. uses
  65. UnitAdd, UnitEdit, UnitDelete, UnitView;
  66.  
  67. procedure TFormMain.BtnAddClick(Sender: TObject);
  68. var
  69. i: Integer;
  70. begin
  71. if not Assigned(FormAdd) then
  72. FormAdd := TFormAdd.Create(FormMain)
  73. else
  74. begin
  75. for i := 1 to FormAdd.StringGridAdd.RowCount - 1 do
  76. FormAdd.StringGridAdd.Rows[i].Clear;
  77. FormAdd.StringGridAdd.RowCount := 1;
  78. FormAdd.StringGridAdd.Width := 455*2;
  79. FormAdd.Visible := True;
  80. FormAdd.EditGrp.SetFocus;
  81. end;
  82. FormMain.Visible := False;
  83. end;
  84.  
  85. function Merge(Left, Right: TStatsArr; CompareFunc: TCompareFunc): TStatsArr;
  86. var
  87. TempArr: TStatsArr;
  88. i, k: Integer;
  89. begin
  90. SetLength(TempArr, Length(Left) + Length(Right));
  91. k := 0;
  92. while (Length(Left) > 0) and (Length(Right) > 0) do
  93. if CompareFunc(Left[0], Right[0]) then
  94. begin
  95. TempArr[k] := Left[0];
  96. Inc(k);
  97. Left := Copy(Left, 1, Length(Left) - 1);
  98. end
  99. else
  100. begin
  101. TempArr[k] := Right[0];
  102. Inc(k);
  103. Right := Copy(Right, 1, Length(Right) - 1);
  104. end;
  105. if Length(Left) > 0 then
  106. for i := 0 to High(Left) do
  107. begin
  108. TempArr[k] := Left[i];
  109. Inc(k);
  110. end;
  111. if Length(Right) > 0 then
  112. for i := 0 to High(Right) do
  113. begin
  114. TempArr[k] := Right[i];
  115. Inc(k);
  116. end;
  117. Result := TempArr;
  118. end;
  119.  
  120. function MergeSort(DataArr: TStatsArr; CompareFunc: TCompareFunc): TStatsArr;
  121. var
  122. i: Integer;
  123. Middle: Integer;
  124. Left, Right: TStatsArr;
  125. begin
  126. if Length(DataArr) <= 1 then
  127. Result := DataArr
  128. else
  129. begin
  130. Middle := (Length(DataArr) div 2) - 1;
  131. for i := 0 to Middle do
  132. begin
  133. SetLength(Left, Length(Left) + 1);
  134. Left[High(Left)] := DataArr[i];
  135. end;
  136. for i := Middle + 1 to High(DataArr) do
  137. begin
  138. SetLength(Right, Length(Right) + 1);
  139. Right[High(Right)] := DataArr[i];
  140. end;
  141. left := MergeSort(Left, CompareFunc);
  142. Right := MergeSort(Right, CompareFunc);
  143. Result := Merge(left, right, CompareFunc);
  144. end;
  145. end;
  146.  
  147. function ReadGrdArr: TStatsArr;
  148. var
  149. Arr: TStatsArr;
  150. TempRec: TStats;
  151. i: Integer;
  152. begin
  153. SetLength(Arr, FormDelete.StringGridDelete.RowCount - 1);
  154. for i := 1 to FormDelete.StringGridDelete.RowCount - 1 do
  155. begin
  156. TempRec.GroupNum := StrToInt(FormDelete.StringGridDelete.Cells[1, i]);
  157. TempRec.Surname := (FormDelete.StringGridDelete.Cells[2, i]);
  158. TempRec.Math := StrToInt(FormDelete.StringGridDelete.Cells[3, i]);
  159. TempRec.Physics := StrToInt(FormDelete.StringGridDelete.Cells[4, i]);
  160. TempRec.Programming := StrToInt(FormDelete.StringGridDelete.Cells[5, i]);
  161. TempRec.EGraphics := StrToInt(FormDelete.StringGridDelete.Cells[6, i]);
  162. Arr[i - 1] := TempRec;
  163. end;
  164. ReadGrdArr := (Arr);
  165. end;
  166.  
  167. procedure PrepareGroupArray(Arr: TStatsArr);
  168. var
  169. i: Integer;
  170. begin
  171. SetLength(GroupArr, 1);
  172. GroupArr[0].GroupNum := Arr[0].GroupNum;
  173. GroupArr[0].FirstIndex := 0;
  174. if Length(Arr) = 1 then
  175. begin
  176. GroupArr[0].LastIndex := 0;
  177. end
  178. else
  179. begin
  180. i := 1;
  181. while i <> Length(Arr) do
  182. begin
  183. if Arr[i].GroupNum <> GroupArr[High(GroupArr)].GroupNum then
  184. begin
  185. SetLength(GroupArr, Length(GroupArr) + 1);
  186. GroupArr[High(GroupArr)].GroupNum := Arr[i].GroupNum;
  187. GroupArr[High(GroupArr)].FirstIndex := i;
  188. GroupArr[High(GroupArr) - 1].LastIndex := i - 1;
  189. end;
  190. Inc(i);
  191. end;
  192. GroupArr[High(GroupArr)].LastIndex := High(Arr);
  193. end;
  194. end;
  195.  
  196. function CompareMarks(x1, x2: TStats): Boolean;
  197. begin
  198. if ((x1.Physics + x1.Math + x1.Programming + x1.EGraphics)) > ((x2.Physics + x2.Math + x2.Programming + x2.EGraphics)) then
  199. CompareMarks := True
  200. else
  201. CompareMarks := False;
  202. end;
  203.  
  204. function CompareGroups(x1, x2: TStats): Boolean;
  205. begin
  206. if x1.GroupNum < x2.GroupNum then
  207. CompareGroups := True
  208. else
  209. CompareGroups := False;
  210. end;
  211.  
  212. function CompareNames(x1, x2: TStats): Boolean;
  213. begin
  214. if x1.Surname < x2.Surname then
  215. CompareNames := True
  216. else
  217. CompareNames := False;
  218. end;
  219.  
  220. procedure ReloadMainGrid(Arr: TStatsArr);
  221. var
  222. i: Integer;
  223. begin
  224. FormDelete.StringGridDelete.RowCount := 1;
  225. for i := 0 to High(Arr) do
  226. begin
  227. FormDelete.StringGridDelete.RowCount := FormDelete.StringGridDelete.RowCount + 1;
  228. FormDelete.StringGridDelete.Cells[0, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(FormDelete.StringGridDelete.RowCount - 1);
  229. FormDelete.StringGridDelete.Cells[1, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].GroupNum);
  230. FormDelete.StringGridDelete.Cells[2, FormDelete.StringGridDelete.RowCount - 1] := (Arr[i].Surname);
  231. FormDelete.StringGridDelete.Cells[3, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].Math);
  232. FormDelete.StringGridDelete.Cells[4, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].Physics);
  233. FormDelete.StringGridDelete.Cells[5, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].Programming);
  234. FormDelete.StringGridDelete.Cells[6, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].EGraphics);
  235. end;
  236. end;
  237.  
  238. procedure PrepareOutput;
  239. const
  240. NotEstated = -1;
  241. var
  242. CompareFunc: TCompareFunc;
  243. TempArr: TStatsArr;
  244. i, k, j, a: Integer;
  245. TempFullArr: TStatsArr;
  246. FirstIndex, LastIndex: Integer;
  247. begin
  248. CompareFunc := CompareGroups;
  249. TempFullArr := ReadGrdArr;
  250. TempFullArr := MergeSort(TempFullArr, CompareFunc);
  251. PrepareGroupArray(TempFullArr);
  252. CompareFunc := CompareMarks;
  253. for i := 0 to High(GroupArr) do
  254. begin
  255. TempArr := Copy(TempFullArr, GroupArr[i].FirstIndex, GroupArr[i].LastIndex - GroupArr[i].FirstIndex + 1);
  256. TempArr := MergeSort(TempArr, CompareFunc);
  257. j := 0;
  258. for k := GroupArr[i].FirstIndex to GroupArr[i].LastIndex do
  259. begin
  260. TempFullArr[k] := TempArr[j];
  261. Inc(j);
  262. end;
  263. GroupArr[i].HiMark := NotEstated;
  264. GroupArr[i].LowMark := NotEstated;
  265. a := GroupArr[i].FirstIndex;
  266. with TempFullArr[a] do
  267. begin
  268. if ((Math + Physics + EGraphics + Programming)) > 27 then
  269. begin
  270. GroupArr[i].HiMark := GroupArr[i].FirstIndex;
  271. while (a < GroupArr[i].LastIndex) and (((TempFullArr[a + 1].Math + TempFullArr[a + 1].Physics + TempFullArr[a + 1].Programming + TempFullArr[a + 1].EGraphics)) > 27) do
  272. Inc(a);
  273. GroupArr[i].LowMark := a;
  274. end;
  275. end;
  276. end;
  277. CompareFunc := CompareNames;
  278. for i := 0 to High(GroupArr) do
  279. begin
  280. if GroupArr[i].LowMark = NotEstated then
  281. begin
  282. FirstIndex := GroupArr[i].FirstIndex;
  283. LastIndex := GroupArr[i].LastIndex;
  284. end
  285. else
  286. begin
  287. FirstIndex := GroupArr[i].HiMark;
  288. LastIndex := GroupArr[i].LowMark;
  289. end;
  290. TempArr := Copy(TempFullArr, FirstIndex, LastIndex - FirstIndex + 1);
  291. TempArr := MergeSort(TempArr, CompareFunc);
  292. j := 0;
  293. for k := FirstIndex to LastIndex do
  294. begin
  295. TempFullArr[k] := TempArr[j];
  296. Inc(j);
  297. end;
  298. end;
  299. ReloadMainGrid(TempFullArr);
  300. end;
  301.  
  302. procedure NoDataMsg;
  303. begin
  304. MessageDlg('There is no data, you need to input data first!', mtError, [mbOk], 0);
  305. end;
  306.  
  307. procedure ShowDeleteForm;
  308. begin
  309. if not Assigned(FormDelete) then
  310. FormDelete := TFormDelete.Create(FormMain);
  311. if (FormDelete.StringGridDelete.RowCount - 1) > 0 then
  312. begin
  313. FormDelete.Visible := True;
  314. if FormDelete.StringGridDelete.RowCount - 1 > 9 then
  315. FormDelete.StringGridDelete.Width := 470*2
  316. else
  317. FormDelete.StringGridDelete.Width := 455*2;
  318. FormMain.Visible := False;
  319. end
  320. else
  321. NoDataMsg;
  322. end;
  323.  
  324. procedure TFormMain.BtnDelClick(Sender: TObject);
  325. begin
  326. ShowDeleteForm;
  327. end;
  328.  
  329. procedure ShowViewForm;
  330. var
  331. i: Integer;
  332. begin
  333. if (FormDelete.StringGridDelete.RowCount - 1) = 0 then
  334. NoDataMsg
  335. else
  336. begin
  337. if not Assigned(FormView) then
  338. FormView := TFormView.Create(FormMain);
  339. if isChanged then
  340. begin
  341. PrepareOutput;
  342. FormView.CBGroup.Items.Clear;
  343. for i := 0 to High(GroupArr) do
  344. FormView.CBGroup.Items.Add(IntToStr(GroupArr[i].GroupNum));
  345. isChanged := False;
  346. FormView.CBGroup.ItemIndex := 0;
  347. if GroupArr[0].HiMark <> - 1 then
  348. begin
  349. { FormView.StringGridView.Visible := True;
  350. FormView.StaticTextInfoPic.Visible := False;
  351. FormView.StaticTextInfoPic2.Visible := False;
  352. FormView.ImageAnimals1.Visible := False;
  353. FormView.ImageAnimals2.Visible := False;
  354. FormView.StringGridView.RowCount := 1;}
  355. for i := GroupArr[0].HiMark to GroupArr[0].LowMark do
  356. with FormView do
  357. begin
  358. StringGridView.RowCount := StringGridView.RowCount + 1;
  359. StringGridView.Cells[0, StringGridView.RowCount - 1] := IntToStr(StringGridView.RowCount - 1);
  360. StringGridView.Cells[1, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[1, i + 1];
  361. StringGridView.Cells[2, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[2, i + 1];
  362. StringGridView.Cells[3, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[3, i + 1];
  363. StringGridView.Cells[4, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[4, i + 1];
  364. StringGridView.Cells[5, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[5, i + 1];
  365. StringGridView.Cells[6, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[6, i + 1];
  366. StringGridView.Cells[7, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[7, i + 1];
  367. end;
  368. if FormView.StringGridView.RowCount < 15 then
  369. begin
  370.  
  371. end
  372. else
  373. begin
  374.  
  375. end;
  376. end
  377. else
  378. { begin
  379. case Random(2) of
  380. 0:
  381. begin
  382. FormView.ImageAnimals1.Visible := True;
  383. end;
  384. 1:
  385. begin
  386. FormView.ImageAnimals2.Visible := True;
  387. end;
  388. end;
  389. FormView.StringGridView.Visible := False;
  390. FormView.StaticTextInfoPic.Visible := True;
  391. FormView.StaticTextInfoPic2.Visible := True;
  392. end; }
  393. end;
  394. FormView.Visible := True;
  395. FormView.SetFocus;
  396. FormMain.Visible := False;
  397. end;
  398. end;
  399.  
  400. procedure TFormMain.BtnViewClick(Sender: TObject);
  401. begin
  402. ShowViewForm;
  403. end;
  404.  
  405. procedure SaveData;
  406. var
  407. TempRec: TStats;
  408. i: Integer;
  409. SaveFile: file of TStats;
  410. begin
  411. with FormMain do
  412. begin
  413. saveDialogMain := TSaveDialog.Create(saveDialogMain);
  414. saveDialogMain.Title := 'Save your file';
  415. saveDialogMain.InitialDir := GetCurrentDir;
  416. saveDialogMain.Filter := 'Typed file|*.dat|Text file|*.txt';
  417. saveDialogMain.DefaultExt := 'dat';
  418. saveDialogMain.FilterIndex := 1;
  419. if saveDialogMain.Execute then
  420. begin
  421. try
  422. AssignFile(SaveFile,saveDialogMain.FileName);
  423. Rewrite(SaveFile);
  424. if (FormDelete.StringGridDelete.RowCount - 1) > 0 then
  425. for i := 1 to FormDelete.StringGridDelete.RowCount - 1 do
  426. begin
  427. TempRec.GroupNum := StrToInt(FormDelete.StringGridDelete.Cells[1, i]);
  428. TempRec.Surname := (FormDelete.StringGridDelete.Cells[2, i]);
  429. TempRec.Math := StrToInt(FormDelete.StringGridDelete.Cells[3, i]);
  430. TempRec.Physics := StrToInt(FormDelete.StringGridDelete.Cells[4, i]);
  431. TempRec.Programming := StrToInt(FormDelete.StringGridDelete.Cells[5, i]);
  432. TempRec.EGraphics := StrToInt(FormDelete.StringGridDelete.Cells[6, i]);
  433. Write(SaveFile, TempRec)
  434. end
  435. else
  436. CloseFile(SaveFile);
  437. isSaved := True;
  438. except
  439. MessageDlg('An error occured while saving data :( ', mtError, [mbOk], 0);
  440. CloseFile(SaveFile);
  441. isSaved:= False;
  442. end;
  443. end;
  444. FreeAndNil(saveDialogMain);
  445. end;
  446. end;
  447.  
  448. procedure AskSaving;
  449. var
  450. buttonSelected : Integer;
  451. strQuestion: string;
  452. begin
  453. strQuestion := 'Whoops, it seems that you''ve forgot to save your document! Would you like to do it before quitting??';
  454. buttonSelected := MessageDlg(strQuestion , mtConfirmation, mbOKCancel, 0);
  455. if buttonSelected = mrOK then
  456. SaveData;
  457. end;
  458.  
  459. function Confirm: Boolean;
  460. var
  461. buttonSelected : Integer;
  462. strQuestion: string;
  463. begin
  464. strQuestion := 'An error occured while loading/opening file. Would you like to load another file (ok) or to work with empty spreadsheet(Сancel)?';
  465. buttonSelected := MessageDlg(strQuestion , mtConfirmation, mbOKCancel, 0);
  466. if buttonSelected = mrOK then
  467. Confirm := True
  468. else
  469. Confirm := False;
  470. end;
  471.  
  472. procedure LoadData;
  473. const
  474. MaxLength = 200;
  475. var
  476. TempRec: TStats;
  477. LoadFile: File of TStats;
  478. begin
  479. FormDelete.StringGridDelete.RowCount := 1;
  480. FormMain.OpenDialogMain := TOpenDialog.Create(FormMain.OpenDialogMain);
  481. FormMain.OpenDialogMain.InitialDir := GetCurrentDir;
  482. if FormMain.OpenDialogMain.Execute then
  483. begin
  484. try
  485. AssignFile(LoadFile, FormMain.OpenDialogMain.FileName);
  486. Reset(LoadFile);
  487. if not EoF(LoadFile) then
  488. begin
  489. Seek(LoadFile, 0);
  490. while not EoF(LoadFile) and (FormDelete.StringGridDelete.RowCount < MaxLength + 1) do
  491. begin
  492. FormDelete.StringGridDelete.RowCount := FormDelete.StringGridDelete.RowCount + 1;
  493. Read(LoadFile, TempRec);
  494. FormDelete.StringGridDelete.Cells[0, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(FormDelete.StringGridDelete.RowCount - 1);
  495. FormDelete.StringGridDelete.Cells[1, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.GroupNum);
  496. FormDelete.StringGridDelete.Cells[2, FormDelete.StringGridDelete.RowCount - 1] := (TempRec.Surname);
  497. FormDelete.StringGridDelete.Cells[3, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.Math);
  498. FormDelete.StringGridDelete.Cells[4, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.Physics);
  499. FormDelete.StringGridDelete.Cells[5, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.Programming);
  500. FormDelete.StringGridDelete.Cells[6, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.EGraphics);
  501. end;
  502. if (FormDelete.StringGridDelete.RowCount - 1 = MaxLength) and not EoF(LoadFile) then
  503. MessageDlg('Max number of students was exceeded. Thus the rest of file was cut.', mtInformation, [mbOk], 0);
  504. end
  505. else
  506. FormDelete.StringGridDelete.RowCount := 0;
  507. CloseFile(LoadFile);
  508. isChanged := True;
  509. except
  510. if not Confirm then
  511. begin
  512. FormDelete.StringGridDelete.RowCount := 0;
  513. end;
  514. end;
  515. end;
  516. FreeAndNil(FormMain.OpenDialogMain);
  517. end;
  518.  
  519. procedure TFormMain.ButtonLoadClick(Sender: TObject);
  520. begin
  521. if (FormDelete.StringGridDelete.RowCount > 0) and not isSaved then
  522. AskSaving;
  523. LoadData;
  524. end;
  525.  
  526. procedure TFormMain.ButtonSaveClick(Sender: TObject);
  527. begin
  528. SaveData;
  529. end;
  530.  
  531. procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  532. begin
  533. if not isSaved then
  534. begin
  535. AskSaving;
  536. end
  537. end;
  538.  
  539. procedure TFormMain.FormCreate(Sender: TObject);
  540. begin
  541. isSaved := True;
  542. isChanged := True;
  543. end;
  544.  
  545. procedure TFormMain.HelpClick(Sender: TObject);
  546. begin
  547. MessageDlg('Programm allows you to work with spreadsheets that represent academic performance of students [max = 200]. To learn about other functions chek their own help tabs.', mtInformation, [mbOk], 0);
  548. end;
  549.  
  550. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement