How to get the number separated by the integer function just below and sort it correctly as described in the main bold question in question in C ++

0

The function to separate an integer from a non-integer is simple

void Inteiro(float n)
 {
      int x = n;
      float y = n;
      if (x==y)
      {
        cout << "E inteiro" << endl;
      }
      else 
      {
        cout << "Nao e inteiro" << endl;
       }
}

I just wanted you to do something like this: Data entry example NOTE: Data is in a txt

0,9
36646
0,4
38833
33882
38849
0,7
36646

I doubt the main thing, what algorithm I need in this question {Accurate logic to be implemented in integer function that reads mode 0.9 is note corresponding to the number of low that in case 36646 an integer, then comes 0.4 that say new broken number where it corresponds the numbers 38833.33882.38849 and 0.7 again corresponds to the number 36646 only that 0.7 must be added with 0.9 because both are of the same number

and when you print, it will show the number with its corresponding note

as

36646 1,6 //com a nota somada
....
..
.
.
    
asked by anonymous 25.05.2015 / 17:44

1 answer

2

It is not clear in the question, but it seems that your difficulty is how to relate pairs of values (student's enrollment and grade, for example, or a player's score to their ID, etc.). >

To do this, use the map (class map ) from the default library. The code below exemplifies its use in the context of your problem (without the rest of the file reading and number identification):

#include <string>
#include <map>

using namespace std;

int main(void)
{
    /* Define o mapa de notas (matrícula x valor da nota). */
    map<int, float> notas;

    notas[36646] += 0.9f;

    notas[38833] += 0.4f;
    notas[33882] += 0.4f;
    notas[38849] += 0.4f;

    notas[36646] += 0.7f; /* <- Observe a repetição da matricula 36646! */


    /* Teste de verificação */

    map<int, float>::iterator it;
    for(it = notas.begin(); it != notas.end(); ++it)
    {
        printf("Matricula #%d -> Nota: %02.02f\n", it->first, it->second);
    }

    return 0;
}

Comments:

  • The map is declared as map<int, float> because it maps an integer (the number plate) to a floating point value (the note). So, whenever you do nota[XXXX] you have access to the value of the note for the XXXX enrollment. If this value has not yet been updated on the map, it is zero (0).
  • Updates are always made using the += operator because it does not replace the value that was already mapped to that number, but adds the new value to the one that already existed.
  • The fixed-floating-point values in my sample code are defined with a f letter at the end to indicate this to the compiler and avoid annoying warnings. In your case you will read from the file then this letter will not appear.
  • The piece of code at the end with the for loop is only meant to demonstrate that the result is what you expect.
  • Exit the previous example program:

    Matricula #33882 -> Nota: 0.40
    Matricula #36646 -> Nota: 1.60
    Matricula #38833 -> Nota: 0.40
    Matricula #38849 -> Nota: 0.40
    
        
    27.05.2015 / 01:56