#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <math.h>
using namespace std;
int compare(const void * a, const void * b) {
if (*(int*)a < *(int*)b) return -1;
if (*(int*)a == *(int*)b) return 0;
if (*(int*)a > *(int*)b) return 1;
}
void shift(int a[], const int size) {
for (int i = 1; i <= size; i++)
a[i - 1] = a[i];
}
class disk {
public:
int request[40];
int number_of_request = 12;
int sorted_request[12];
int max = 40;
int direction;
public:
void receive_request() {
current_location:
cout << "Introduzca la posicion de inicio:\n";
cin >> request[0];
sorted_request[0] = request[0];
if (request[0]>max || request[0]<0)
goto current_location;
cout << "\nIngrese las pistas:\n";
for (int i = 1; i <= number_of_request; i++) {
cin >> request[i];
sorted_request[i] = request[i];
if (request[i] == request[0]) {
cout << "Pista no valida introdusca otra\n";
i--;
}
if (request[i]>max || request[i]<0) {
cout << "Pista no valida introdusca otra\n";
i--;
}
}
qsort(sorted_request + 1, number_of_request, sizeof(int), compare);
}
int SCAN() {
int head_movement = 0, flag = 0;
for (int i = 1; i <= number_of_request; i++) {
if (sorted_request[i] > sorted_request[0] && flag == 0) {
flag = i;
}
}
if (flag == 1) {
head_movement += sorted_request[number_of_request] - sorted_request[0];
} else {
head_movement += max - sorted_request[0];
head_movement += max - sorted_request[1];
}
return head_movement;
}
~disk() {}
};
int main() {
disk hdd;
hdd.receive_request();
cout << "Movimientos totales ";
cout << hdd.SCAN() << endl;
for (int i = 0; i <= hdd.number_of_request; i++) {
if (hdd.sorted_request[i] == hdd.request[0]) continue;
if (hdd.sorted_request[i] > hdd.request[0]) {
cout << hdd.sorted_request[i];
cout << "-->";
}
}
for (int i = hdd.number_of_request; i >= 0 ; i--) {
if (hdd.sorted_request[i] < hdd.request[0]) {
cout << hdd.sorted_request[i];
if (i - 1 == 0) {
cout << endl;
} else {
cout << "-->";
}
}
}
_getche();
}