Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // esame 29-01-2019
- // file 290119.txt
- informatica fondamenti
- aiuola abcd
- qwertytrewq senzasignificato
- altra riga
- ultima riga
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- struct parola{
- char word[21];
- };
- struct parola *letturaFile(FILE *f,int *n);
- void stampaStructParola(struct parola *p,int n);
- int funzioneMassimoVocali(struct parola *p,int n);
- int funzionePalindromo(struct parola *p,int n);
- struct parola *funzioneFiltroCarattere(struct parola *p,int n);
- void stampaFunzioneFiltroCarattere(struct parola *p,int n);
- int funzioneDuplicati(struct parola *p,int n);
- void funzioneOrdinamento(struct parola *p,int n);
- int main( int argc,char *argv[]){
- int n,maxVocali,palindromo,duplicati;
- struct parola *filtroCarattere;
- FILE *f;
- struct parola *parola;
- if(argc!=2){
- return 1;
- }
- f=fopen(argv[1],"r");
- if(f==NULL){
- printf("file non trovato");
- return 1;
- }
- // funzioni lettura e stampa file
- parola=letturaFile(f,&n);
- fclose(f);
- stampaStructParola(parola,n);
- //richiesta 1
- maxVocali=funzioneMassimoVocali(parola,n);
- printf("[MAX-VOCALI]\n%d\n",maxVocali);
- // richiesta 2
- palindromo=funzionePalindromo(parola,n);
- printf("[PALINDROMI]\n%d\n",palindromo);
- // richiesta 3
- filtroCarattere=funzioneFiltroCarattere(parola,n);
- printf("[FILTRO]\n");
- stampaFunzioneFiltroCarattere(filtroCarattere,8);
- //richiesta 4
- duplicati=funzioneDuplicati(parola,n);
- if(duplicati>=1){
- printf("[DUPLICATI]\nSI\n");
- }else{
- printf("[DUPLICATI]\nNO\n");
- }
- //richiesta 5
- printf("[ORDINAMENTO]");
- funzioneOrdinamento(parola,n);
- return 0;
- }
- struct parola *letturaFile(FILE *f,int *n){
- int nConv;
- char buffer[100];
- int size=10;
- struct parola *p1,*p2,*p3;
- *n=0;
- p3=malloc(size*sizeof(struct parola));
- while(fgets(buffer,sizeof(buffer),f)){
- p1=p3 +*n;
- p2=p1 +1;
- nConv=sscanf(buffer,"%s %s",p1->word, p2->word);
- (*n)= (*n)+2;
- if(*n >= size){
- size=2*size;
- p3=realloc(p3,size*sizeof(struct parola));
- }
- }
- p3=realloc(p3,*n*sizeof(struct parola));
- return p3;
- }
- void stampaStructParola(struct parola *p,int n){
- int i;
- for(i=0;i<n;i++){
- printf("%s\n",p[i].word);
- }
- }
- int funzioneMassimoVocali(struct parola *p,int n){
- int i,j,count;
- int max=0;
- char *s;
- for(i=0;i<n;i++){
- s=p[i].word;
- count=0;
- for(j=0;j<21;j++){
- if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u') {
- count++;
- }
- }
- printf("count: %d, max: %d \n", count, max);
- if(count>max){
- max=count;
- }
- }
- return max;
- }
- int funzionePalindromo(struct parola *p,int n){
- int i,j,k; //k per ciclo esterno , i per cico interno e la j per leggere dalla fine
- int N;
- int count;
- int countP=0;
- char *s;
- for(k=0;k<n;k++){
- s=p[k].word;
- if(strlen(s)%2==0){ // stringa s pari
- N=strlen(s)/2;
- }else{
- N=(strlen(s)-1)/2;
- }
- j=strlen(s)-1;
- count=0;
- for(i=0;i<N;i++){
- if(s[i]==s[j]){
- count++;
- }
- j--;
- }
- if(count==N){
- countP++;
- }
- }
- return countP;
- }
- struct parola * funzioneFiltroCarattere(struct parola *p,int n){
- int i,j,k;
- char *s;
- char r[21];
- //struct parola v[8];
- struct parola *v;
- v = malloc(8*sizeof(struct parola));
- for(i=0;i<4;i++){
- strcpy(v[i].word, p[i].word);
- }
- for(j=n-5;j<n;j++){
- i++;
- strcpy(v[i].word,p[j].word);
- }
- for(i=0;i<8;i++){
- s=v[i].word;
- k=0;
- for(j=0;j<=strlen(s);j++){
- //printf("%c\n", s[j]);
- if( !(s[j]=='a' || s[j]=='b' || s[j]=='c' || s[j]=='d' || s[j]=='e')){
- r[k]=s[j];
- k++;
- }
- }
- strcpy(v[i].word,r);
- }
- return v;
- }
- void stampaFunzioneFiltroCarattere(struct parola *p,int n){
- int i;
- for(i=0;i<n;i++){
- if(strlen(p[i].word)!=0){
- printf("%s\n",p[i].word);
- }
- }
- }
- int funzioneDuplicati(struct parola *p,int n){
- int i,j,flag;
- int count=0;
- for(i=0;i<n;i++){
- for(j=0;j<n;j++){
- if(strcmp(p[i].word,p[j].word)==0){
- count++;
- }
- }
- if(count>1){
- flag=1;
- }
- }
- return flag;
- }
- void funzioneOrdinamento(struct parola *p,int n){
- int i,j;
- struct parola temp; // temporaneo=temp
- char *s1,*s2;
- for(i=0;i<n;i++){
- for(j=0;j<n;j++){
- s1 = p[i].word;
- s2 = p[j].word;
- if(strcmp(s1,s2)<0){
- temp=p[i];
- p[i]=p[j];
- p[j]=temp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement