Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. ############### TABLICA WSKAŹNIKÓW #########################
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int sum(int a, int b){
  5. return a + b;}
  6.  
  7. int substract(int a, int b){
  8. return a - b;}
  9.  
  10. int multi (int a, int b){
  11. return a * b;}
  12.  
  13. int (*p[3])(int a, int b);
  14.  
  15. int main()
  16. {
  17.     int result;
  18.     p[0] = sum;
  19.     p[1] = substract;
  20.     p[2] = multi;
  21.  
  22.     result = (*p[2])(4,2);
  23.     printf("%d", result);
  24.     return 0;
  25. }
  26.  
  27.  
  28. ############### TABLICE DWUWYMIAROWE #########################
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32.  
  33. int jedynki(int liczba){
  34.     int count = 0;
  35.     while (liczba){
  36.         liczba &= liczba - 1;
  37.         count++;
  38.     }
  39.     return count;
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45.     int wiersze;
  46.     int kolumny;
  47.     unsigned int currentMax = 0;
  48.     int row;
  49.     printf("Podjac liczbê wierszy:\n");
  50.     scanf("%d", &wiersze);
  51.     printf("Podaj liczbê kolumn:\n");
  52.     scanf("%d", &kolumny);
  53.  
  54.     int** xy;
  55.     xy = (int**)malloc(wiersze*sizeof(unsigned int));
  56.     for (int i = 0; i < wiersze; i++){
  57.         xy[i] = (int*)malloc(2 * sizeof(unsigned int));
  58.  
  59.     }
  60.     for (int i = 0; i < wiersze; i++){
  61.             printf("Uzupe³nij wiersz");
  62.         for (int j = 0; j < kolumny; j++){
  63.             scanf("%d", &(xy[i][j]));
  64.         }
  65.     }
  66.  
  67.  
  68.     printf("Tablica zostanie wydrukowana");
  69.     for (int i = 0; i < wiersze; i++){
  70.         for (int j = 0; j < kolumny; j++){
  71.             printf("%d", xy[i][j]);
  72.         }
  73.         printf("\n");
  74.     }
  75.  
  76.     for (int i = 0; i<wiersze; i++){
  77.         unsigned int jedyneczki = 0;
  78.         for (int j = 0; j<kolumny; j++){
  79.             jedyneczki += jedynki(xy[i][j]);
  80.         }
  81.         if (jedyneczki > currentMax){
  82.             currentMax = jedyneczki;
  83.             row = i;
  84.         }
  85.     }
  86.     printf("%d", currentMax);
  87.     for (int j = 0; j<kolumny; j++){
  88.         printf("%d", xy[row][j]);
  89.     }
  90.  
  91.     free(xy);
  92.     return 0;
  93. }
  94.  
  95. ################ DYNAMICZNE TABLICE JEDNOWYMIAROWE #####################
  96.  
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99.  
  100. int main()
  101. {
  102.     int N;
  103.     printf("Podaj wielkoœæ tablicy");
  104.     scanf("%d", &N);
  105.  
  106.     int *x = (int*)malloc(N*sizeof(int));
  107.  
  108.     for (int i = 0; i < N; i++){
  109.         scanf("%d", &(x[i]));
  110.     }
  111.  
  112.     printf("drukowanie tablicy");
  113.     for (int i = 0; i<N; i++){
  114.         printf("%d\n", x[i]);
  115.     }
  116. }
  117.  
  118. ############## CZYTANIE WORD BY WORD ###################
  119. #include <stdio.h>
  120. #include <stdlib.h>
  121.  
  122.  
  123.  
  124. int main()
  125. {
  126.     char costam[30];
  127.     FILE *fpointer = fopen("tekst.txt", "ra");
  128.     while (!feof(fpointer)){
  129.         fscanf(fpointer, "%s", costam);
  130.         printf("Next word is: %s\n", costam);
  131.     }
  132.     return 0;
  133. }
  134.  
  135. ############# CZYTANIE CHAR BY CHAR ###################
  136. #include <stdio.h>
  137. #include <stdlib.h>
  138.  
  139.  
  140.  
  141. int main()
  142. {
  143.     char ch;
  144.     FILE *fpointer = fopen("tekst.txt", "ra");
  145.     while (!feof(fpointer)){
  146.         ch = fgetc(fpointer);
  147.         printf("Next sign is: \"%c\"\n", ch);
  148.     }
  149.     return 0;
  150. }
  151.  
  152.  
  153. ############## CZYTANIE LINE BY LINE ##################
  154. #include <stdio.h>
  155. #include <stdlib.h>
  156.  
  157.  
  158.  
  159. int main()
  160. {
  161.     char line[255];
  162.     FILE *fpointer = fopen("tekst.txt", "ra");
  163.     while (!feof(fpointer)){
  164.         fgets(line, 255, fpointer);
  165.         printf("Next line is: %s\n", line);
  166.     }
  167.     fclose(fpointer);
  168.     return 0;
  169. }
  170.  
  171.  
  172. ############## APPENDING AND READING ####################
  173. #include <stdio.h>
  174. #include <stdlib.h>
  175.  
  176.  
  177.  
  178. int main()
  179. {
  180.     char line[255];
  181.     FILE *fpointer = fopen("tekst.txt", "a+");
  182.     fprintf(fpointer, "\nuwaga dodaje linie");
  183.     fseek(fpointer, 0, 0);
  184.     while (!feof(fpointer)){
  185.  
  186.         fgets(line, 255, fpointer);
  187.         printf("Next line is: %s\n", line);
  188.     }
  189.     fclose(fpointer);
  190.     return 0;
  191. }
  192.  
  193. ############ SZESNASTKOWY ##############
  194.  
  195. #include <stdio.h>
  196. #include <stdlib.h>
  197.  
  198.  
  199. void heksa(int liczba, char* heksadecymalna){
  200.     char szesnastkowa[20];
  201.     char tablica[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  202.     int reszta;
  203.     int index = 0;
  204.     while(liczba != 0){
  205.         reszta = liczba % 16;
  206.         liczba = liczba / 16;
  207.         szesnastkowa[index] = tablica[reszta];
  208.         index++;
  209.     }
  210.     int heksaindex = 0;
  211.     int nowy_index = index - 1;
  212.     for (nowy_index; nowy_index >= 0; nowy_index--){
  213.         heksadecymalna[heksaindex] = szesnastkowa[nowy_index];
  214.         heksaindex++;
  215.     }
  216. }
  217.  
  218. int main()
  219. {
  220.     char arr[15];
  221.     heksa(375098, arr);
  222.     printf("Konwersja: %s\n", arr);
  223.     return 0;
  224. }
  225. ############ TABLICA STRUKTUR ############
  226. #include <stdio.h>
  227. #include <stdlib.h>
  228.  
  229. int main()
  230. {
  231.     FILE *fpointer = fopen("tekst.txt", "r");
  232.  
  233.     struct Student{
  234.         int age;
  235.     };
  236.  
  237.     struct Student s[5];
  238.     int counter = 0;
  239.     int age1;
  240.     while (!feof(fpointer)){
  241.            fscanf(fpointer, "%d\n", &age1);
  242.            s[counter].age = age1;
  243.            counter++;
  244.  
  245.     };
  246.  
  247.     printf("%d", s[3].age);
  248.     return 0;
  249. }
  250. ########### NOTATKI ##################
  251. - przekazywanie stringu do struktury
  252.     ctrcpy(student1.name, "Jim");
  253.  
  254. - uzyskiwanie adresu
  255.     printf("%p", &age);
  256.  
  257. - storing pointer variable
  258.     int *pAge = &age;
  259.  
  260. - dereferencing pointer
  261.     printf("%d", *pAge);
  262.  
  263. - wprowadzanie stałej
  264.     const int NUM = 5;
  265.  
  266. -getting double
  267.     scanf("%lf", &gpa);
  268.  
  269. - getting string
  270.     scanf("%s", name);
  271.  
  272. - getting line of text
  273.     scanf(name, 20, stdin);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement