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-6.99
/
turno1
/
parte4.c
< prev
Wrap
C/C++ Source or Header
|
1999-06-16
|
3KB
|
141 lines
/* Una possibile soluzione Esame di Laboratorio di Algoritmi 15 giugno 1999
* Turno 1 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
/* definizione dei tipi */
typedef struct stackel * stack; /* uno stack e' un puntatore */
typedef struct stackel{ int el; stack next; } stck;
/* operazioni sullo stack */
stack empty(void); /* restituisce uno stack vuoto, cioe' restituisce NULL */
stack push(int,stack); /* aggiunge in testa */
void print(stack); /* stampa lo stack */
stack makestackel(void); /* alloca una nuova cella */
void even_odd(stack s, stack * s1, stack * s2);
/* main */
main()
{
FILE * fp;
char nome[MAX];
int n;
stack s,s1=empty(),s2=empty();
/* creo stack vuoto */
s = empty();
/* leggo il nome del file */
printf("Fornisci il nome del file: ");
scanf("%s", nome);
/* apro il file in lettura */
fp = fopen(nome,"r");
/* carico lo stack con i dati letti da file
* (assumendo il file del formato corretto */
while (fscanf(fp,"%d",&n) && n != -1)
s = push(n,s);
/* stampo lo stack */
print(s);
/* creo i nuovi file */
even_odd(s,&s1,&s2);
/* stampo i nuovi file */
print(s1);
print(s2);
}
/* funzioni */
stack empty()
{
return NULL;
}
stack push(int el, stack s)
{
stack t;
t=makestackel();
t->el=el;
t->next=s;
return t;
}
stack makestackel(){ return (stack)malloc(sizeof(stck)); }
void print(stack s)
{
printf("\nLo stack ha il seguente contenuto: ");
for(; s !=NULL ; s = s -> next)
printf(" %d",s->el);
printf("\n\n");
}
void even_odd(stack s, stack * s1, stack * s2)
{
stack aux1=empty(),aux2=empty();
for (; s != empty(); s = s-> next)
{
if (s->el %2 == 0) /* elemento pari */
if (aux1 == empty()) /*primo elemento */
{
*s1 = s;
aux1 = s;
}
else /* elementi successivi */
{
aux1 -> next = s;
aux1 = s;
}
else /* elemento dispari */
if (aux2 == empty()) /*primo elemento */
{
*s2 = s;
aux2 = s;
}
else /* elementi successivi */
{
aux2 -> next = s;
aux2 = s;
}
}
if (aux1 != empty())
aux1->next = empty();
if (aux2 != empty())
aux2 -> next = empty();
}