Ir ao conteúdo
  • Cadastre-se

wedsonextremo

Membro Júnior
  • Posts

    3
  • Cadastrado em

  • Última visita

Reputação

0
  1. Bom galera estou precisando ajuda de vocês em um exercício em C++ Descrição: Fazer um programa que ordene um arquivo já gerado em .txt em outro codigo em c++ Nesse arquivo contem 50 Caracteres em cada linha, e conta o tempo decorrido em milhões de linhas. Em outro programa devo abrir esse arquivo gerando, onde o mesmo tem que possui os seguintes metodos de ordenação e também o tempo gasto para ordenar o arquivo .txt em cada método 1- Bubble Sort 2 - Inserction Sort 3 - Selection Sort 4 - Quick Sort "Consegui fazer o codigo em Bubble Sort, mas nao conseguir com o restante dos metodos. Agradeceria muito a quem me ajudasse. Segue em anexo os codigos em cpp. segue o codigo--> #include <iostream> #include <string.h> #include <fstream> #include <stdlib.h> #include <time.h> using namespace std; void convertesegundo(int tempo) { int hor = tempo/3600; int seg = tempo%60; int min = tempo/60; cout<<"Tempo para gerar aquivo: "; if(hor<10){cout<<"0";} cout<<hor<<":"; if(min<10){cout<<"0";} cout<<min<<":"; if(seg<10){cout<<"0";} cout<<seg<<endl; } void bubbleSort(string *content, int tam) { int x; string temp; bool sw=true; while(sw){ sw = false; for(x = 0; x<tam; x++) { if(content[x] > content[x+1]) { temp = content[x]; content[x] = content[x+1]; content[x+1] = temp; sw = true; } } } } void selection(int *array, int length) { int max, i, temp; while (length > 0) { max = 0; for (i = 1; i < length; i++) if (array > array[max]) max = i; temp = array[length-1]; array[length-1] = array[max]; array[max] = temp; length--; } } int main() { string line, lines[100001]; int i=0, j=0, op=100, tempo; time_t tempoinicial; srand(time(NULL)); char iArqName[50], oArqName[50]; cout << "Informe o nome do arquivo a ordenar> "; cin.getline(iArqName, 50); cout << "Informe o nome do arquivo depois de ordenado> "; cin.getline(oArqName, 50); ifstream myfile (iArqName); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); lines = line; i++; } myfile.close(); i = i-2; cout << "Arquivo lido com Sucesso!"<<endl; while(op<1 || op>4) { cout << "### SELECIONE O METODO DE ORDENACAO ###"<<endl; cout << " 1 - Bubble Sort" << endl; cout << " 2 - Inserction Sort" << endl; cout << " 3 - Selection Sort" << endl; cout << " 4 - Quick Sort" <<endl; cout << "#######################################"<<endl; cout << "Informe o metodo desejado> "; cin >> op; switch(op) { case 1: tempoinicial = time(NULL); bubbleSort(lines, i); break; case 3: //SELECT break; case 4: //Quick Sort break; default: cout << "Opcao Invalida!"; } } ofstream myFile2(oArqName); while(j <= i) { myFile2 << lines[j]<<endl; j++; } myFile2.close(); time_t tempofinal(time(NULL)); tempo = tempofinal-tempoinicial; convertesegundo(tempo); } else cout << "Unable to open file"; system("pause"); return 0; }
  2. Bom galera estou precisando ajuda de vocês em um exercício em C++ Descrição: Fazer um programa que ordene um arquivo já gerado em .txt em outro codigo em c++ Nesse arquivo contem 50 Caracteres em cada linha, e conta o tempo decorrido em milhões de linhas. Em outro programa devo abrir esse arquivo gerando, onde o mesmo tem que possui os seguintes metodos de ordenação e também o tempo gasto para ordenar o arquivo .txt em cada método 1- Bubble Sort 2 - Inserction Sort 3 - Selection Sort 4 - Quick Sort "Consegui fazer o codigo em Bubble Sort, mas nao conseguir com o restante dos metodos. Agradeceria muito a quem me ajudasse. Segue em anexo os codigos em cpp. segue o codigo--> #include <iostream> #include <string.h> #include <fstream> #include <stdlib.h> #include <time.h> using namespace std; void convertesegundo(int tempo) { int hor = tempo/3600; int seg = tempo%60; int min = tempo/60; cout<<"Tempo para gerar aquivo: "; if(hor<10){cout<<"0";} cout<<hor<<":"; if(min<10){cout<<"0";} cout<<min<<":"; if(seg<10){cout<<"0";} cout<<seg<<endl; } void bubbleSort(string *content, int tam) { int x; string temp; bool sw=true; while(sw){ sw = false; for(x = 0; x<tam; x++) { if(content[x] > content[x+1]) { temp = content[x]; content[x] = content[x+1]; content[x+1] = temp; sw = true; } } } } void selection(int *array, int length) { int max, i, temp; while (length > 0) { max = 0; for (i = 1; i < length; i++) if (array > array[max]) max = i; temp = array[length-1]; array[length-1] = array[max]; array[max] = temp; length--; } } int main() { string line, lines[100001]; int i=0, j=0, op=100, tempo; time_t tempoinicial; srand(time(NULL)); char iArqName[50], oArqName[50]; cout << "Informe o nome do arquivo a ordenar> "; cin.getline(iArqName, 50); cout << "Informe o nome do arquivo depois de ordenado> "; cin.getline(oArqName, 50); ifstream myfile (iArqName); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); lines = line; i++; } myfile.close(); i = i-2; cout << "Arquivo lido com Sucesso!"<<endl; while(op<1 || op>4) { cout << "### SELECIONE O METODO DE ORDENACAO ###"<<endl; cout << " 1 - Bubble Sort" << endl; cout << " 2 - Inserction Sort" << endl; cout << " 3 - Selection Sort" << endl; cout << " 4 - Quick Sort" <<endl; cout << "#######################################"<<endl; cout << "Informe o metodo desejado> "; cin >> op; switch(op) { case 1: tempoinicial = time(NULL); bubbleSort(lines, i); break; case 3: //SELECT break; case 4: //Quick Sort break; default: cout << "Opcao Invalida!"; } } ofstream myFile2(oArqName); while(j <= i) { myFile2 << lines[j]<<endl; j++; } myFile2.close(); time_t tempofinal(time(NULL)); tempo = tempofinal-tempoinicial; convertesegundo(tempo); } else cout << "Unable to open file"; system("pause"); return 0; }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...