hamaXD

CPT 7.2

Nov 8th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. struct song{
  5.   char title[128];
  6.   double duration;
  7. };
  8. typedef struct song Song;
  9.  
  10. struct listnode{
  11.   Song *s;
  12.   struct listnode *next;
  13. };
  14. typedef struct listnode LN;
  15.  
  16. Song *input_song();
  17. void insert(LN **hptr, Song *sp);
  18. LN *find_tail(LN *head);
  19. double sum_duration(LN *head);
  20.  
  21. int main(){
  22. printf("main\n");
  23.     LN *head;
  24. head=NULL;
  25.   Song *sp;
  26.   int i;
  27.   for(i = 0; i < 5; i++){
  28.     sp = input_song();  //malloc, scanf Song in this function
  29.     insert(&head,sp);
  30.   }
  31.  
  32.     printf("=%.2f",sum_duration(head));
  33.   return 0;
  34. }
  35.  
  36.  
  37. Song *input_song(){
  38.     printf("input\n");
  39.     Song temp,*sp;
  40.     sp=(Song*)malloc(sizeof(Song));
  41.  
  42.     printf("input title\n");
  43.     scanf("%s",temp.title);
  44.     printf("title ;%s input duration\n",temp.title);
  45.     scanf("%lf",&temp.duration);
  46.     printf("duration : %lf send insert\n",temp.duration);
  47.    
  48.     *sp = temp;
  49.    
  50.     return sp;
  51.    
  52.    
  53.    
  54. }
  55.  
  56. void insert(LN **hptr, Song *sp){
  57.     printf("insert\n");
  58.     LN *new_node = (LN*)malloc(sizeof(LN));
  59.     LN *tail;
  60.     new_node->s=sp;
  61.     new_node->next=NULL;
  62.     tail=find_tail(*hptr);
  63.     if(tail==NULL)
  64.         *hptr=new_node;
  65.     else
  66.         tail->next=new_node;
  67. }
  68.  
  69.  
  70. LN *find_tail(LN *head){
  71.     printf("find_tail\n");
  72.     LN *tail;
  73.     if(head==NULL)
  74.         return NULL;
  75.     tail=head;
  76.     while(tail->next!=NULL){
  77.         tail=tail->next;
  78.     }
  79.     return tail;
  80. }
  81.  
  82. double sum_duration(LN *head){
  83.     printf("sum_duration\n");
  84.     float num=0;
  85.         while(head!=NULL){
  86.             num=num+head->s->duration;
  87.             head=head->next;
  88.         }
  89.         return num;
  90.  
  91. }
Add Comment
Please, Sign In to add comment