All pastes #976251 Raw Edit

Anonymous

public c v1 · immutable
·published 2008-04-08 00:42 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>void ordenavetor(int *, int);           //prototipos das funçõesvoid imprimevetor(int *, int);int tamanhovetor(char *, int *);void montavetor(char *, int *, int);void insert_shell(int *, int, int, int);void shellsort(int *, int, int);int main(void){            char *nome;     //nome do arquivo      int tamanho,    //variavel para o tamanho do vetor a ser criado          h,          *vetord;    //vetor a ser ordenado      nome = (char*)calloc(50, sizeof(char));      printf("Entre com o nome do arquivo: ");      fflush(stdin);      gets(nome);            printf("\nEntre com o numero do decremento(h): ");      scanf("%d",&h);            tamanho = tamanhovetor(nome,vetord);      vetord = (int*)calloc(tamanho, sizeof(int));            //insere elementos desordenadamente no vetor      montavetor(nome, vetord, tamanho);                  printf("\n\nO vetor desordenado era:\n\n");            //chamada da função que imprime o vetor DESordenado      imprimevetor(vetord, tamanho);                //chamada da função que ordena o vetor      ordenavetor(vetord,tamanho);                 //chamada da função que imprime o vetor ordenado            printf("\n\nO vetor ordenado eh:\n\n");       imprimevetor(vetord, tamanho);      printf("\n\n");      system("pause");}//----------------------------------------------------------------------------------------------//---------------------------Função que ordena o vetor------------------------------------------void ordenavetor(int vet[],                 int tamanho){                          int i;     int k;     int t;     int ch;          for(i = 1; i < tamanho; i++){      //algoritmo de inserção ordenada em vetor           k = 0;           t = i - 1;           ch = vet[i];                      while((t >= 0) && (k == 0)){              if(ch < vet[t]){                   vet[t + 1] = vet[t];                   t--;              }else{                    k = t + 1;              }           }                      vet[k] = ch;     }}//----------------------------------------------------------------------------------------------//---------------------------Função que imprime o vetor-----------------------------------------void imprimevetor(int vet[],                  int tamanho){     int i;          for(i = 0;i < tamanho;i++){    //imprime os elemeentos         printf("%5d ", vet[i]);         if (!((i+1)%10))            printf(" ->\n\n");         }}//----------------------------------------------------------------------------------------------//Função que retorna a quantidade de números presentes no arquivo, para que seja criado---------//dinamicamente um vetor com essa dimensão------------------------------------------------------//----------------------------------------------------------------------------------------------int tamanhovetor(char nome[],                 int vet[]){      FILE *arq;      int tamanho,          valor;      if(!(arq = fopen(nome, "rt")))      {         printf("Arquivo nao encontrado.\n\n");         system("pause");         exit(1);      }            tamanho = 0;      valor = 0;            for(;;){                             //encontra a quantidade de números presente no arquivo         fscanf(arq, "%d", &valor);        //até encontrar um flag -1            if (valor == -1)               break;         tamanho++;}      fclose(arq);      if (tamanho == 0){                   //exceção caso o vetor seja vazio         printf("\nVetor nulo\n");         system("pause");         exit(1);         }      return tamanho;}//----------------------------------------------------------------------------------------------//--------------------Função que insere no vetor os números encontrados no arquivo--------------//---------------------------------Vetor NÃO-ordenado-------------------------------------------void montavetor(char nome[],                int vet[],                int tamanho){     FILE *arq;     int i;          arq = fopen(nome, "rt");            //não é necessário um tratamento de exceção, pois o arquivo foi aberto uma vez     for(i = 0; i < tamanho; i++)        //o que garante sua existência        fscanf(arq, "%d", &vet[i]);     fclose(arq);}void insert_shell(int *vet, int n, int f, int h){     }void shellsort(int *vet, int n, int h){   int f;   while(h>=1)   {      for(f=0;f<h;f++)      {         insert_shell(vet,n,f,h);      }      h=h/2;   }    }