Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prog-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pedro Folloni Pesserl
prog-1
Commits
6caa27ce
Commit
6caa27ce
authored
2 years ago
by
Pedro Folloni Pesserl
Browse files
Options
Downloads
Patches
Plain Diff
lib is functional
parent
3150eee6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
tp4/lib_conjunto.c
+148
-38
148 additions, 38 deletions
tp4/lib_conjunto.c
tp4/makefile
+1
-1
1 addition, 1 deletion
tp4/makefile
tp4/testa_conjunto
+0
-0
0 additions, 0 deletions
tp4/testa_conjunto
tp4/testa_conjunto.c
+2
-0
2 additions, 0 deletions
tp4/testa_conjunto.c
with
151 additions
and
39 deletions
tp4/lib_conjunto.c
+
148
−
38
View file @
6caa27ce
#include
<stdio.h>
#include
<stdio.h>
#include
<std
io
.h>
#include
<std
lib
.h>
#include
"lib_conjunto.h"
#include
"lib_conjunto.h"
/* retorna a posicao de um elemento em um conjunto.
/* retorna a posicao de um elemento em um conjunto
([0..card-1])
.
* como um conjunto nao necessariamente eh ordenado, eh uma busca linear.
* como um conjunto nao necessariamente eh ordenado, eh uma busca linear.
* se o elemento nao esta no conjunto, retorna -1. */
* se o elemento nao esta no conjunto, retorna -1. */
int
busca_cjt
(
conjunto_t
*
c
,
int
elemento
)
{
int
busca_cjt
(
conjunto_t
*
c
,
int
elemento
)
{
int
pos
=
-
1
;
int
pos
=
0
;
int
i
;
while
(
pos
<
c
->
card
&&
c
->
v
[
pos
]
!=
elemento
)
for
(
i
=
0
;
i
<
c
->
card
;
i
++
)
{
pos
++
;
if
(
c
->
v
[
i
]
==
elemento
)
{
pos
=
i
;
return
pos
>=
c
->
card
?
-
1
:
pos
;
break
;
}
}
return
pos
;
}
}
conjunto_t
*
cria_cjt
(
int
max
)
{
conjunto_t
*
cria_cjt
(
int
max
)
{
conjunto_t
*
c
;
conjunto_t
*
c
;
if
(
!
(
c
=
malloc
(
sizeof
(
conjunto_t
)))
)
return
NULL
;
c
->
max
=
max
;
c
->
max
=
max
;
c
->
card
=
0
;
c
->
card
=
0
;
c
->
ptr
;
c
->
ptr
=
0
;
if
(
!
(
c
->
v
=
malloc
(
sizeof
(
int
)
*
max
))
)
if
(
!
(
c
->
v
=
malloc
(
sizeof
(
int
)
*
max
))
)
return
NULL
;
return
NULL
;
return
c
;
return
c
;
...
@@ -31,6 +29,7 @@ conjunto_t *destroi_cjt(conjunto_t *c) {
...
@@ -31,6 +29,7 @@ conjunto_t *destroi_cjt(conjunto_t *c) {
c
->
card
=
0
;
c
->
card
=
0
;
free
(
c
->
v
);
free
(
c
->
v
);
c
->
v
=
NULL
;
c
->
v
=
NULL
;
free
(
c
);
return
NULL
;
return
NULL
;
}
}
...
@@ -49,7 +48,7 @@ int insere_cjt(conjunto_t *c, int elemento) {
...
@@ -49,7 +48,7 @@ int insere_cjt(conjunto_t *c, int elemento) {
if
(
pertence_cjt
(
c
,
elemento
))
if
(
pertence_cjt
(
c
,
elemento
))
return
1
;
return
1
;
c
->
v
[
card
]
=
elemento
;
c
->
v
[
c
->
card
]
=
elemento
;
c
->
card
++
;
c
->
card
++
;
return
1
;
return
1
;
}
}
...
@@ -85,49 +84,160 @@ int sao_iguais_cjt(conjunto_t *c1, conjunto_t *c2) {
...
@@ -85,49 +84,160 @@ int sao_iguais_cjt(conjunto_t *c1, conjunto_t *c2) {
return
c1
->
card
==
c2
->
card
&&
contido_cjt
(
c1
,
c2
);
return
c1
->
card
==
c2
->
card
&&
contido_cjt
(
c1
,
c2
);
}
}
/* conjunto_t *diferenca_cjt(conjunto_t *c1, conjunto_t *c2) { */
conjunto_t
*
diferenca_cjt
(
conjunto_t
*
c1
,
conjunto_t
*
c2
)
{
conjunto_t
*
dif
;
if
(
!
(
dif
=
cria_cjt
(
c1
->
max
))
)
return
NULL
;
/* } */
int
i
;
for
(
i
=
0
;
i
<
c1
->
card
;
i
++
)
{
if
(
!
(
pertence_cjt
(
c2
,
c1
->
v
[
i
])))
insere_cjt
(
dif
,
c1
->
v
[
i
]);
}
/* conjunto_t *interseccao_cjt(conjunto_t *c1, conjunto_t *c2) { */
return
dif
;
}
/* } */
conjunto_t
*
interseccao_cjt
(
conjunto_t
*
c1
,
conjunto_t
*
c2
)
{
conjunto_t
*
inter
;
if
(
!
(
inter
=
cria_cjt
(
c1
->
max
))
)
return
NULL
;
/* conjunto_t *uniao_cjt(conjunto_t *c1, conjunto_t *c2) { */
int
i
;
for
(
i
=
0
;
i
<
c1
->
card
;
i
++
)
{
if
(
pertence_cjt
(
c2
,
c1
->
v
[
i
]))
insere_cjt
(
inter
,
c1
->
v
[
i
]);
}
return
inter
;
}
conjunto_t
*
uniao_cjt
(
conjunto_t
*
c1
,
conjunto_t
*
c2
)
{
conjunto_t
*
uniao
;
if
(
!
(
uniao
=
cria_cjt
(
c1
->
max
))
)
return
NULL
;
/* para nao ter que alocar um conjunto novo, a funcao usa ponteiros para
* ponteiro de conjunto */
conjunto_t
**
menor_cjt
=
c1
->
card
<
c2
->
card
?
&
c1
:
&
c2
;
conjunto_t
**
maior_cjt
=
c1
->
card
>
c2
->
card
?
&
c1
:
&
c2
;
/* } */
int
i
;
for
(
i
=
0
;
i
<
(
*
menor_cjt
)
->
card
;
i
++
)
{
insere_cjt
(
uniao
,
c1
->
v
[
i
]);
insere_cjt
(
uniao
,
c2
->
v
[
i
]);
}
for
(;
i
<
(
*
maior_cjt
)
->
card
;
i
++
)
insere_cjt
(
uniao
,
(
*
maior_cjt
)
->
v
[
i
]);
return
uniao
;
}
conjunto_t
*
copia_cjt
(
conjunto_t
*
c
)
{
conjunto_t
*
copia_cjt
(
conjunto_t
*
c
)
{
conjunto_t
*
copia
;
conjunto_t
*
copia
;
if
(
!
(
copia
=
cria_cjt
(
c
->
max
))
)
if
(
!
(
copia
=
cria_cjt
(
c
->
max
))
)
return
NULL
;
return
NULL
;
copia
->
card
=
c
->
card
;
int
i
;
int
i
;
for
(
i
=
0
;
i
<
c
opia
->
card
;
i
++
)
{
for
(
i
=
0
;
i
<
c
->
card
;
i
++
)
{
copia
->
v
[
i
]
=
c
->
v
[
i
];
insere_cjt
(
copia
,
c
->
v
[
i
]
)
;
}
}
return
copia
;
return
copia
;
}
}
/* conjunto_t *cria_subcjt_cjt(conjunto_t *c, int n) { */
conjunto_t
*
cria_subcjt_cjt
(
conjunto_t
*
c
,
int
n
)
{
if
(
n
>=
c
->
card
)
return
copia_cjt
(
c
);
/* } */
conjunto_t
*
sub
;
if
(
!
(
sub
=
cria_cjt
(
c
->
max
))
)
return
NULL
;
/* void imprime_cjt(conjunto_t *c) { */
conjunto_t
*
disponiveis
;
if
(
!
(
disponiveis
=
copia_cjt
(
c
))
)
return
NULL
;
/* } */
int
i
;
int
aleat
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
aleat
=
rand
()
%
disponiveis
->
card
;
insere_cjt
(
sub
,
disponiveis
->
v
[
aleat
]);
retira_cjt
(
disponiveis
,
disponiveis
->
v
[
aleat
]);
}
/* void inicia_iterador_cjt(conjunto_t *c) { */
destroi_cjt
(
disponiveis
);
return
sub
;
}
/* } */
/* funcao auxiliar para a ordenacao */
void
troca
(
int
*
a
,
int
*
b
)
{
int
temp
=
*
a
;
*
a
=
*
b
;
*
b
=
temp
;
}
/* int incrementa_iterador_cjt(conjunto_t *c, int *ret_iterador) { */
/* implementacao do bubble sort */
conjunto_t
*
ordena_cjt
(
conjunto_t
*
c
)
{
conjunto_t
*
ord
;
if
(
!
(
ord
=
copia_cjt
(
c
))
)
return
NULL
;
/* } */
int
tam
=
ord
->
card
;
int
trocou
=
1
;
int
i
;
while
(
trocou
)
{
/* trocou eh uma flag para sair do loop quando ja */
trocou
=
0
;
/* estiver ordenado (nao trocou = ordenado) */
for
(
i
=
0
;
i
<
tam
-
1
;
i
++
)
{
if
(
ord
->
v
[
i
+
1
]
<
ord
->
v
[
i
])
{
troca
(
&
ord
->
v
[
i
+
1
],
&
ord
->
v
[
i
]);
trocou
=
1
;
}
}
tam
--
;
}
return
ord
;
}
/* int retira_um_elemento_cjt(conjunto_t *c) { */
void
imprime_cjt
(
conjunto_t
*
c
)
{
if
(
vazio_cjt
(
c
))
{
printf
(
"Conjunto vazio.
\n
"
);
return
;
}
conjunto_t
*
ord
;
if
(
!
(
ord
=
ordena_cjt
(
c
))
)
{
printf
(
"Erro na alocacao do conjunto."
);
return
;
}
/* } */
int
i
;
for
(
i
=
0
;
i
<
ord
->
card
-
1
;
i
++
)
printf
(
"%d "
,
ord
->
v
[
i
]);
printf
(
"%d
\n
"
,
ord
->
v
[
ord
->
card
-
1
]);
destroi_cjt
(
ord
);
}
void
inicia_iterador_cjt
(
conjunto_t
*
c
)
{
c
->
ptr
=
0
;
}
int
incrementa_iterador_cjt
(
conjunto_t
*
c
,
int
*
ret_iterador
)
{
*
ret_iterador
=
c
->
v
[
c
->
ptr
];
c
->
ptr
++
;
if
(
c
->
ptr
>
c
->
card
)
return
0
;
return
1
;
}
int
retira_um_elemento_cjt
(
conjunto_t
*
c
)
{
int
aleat
=
rand
()
%
c
->
card
;
int
retirar
=
c
->
v
[
aleat
];
int
i
;
for
(
i
=
aleat
;
i
<
c
->
card
-
1
;
i
++
)
c
->
v
[
i
]
=
c
->
v
[
i
+
1
];
c
->
card
--
;
return
retirar
;
}
This diff is collapsed.
Click to expand it.
tp4/makefile
+
1
−
1
View file @
6caa27ce
...
@@ -15,7 +15,7 @@ lib_conjunto.o: lib_conjunto.c
...
@@ -15,7 +15,7 @@ lib_conjunto.o: lib_conjunto.c
$(
CC
)
-c
$(
CFLAGS
)
lib_conjunto.c
$(
CC
)
-c
$(
CFLAGS
)
lib_conjunto.c
tp4.o
:
tp4.c
tp4.o
:
tp4.c
$(
CC
)
-c
$(
CFLAGS
)
t
esta_conjunto
.c
$(
CC
)
-c
$(
CFLAGS
)
t
p4
.c
clean
:
clean
:
rm
-f
$(
objects
)
rm
-f
$(
objects
)
...
...
This diff is collapsed.
Click to expand it.
tp4/testa_conjunto
0 → 100755
+
0
−
0
View file @
6caa27ce
File added
This diff is collapsed.
Click to expand it.
tp4/testa_conjunto.c
+
2
−
0
View file @
6caa27ce
...
@@ -7,10 +7,12 @@
...
@@ -7,10 +7,12 @@
#include
<stdio.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<time.h>
#include
"lib_conjunto.h"
#include
"lib_conjunto.h"
#define TAM 21
#define TAM 21
int
main
(
void
)
{
int
main
(
void
)
{
srand
(
time
(
0
));
conjunto_t
*
a
,
*
b
,
*
u
,
*
i
,
*
d
,
*
d1
,
*
copia
,
*
sub
;
conjunto_t
*
a
,
*
b
,
*
u
,
*
i
,
*
d
,
*
d1
,
*
copia
,
*
sub
;
int
k
,
ex
;
int
k
,
ex
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment