-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
329 lines (301 loc) · 6.92 KB
/
functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "sis.h"
#include "time.h"
#include "write.h"
#include "busca.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"
#include "stdlib.h"
#include "functions.h"
#define NM 25
/*
* veja mais em
* https://github.com/rafatrinity
*/
//=================================estruturas==============================
typedef struct data{
int dia;
int mes;
int ano;
} data;
typedef struct aluno{
char nome[NM];
int matricula;
float cr;
data niver;
} aluno;
aluno al;
//===================================ADD======================================
void ADD(){
FILE *fp;
char op;
int mat=0;
float cr;
//===========================gerador de matriculas============================
do{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
fp = fopen("cadastro.bin", "rb");
if (fp==NULL)
escreva(1);
while(fread(&al, sizeof(aluno),1,fp)==1){
if (mat+((tm.tm_year+1900)*1000)<=al.matricula)
mat++;
}
fclose(fp);
//============================================================================
fp = fopen("cadastro.bin", "ab");
if (fp==NULL)
escreva(1);
cab();
getchar();
printf("DIGITE O NOME DO ALUNO: ");
fgets(al.nome, NM, stdin);
for (int i = 0; i < strlen(al.nome); i++)
al.nome[i]=tolower(al.nome[i]);
al.nome[0]=toupper(al.nome[0]);
for (int i = 0; i < strlen(al.nome); i++){
if (al.nome[i]==' ')
al.nome[i+1]=toupper(al.nome[i+1]);
}
printf("\nDIGITE A DATA DE NASCIMENTO DO ALUNO: ");
fflush(stdin);
scanf("%d %d %d",&al.niver.dia,&al.niver.mes,&al.niver.ano);
pcr:
printf("\nDIGITE CR DO ALUNO: ");
fflush(stdin);
scanf("%f",&cr);
if((cr>=0)&&(cr<=10))
al.cr=cr;
else{
escreva(7);
goto pcr;
}
al.matricula = (2017000+mat);
fwrite(&al,sizeof(aluno),1,fp);
getchar();
printf("\ndeseja add outro aluno?(s/n)\n");
scanf("%c",&op);
fclose(fp);
} while (op=='s');
}
//================================exibir======================================
void exibir(){
FILE *fp;
fp = fopen("cadastro.bin", "rb");
if (fp==NULL)
escreva(1);
cab();
while(fread(&al, sizeof(aluno),1,fp)==1){
printf("Nome: %s",al.nome);
printf("MATRICULA: %d\n",al.matricula);
printf("CR: %.2f\n",al.cr);
printf("Data de nasc: %2.d/%2.d/%4.d\n",al.niver.dia,al.niver.mes,al.niver.ano);
linha();
}
getchar();
fclose(fp);
getchar();
}
//===================================pesquisar================================
void PesquisarNome(){
FILE *fp;
char nome[NM];
char*pn=nome;
char*pa=al.nome;
int tot =0;
fp = fopen("cadastro.bin", "rb");
if (fp==NULL)
escreva(1);
cab();
getchar();
printf("Digite o nome a ser pesquisado: ");
fgets(nome, NM, stdin);
for (int i = 0; i < strlen(nome); i++)
nome[i]=tolower(nome[i]);
nome[0]=toupper(nome[0]);
for (int i = 0; i < strlen(nome); i++){
if (nome[i]==' ')
nome[i+1]=toupper(nome[i+1]);
}
while(fread(&al, sizeof(aluno),1,fp)==1){
if (kmp(strlen(al.nome), strlen(nome), pa, pn)==1){
printf("Nome: %s",al.nome);
printf("Matricula: %d\n",al.matricula);
printf("CR: %.2f\n",al.cr);
printf("Data de nasc: %2.d/%2.d/%4.d\n",al.niver.dia,al.niver.mes,al.niver.ano);
linha();
tot++;
}
}
if (tot==0)
escreva(4);
fclose(fp);
getchar();
}
//============================================================================
void PesquisarMatricula(){
FILE *fp;
int tot =0, mat;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
fp = fopen("cadastro.bin", "rb");
if (fp==NULL)
escreva(1);
cab();
getchar();
printf("Digite a matricula do aluno: ");
scanf("%d",&mat);
int val = mat-((tm.tm_year+1900)*1000);
while(val<0)
val+=1000;
fseek (fp ,sizeof(aluno)*val, SEEK_SET);
if(fread(&al, sizeof(aluno),1,fp)==1){
if (mat==al.matricula){
printf("Nome: %s",al.nome);
printf("Matricula: %d\n",al.matricula);
printf("CR: %.2f\n",al.cr);
printf("Data de nasc: %2.d/%2.d/%4.d\n",al.niver.dia,al.niver.mes,al.niver.ano);
linha();
tot++;
}
}
if (tot==0)
escreva(4);
getchar();
fclose(fp);
getchar();
}
//============================================================================
void PesquisarPosicao(){
FILE *fp;
int tot=0, val;
fp = fopen("cadastro.bin", "rb");
if (fp==NULL)
escreva(1);
cab();
printf("Digite a posição a ser pesquisada: ");
scanf("%d",&val);
fseek (fp ,sizeof(aluno)*val, SEEK_SET);
if(fread(&al, sizeof(aluno),1,fp)==1){
printf("Nome: %s",al.nome);
printf("MATRICULA: %d\n",al.matricula);
printf("CR: %.2f\n",al.cr);
printf("Data de nasc: %2.d/%2.d/%4.d\n",al.niver.dia,al.niver.mes,al.niver.ano);
linha();
tot++;
}
if (tot==0)
escreva(4);
getchar();
fclose(fp);
getchar();
}
//==================pesquisar por aniversariantes do mes======================
void PesquisarNiv(){
time_t t = time(NULL);
struct tm tm = *localtime(&t);
FILE *fp;
int tot=0;
fp = fopen("cadastro.bin", "rb");
if (fp==NULL)
escreva(1);
cab();
while(fread(&al, sizeof(aluno),1,fp)==1){
if (al.niver.mes==tm.tm_mon+1){
printf("Nome: %s",al.nome);
printf("Matricula: %d\n",al.matricula);
printf("CR: %.2f\n",al.cr);
printf("Data de nasc: %2.d/%2.d/%4.d\n",al.niver.dia,al.niver.mes,al.niver.ano);
linha();
tot++;
}
}
if (tot==0)
escreva(6);
getchar();
fclose(fp);
getchar();
}
//=================================remover====================================
int deleteRecordByName(char *fname){
FILE *fp;
FILE *fp_tmp;
int found=0;
char nome[NM];
fp=fopen(fname, "rb");
if (!fp) {
printf("Unable to open file %s", fname);
return -1;
}
fp_tmp=fopen("tmp.bin", "wb");
if (!fp_tmp) {
printf("Unable to open file temp file.");
return -1;
}
cab();
getchar();
printf("Digite o nome a ser removido: ");
fgets(nome, NM, stdin);
for (int i = 0; i < strlen(nome); i++)
nome[i]=tolower(nome[i]);
nome[0]=toupper(nome[0]);
for (int i = 0; i < strlen(nome); i++){
if (nome[i]==' ')
nome[i+1]=toupper(nome[i+1]);
}
while (fread(&al,sizeof(aluno),1,fp) == 1) {
if (strcmp (nome, al.nome) == 0) {
printf("%s foi removido(a) com sucesso!\n", nome);
found=1;
}
else
fwrite(&al, sizeof(aluno), 1, fp_tmp);
}
if (!found)
escreva(4);
fclose(fp);
fclose(fp_tmp);
remove(fname);
rename("tmp.bin", fname);
getchar();
return 0;
}
//==============================deletar pela matricula========================
int deleteRecordByMatricula(char *fname){
FILE *fp;
FILE *fp_tmp;
int found=0, mat;
fp=fopen(fname, "rb");
if (!fp) {
printf("Unable to open file %s", fname);
return -1;
}
fp_tmp=fopen("tmp.bin", "wb");
if (!fp_tmp) {
printf("Unable to open file temp file.");
return -1;
}
cab();
getchar();
printf("Digite a matricula a ser removida: ");
scanf("%d",&mat);
while (fread(&al,sizeof(aluno),1,fp) == 1) {
if (mat==al.matricula) {
linha();
printf("\n\t%s foi removido(a) com sucesso!\n", al.nome);
found=1;
}
else
fwrite(&al, sizeof(aluno), 1, fp_tmp);
}
if (!found)
escreva(4);
fclose(fp);
fclose(fp_tmp);
remove(fname);
rename("tmp.bin", fname);
getchar();
getchar();
return 0;
}