Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1.

#include <stdio.h>

2.

#include <stdlib.h>

3.
4.

#define MAX_VEL 300

5.

#define PUTANJA "studenti.txt"

6.
7.

struct Student

8.

9.

char prezime[20];

10.

char ime[15];

11.

int broj_bodova;

12.

};

13.
14.

struct Student unos_studenta()

15.

16.

struct Student student;

17.
18.

printf("Unesite ime: ");

19.

scanf("%s", student.ime);

20.
21.

printf("Unesite prezime: ");

22.

scanf("%s", student.prezime);

23.
24.

printf("Unesite broj bodova: ");

25.

scanf("%d", &student.broj_bodova);

26.
27.

return student;

28.

29.
30.

void ispisi_studenta(struct Student student)

31.

32.

printf("Ime: %s\n", student.ime);

33.

printf("Prezime: %s\n", student.prezime);

34.

printf("Broj bodova: %d\n", student.broj_bodova);

35.

36.
37.

void ispisi_studente(struct Student* studenti, int vel)

38.

39.

int i;

40.
41.

for (i = 0; i < vel; i++)

42.

43.

ispisi_studenta(studenti[i]);

44.
45.

}
}

46.
47.

void snimi_studente(char putanja[], struct Student* studenti, in

t vel)
48.

49.

FILE* izlaz;

50.

int i;

51.
52.
53.

izlaz = fopen(putanja, "w");

54.

if (izlaz == NULL)

55.

56.

printf("Greska pri otvaranju datoteke %s!", putanja);

57.
58.

exit(1);

59.

60.
61.

for (i = 0; i < vel; i++)

62.

63.

fprintf(izlaz, "%-20s%-15s

%2d\n", studenti[i].prezime, studenti[i].ime, studenti[i].broj_bodova);


64.

65.
66.
67.

fclose(izlaz);
}

68.
69.

int ucitaj_studente(char putanja[], struct Student* studenti, int

max_vel)
70.

71.

FILE* ulaz;

72.

int vel = 0;

73.
74.

ulaz = fopen(putanja, "r");

75.
76.

if (ulaz == NULL)

77.

78.

return 0;

79.

80.
81.

while (fscanf(ulaz, "%20s%15s

%2d\n", studenti[vel].prezime, studenti[vel].ime, &studenti[vel].broj_b


odova) == 3 && vel < max_vel)
82.

83.

vel++;

84.

85.
86.
87.

return vel;
}

88.
89.

void sortiraj(struct Student* studenti, int vel)

90.

91.

int i, j;

92.
93.

for (i = 0; i < vel; i++)

94.

95.

for (j = i + 1; j < vel; j++)

96.

97.

if (studenti[j].broj_bodova > studenti[i].broj_bodova)

98.

99.

struct Student temp = studenti[i];

100.

studenti[i] = studenti[j];

101.

studenti[j] = temp;

102.
103.

}
}

104.
105.

}
}

106.
107.

int meni()

108.

109.

int izbor;

110.
111.

printf("Meni:\n");

112.

printf("\tDodavanje studenta

113.

printf("\tBrisanje studenta

- 2\n");

114.

printf("\tIzmjena studenta

- 3\n");

115.

printf("\tIspis sadrzaja datoteke - 4\n");

116.

printf("\tIzlaz

- 1\n");

- 5\n\n");

117.
118.

printf("Unesite izbor: ");

119.

scanf("%d", &izbor);

120.
121.
122.

return izbor;
}

123.
124.

int dodaj_studenta(struct Student* studenti, int vel, int max_vel

)
125.

126.

if (vel < max_vel)

127.

128.

studenti[vel] = unos_studenta();

129.

vel++;

130.

131.

else

132.

133.

printf("Zalimo, niz je popunjen.");

134.

135.
136.
137.

return vel;
}

138.
139.

int brisi_studenta(struct Student* studenti, int vel)

140.

141.

char prezime[20];

142.

char ime[15];

143.
144.

int i, j, pronadjen = 0;

145.
146.

printf("Unesite studenta kojeg zelite obrisati:\n");

147.

printf("Ime: ");

148.

scanf("%s", ime);

149.

printf("Prezime: ");

150.

scanf("%s", prezime);

151.
152.
153.

for (i = 0; i < vel; i++)

154.

155.
if ((strcmp(studenti[i].prezime, prezime) == 0) && (strcmp(studenti[i]
.ime, ime) == 0))
156.

157.

pronadjen = 1;

158.
159.

for (j = i; j < vel - 1; j++)

160.

161.

studenti[j] = studenti[j + 1];

162.

163.
164.

i--;

165.

vel--;

166.

167.

168.
169.

if (pronadjen == 0)

170.

171.

printf("Greska, ne postoji student sa unesenim imenom i

prezimenom u sistemu.\n");
172.

173.
174.
175.

return vel;
}

176.
177.

void prepravi_studenta(struct Student* studenti, int vel)

178.

179.

char prezime[20];

180.

char ime[15];

181.
182.

int i, j, pronadjen = 0;

183.
184.

printf("Unesite studenta kojeg zelite prepraviti:\n");

185.

printf("Ime: ");

186.

scanf("%s", ime);

187.

printf("Prezime: ");

188.

scanf("%s", prezime);

189.
190.
191.

for (i = 0; i < vel; i++)

192.

193.
if ((strcmp(studenti[i].prezime, prezime) == 0) && (strcmp(studenti[i]
.ime, ime) == 0))
194.

195.

studenti[i] = unos_studenta();

196.
197.

}
}

198.
199.

if (pronadjen == 0)

200.

201.

printf("Greska, ne postoji student sa unesenim imenom i

prezimenom u sistemu.\n");
202.

203.

204.
205.

int main()

206.

207.

struct Student studenti[MAX_VEL];

208.

int vel = ucitaj_studente(PUTANJA, studenti, MAX_VEL);

209.
210.

while (1)

211.

212.

switch (meni())

213.

214.

case 1:

215.

vel = dodaj_studenta(studenti, vel, MAX_VEL);

216.

snimi_studente(PUTANJA, studenti, vel);

217.

break;

218.
219.

case 2:

220.

vel = brisi_studenta(studenti, vel);

221.

snimi_studente(PUTANJA, studenti, vel);

222.

break;

223.
224.

case 3:

225.

prepravi_studenta(studenti, vel);

226.

snimi_studente(PUTANJA, studenti, vel);

227.

break;

228.
229.

case 4:

230.

ispisi_studente(studenti, vel);

231.

break;

232.
233.

case 5:

234.

return 0;

235.

236.

237.
238.
239.

return 0;
}

You might also like