Relate two vectors/arrays of different types and sort them in C++

Asked

Viewed 128 times

0

I have a file . txt from where I need to collect the data (will be placed below). The first column refers to the part code and the second refers to the piece name. I need to find a way to sort the code with the part and then use a search engine for a customer to search the part possessing the code at hand. I did the Quick Sort sorting and the Binary Search.

I’m in doubt on how to leave the iCodigo along with the sPeca related even after sorting. The idea would be to create two vectors and try to relate them to some meneira.

The TXT file:

54 ARRUELA  
93 ABRACADEIRA 
55 PINO 
49 PORCA 
60 RELE 
30 DISJUNTOR 
27 FUSIVEL 
72 MOUSE 
40 LAMPADA 
14 TECLA 
21 CAIXA 
33 TAMPA 
76 INTERRUPTOR 
26 SOQUETE  
7 BASE
63 LAMINA  
50 PLACA 
31 TUBO 
17 LATERAL 
92 VIDEO 
11 PLUG 
36 CABO 
52 SUPORTE 
83 BOTAO 
22 PARAFUSO

The program:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>

using namespace std;

void montVetorCodigo(int *v, int n, int x);
void montVetorPeca(string *vP, int n, string x);
void showVetorCodigo(int *v, int n);
void showVetorPeca(string *v, int n);
int buscaBinaria(int v[], int n, int x);
void quickSort(int *v, int esq, int dir);
int separar(int *v, int esq, int dir);
void trocar(int *v, int i, int j);

int main()
{
    fstream arquivo;
    string sArqNome,sLinha,sCodigo,sPeca;
    int iCodigo,iQtdLinhas=0,n;
    char cOp;

    cout << "Entre com o nome do arquivo: ";
    cin >> sArqNome;
    arquivo.open(sArqNome);
    if (arquivo.is_open()){
        while(!arquivo.eof()){  // contar as linhas para definir n
            getline(arquivo,sLinha);
            iQtdLinhas++;
        }
        n = iQtdLinhas;
        int iVetorCodigo[n];
        string sVetorPeca[n];
        int iQtdLinhas=0;
        arquivo.clear();
        arquivo.seekg(ios::beg);
        while(!arquivo.eof()){  // montagem do vetor coletando dados das colunas
            getline(arquivo,sLinha);
            sCodigo = sLinha.substr(0,2);
            sPeca = sLinha.substr(2,14);
            iCodigo = stoi(sCodigo);  // transformar string para int
            sPeca.erase(remove(sPeca.begin(), sPeca.end(), ' '), sPeca.end());  // remover espaços em branco da captura da string 'sPeca'
            cout << iCodigo << endl;
            cout << sPeca << endl;
            montVetorCodigo(iVetorCodigo,n,iCodigo);
            montVetorPeca(sVetorPeca,n,sPeca);
            iQtdLinhas++;
        }
        showVetorCodigo(iVetorCodigo,n);
        showVetorPeca(sVetorPeca,n);
        quickSort(iVetorCodigo,0,n-1);  // ordenação do vetor
        cout << endl;
        do{
            buscaBinaria(iVetorCodigo,n,iCodigo);  // mostrar a peça referente ao código digitado
            cout << endl;
            cout << "Deseja continuar a pesquisa?(Digite 's' para SIM e 'n' para NAO)-> ";
            cin >> cOp;
            cin.ignore();
            cout << endl;
        } while (cOp == 's' || cOp == 'S');
    }else{
            cout << "Arquivo nao encontrado" << endl;
    }
    arquivo.close();
    return 0;
}

void montVetorCodigo(int *v, int n, int x){
    for (int i = 0; i < n; i++){
        v[i] = x;
    }
}

void montVetorPeca(string *vP, int n, string x){
    for (int i = 0; i < n; i++){
        vP[i] = x;
    }
}

void showVetorCodigo(int *v, int n){
    cout << "Indices:   ";
    for (int i = 0; i < n; i++){
        cout << setw(3) << i << " ";
    }
    cout << endl;
    cout << "Elementos: ";
    for (int i = 0; i < n; i++){
        cout << setw(3) << v[i] << " ";
    }
    cout << endl;
}

void showVetorPeca(string *vP, int n){
    cout << "Indices:   ";
    for (int i = 0; i < n; i++){
        cout << setw(3) << i << " ";
    }
    cout << endl;
    cout << "Elementos: ";
    for (int i = 0; i < n; i++){
        cout << setw(3) << vP[i] << " ";
    }
    cout << endl;
}

// algoritmo de busca binária
int buscaBinaria(int v[], int n, int x){
    cout << "Digite o codigo referente a peca desejada: ";
    cin >> x;
    int esq = -1, dir = n;
    while (esq < dir - 1){
        int meio = (esq + dir) / 2;
        if (v[meio] < x) esq = meio;
        else dir = meio;
    }
    cout << endl;
    cout << "CODIGO: " << x << endl;
    cout << "PECA: " << dir << endl;
    cout << endl;
    return dir;
}

// algoritmo de ordenação quick sort
void quickSort(int *v, int esq, int dir){
    if (esq < dir){
        int j = separar(v, esq, dir);
        quickSort(v, esq, j - 1);
        quickSort(v, j + 1, dir);
    }
}

int separar(int *v, int esq, int dir){
    int iPivo = v[esq];
    int i = esq + 1;
    int j = dir;
    while (i <= j){
        if (v[i] <= iPivo)
            i++;
        else if (v[j] > iPivo)
            j--;
        else if (i <= j){
            trocar(v,i,j);
        }
    }
    trocar(v,esq,j);
    return j;
}

void trocar(int *v, int i, int j){
    int iAux;
    iAux = v[i];
    v[i] = v[j];
    v[j] = iAux;
}

In this specific part I can only collect the last code and the last piece, being stored the same value in all indexes of the array.

montVetorCodigo(iVetorCodigo,n,iCodigo);
montVetorPeca(sVetorPeca,n,sPeca);

Getting the result shown by the function showVetorCodigo and showVetorPeca as follows:

Indices:     0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24
Elementos:  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22  22
Indices:     0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24
Elementos: PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO PARAFUSO

1 answer

0


Henry, there are several ways to do this.

  1. Keep data in two different vectors, but perform mirror operations.

You will have an integer vector and a string vector. However, any operation you do on one vector, you have to do on the other. For example, if during ordering you make a switch between positions i and j in the integer vector, you must also do it in the string vector. This way you ensure that the relationship between code and part name will be consistent even after any operation you do.

  1. Keep data together in one vector.

You can do this using a vector of pair<int, string>, or objects of the class Produto with code properties and name. With this you will have the two information always together and can continue operating the way you operate.

  1. Use a map.

A map is a data structure structured with keys and values. You can organize your map with the code as key and the product name as value. It also comes with search, insertion and modification operations ready. The map search has logarithmic efficiency as well as binary search.

In case it is not clear I can add more details, I hope to have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.