#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "vector_utils.h"
int main()
{
int array_size = 0, positions_to_shift = 0, kierunek = 0, T[150];
int *array = T;
int read = 0;
printf("Podaj wektor: \n");
array_size = read_vector(array, 150, 0);
fflush(stdin);
fflush(stdout);
if(array_size == 0)
{
printf("Not enough data available");
exit(3);
}
if(array_size == -1)
{
printf("Incorrect input");
exit(1);
}
else
{
printf("Podaj przesuniecie: \n");
read = scanf("%d", &positions_to_shift);
if (read <= 0)
{
printf("Incorrect input");
exit(1);
}
else
{
fflush(stdin);
fflush(stdout);
printf("Podaj kierunek przesuniecia: \n");
int read2 = scanf("%d", &kierunek);
fflush(stdin);
if(read2 <= 0 || (kierunek != 1 && kierunek != 2))
{
printf("Incorrect data input");
exit(2);
}
}
}
if(kierunek == 1)
{
read = shift(array, array_size, positions_to_shift, ROTATE_RIGHT);
}
else if(kierunek == 2)
{
read = shift(array, array_size, positions_to_shift, ROTATE_LEFT);
}
if (array_size == 0)
{
printf("Not enough data available");
exit(3);
}
display_vector(array, array_size);
system("pause");
return 0;
}
int shift(int *array, int array_size, int positions_to_shift, enum direction dir)
{
if( array == NULL || array_size <= 0 || (dir != 1 && dir != 2)) return 1;
int indeks = 0, arrays[array_size];
int *pointer = arrays;
if(dir == 1)
{
positions_to_shift = (positions_to_shift + array_size) % array_size;
do
{
int poz = (indeks + positions_to_shift + array_size) % array_size;
*(arrays + poz) = *(array + indeks);
indeks++;
}while (indeks < array_size);
}
else if(dir == 2)
{
positions_to_shift = (positions_to_shift + array_size) % array_size;
do
{
int poz = (indeks - positions_to_shift + array_size) % array_size;
*(arrays + poz) = *(array + indeks);
indeks++;
}while (indeks < array_size);
}
for (int i = 0; i < array_size; i++)
{
*(array + i) = *(pointer + i);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "vector_utils.h"
int read_vector(int *array, int array_size, int stop_value)
{
if(array == NULL || array_size <= 0 ) return -1;
int sizeD = 0;
int array2[150];
int *array2wsk = array2;
while (sizeD < array_size)
{
int read = scanf("%150d", &(*(array2wsk + sizeD)));
if(sizeD >= 150) break;
if((read <= 0) ) return -1;
if(read != 1 || *(array2wsk+sizeD) == stop_value) break;
*(array + sizeD) = *(array2wsk + sizeD);
++sizeD;
}
return sizeD;
}
void display_vector(const int* tab, int size)
{
int i;
for(i=0; i<size; i++)
{
printf("%d ", *(tab+i));
}
}
#ifndef vector_utils_h
#define vector_utils_h
enum direction {ROTATE_RIGHT = 1, ROTATE_LEFT = 2};
int read_vector(int* tab, int size, int stop_value);
int shift(int *array, int array_size, int positions_to_shift, enum direction dir);
void display_vector(const int* tab, int size);
#endif