How to create a C ranking?

1

How can I read and sort a text file where the information looks like this:

  carlos   5 
  lucas   20 
  josue   10 

* The file looks like this.

Saved randomly but when I read the precise information that is in the order of those who have more points for less points.

I use the following code to read the file itself:

void ranking(){         



     system("cls");
     printf("\n ######- Ranking de Jogadores -######\n");
     printf("  \n");
     printf(" NOME - PONTOS \n");
     printf("  \n");
     printf(" ");


  char texto_str[999];
  char usuario[20];

  //abrindo o arquivo_frase em modo "somente leitura"
  pont_arq = fopen("ranking.txt", "r");

 while(fgets(texto_str, 20, pont_arq) != NULL)
 printf("%s \n ", texto_str);



  fclose(pont_arq);

  getch();

system("PAUSE");
carregaMenu();

}

    
asked by anonymous 16.06.2017 / 22:10

1 answer

0

For you to get the names and punctuation, as the size of the name and punctuation characters are different, we can use fscanf to accomplish this task. We would have, for example:

fscanf(pont_arq, "%s %d\n", nome, &pontuacao);

Regarding ordering we would have several ways to accomplish this task. I think the most correct way would be for you to create a priority list data structure and a players struct by inserting them in an orderly way in this list. You could also add to the list anyway and use a sort algorithm like bubblesort, heapsort, quicksort, using the sorting parameter as a parameter.

Another perhaps simpler way, but less organized would be to work with a vector of names, another vector with the scores corresponding to the names and thus order the two vectors using the punctuation as the ordering parameter.

I'm going to implement an implementation of the last mentioned option because it's the simplest one. I also used bubblesort as the sorting algorithm. I decided to do it in a static way, but if it is not possible to define the file size of the file use malloc to perform dynamic instantiation of the players.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//algoritmo de sort, perceba que aqui eu ordeno os dois vetores (nome e pontuação)

void bubbleSort(int* pontuacao, char nomes[10][255], int tamanho)
{   
    int i;
    int trocou;
    do  
    {
        trocou = 0;
    for (i=tamanho; i > 0; i--)
    {   
        if (pontuacao[i] > pontuacao[i-1]) 
        {   
            int pAux;
            char nAux[255];
            pAux = pontuacao[i];
            strcpy(nAux, nomes[i]);
            pontuacao[i] = pontuacao[i-1];
            strcpy(nomes[i], nomes[i-1]);
            pontuacao[i-1] = pAux;
            strcpy(nomes[i-1], nAux);
            trocou = 1;
        }   
    }   

    }while (trocou);
}   



void ranking(){         

    //system("clear");
    printf("\n ######- Ranking de Jogadores -######\n");
    printf("  \n");
    printf(" NOME - PONTOS \n");
    printf("  \n");

    // cria ponteiro de arquivo
    FILE * pont_arq;
    //abrindo o arquivo_frase em modo "somente leitura"
    pont_arq = fopen("data.txt", "r");
    // cria matriz para 10 nomes (poderia ser dinamico) e array de pontuações
    char nomes[10][255];
    int pontuacoes[10];
    //variaveis que irá receber o nome ea pontuação do arquivo
    char nome[255];
    int pontuacao;
    //quantidade de jogadores
    int tamanho = 0;

    //lê do arquivo
    while(fscanf(pont_arq, "%s   %d\n", nome, &pontuacao) != EOF)
    {   
        strcpy(nomes[tamanho],nome);
        pontuacoes[tamanho] = pontuacao;
        tamanho++;  
    }

    //Ordena
    bubbleSort(pontuacoes, nomes, tamanho);

    //Imprime
    int i;
    for (i=0; i<tamanho; i++)
    {
        printf("%s %d\n", nomes[i], pontuacoes[i]);
    }

  fclose(pont_arq);

  //getch();

//system("PAUSE");
//carregaMenu();

}
    
17.06.2017 / 02:15