Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Pabullo

Ordenação Externa

Recommended Posts

Boa tarde amigos, eu e alguns amigos da faculdade estamos desenvolvendo um algoritmo que realiza um processo de Ordenação Externa em arquivos porém estamos no deparando com o seguinte erro quando tentamos executar o programa.

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Alguma dica de como resolver isso?

Desde já Obrigado.
Fico no aguardo.

int main()
{

    int k;
    cin>>k;

    ExternalSorting sort(k);

    string input = "INPUT.txt";

    sort.run(input);

    return 0;
}



int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}


using namespace std;

string convertInt(int number)
{
   ostringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}

class ExternalSorting{
public:
    int k;
    int quant;

    int quantPerArq; //quantidade de registros por arquivo.

    ofstream *arquivo;

    ExternalSorting(int _quantPerArq){
        quantPerArq = _quantPerArq;
        quant = 0;
    }

    void setK(int _k){
        k = _k;
    }
    void calcQuant(string input){
        ifstream in(input.c_str());

        int temp; //variável temporaria

        for(;!in.eof();){
            in>>temp;
            quant++;
        }

        in.close();

        k = ceill((double)quant/(double)quantPerArq);
    }

    void criarArquivos(){
        //Separar em arquivos.
        arquivo = new ofstream[k];


        //Abrir os arquivos para imprimir os valores.
        string filename;
        for(int i=0;i<k;i++){
            filename = "ARQUIVOS//arq";
            filename += convertInt(i);
            arquivo[i].open(filename.c_str());
        }
    }

    void escreverArquivos(string input){
        ifstream in(input.c_str());

        int valor;

        //Escrever ordenado
        int vetor[quantPerArq];

        int q; //quantidades de registros no arquivo atual.

        string filename;

        for(int i=0;i<k;i++){

            //cout<<"Criando arquivo "<<i<<endl;

            filename = "ARQUIVOS//arq";
            filename += convertInt(i);

			//arquivo[i].open(filename.c_str());

            q = 0;
            for(;q<quantPerArq && !in.eof();){
                in>>valor;
                if(in.eof()) break;

                vetor[q] = valor;
                q++;
            }

            //cout<<"Ordenando arquivo "<<i<<endl;

            qsort(vetor,q,sizeof(int),compare);

            for(int j=0;j<q;j++){
                arquivo[i]<<vetor[j]<<endl;
            }

            //cout<<"OK "<<i<<endl;
        }

        in.close();
    }

    void ordenarArquivos(){
        int vetor[quantPerArq];

        int q; //quantidades de registros no arquivo atual.
        int valor;

        string filename;

        for(int i=0;i<k;i++){

            filename = "ARQUIVOS//arq";
            filename += convertInt(i);


            ifstream daVez(filename.c_str());

            q = 0;
            for(;;){
                daVez>>valor;
                if(daVez.eof()) break;

                vetor[q] = valor;
                q++;
            }

            qsort(vetor,q,sizeof(int),compare);

            arquivo[i].close();
            arquivo[i].open(filename.c_str());

            for(int j=0;j<q;j++){
                arquivo[i]<<vetor[j]<<endl;
                //cout<<vetor[j]<<endl;
            }
            //cout<<endl;
        }

    }

    void merge(){
        int valor[k];

        string filename;

        ifstream in[k];

        for(int i=0;i<k;i++){
            filename = "ARQUIVOS//arq";
            filename += convertInt(i);

            in[i].open(filename.c_str());
            in[i]>>valor[i];
        }

        bool sorting = true;
        int menor = 0x7f7f7f7f;
        int posMenor;

        ofstream output("output");

        while(sorting){
            menor = 0x7f7f7f7f;
            sorting = false;
            for(int i=0;i<k;i++){
                if(!in[i].eof()){
                    if(valor[i]<menor){
                        menor = valor[i];
                        posMenor = i;
                        sorting = true;
                    }
                }
            }

            if(sorting){
                output<<menor<<endl;

                //cout<<menor<<" "<<posMenor<<endl;

                in[posMenor]>>valor[posMenor];
            }

        }
    }

    void run(string input){

        calcQuant(input);

        criarArquivos();

        escreverArquivos(input);

        //ordenarArquivos();

        merge();

        exit();

    }

    void exit(){
        for(int i=0;i<k;i++) arquivo[i].close();
    }



};

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.