youjnhuhn2 Posted December 4, 2013 Report Posted December 4, 2013 (edited) Am un proiect de facut in Delphi7 care imi cere urmatorul lucru:sa afisez elementul maxim/minim al unui vector ce-l citesc dintr-un fisier .txtproblema e ca nu stiu cum sa urc vectorul din fisier intr-o variabila si sa-l parcurg sa gasesc maxim si minim.vectorul e de genul:[VECTOR]1101.2-223199.14.12-6.72-6.444.13http://postimg.org/image/cmmqgstkl/ Edited December 4, 2013 by youjnhuhn2 Quote
Vlachs Posted December 4, 2013 Report Posted December 4, 2013 Cea mai simpla metoda TStringList.LoadFromFile Quote
youjnhuhn2 Posted December 4, 2013 Author Report Posted December 4, 2013 pana acum am facut asta si aici sunt mort n-am niciun dumnezeu unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus; type TForm1 = class(TForm) MainMenu1: TMainMenu; Fisier1: TMenuItem; Deschidere1: TMenuItem; Parasireaplicatie1: TMenuItem; Vizualizare1: TMenuItem; Vizualizarevector1: TMenuItem; Calcul1: TMenuItem; Calculeaza1: TMenuItem; procedure Deschidere1Click(Sender: TObject); procedure Parasireaplicatie1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Deschidere1Click(Sender: TObject); var openDialog : TOpenDialog; begin openDialog := TOpenDialog.Create(self); openDialog.InitialDir := GetCurrentDir; openDialog.Options := [ofFileMustExist]; openDialog.Filter := 'Text Files|*.txt|' ; openDialog.FilterIndex := 2; if openDialog.Execute then ShowMessage('File : '+openDialog.FileName) else ShowMessage('Deschiderea fisierului e fost anulata'); openDialog.Free; end; procedure TForm1.Parasireaplicatie1Click(Sender: TObject); var buttonSelected : Integer; begin buttonSelected := MessageDlg('Sigur doriti sa parasiti aplicatia?',mtCustom, [mbYes,mbNo], 0); if buttonSelected = mrYes then Application.Terminate; end; end. Quote
sulea Posted December 4, 2013 Report Posted December 4, 2013 in pascal:assign(f,'numere.txt');reset(f);readln(f); //treci peste stringul '[vector]'readln(f,max); //min=max=min:=max; // = primul nr din fisiera[1]:=min; //il pui in vector[1]i:=2; //restul valorilor le vei pune in vector incepand cu poz. 2while (not eof(f)) do begin readln(f,n); if max<n then max:=n else if min>n then min:=n; a[i]:=n; inc(i);end;writeln(min,' ',max);end. Quote
staticwater Posted December 4, 2013 Report Posted December 4, 2013 The good old delphi... Link proiect: http://rapidshare.com/share/A6C3EA1ED588D085902E300DF8AA1ADA unit fVectorUnit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Menus;type TfVector = class(TForm) mm: TMainMenu; Fiseire1: TMenuItem; Deschidere1: TMenuItem; ParasireAplicatie1: TMenuItem; Vizualizare1: TMenuItem; Calcul1: TMenuItem; Calculeaza1: TMenuItem; Vizualizarevector1: TMenuItem; reVector: TRichEdit; OD: TOpenDialog; procedure Deschidere1Click(Sender: TObject); procedure ParasireAplicatie1Click(Sender: TObject); procedure Vizualizarevector1Click(Sender: TObject); procedure Calculeaza1Click(Sender: TObject); private { Private declarations } public Vector: array of real; { Public declarations } end;var fVector: TfVector;implementationuses fMinMaxUnit;{$R *.dfm}procedure TfVector.Deschidere1Click(Sender: TObject);var List: TStringList; I: Integer;begin OD.InitialDir := ExtractFilePath(Application.ExeName); if OD.Execute then begin List := TStringList.Create; try List.LoadFromFile(OD.FileName); SetLength(Vector, List.Count); for I := 0 to High(Vector) do begin try Vector[I] := StrToFloat(List.Strings[I]) except MessageDlg('Date invalide!', mtError, [mbOk], 0); Vector := nil; Break; end; end; finally List.Free; end; end;end;procedure TfVector.ParasireAplicatie1Click(Sender: TObject);begin if MessageDlg('Sigur doriti sa parasiti aplicatia?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then Application.Terminate;end;procedure TfVector.Vizualizarevector1Click(Sender: TObject);var I: Integer;begin if Length(Vector) = 0 then begin MessageDlg('Baza de date nu a fost incarcata!', mtInformation, [mbOk], 0); Exit; end; for I := 0 to High(Vector) do reVector.Lines.Add('v[' + IntToStr(I) + ']='+ FloatToStrF(Vector[I], ffFixed, 3, 3))end;procedure TfVector.Calculeaza1Click(Sender: TObject);begin if Length(Vector) = 0 then begin MessageDlg('Baza de date nu a fost incarcata!', mtInformation, [mbOk], 0); Exit; end; fMinMax.ShowModal;end;end.unit fMinMaxUnit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TfMinMax = class(TForm) rbMin: TRadioButton; rbMax: TRadioButton; bCalc: TButton; bClose: TButton; procedure bCloseClick(Sender: TObject); procedure bCalcClick(Sender: TObject); private { Private declarations } public { Public declarations } end;var fMinMax: TfMinMax;implementationuses fVectorUnit;{$R *.dfm}function MaxVector: real;var I: Integer; M: real;begin M := fVector.Vector[Low(fVector.Vector)]; for I := 1 to High(fVector.Vector) do if fVector.Vector[I] > M then M := fVector.Vector[I]; Result := M;end;function MinVector: real;var I: Integer; M: real;begin M := fVector.Vector[Low(fVector.Vector)]; for I := 1 to High(fVector.Vector) do if fVector.Vector[I] < M then M := fVector.Vector[I]; Result := M;end;procedure TfMinMax.bCalcClick(Sender: TObject);var MinMax: real; I: Integer;begin if rbMin.Checked then MessageDlg('Elementul minim: ' + FloatToStrF(MinVector, ffFixed, 3, 3), mtInformation, [mbOk], 0) else MessageDlg('Elementul maxim: ' + FloatToStrF(MaxVector, ffFixed, 3, 3), mtInformation, [mbOk], 0)end;procedure TfMinMax.bCloseClick(Sender: TObject);begin Close;end;end. 1 Quote