Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. /*Data L una lista di caratteri si realizzino in C:
  2. 1)Una funzione che popoli L con 60 caratteri casuali tra a e z (codice ascii a = 97 , z=122)[.
  3. 2)Una funzione che calcoli il carattere che appare con maggiore frequenza in L.
  4. 3)Una funzione che letto un carattere c da tastiera crei due nuove liste L1 ed L2 dove L1 contiene tutti i caratteri alfabeticamente minori o uguali a c
  5. ed L2 tutti quelli maggiori.
  6. Le tre funzioni devono essere invocate in sequenza dal main e deve essere stampato a schermo il contenuto delle liste al termine di ogni funzione.*/
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include <malloc.h>
  14.  
  15.  
  16. struct L{
  17. char c;
  18. struct L*next;
  19. };
  20.  
  21. struct L *newElem(char x){
  22. struct L* tmp=(struct L *)malloc(sizeof(struct L));
  23. tmp->c=x;
  24. tmp->next=NULL;
  25. return tmp;
  26. }
  27.  
  28.  
  29.  
  30. struct L *inserisciInCodaR(struct L *top, char x){
  31. struct L *e;
  32. if (top==NULL)
  33. top=newElem(x);
  34. else
  35. top->next=inserisciInCodaR(top->next,x);
  36.  
  37. return top;
  38. }
  39.  
  40. struct L *inserisciInOrdine(struct L *top,int k){
  41. struct L *e;
  42. if(top==NULL || top->c>k){
  43. e=newElem(k);
  44. e->next=top;
  45. top=e;
  46. }
  47. else{
  48. top->next=inserisciInOrdine(top->next,k);
  49.  
  50. }
  51. return top;
  52. }
  53.  
  54. struct L *caratteri(struct L *top){
  55. int i;
  56. char c;
  57. struct L *tmp=top;
  58. srand(time(0));
  59.  
  60. for(i=0;i<59;i++){
  61. c=95+rand()%27;
  62. printf("%c ",c);
  63. top=inserisciInCodaR(top,c);
  64. }
  65. printf("\n");
  66.  
  67. return top;
  68. }
  69.  
  70.  
  71.  
  72. char maggiore(struct L *top){
  73. struct L *tmp=top;
  74. struct L *tmp2=top;
  75. char max;
  76. int occ_max=INT_MIN;
  77. int contatore=0;
  78. while(tmp!=NULL){
  79. contatore=0;
  80. tmp2=top;
  81. while(tmp2!=NULL){
  82. if(tmp->c==tmp2->c){
  83. contatore++;
  84. }
  85. tmp2=tmp2->next;
  86. }
  87. if(contatore>occ_max){
  88. occ_max=contatore;
  89. max=tmp->c;
  90. }
  91. tmp=tmp->next;
  92. }
  93. return max;
  94. }
  95.  
  96.  
  97.  
  98. void punto3 (struct L *top, struct L *L1, struct L *L2, char v){
  99. struct L *tmp=top;
  100. int count;
  101. while(tmp!=NULL){
  102. if(tmp->c<='c'){
  103. L1=inserisciInCodaR(L1,tmp->c);
  104. }
  105. else{
  106. L2=inserisciInCodaR(L2,tmp->c);
  107. }
  108. tmp=tmp->next;
  109. }
  110. }
  111.  
  112.  
  113. int main(){
  114. int result=0;
  115. char c;
  116. char v;
  117. struct L *top=NULL;
  118. struct L *L1=NULL;
  119. struct L *L2=NULL;
  120. top=caratteri(top);
  121. c=maggiore(top);
  122. printf("La lettera che si ripete più volte e': %c",c);
  123. printf("inserisci il carattere v:\n");
  124. scanf("%c",&v);
  125. punto3(top,L1,L2,v);
  126.  
  127. printf("Stampo L1\n");
  128. stampaLista(L1);
  129.  
  130. printf("Stampo L2\n");
  131. stampaLista(L2);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement