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-7.00
/
parte2-4.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-01-29
|
2KB
|
132 lines
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
/* tipi */
typedef unsigned char bool;
typedef int elem;
typedef struct tsucc {
elem info[MAX];
int lg;
} succ;
/* prototipi */
succ empty();
void insert(elem,succ *);
void print(succ);
void order(succ,succ,succ*);
/* main */
int main()
{
succ s1 = empty(), s2 =empty(),so;
int m;
FILE *fp;
char nome_f[20];
printf("\nInserire il nome del file ");
scanf("%s",nome_f);
fp=fopen(nome_f,"r");
while (fscanf(fp,"%d",&m) && (m!=-1))
{
insert(m,&s1);
}
while (fscanf(fp,"%d",&m) && (m!=-1))
{
insert(m,&s2);
}
printf("\nLa prima successione e': ");
print(s1);
printf("\nLa seconda successione e': ");
print(s2);
order(s1,s2,&so);
printf("\nLa concatenazione ordinata e': ");
print(so);
}
succ empty()
{
succ s;
s.lg = 0;
return s;
}
void insert(elem el,succ *s)
{
if (s->lg == MAX)
{
printf("Errore: spazio insufficiente");
exit(EXIT_FAILURE);
}
s->info[s->lg] = el;
s->lg++;
}
void print(succ s)
{
int i;
printf("{ ");
for (i=0;i< s.lg;i++)
{
printf("%d ",s.info[i]);
}
printf("}\n");
}
void order(succ s1,succ s2, succ *so)
{
int i,j,imin,aux;
*so = empty();
/* copio s1 in so */
for (i=0;i<s1.lg;i++)
insert(s1.info[i],so);
/* copio s2 in so */
for (i=0;i<s2.lg;i++)
insert(s2.info[i],so);
/* ordino so usando Selection Sort*/
for (i=0;i<so->lg -1;i++)
{
imin =i;
for(j=i+1;j<so->lg;j++)
if (so->info[j] < so->info[imin]) imin=j;
aux = so->info[i];
so->info[i] = so->info[imin];
so->info[imin] = aux;
}
}