Advertisement
Unisa05121

Sistema Solare

Apr 14th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct stella *star;
  5. typedef struct pianeta *pl;
  6. typedef struct satellite *sat;
  7.  
  8. typedef struct stella{
  9.     char nome[25];
  10.     star next;
  11.     pl succ;
  12. } stella;
  13.  
  14. typedef struct pianeta{
  15.     char nome[25];
  16.     pl next;
  17.     sat succ;
  18. } pianeta;
  19.  
  20. typedef struct satellite{
  21.     char nome[25];
  22.     sat next;
  23. } satellite;
  24.  
  25. sat insSatellite(sat Z,char A[])
  26. {
  27.     satellite *n,*temp=Z;
  28.     n=malloc(sizeof(satellite));
  29.     if(n==NULL) return NULL;
  30.     strcpy(n->nome,A);
  31.     n->next=NULL;
  32.     if(Z==NULL) return n;
  33.     while(temp->next!=NULL) temp=temp->next;
  34.     temp->next=n;
  35.     return Z;
  36. }
  37.  
  38. sat leggiSatellite(pl n)
  39. {
  40.     char c[2],A[25];
  41.     sat Z=NULL;
  42.     while(1)
  43.     {
  44.         printf("\nInserisci il nome del satellite: ");
  45.         scanf("%s",A);
  46.         Z=insSatellite(Z,A);
  47.         printf("\nInserire altri satelliti? (y/n): ");
  48.         scanf("%s",c);
  49.         if(c[0]=='n') break;
  50.     }
  51.     return Z;
  52. }
  53.  
  54. pl insPianeta(pl P,char A[])
  55. {
  56.     char c[2];
  57.     pianeta *n,*temp=P;
  58.     n=malloc(sizeof(pianeta));
  59.     if(n==NULL) return NULL;
  60.     strcpy(n->nome,A);
  61.     n->next=NULL;
  62.     printf("\nVuoi inserire satelliti per questo pianeta?: (y/n) ");
  63.     scanf("%s",c);
  64.     if(c[0]=='n') n->succ=NULL;
  65.     else
  66.     {
  67.         n->succ=leggiSatellite(n);
  68.     }
  69.     if(P==NULL) return n;
  70.     while(temp->next!=NULL) temp=temp->next;
  71.     temp->next=n;
  72.     return P;
  73. }
  74.  
  75. pl leggiPianeta(star n)
  76. {
  77.     char c[2],A[25];
  78.     pl P=NULL;
  79.     while(1)
  80.     {
  81.         printf("\nInserisci il nome del pianeta: ");
  82.         scanf("%s",A);
  83.         P=insPianeta(P,A);
  84.         printf("\nInserire altri pianeti? (y/n): ");
  85.         scanf("%s",c);
  86.         if(c[0]=='n') break;
  87.     }
  88.     return P;
  89. }
  90.  
  91. star leggiStella(star S)
  92. {
  93.     char c[2];
  94.     stella *n;
  95.     n=malloc(sizeof(stella));
  96.     if(n==NULL) return S;
  97.     printf("\nInserisci il nome della stella: ");
  98.     scanf("%s",n->nome);
  99.     n->next=NULL;
  100.     printf("\nVuoi inserire pianeti per questa stella? (y/n): ");
  101.     scanf("%s",c); //getchar();
  102.     if(c[0]=='n')
  103.     {
  104.         n->succ=NULL;
  105.         return n;
  106.     }
  107.     n->succ=leggiPianeta(n);
  108.     return n;
  109. }
  110.  
  111. void stampaSistema(star S)
  112. {
  113.     printf("\n\n");
  114.     pl p;
  115.     sat q;
  116.     printf("%s\n",S->nome);
  117.     p=S->succ;
  118.     while(p!=NULL)
  119.     {
  120.         printf("\t%s\n",p->nome);
  121.         q=p->succ;
  122.         while(q!=NULL)
  123.         {
  124.             printf("\t\t%s\n",q->nome);
  125.             q=q->next;
  126.         }
  127.         p=p->next;
  128.     }
  129. }
  130.  
  131. int main(void)
  132. {
  133.     star S;
  134.     S=leggiStella(S);
  135.     stampaSistema(S);
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement