home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
CataniaB
/
teach-act
/
testi-esami
/
labo-2.00
/
parte4.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-01-29
|
2KB
|
143 lines
/* Una possibile soluzione Esame di Laboratorio di Algoritmi
febbraio 2000 */
#include <stdio.h>
#include <stdlib.h>
#define B 20
#define MAX 20
/* fornisco l'implementazione piu' semplice, che non richiede
allocazione dinamica */
/* PROTOTIPI */
int h(int x);
int h1(int x, int n);
void empty(int * tab);
void insert(int n, int * tab);
void print(int * tab);
int member(int n,int *tab);
void my_union(int * tab1,int * tab2);
/* MAIN */
main() {
FILE * fp;
char nome[MAX];
int n;
int tab1[B], tab2[B] ;
/* svuoto le tabelle */
empty(tab1);
empty(tab2);
/* leggo il nome del file */
printf("Fornisci il nome del file: ");
scanf("%s", nome);
/* apro il file in lettura */
fp = fopen(nome,"r");
/* carico i dati nelle tabelle */
while (fscanf(fp,"%d",&n) && n != -2)
insert(n,tab1);
while (fscanf(fp,"%d",&n) && n != -2)
insert(n,tab2);
/* stampo le tabelle */
printf("\nPrima tabella: \n");
print(tab1);
printf("\nSeconda tabella: \n");
print(tab2);
/* calcolo union */
my_union(tab1,tab2);
/* stampo tabella unione */
printf("\nTabella Unione: \n");
print(tab1);
}
/* FUNZIONI */
int h(int x) {
return x % B;
}
int h1(int x, int n) {
return (h(x) + n) % B;
}
void empty(int * tab) {
int i;
for (i=0;i<B;i++)
tab[i] = 0;
}
int member (int n,int * tab) {
int b,m=0;
b = h1(n,m);
while (m<B && tab[b] != 0)
{if (tab[b] == n) return 1;
m++;
b = h1(n,m);
}
return 0;
}
void insert(int n,int * tab) {
int b,m=0;
if (!(member(n,tab)))
{
b = h1(n,m);
while (m<B && tab[b] != 0 && tab[b] != -1)
{
m++;
b = h1(n,m);
}
if ( m < B ) tab[b] = n;
}
}
void print(int * tab) {
int i;
for (i=0;i<B;i++)
{
if (tab[i] != 0 && tab[i] != -1)
printf("%d: %d\n",i,tab[i]);
else
printf("%d: \n",i);
}
}
void my_union(int * tab1,int * tab2)
{
int i;
for (i=0;i<B;i++)
insert(tab2[i],tab1);
}