Sei sulla pagina 1di 7

1Exemplu 1 sdmp

2#include <conio.h>
3#include <stdio.h>
4#include <iostream.h>
5#include <string.h>
6#include <math.h>
7#include <stdlib.h>
8#include <time.h>
9#include <dos.h>
10//*********************clasa abstracta
11class elem {
12public:
13

virtual int fscanf_el (FILE * f)=0;

14

virtual int show(const char * opening,const char * ending)=0;

15

virtual int free ()=0;

16

int operator > (elem &) {

17

error("Error should overide operator \">\"!\n"); return 0; }

18

int operator < (elem &) {

19

error("Error should overide operator \"<\"!\n"); return 0; }

20

int operator >= (elem &) {

21

error("Error should overide operator \">=\"!\n"); return 0; }

22

int operator <= (elem &) {

23

error("Error should overide operator \"<=\"!\n"); return 0; }

24

int operator == (elem &) {

25

error("Error should overide operator \"==\"!\n"); return 0; }

26

int operator != (elem &) {

27

error("Error should overide operator \"!=\"!\n"); return 0; }

28protected :
29

void error (char * message){

30

cout<<message; cout<<"press any key to fin....\n";

31};
32
33//******************************clasa derivata student
34class student

getch(); exit(1); }

35{
36 int cod;
37 char nume[20];
38 char prenume[20];
39 int an;
40 char raion[30];
41 char obiect[15];
42
43 public:
44
45
46

student()
{ cod=0; strcpy(nume,""); strcpy(prenume,""); an=0;
strcpy(raion,""); strcpy(obiect,"");

47
48
49

void setCod(int i) { cod=i;}

50

void setNume(char* n) {

51

void setPrenume(char* a) { strcpy(prenume,a);}

52

void setAn(int i) {an=i; }

53

void setRaion(char* d) { strcpy(raion,d); }

54

void setObiect(char* di) { strcpy(obiect,di); }

55

int getCod() { return cod; }

56

char* getNume() { return nume; }

57

char* getPrenume() { return prenume;}

58

int getAn(){ return an; }

59

char* getRaion() { return raion; }

60

char* getObiect() { return obiect; }

61

int fscanf_el (FILE * f)

62
63
64

strcpy(nume,n); }

{return fscanf(f,"%i %s %s %i %s %s",&cod,nume,prenume,&an,raion,obiect);}


void virtual show( ) {
cout<<cod<<" "<<nume<<" "<<prenume<<" "<<an<<" "<<raion<<"

65"<<obiect<<endl; }
66

virtual int free() { return cod==0; }

67

int operator >(student &e2) {

68

return (this->cod>e2.cod);}

int operator < (student &e2) {

69

return (this->cod<e2.cod); }

70

int operator <= (student &e2) {

71

return (this->cod<=e2.cod); }

72

int operator >= (student &e2){

73

return (this->cod>=e2.cod); }

74

int operator == (student &e2) {

75

return (this->cod==e2.cod); }

76

int operator != (student &e2) {

77

return (this->cod!=e2.cod); }

78
79};

80template <class el> class tabel


81{ protected:
82 int n;
83 el t[200];
84public:
85

tabel() {n=0;}

86

tabel (char * file);

87

void search (int c) ;

88

void searchbin (int c);

89

void sort();

90
91

void show(const char *opening,const char *ending);

92
93protected:
94

void error(char *message){

95

cout<<message; cout<<"press any key to fin....\n";

96

getch(); exit(1); }

97};
98
99template
100<class el>
101tabel<el>::tabel (char * file) {
102

FILE *pf;

103

pf=fopen(file,"rt"); n=0;

104

while(!feof(pf))

105

if (t[n].fscanf_el(pf)>0)

106

n++;

107

fclose(pf); }

108template
109<class el>
110void tabel<el>::search (int c) { int position=-1,a,j;
111
112

for(int i=0;(position==-1)&&(i<n);i++)

113

{ a=t[i].getCod(); delay(100);

114

if(c==a )

115

{ position=i; t[i].show(); } }

116

if(position==-1 ) cout<<"\n Inexistent";

117

else{

118

cout<<"\nLungimea practica de cautare este:"<<position+1;

119

cout<<"\nLungimea teoretica de cautare este: "<<n/2; }

120

121
122template
123<class el>
124void tabel<el>::show(const char *opening,const char *ending)
125

{ cout<<opening;

126

for(int i=0;i<n;i++){

127

t[i].show();if(i%20==0&&i!=0){ cout<<"\nApasati o tasta pentru a continua inregistrarile";

128

getch();clrscr();}

129

cout<<ending;

130

cout<<"\n " ;} }

131
132template
133<class el>
134void tabel<el>::sort()
135{
136 int j,count=0;

137 el aux;
138 for(int i=0;i<n-1;i++)
139 for(j=i;j<n;j++)
140 {
141

if(t[i].getCod()>t[j].getCod())

142

143

aux=t[i];

144

t[i]=t[j];

145

t[j]=aux;

146

count++;

147

148 }
149 // cout<<"\n Sortarea a fost indeplinita cu succes!:"<<" "<<count<<"\n";
150 getch();
151}
152
153
154
155template
156<class el>
157void tabel<el>::searchbin(int c)
158{
159 if(!n)
160 {
161 cout<<"ERROR! Introduceti date.";
162 getch();

return; }

163 int s=0,f=n-1,count=1,j;


164 int m=(s+f)/2;
165
166 while((c!=t[m].getCod())&&(s<=f))
167 { count++; delay(100);
168 if(t[m].getCod()>c)
169
170 else

f=m-1;

171

s=m+1;}

172

m=(s+f)/2; }

173 if(s<=f)
174 {
175

t[m].show();

176

cout<<"\nLungimea practica de cautare este"<<" "<<count;

177

cout<<"\n Lungimea teoretica de cautare este: "<<log(n)/log(2);

178 }
179 else
180 {
181

cout<<"\n Inregistrare inexistenta!";

182 }
183
184}
185
186
187void main()
188{
189clrscr();
190time_t first, second;
191tabel <student> gr("d:\\caro\\f1-1.txt");
192char ch='n';
193int id,i;
194gr.show("\n Continutul fisierului \n"," ");
195while(ch!='y'){
196clrscr();
197cout<<"\n Introduceti codul pt cautare:\n ";
198cin>>id;
199first = time(NULL);
200delay(1000);
201gr.search(id);
202second = time(NULL);
203// printf("\nTimpul de executie este : %f secunde \n",difftime(second,first));
204gr.sort();

205//gr.show("\n Continutul fisierului sortat este \n"," ");


206first = time(NULL);
207delay(1000);
208gr.searchbin(id);
209second = time(NULL);
210//printf("\nTimpul de executie este : %f secunde \n",difftime(second,first));
211
212cout<<"\n Sfirsit? (Y/N) ;"; ch=getch(); cout<<endl; }
213}

Potrebbero piacerti anche