Advertisement
Perlamado

Untitled

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