How to do so that he does not repeat a clue and tell me to put another clue

1
#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();

}

    
asked by GanzoPro 17.12.2018 в 22:49
source

2 answers

1

You're just checking if the current track is the same as track 0:

    for (int i = 1; i <= number_of_request; i++) {
        cin >> request[i];
        sorted_request[i] = request[i];
        if (request[i] == request[0]) { // <<--- AQUI
            cout << "Pista no valida introdusca otra\n";
            i--;
        }

If your idea is to avoid duplicates, you have to check ALL the clues:

    for (int i = 1; i <= number_of_request; i++) {
        cin >> request[i];
        sorted_request[i] = request[i];
        bool ok = true;
        for( int j=0; j<i; j++ )
          ok &= (request[i] != request[j]);

        if (!ok)
        {
            cout << "Pista no valida introdusca otra\n";
            i--;
        }

Of course, you can also use the stl for this:

    std::set<std::string> pistas;
    for (int i = 1; i <= number_of_request; i++) {
        cin >> request[i];
        sorted_request[i] = request[i];

        bool ok;
        std::tie(std::ignore,ok) = pistas.insert(request[i]);

        if (!ok)
        {
            cout << "Pista no valida introdusca otra\n";
            i--;
        }

std::set is a container that does not support duplicates . Then, when inserting a new element, it will return a couple of values:

  • an iterator to the element
  • a Boolean that indicates whether the element is new (true) or if it already existed (false)

What we do with the function tie is to extract the boolean and ignore the iterator since we do not need it (for that we use std::ignore ).

The container set does not help you to save the list of the tracks since this container will order the elements automatically and that collides with the requirement that the tracks keep the insertion order ... in this case set it only serves as a tool to avoid duplicates.

    
answered by 18.12.2018 / 11:00
source
0

It has a lot that I do not program in c ++ so I'll leave you only some snippets:

To begin with I would use a data structure as a set that does not allow duplicates, and in that case trying to insert would not be successful and we use the result to request data again.

#include <set>
#include ...
using namespace std;

.
.
.
    set<int> mis_pistas;

   for (int i = 1; i <= number_of_request; i++) {
     int valor_leido;
     cin >> valor_leido;
     // mientras no se pueda guardar porque seguramente ya estaba
     while(!mis_pistas.insert(valor_leido)){
       cin >> valor_leido;
     }

Another possible alternative is to verify your entire arrangement before inserting it using a flag to avoid passing while the duplicity flag is active:

 for (int i = 1; i <= number_of_request; i++) {
     int valor_leido;
     cin >> valor_leido;

     bool duplicado = false;
    while(duplicado){
      for (int j = 1; !duplicado && j <= number_of_request; j++) {
        if(request[j] == valor_leido ){
          duplicado=true;
          break;
        }
      }
      cin >> valor_leido;
    }
    
answered by 17.12.2018 в 23:22