Advertisement
Wojtekd

LAB 2 (4 zadania, l.zesp, struktury)

Mar 3rd, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* zadanie 1
  4.  
  5. struct pracownik
  6. {
  7.     char imie [20];
  8.     char nazwisko[30];
  9.     int numer_pracownika;
  10.     long long pesel;
  11. };
  12.  
  13. int main( void )
  14. {
  15.     struct pracownik p1 = { .imie = "Jan", .nazwisko = "Kowalski", .numer_pracownika = 4, .pesel = 92080212194};
  16.     struct pracownik p2;
  17.    
  18.     printf("podaj imie pracownika\n");
  19.     scanf("%s",&p2.imie);
  20.     printf("podaj nazwisko pracownika\n");
  21.     scanf("%s",&p2.nazwisko);
  22.     printf("podaj identyfikator pracownika\n");
  23.     scanf("%s",&p2.numer_pracownika);
  24.     printf("podaj pesel pracownika\n");
  25.     scanf("%s",&p2.pesel);
  26.    
  27.     return 0;
  28. }
  29. */
  30.  
  31. /* zadanie 2
  32. struct zesp
  33. {
  34.     double x;
  35.     double y;
  36. };
  37.  
  38. struct zesp z_add(struct zesp* A, struct zesp* B)
  39. {
  40.     struct zesp zesp_result;
  41.     zesp_result.x = A->x + B->x;
  42.     zesp_result.y = A->y + B->y;
  43.     printf("z1 + z2 = %f + %fi",zesp_result.x,zesp_result.y);
  44.     return zesp_result;
  45. }
  46. struct zesp z_sub(struct zesp* A, struct zesp* B)
  47. {
  48.     struct zesp zesp_result;
  49.     zesp_result.x = A->x - B->x;
  50.     zesp_result.y = A->y - B->y;
  51.     printf("z1 - z2 = %f + %fi",zesp_result.x,zesp_result.y);
  52.     return zesp_result;
  53. }
  54. struct zesp z_mul(struct zesp* A, struct zesp* B)
  55. {
  56.     struct zesp zesp_result;
  57.     zesp_result.x = (A->x * B->x) - (A->y * B->y);
  58.     zesp_result.y = (A->x * B->y) + (A->y * B->x);
  59.     printf("z1 * z2 = %f + %fi",zesp_result.x,zesp_result.y);
  60.     return zesp_result;
  61. }
  62. struct zesp z_div(struct zesp* A, struct zesp* B)
  63. {
  64.     struct zesp zesp_result;
  65.     // aliasy
  66.     double a = A->x;
  67.     double b = A->y;
  68.     double c = B->x;
  69.     double d = B->y;
  70.    
  71.     if(B->x == 0 && B->y == 0)
  72.     {
  73.         return;
  74.     }  
  75.     zesp_result.x = (a*c + b*d) / (c*c + d*d);
  76.     zesp_result.y = (b*c - a*d) / (c*c + d*d); 
  77.     printf("z1 / z2 = %f + %fi",zesp_result.x,zesp_result.y);
  78.     return zesp_result;
  79. }
  80.  
  81. int main( void )
  82. {  
  83.     return 0;
  84. }
  85. */
  86.  
  87. /* zadanie 3
  88. #include <time.h>
  89. const int tab_size = 100;
  90. struct point
  91. {
  92.     int x, y;
  93. };
  94.  
  95. void inicjalizuj_losowymi(struct point* tab)
  96. {
  97.     int i;
  98.     for(i = 0; i < tab_size; i++)
  99.     {
  100.         tab[i].x = (rand() % 201 - 100);
  101.         tab[i].y = (rand() % 201 - 100);
  102.     }
  103. }
  104. void display(struct point* tab)
  105. {
  106.     int i;
  107.     for(i = 0; i < tab_size; i++)
  108.     {
  109.         printf("x = %d, y = %d\n",tab[i].x,tab[i].y);
  110.     }
  111. }
  112. struct point findFarthest(struct point* tab, struct point mPoint)
  113. {
  114.     int i;
  115.     double distance;
  116.     double maxDistance = 0;
  117.     double diffX,diffY;
  118.     diffX = tab[0].x - mPoint.x;
  119.     diffY = tab[0].y - mPoint.y;
  120.     maxDistance = sqrt(diffX * diffX + diffY * diffY);  
  121.    
  122.     struct point result;
  123.    
  124.     for(i = 0; i < tab_size; i++)
  125.     {
  126.         diffX = tab[i].x - mPoint.x;
  127.         diffY = tab[i].y - mPoint.y;       
  128.         distance = sqrt(diffX * diffX + diffY * diffY);    
  129.         if(distance > maxDistance)
  130.         {
  131.             maxDistance = distance;
  132.             result.x = tab[i].x;
  133.             result.y = tab[i].y;
  134.         }
  135.     }
  136.     printf("najdalszy od punktu(%d,%d) jest punkt z tablicy (%d,%d)",mPoint.x,mPoint.y,result.x,result.y);
  137.    
  138.     return result;
  139. }
  140. int main ( void )
  141. {
  142.     srand(time(NULL));
  143.     struct point points[tab_size];
  144.     inicjalizuj_losowymi(points);
  145.     display(points);
  146.    
  147.     struct point MPOINT = {0,0};
  148.     findFarthest(points,MPOINT);
  149.    
  150.     return 0;
  151. }
  152. */
  153. /* zadanie 4
  154.  
  155. struct extra_point
  156. {
  157.     double x;
  158.     char start;
  159.     char end;
  160.     short y;
  161.     int z; 
  162. };
  163.  
  164. int main( void )
  165. {
  166.     printf("%d",sizeof(struct extra_point));
  167.     return 0;
  168. }
  169. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement