Advertisement
Perlamado

Untitled

Feb 24th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. // esame 29-01-2019
  2. // file 290119.txt
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <math.h>
  7.  
  8. struct parola{
  9. char word[21];
  10. };
  11. struct parola *letturaFile(FILE *f,int *n);
  12. void stampaStructParola(struct parola *p,int n);
  13. int funzioneMassimoVocali(struct parola *p,int n);
  14. int funzionePalindromo(struct parola *p,int n);
  15. struct parola *funzioneFiltroCarattere(struct parola *p,int n);
  16. void stampaFunzioneFiltroCarattere(struct parola *p,int n);
  17. int funzioneDuplicati(struct parola *p,int n);
  18. void funzioneOrdinamento(struct parola *p,int n);
  19.  
  20. int main( int argc,char *argv[]){
  21.  
  22. int n,maxVocali,palindromo,duplicati;
  23. struct parola *filtroCarattere;
  24. FILE *f;
  25. struct parola *parola;
  26.  
  27. if(argc!=2){
  28. return 1;
  29. }
  30. f=fopen(argv[1],"r");
  31. if(f==NULL){
  32. printf("file non trovato");
  33. return 1;
  34. }
  35.  
  36. // funzioni lettura e stampa file
  37. parola=letturaFile(f,&n);
  38. fclose(f);
  39. stampaStructParola(parola,n);
  40. //richiesta 1
  41. maxVocali=funzioneMassimoVocali(parola,n);
  42. printf("[MAX-VOCALI]\n%d\n",maxVocali);
  43. // richiesta 2
  44. palindromo=funzionePalindromo(parola,n);
  45. printf("[PALINDROMI]\n%d\n",palindromo);
  46. // richiesta 3
  47. filtroCarattere=funzioneFiltroCarattere(parola,n);
  48. printf("[FILTRO]\n");
  49. stampaFunzioneFiltroCarattere(filtroCarattere,8);
  50. //richiesta 4
  51. duplicati=funzioneDuplicati(parola,n);
  52. if(duplicati>=1){
  53. printf("[DUPLICATI]\nSI\n");
  54. }else{
  55. printf("[DUPLICATI]\nNO\n");
  56. }
  57. //richiesta 5
  58. printf("[ORDINAMENTO]");
  59. funzioneOrdinamento(parola,n);
  60. return 0;
  61. }
  62.  
  63. struct parola *letturaFile(FILE *f,int *n){
  64. int nConv;
  65. char buffer[100];
  66. int size=10;
  67. struct parola *p1,*p2,*p3;
  68. *n=0;
  69. p3=malloc(size*sizeof(struct parola));
  70. while(fgets(buffer,sizeof(buffer),f)){
  71.  
  72. p1=p3 +*n;
  73. p2=p1 +1;
  74. nConv=sscanf(buffer,"%s %s",p1->word, p2->word);
  75. (*n)= (*n)+2;
  76. if(*n >= size){
  77. size=2*size;
  78. p3=realloc(p3,size*sizeof(struct parola));
  79. }
  80. }
  81. p3=realloc(p3,*n*sizeof(struct parola));
  82. return p3;
  83. }
  84. void stampaStructParola(struct parola *p,int n){
  85. int i;
  86. for(i=0;i<n;i++){
  87. printf("%s\n",p[i].word);
  88. }
  89. }
  90. int funzioneMassimoVocali(struct parola *p,int n){
  91. int i,j,count;
  92. int max=0;
  93. char *s;
  94.  
  95. for(i=0;i<n;i++){
  96. s=p[i].word;
  97. count=0;
  98.  
  99. for(j=0;j<21;j++){
  100. if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u') {
  101. count++;
  102. }
  103. }
  104. printf("count: %d, max: %d \n", count, max);
  105. if(count>max){
  106. max=count;
  107. }
  108. }
  109. return max;
  110. }
  111. int funzionePalindromo(struct parola *p,int n){
  112. int i,j,k; //k per ciclo esterno , i per cico interno e la j per leggere dalla fine
  113. int N;
  114. int count;
  115. int countP=0;
  116. char *s;
  117. for(k=0;k<n;k++){
  118. s=p[k].word;
  119. if(strlen(s)%2==0){ // stringa s pari
  120. N=strlen(s)/2;
  121. }else{
  122. N=(strlen(s)-1)/2;
  123. }
  124. j=strlen(s)-1;
  125. count=0;
  126. for(i=0;i<N;i++){
  127. if(s[i]==s[j]){
  128. count++;
  129. }
  130. j--;
  131. }
  132. if(count==N){
  133. countP++;
  134. }
  135. }
  136. return countP;
  137. }
  138. struct parola * funzioneFiltroCarattere(struct parola *p,int n){
  139. int i,j,k;
  140. char *s;
  141. char r[21];
  142. //struct parola v[8];
  143. struct parola *v;
  144. v = malloc(8*sizeof(struct parola));
  145. for(i=0;i<4;i++){
  146. strcpy(v[i].word, p[i].word);
  147. }
  148. for(j=n-5;j<n;j++){
  149. i++;
  150. strcpy(v[i].word,p[j].word);
  151. }
  152. for(i=0;i<8;i++){
  153. s=v[i].word;
  154. k=0;
  155. for(j=0;j<=strlen(s);j++){
  156. //printf("%c\n", s[j]);
  157. if( !(s[j]=='a' || s[j]=='b' || s[j]=='c' || s[j]=='d' || s[j]=='e')){
  158.  
  159. r[k]=s[j];
  160. k++;
  161. }
  162. }
  163.  
  164. strcpy(v[i].word,r);
  165. }
  166. return v;
  167. }
  168. void stampaFunzioneFiltroCarattere(struct parola *p,int n){
  169. int i;
  170. for(i=0;i<n;i++){
  171. if(strlen(p[i].word)!=0){
  172. printf("%s\n",p[i].word);
  173. }
  174. }
  175. }
  176. int funzioneDuplicati(struct parola *p,int n){
  177. int i,j,flag;
  178. int count=0;
  179. for(i=0;i<n;i++){
  180. for(j=0;j<n;j++){
  181. if(strcmp(p[i].word,p[j].word)==0){
  182. count++;
  183. }
  184. }
  185. if(count>1){
  186. flag=1;
  187. }
  188. }
  189. return flag;
  190. }
  191. void funzioneOrdinamento(struct parola *p,int n){
  192.  
  193. int i,j;
  194. struct parola temp; // temporaneo=temp
  195. char *s1,*s2;
  196. for(i=0;i<n;i++){
  197. for(j=0;j<n;j++){
  198. s1 = p[i].word;
  199. s2 = p[j].word;
  200. if(strcmp(s1,s2)<0){
  201. temp=p[i];
  202. p[i]=p[j];
  203. p[j]=temp;
  204. }
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement