Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit UnitMain;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IBX.IBDatabaseInfo,
- Vcl.Menus;
- type
- TFormMain = class(TForm)
- BtnAdd: TButton;
- BtnEdit: TButton;
- BtnDel: TButton;
- BtnView: TButton;
- MainMenu: TMainMenu;
- Help: TMenuItem;
- ButtonLoad: TButton;
- ButtonSave: TButton;
- OpenDialogMain: TOpenDialog;
- SaveDialogMain: TSaveDialog;
- procedure BtnAddClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure BtnDelClick(Sender: TObject);
- procedure BtnViewClick(Sender: TObject);
- procedure ButtonLoadClick(Sender: TObject);
- procedure ButtonSaveClick(Sender: TObject);
- procedure HelpClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- TStats = record
- GroupNum: Integer;
- Surname: string[15];
- Math: Integer;
- Physics: Integer;
- Programming: Integer;
- EGraphics: Integer;
- end;
- TStatsArr = array of TStats;
- TGroupInfo = record
- GroupNum: Integer;
- FirstIndex: Integer;
- LastIndex: Integer;
- HiMark: Integer;
- LowMark: Integer;
- end;
- TGroupArr = array of TGroupInfo;
- TCompareFunc = function(x1, x2: TStats): Boolean;
- var
- FormMain: TFormMain;
- isSaved: Boolean;
- isChanged: Boolean;
- GroupArr: TGroupArr;
- implementation
- {$R *.dfm}
- uses
- UnitAdd, UnitEdit, UnitDelete, UnitView;
- procedure TFormMain.BtnAddClick(Sender: TObject);
- var
- i: Integer;
- begin
- if not Assigned(FormAdd) then
- FormAdd := TFormAdd.Create(FormMain)
- else
- begin
- for i := 1 to FormAdd.StringGridAdd.RowCount - 1 do
- FormAdd.StringGridAdd.Rows[i].Clear;
- FormAdd.StringGridAdd.RowCount := 1;
- FormAdd.StringGridAdd.Width := 455*2;
- FormAdd.Visible := True;
- FormAdd.EditGrp.SetFocus;
- end;
- FormMain.Visible := False;
- end;
- function Merge(Left, Right: TStatsArr; CompareFunc: TCompareFunc): TStatsArr;
- var
- TempArr: TStatsArr;
- i, k: Integer;
- begin
- SetLength(TempArr, Length(Left) + Length(Right));
- k := 0;
- while (Length(Left) > 0) and (Length(Right) > 0) do
- if CompareFunc(Left[0], Right[0]) then
- begin
- TempArr[k] := Left[0];
- Inc(k);
- Left := Copy(Left, 1, Length(Left) - 1);
- end
- else
- begin
- TempArr[k] := Right[0];
- Inc(k);
- Right := Copy(Right, 1, Length(Right) - 1);
- end;
- if Length(Left) > 0 then
- for i := 0 to High(Left) do
- begin
- TempArr[k] := Left[i];
- Inc(k);
- end;
- if Length(Right) > 0 then
- for i := 0 to High(Right) do
- begin
- TempArr[k] := Right[i];
- Inc(k);
- end;
- Result := TempArr;
- end;
- function MergeSort(DataArr: TStatsArr; CompareFunc: TCompareFunc): TStatsArr;
- var
- i: Integer;
- Middle: Integer;
- Left, Right: TStatsArr;
- begin
- if Length(DataArr) <= 1 then
- Result := DataArr
- else
- begin
- Middle := (Length(DataArr) div 2) - 1;
- for i := 0 to Middle do
- begin
- SetLength(Left, Length(Left) + 1);
- Left[High(Left)] := DataArr[i];
- end;
- for i := Middle + 1 to High(DataArr) do
- begin
- SetLength(Right, Length(Right) + 1);
- Right[High(Right)] := DataArr[i];
- end;
- left := MergeSort(Left, CompareFunc);
- Right := MergeSort(Right, CompareFunc);
- Result := Merge(left, right, CompareFunc);
- end;
- end;
- function ReadGrdArr: TStatsArr;
- var
- Arr: TStatsArr;
- TempRec: TStats;
- i: Integer;
- begin
- SetLength(Arr, FormDelete.StringGridDelete.RowCount - 1);
- for i := 1 to FormDelete.StringGridDelete.RowCount - 1 do
- begin
- TempRec.GroupNum := StrToInt(FormDelete.StringGridDelete.Cells[1, i]);
- TempRec.Surname := (FormDelete.StringGridDelete.Cells[2, i]);
- TempRec.Math := StrToInt(FormDelete.StringGridDelete.Cells[3, i]);
- TempRec.Physics := StrToInt(FormDelete.StringGridDelete.Cells[4, i]);
- TempRec.Programming := StrToInt(FormDelete.StringGridDelete.Cells[5, i]);
- TempRec.EGraphics := StrToInt(FormDelete.StringGridDelete.Cells[6, i]);
- Arr[i - 1] := TempRec;
- end;
- ReadGrdArr := (Arr);
- end;
- procedure PrepareGroupArray(Arr: TStatsArr);
- var
- i: Integer;
- begin
- SetLength(GroupArr, 1);
- GroupArr[0].GroupNum := Arr[0].GroupNum;
- GroupArr[0].FirstIndex := 0;
- if Length(Arr) = 1 then
- begin
- GroupArr[0].LastIndex := 0;
- end
- else
- begin
- i := 1;
- while i <> Length(Arr) do
- begin
- if Arr[i].GroupNum <> GroupArr[High(GroupArr)].GroupNum then
- begin
- SetLength(GroupArr, Length(GroupArr) + 1);
- GroupArr[High(GroupArr)].GroupNum := Arr[i].GroupNum;
- GroupArr[High(GroupArr)].FirstIndex := i;
- GroupArr[High(GroupArr) - 1].LastIndex := i - 1;
- end;
- Inc(i);
- end;
- GroupArr[High(GroupArr)].LastIndex := High(Arr);
- end;
- end;
- function CompareMarks(x1, x2: TStats): Boolean;
- begin
- if ((x1.Physics + x1.Math + x1.Programming + x1.EGraphics)) > ((x2.Physics + x2.Math + x2.Programming + x2.EGraphics)) then
- CompareMarks := True
- else
- CompareMarks := False;
- end;
- function CompareGroups(x1, x2: TStats): Boolean;
- begin
- if x1.GroupNum < x2.GroupNum then
- CompareGroups := True
- else
- CompareGroups := False;
- end;
- function CompareNames(x1, x2: TStats): Boolean;
- begin
- if x1.Surname < x2.Surname then
- CompareNames := True
- else
- CompareNames := False;
- end;
- procedure ReloadMainGrid(Arr: TStatsArr);
- var
- i: Integer;
- begin
- FormDelete.StringGridDelete.RowCount := 1;
- for i := 0 to High(Arr) do
- begin
- FormDelete.StringGridDelete.RowCount := FormDelete.StringGridDelete.RowCount + 1;
- FormDelete.StringGridDelete.Cells[0, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(FormDelete.StringGridDelete.RowCount - 1);
- FormDelete.StringGridDelete.Cells[1, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].GroupNum);
- FormDelete.StringGridDelete.Cells[2, FormDelete.StringGridDelete.RowCount - 1] := (Arr[i].Surname);
- FormDelete.StringGridDelete.Cells[3, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].Math);
- FormDelete.StringGridDelete.Cells[4, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].Physics);
- FormDelete.StringGridDelete.Cells[5, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].Programming);
- FormDelete.StringGridDelete.Cells[6, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(Arr[i].EGraphics);
- end;
- end;
- procedure PrepareOutput;
- const
- NotEstated = -1;
- var
- CompareFunc: TCompareFunc;
- TempArr: TStatsArr;
- i, k, j, a: Integer;
- TempFullArr: TStatsArr;
- FirstIndex, LastIndex: Integer;
- begin
- CompareFunc := CompareGroups;
- TempFullArr := ReadGrdArr;
- TempFullArr := MergeSort(TempFullArr, CompareFunc);
- PrepareGroupArray(TempFullArr);
- CompareFunc := CompareMarks;
- for i := 0 to High(GroupArr) do
- begin
- TempArr := Copy(TempFullArr, GroupArr[i].FirstIndex, GroupArr[i].LastIndex - GroupArr[i].FirstIndex + 1);
- TempArr := MergeSort(TempArr, CompareFunc);
- j := 0;
- for k := GroupArr[i].FirstIndex to GroupArr[i].LastIndex do
- begin
- TempFullArr[k] := TempArr[j];
- Inc(j);
- end;
- GroupArr[i].HiMark := NotEstated;
- GroupArr[i].LowMark := NotEstated;
- a := GroupArr[i].FirstIndex;
- with TempFullArr[a] do
- begin
- if ((Math + Physics + EGraphics + Programming)) > 27 then
- begin
- GroupArr[i].HiMark := GroupArr[i].FirstIndex;
- while (a < GroupArr[i].LastIndex) and (((TempFullArr[a + 1].Math + TempFullArr[a + 1].Physics + TempFullArr[a + 1].Programming + TempFullArr[a + 1].EGraphics)) > 27) do
- Inc(a);
- GroupArr[i].LowMark := a;
- end;
- end;
- end;
- CompareFunc := CompareNames;
- for i := 0 to High(GroupArr) do
- begin
- if GroupArr[i].LowMark = NotEstated then
- begin
- FirstIndex := GroupArr[i].FirstIndex;
- LastIndex := GroupArr[i].LastIndex;
- end
- else
- begin
- FirstIndex := GroupArr[i].HiMark;
- LastIndex := GroupArr[i].LowMark;
- end;
- TempArr := Copy(TempFullArr, FirstIndex, LastIndex - FirstIndex + 1);
- TempArr := MergeSort(TempArr, CompareFunc);
- j := 0;
- for k := FirstIndex to LastIndex do
- begin
- TempFullArr[k] := TempArr[j];
- Inc(j);
- end;
- end;
- ReloadMainGrid(TempFullArr);
- end;
- procedure NoDataMsg;
- begin
- MessageDlg('There is no data, you need to input data first!', mtError, [mbOk], 0);
- end;
- procedure ShowDeleteForm;
- begin
- if not Assigned(FormDelete) then
- FormDelete := TFormDelete.Create(FormMain);
- if (FormDelete.StringGridDelete.RowCount - 1) > 0 then
- begin
- FormDelete.Visible := True;
- if FormDelete.StringGridDelete.RowCount - 1 > 9 then
- FormDelete.StringGridDelete.Width := 470*2
- else
- FormDelete.StringGridDelete.Width := 455*2;
- FormMain.Visible := False;
- end
- else
- NoDataMsg;
- end;
- procedure TFormMain.BtnDelClick(Sender: TObject);
- begin
- ShowDeleteForm;
- end;
- procedure ShowViewForm;
- var
- i: Integer;
- begin
- if (FormDelete.StringGridDelete.RowCount - 1) = 0 then
- NoDataMsg
- else
- begin
- if not Assigned(FormView) then
- FormView := TFormView.Create(FormMain);
- if isChanged then
- begin
- PrepareOutput;
- FormView.CBGroup.Items.Clear;
- for i := 0 to High(GroupArr) do
- FormView.CBGroup.Items.Add(IntToStr(GroupArr[i].GroupNum));
- isChanged := False;
- FormView.CBGroup.ItemIndex := 0;
- if GroupArr[0].HiMark <> - 1 then
- begin
- { FormView.StringGridView.Visible := True;
- FormView.StaticTextInfoPic.Visible := False;
- FormView.StaticTextInfoPic2.Visible := False;
- FormView.ImageAnimals1.Visible := False;
- FormView.ImageAnimals2.Visible := False;
- FormView.StringGridView.RowCount := 1;}
- for i := GroupArr[0].HiMark to GroupArr[0].LowMark do
- with FormView do
- begin
- StringGridView.RowCount := StringGridView.RowCount + 1;
- StringGridView.Cells[0, StringGridView.RowCount - 1] := IntToStr(StringGridView.RowCount - 1);
- StringGridView.Cells[1, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[1, i + 1];
- StringGridView.Cells[2, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[2, i + 1];
- StringGridView.Cells[3, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[3, i + 1];
- StringGridView.Cells[4, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[4, i + 1];
- StringGridView.Cells[5, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[5, i + 1];
- StringGridView.Cells[6, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[6, i + 1];
- StringGridView.Cells[7, StringGridView.RowCount - 1] := FormDelete.StringGridDelete.Cells[7, i + 1];
- end;
- if FormView.StringGridView.RowCount < 15 then
- begin
- end
- else
- begin
- end;
- end
- else
- { begin
- case Random(2) of
- 0:
- begin
- FormView.ImageAnimals1.Visible := True;
- end;
- 1:
- begin
- FormView.ImageAnimals2.Visible := True;
- end;
- end;
- FormView.StringGridView.Visible := False;
- FormView.StaticTextInfoPic.Visible := True;
- FormView.StaticTextInfoPic2.Visible := True;
- end; }
- end;
- FormView.Visible := True;
- FormView.SetFocus;
- FormMain.Visible := False;
- end;
- end;
- procedure TFormMain.BtnViewClick(Sender: TObject);
- begin
- ShowViewForm;
- end;
- procedure SaveData;
- var
- TempRec: TStats;
- i: Integer;
- SaveFile: file of TStats;
- begin
- with FormMain do
- begin
- saveDialogMain := TSaveDialog.Create(saveDialogMain);
- saveDialogMain.Title := 'Save your file';
- saveDialogMain.InitialDir := GetCurrentDir;
- saveDialogMain.Filter := 'Typed file|*.dat|Text file|*.txt';
- saveDialogMain.DefaultExt := 'dat';
- saveDialogMain.FilterIndex := 1;
- if saveDialogMain.Execute then
- begin
- try
- AssignFile(SaveFile,saveDialogMain.FileName);
- Rewrite(SaveFile);
- if (FormDelete.StringGridDelete.RowCount - 1) > 0 then
- for i := 1 to FormDelete.StringGridDelete.RowCount - 1 do
- begin
- TempRec.GroupNum := StrToInt(FormDelete.StringGridDelete.Cells[1, i]);
- TempRec.Surname := (FormDelete.StringGridDelete.Cells[2, i]);
- TempRec.Math := StrToInt(FormDelete.StringGridDelete.Cells[3, i]);
- TempRec.Physics := StrToInt(FormDelete.StringGridDelete.Cells[4, i]);
- TempRec.Programming := StrToInt(FormDelete.StringGridDelete.Cells[5, i]);
- TempRec.EGraphics := StrToInt(FormDelete.StringGridDelete.Cells[6, i]);
- Write(SaveFile, TempRec)
- end
- else
- CloseFile(SaveFile);
- isSaved := True;
- except
- MessageDlg('An error occured while saving data :( ', mtError, [mbOk], 0);
- CloseFile(SaveFile);
- isSaved:= False;
- end;
- end;
- FreeAndNil(saveDialogMain);
- end;
- end;
- procedure AskSaving;
- var
- buttonSelected : Integer;
- strQuestion: string;
- begin
- strQuestion := 'Whoops, it seems that you''ve forgot to save your document! Would you like to do it before quitting??';
- buttonSelected := MessageDlg(strQuestion , mtConfirmation, mbOKCancel, 0);
- if buttonSelected = mrOK then
- SaveData;
- end;
- function Confirm: Boolean;
- var
- buttonSelected : Integer;
- strQuestion: string;
- begin
- strQuestion := 'An error occured while loading/opening file. Would you like to load another file (ok) or to work with empty spreadsheet(Сancel)?';
- buttonSelected := MessageDlg(strQuestion , mtConfirmation, mbOKCancel, 0);
- if buttonSelected = mrOK then
- Confirm := True
- else
- Confirm := False;
- end;
- procedure LoadData;
- const
- MaxLength = 200;
- var
- TempRec: TStats;
- LoadFile: File of TStats;
- begin
- FormDelete.StringGridDelete.RowCount := 1;
- FormMain.OpenDialogMain := TOpenDialog.Create(FormMain.OpenDialogMain);
- FormMain.OpenDialogMain.InitialDir := GetCurrentDir;
- if FormMain.OpenDialogMain.Execute then
- begin
- try
- AssignFile(LoadFile, FormMain.OpenDialogMain.FileName);
- Reset(LoadFile);
- if not EoF(LoadFile) then
- begin
- Seek(LoadFile, 0);
- while not EoF(LoadFile) and (FormDelete.StringGridDelete.RowCount < MaxLength + 1) do
- begin
- FormDelete.StringGridDelete.RowCount := FormDelete.StringGridDelete.RowCount + 1;
- Read(LoadFile, TempRec);
- FormDelete.StringGridDelete.Cells[0, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(FormDelete.StringGridDelete.RowCount - 1);
- FormDelete.StringGridDelete.Cells[1, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.GroupNum);
- FormDelete.StringGridDelete.Cells[2, FormDelete.StringGridDelete.RowCount - 1] := (TempRec.Surname);
- FormDelete.StringGridDelete.Cells[3, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.Math);
- FormDelete.StringGridDelete.Cells[4, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.Physics);
- FormDelete.StringGridDelete.Cells[5, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.Programming);
- FormDelete.StringGridDelete.Cells[6, FormDelete.StringGridDelete.RowCount - 1] := IntToStr(TempRec.EGraphics);
- end;
- if (FormDelete.StringGridDelete.RowCount - 1 = MaxLength) and not EoF(LoadFile) then
- MessageDlg('Max number of students was exceeded. Thus the rest of file was cut.', mtInformation, [mbOk], 0);
- end
- else
- FormDelete.StringGridDelete.RowCount := 0;
- CloseFile(LoadFile);
- isChanged := True;
- except
- if not Confirm then
- begin
- FormDelete.StringGridDelete.RowCount := 0;
- end;
- end;
- end;
- FreeAndNil(FormMain.OpenDialogMain);
- end;
- procedure TFormMain.ButtonLoadClick(Sender: TObject);
- begin
- if (FormDelete.StringGridDelete.RowCount > 0) and not isSaved then
- AskSaving;
- LoadData;
- end;
- procedure TFormMain.ButtonSaveClick(Sender: TObject);
- begin
- SaveData;
- end;
- procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- if not isSaved then
- begin
- AskSaving;
- end
- end;
- procedure TFormMain.FormCreate(Sender: TObject);
- begin
- isSaved := True;
- isChanged := True;
- end;
- procedure TFormMain.HelpClick(Sender: TObject);
- begin
- 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);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement