Advertisement
Patresss

1.82

Sep 2nd, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. struct lista_trig
  7. {
  8.     double (*fun)(double);
  9.     char *fnm;
  10.     struct lista_trig * next;
  11. };
  12.  
  13. void ADD_END(struct lista_trig **head , char fun_name[3] , double (*fun_ptr)(double));
  14. void FREE_LIST_ITERATIVE(struct lista_trig **head);
  15. void PRINTF_LIST_RECURSIVE(struct lista_trig *head, double value_deg);
  16.  
  17. int main(void)
  18. {
  19.     char fun_name[3][4] = {{"sin"}, {"cos"}, {"tan"}};
  20.     double (*fun_ptr[3])(double) = { sin, cos, tan };
  21.     struct lista_trig * head = NULL;
  22.  
  23.     int i;
  24.  
  25.     printf("DODAJE NA KONIEC\n");
  26.     for(i=0; i<3; i++)
  27.     {
  28.         ADD_END(&head, fun_name[i], fun_ptr[i]);
  29.     }
  30.  
  31.         printf("\nWYPISUJE REKURENCYJNIE\n");
  32.     double value_deg = 0;
  33.  
  34.     while(value_deg<90)
  35.     {
  36.         PRINTF_LIST_RECURSIVE(head,value_deg);
  37.         value_deg += 30;
  38.     }
  39.  
  40.  
  41.     printf("\nZWALNIAM ITERACYJNIE\n");
  42.     FREE_LIST_ITERATIVE(&head);
  43.  
  44.     return 0;
  45. }
  46.  
  47.  
  48. void ADD_END(struct lista_trig **head , char fun_name[3] , double (*fun_ptr)(double))
  49. {
  50.     struct lista_trig *temp = (struct lista_trig*)malloc(sizeof(struct lista_trig)) ;
  51.     if(!temp)
  52.     {
  53.         printf("MALLOC ERROR!\n");
  54.         exit(-1);
  55.     }
  56.  
  57.     temp->fun = fun_ptr;
  58.     temp->fnm = fun_name;
  59.     temp->next = NULL;
  60.  
  61.     if(*head)
  62.     {
  63.         struct lista_trig *tmp = *head;
  64.         for(;tmp->next;)
  65.         {
  66.             tmp = tmp->next;
  67.         }
  68.         tmp->next = temp;
  69.     }
  70.     else
  71.     {
  72.         *head = temp;
  73.     }
  74. }
  75.  
  76. void PRINTF_LIST_RECURSIVE (struct lista_trig *head, double value_deg)
  77. {
  78.         struct lista_trig *temp = head;
  79.         printf("c:%p, c->%p, %s(%.0lf)=%lf \n", temp, temp->next, temp->fnm, value_deg, temp->fun(value_deg/180*M_PI));
  80.         temp=temp->next;
  81.         if(temp)
  82.             PRINTF_LIST_RECURSIVE(temp,value_deg);
  83. }
  84.  
  85.  
  86. void FREE_LIST_ITERATIVE(struct lista_trig**head)
  87. {
  88.     struct lista_trig *temp=*head;
  89.     struct lista_trig *tmp;
  90.     while(temp->next!=NULL)
  91.     {
  92.         tmp=temp->next;
  93.         free(temp);
  94.         temp=tmp->next;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement