nikolas_serafini

Lista 11 - Exercício 1

Sep 2nd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void bubbleSort(char *pointVet[], int size);
  6. void archiveGen(char *pointVet[], int size);
  7. void archiveLoad(char *pointVet[])
  8.  
  9. int main() {
  10.     int size,i = 0,j;
  11.     FILE *fp;
  12.     char *token;
  13.    
  14.     fp = fopen("nomes.txt","r");
  15.     fscanf(fp,"%d",&size);
  16.     char *pointVet[size];
  17.     for(i = 0; i < size; i++){
  18.         fscanf(fp,"%s",token);
  19.         pointVet[i] = (char*)malloc(sizeof(char)*strlen(token));
  20.         strcpy(pointVet[i],token);
  21.     }
  22.     fclose(fp);
  23.    
  24.     bubbleSort(pointVet,size);
  25.     archiveGen(pointVet,size);
  26.     return 0;
  27. }
  28.  
  29. void bubbleSort(char *pointVet[], int size){
  30.     int i,j;
  31.     char *token;
  32.    
  33.     for(i = 0; i < size; i++){
  34.         for(j = 0; j < size-1; j++){
  35.             if( strcmp(pointVet[j],pointVet[j+1]) > 0){
  36.                 token = pointVet[j+1];
  37.                 pointVet[j+1] = pointVet[j];
  38.                 pointVet[j] = token;
  39.             }
  40.         }
  41.         size--;
  42.     }
  43. }
  44.  
  45. void archiveGen (char *pointVet[], int size){
  46.     int i;
  47.     FILE *fp;
  48.    
  49.     fp = fopen("names_arranged.txt","w");
  50.     for(i = 0; i < size; i++)
  51.         fprintf(fp,"%d   %s\n",i+1,pointVet[i]);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment