Advertisement
Bisus

Untitled

Oct 24th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void Invert(double *a, unsigned int n);
  4. void ShiftLeft(double *a, unsigned int n);
  5. int RemoveNegative(double *a, unsigned int n);
  6. void ShiftLeftForK(double *a, unsigned int n, unsigned int k);
  7. void printf_array(double *a, unsigned int n);
  8.  
  9. void printf_array(double *a, unsigned int n)
  10. {
  11.     unsigned int i;
  12.  
  13.     for(i = 0; i<n; i++)
  14.         printf("a[%u]=%lf\t", i, a[i]);
  15.  
  16.     printf("\n");
  17. }
  18.  
  19. // Сдвинуть массив на k позиций влево
  20. void ShiftLeftForK(double *a, unsigned int n, unsigned int k)
  21. {
  22.     unsigned int i0, i, j, cnt = 0;
  23.     double c;
  24.  
  25.     for(i0 = 0; cnt<n; i0++)
  26.     {
  27.         c = a[i0];
  28.         for(i = i0, j = (i + k)%n; j!=i0; i = j, j = (j + k)%n)
  29.         {
  30.             a[i] = a[j];
  31.             cnt++;
  32.         }
  33.         a[i] = c;
  34.         cnt++;
  35.     }
  36. }
  37.  
  38. // Все не отрицательные группируются в начале, возвращается их количество
  39. int RemoveNegative(double *a, unsigned int n)
  40. {
  41.     unsigned int i, j;
  42.  
  43.     i = 0;
  44.     for(j = 0; j<n; j++)
  45.     {
  46.         if(a[j]>=0)
  47.         {
  48.             a[i] = a[j];
  49.             i++;
  50.         }
  51.     }
  52.  
  53.     return (i - 1);
  54. }
  55.  
  56. // Расположить элементы массива в обратном порядке
  57. /* Пример использования:
  58.  * Invert(a,n);
  59.  * Invert(a, n - k);
  60.  * Invert(a + n - k, k);
  61.  * Здесь реализована перестановка подмассива, стоящего в начала, и его дополнения.
  62.  * */
  63. void Invert(double *a, unsigned int n)
  64. {
  65.     double c;
  66.     unsigned int i;
  67.  
  68.     if(n<2) return;
  69.  
  70.     for(i = 0; i<n/2; i++)
  71.     {
  72.         c = a[i];
  73.         a[i] = a[n - i - 1];
  74.         a[n - i - 1] = c;
  75.     }
  76. }
  77.  
  78. // Циклический сдвиг
  79. void ShiftLeft(double *a, unsigned int n)
  80. {
  81.     double c;
  82.     unsigned int i;
  83.  
  84.     if(n<2) return;
  85.  
  86.     c = a[0];
  87.     for(i = 1; i<n; i++)
  88.     {
  89.         a[i-1] = a[i];
  90.     }
  91.     a[n-1] = c;
  92. }
  93.  
  94. int main(void)
  95. {
  96.     FILE *input_file;
  97.     double *a;
  98.     unsigned int i,n,k;
  99.  
  100.     if((input_file = fopen("input", "r"))==NULL)
  101.     {
  102.         printf("Open file error!\n");
  103.         return 1;
  104.     }
  105.  
  106.     if(fscanf(input_file, "%d", &n)<=0)// Первое считываемое число - кол-во элементов в массиве
  107.     {
  108.         printf("Input from file error!\n");
  109.         return 2;
  110.     }
  111.  
  112.     if(n==0)
  113.     {
  114.         printf("Result: no elements!\n");
  115.         return 0;
  116.     }
  117.     a = (double *)malloc(n*sizeof(double));
  118.     for(i = 0; i<n; i++)
  119.     {
  120.         if(fscanf(input_file, "%lf", &a[i])<=0)
  121.         {
  122.             printf("Input from file error!\n");
  123.             free(a);
  124.             return 2;
  125.         }
  126.     }
  127.  
  128.     printf_array(a,n);
  129.     printf("k=");
  130.     scanf("%u", &k);
  131.     ShiftLeftForK(a,n,k);
  132.     printf_array(a,n);
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement