Guest User

Untitled

a guest
Apr 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define namein "plik.txt"
  4. #define nameout "plik1.txt"
  5.  
  6. int compare(const void *a, const void *b);
  7. int primecheck(int a);
  8. void dop(FILE *plik, FILE *plikout);
  9.  
  10. int main()
  11. {
  12.     FILE *dat,*datout;
  13.     if ((dat=fopen(namein, "r"))==NULL || (datout=fopen(nameout, "w"))==NULL)
  14.     {
  15.             printf ("Nie moge otworzyc pliku.\n");
  16.             exit(1);
  17.     }
  18.     else
  19.     {
  20.         dop(dat,datout);
  21.         fclose(dat);
  22.         fclose(datout);
  23.     }
  24.     return 0;
  25. }
  26. void dop(FILE *plik, FILE *plikout)
  27. {
  28.     int n, *array, *i;
  29.     fscanf(plik,"%d",&n);
  30.     array=(int*)malloc(n*sizeof(int));
  31.     for(i=array; i < array+n*sizeof(int); i=i+sizeof(int))
  32.     {
  33.         fscanf(plik,"%d",i);
  34.         fprintf(stdout,"%d ",*i);
  35.     }
  36.     qsort(array, n, sizeof(int), compare);
  37.   //  for(i=array; i < array+n*sizeof(int); i=i+sizeof(int)) fprintf(stdout,"%d ",i);
  38.     free(array);
  39. }
  40.  
  41. int compare(const void *a, const void *b)
  42. {
  43.   return ( *(int*)a - *(int*)b );
  44. }
  45. int primecheck(int a)
  46. {
  47.     if(a==1 || a == -1 || a == 0) return 0;
  48.     for(int i = 2; i*i <= a; i++) if(a%i==0) return 0;
  49.     return 1;
  50. }
Add Comment
Please, Sign In to add comment