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-3.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-01-29
|
1KB
|
95 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);
bool is_pal(succ s);
/* main */
int main()
{
succ s = empty();
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,&s);
}
printf("La successione e': ");
print(s);
if (is_pal(s)) printf("\nLa successione e' palindroma");
else printf("\nLa successione non e' palindroma");
}
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");
}
bool is_pal(succ s)
{
int i;
for (i=0;i< s.lg/2;i++)
if (s.info[i] != s.info[s.lg -i-1])
return 0;
return 1;
}