Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7. const int STRMAX = 51;
  8.  
  9. struct Pessoa {
  10.     char nome[STRMAX];
  11.     int idade;
  12. };
  13.  
  14. int search(Pessoa pessoas[50], int n, char p[STRMAX]);
  15.  
  16. int main()
  17. {
  18.     fstream arq;
  19.     int n = 0;
  20.     char pesquisa[STRMAX];
  21.     arq.open("pessoas2.txt");
  22.     if(!arq.is_open()){
  23.         cout << "Erro" << endl;
  24.         exit(0);
  25.     }else{
  26.         arq >> n;
  27.         Pessoa pessoas[n];
  28.         int m[n][n];
  29.         cout << "Pessoas:" << endl;
  30.         for(int i = 0; i < n; i++)
  31.         {
  32.             arq.ignore();
  33.             arq.getline(pessoas[i].nome, 30);
  34.             arq >> pessoas[i].idade;
  35.             cout << pessoas[i].nome << " - " << pessoas[i].idade << endl;
  36.         }
  37.         arq.ignore();
  38.         cout << "Afinidades armazenadas:" << endl;
  39.         int somatorio[3]={0};
  40.         for(int i = 0; i < n; i++){
  41.             for(int j = 0; j < n; j++){
  42.                 arq >> m[i][j];
  43.                 cout << m[i][j] << " ";
  44.                 if(m[i][j] != -1){
  45.                     somatorio[i] += m[i][j];
  46.                 }
  47.             }
  48.             cout << endl;
  49.         }
  50.  
  51.  
  52.         cout << "Insira um nome para consulta" << endl;
  53.         cin.getline(pesquisa, STRMAX);
  54.         int k = 0;
  55.  
  56.         k = search(pessoas, n, pesquisa);
  57.         if(k == -1){
  58.             cout << "Pessoa nao encontrada" << endl;
  59.         }else{
  60.             int aux = 0;
  61.             int jaux = 0;
  62.  
  63.             for(int i = 0; i < n; i++){
  64.                 if(m[k][i] > aux){
  65.                     aux = m[k][i];
  66.                     jaux = i;
  67.                 }
  68.             }
  69.             cout << "A pessoa com mais afinidade com "
  70.                  << pessoas[k].nome << " eh "
  71.                  << pessoas[jaux].nome << ", "
  72.                  << pessoas[jaux].idade << endl;
  73.         }
  74.  
  75.  
  76.         int kaux = 0;
  77.         int laux = 0;
  78.         for(int i = 0; i< 3; i++){
  79.             //cout << somatorio[i] << " ";
  80.             if(somatorio[i] > kaux){
  81.                 kaux = somatorio[i];
  82.                 laux = i;
  83.             }
  84.         }
  85.         cout << "Pessoa mais sociavel: " << pessoas[laux].nome << endl;
  86.     }
  87.  
  88.  
  89.     return 0;
  90. }
  91.  
  92. int search(Pessoa pessoas[50], int n, char p[STRMAX]){
  93.     for(int i = 0; i < n; i++){
  94.         if(strcmp(p, pessoas[i].nome) == 0){
  95.             return i;
  96.         }
  97.     }
  98.     return -1;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement