-
Notifications
You must be signed in to change notification settings - Fork 0
/
t1.c
143 lines (113 loc) · 2.26 KB
/
t1.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
char** aloca(char** vet, int tamanho_vetor_string){
int i;
vet = (char**)malloc(sizeof(char*)*tamanho_vetor_string);
for(i=0; i<1 ; i++)
vet[i] = (char*)malloc(sizeof(char)*50);
}
int contalinhas (FILE* arq, char* s, int linhas){
int i=0;
while (s[i] != '\0'){
if (s[i] == '\n'){
linhas++;
}
i++;
}
return linhas+1;
}
int contacolunas (FILE* arq, char* s, int colunas){
int i=0, maior=0;
while (s[i] != '\0'){
if (s[i] != '\n'){
colunas++;
} else if (s[i] == '\n'){
if (colunas > maior)
maior = colunas;
colunas = 0;
}
i++;
}
colunas = maior;
return colunas;
}
int comp (const void* string1, const void* string2){
if (string1 == NULL || string2 == NULL){
exit(1);//n pode dar return
}
return strcmp(string1, string2);
}
void fim(char** mat, int cont){
int i;
FILE* ed = fopen("saida.html", "w+");
fprintf (ed, "<body>");
for (i=0 ; i<cont ; i++){
fprintf (ed, "-> %s<br>", mat[i]);
}
fprintf (ed, "</body>");
}
void inicio(FILE* arq, char** mat, char ch, char* s, char* aux){
int i=0, j=0, cont=0;
printf ("Exibindo todos os candidatos:\n");
while( (ch=fgetc(arq))!= EOF){
*(s+i) = ch; //passa os nomes para string
*(aux+j) = ch; //salva linhas
if (ch == '\n'){
*(aux+j+1) = '\0';
if (aux[0] == 'R'){
cont++;
}
j=0;
} else {
j++;
}
i++;
putchar(ch);
}
*(s+i) = '\0'; //evitar problema com o ultmo nome
*(aux+j+1) = '\0';
if (aux[0] == 'R')
cont++;
mat = aloca(mat, cont+1); //aloca a matriz
i=0;
j=0;
cont=0;
while (*(s+i) == '\0'){
*(aux+j) = *(s+i);
if (*(s+i) = '\n'){
*(aux+j+1) = '\0';
if (aux[0] == 'R'){
strcpy (mat[cont], aux); //passa os nomes na string pra matriz
cont++;
}
j=0;
} else {
j++;
}
i++;
}
*(aux+j+1) = '\0';
if (aux[0] == 'R'){
strcpy (mat[cont], aux);//salva os clientes com R
cont++;
}
qsort (mat, (size_t)cont, sizeof(char*), comp);
fim(mat, cont);
}
int main(){
setlocale(LC_ALL, "Portuguese");
FILE *arq;
char** mat;
char ch;
char s[200], aux[50];
int linhas=0, colunas=0, i, j;
arq = fopen("Cliente.txt", "r");
if (arq==NULL){
printf ("Erro.\n");
}
inicio(arq, mat, ch, s, aux);
fclose(arq);
return 0;
}