Advertisement
Caio_25

aeroporto

Nov 1st, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.37 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<stdio.h>
  4. #include<mpi.h>
  5.  
  6. using namespace std;
  7.  
  8. class Pouso{
  9. private:
  10.   int codigo;
  11.   int origem;
  12.   int horarioChegada;
  13.   int tempoVoo;
  14.  
  15. public:
  16.   Pouso(int codigo, int origem, int horarioChegada, int tempoVoo)
  17.   {
  18.     this->codigo = codigo;
  19.     this->origem = origem;
  20.     this->horarioChegada = horarioChegada;
  21.     this->tempoVoo = tempoVoo;
  22.   }
  23.  
  24.   //getters
  25.   int getCodigo()
  26.   {
  27.     return(this->codigo);
  28.   }
  29.  
  30.   int getOrigem()
  31.   {
  32.     return(this->origem);
  33.   }
  34.  
  35.   int getHorarioChegada()
  36.   {
  37.     return(this->horarioChegada);
  38.   }
  39.  
  40.   int getTempoVoo()
  41.   {
  42.     return(this->tempoVoo);
  43.   }
  44.  
  45.   void setHorarioChegada(int horarioChegada)
  46.   {
  47.     this->horarioChegada = horarioChegada;
  48.   }
  49.  
  50.  
  51.  
  52. };
  53.  
  54. class Decolagem{
  55. private:
  56.   int codigo;
  57.   int destino;
  58.   int horarioPartida;
  59.   int tempoVoo;
  60.  
  61. public:
  62.   Decolagem(int codigo, int destino, int horarioPartida, int tempoVoo)
  63.   {
  64.     this->codigo = codigo;
  65.     this->destino = destino;
  66.     this->horarioPartida = horarioPartida;
  67.     this->tempoVoo = tempoVoo;
  68.   }
  69.  
  70.   //getters
  71.   int getCodigo()
  72.   {
  73.     return(this->codigo);
  74.   }
  75.  
  76.   int getDestino()
  77.   {
  78.     return(this->destino);
  79.   }
  80.  
  81.   int getHorarioPartida()
  82.   {
  83.     return(this->horarioPartida);
  84.   }
  85.  
  86.   int getTempoVoo()
  87.   {
  88.     return(this->tempoVoo);
  89.   }
  90.  
  91.   void setHorarioPartida(int horarioPartida)
  92.   {
  93.     this->horarioPartida = horarioPartida;
  94.   }
  95.  
  96. };
  97.  
  98. class Aeroporto{
  99.  
  100. private:
  101.     static int StaticCodigo;
  102.  
  103.     int codigo;
  104.     vector<Pouso> pousos;
  105.     int numeroPousos;
  106.     vector<Decolagem> decolagens;
  107.     int numeroDecolagens;
  108.     int relogio;
  109.     vector<int> qtdMensagensEnviar;
  110.     vector<int> qtdMensagensReceber;
  111.  
  112.  
  113.  
  114. public:
  115.     Aeroporto(int codigo, int comm_size)
  116.     {
  117.       //string cod;
  118.       //char *fileName= "entrada";
  119.       this->StaticCodigo++;
  120.       this->codigo = codigo;
  121.       this->relogio = 0;
  122.       numeroPousos = 0;
  123.       numeroDecolagens = 0;
  124.       qtdMensagensReceber.assign(comm_size, 0);
  125.       qtdMensagensEnviar.assign(comm_size, 0);
  126.  
  127.       /*cod = to_string(codigo);
  128.       strcat(fileName, cod.c_str())
  129.       FILE *f;
  130.       f = fopen(fileName, "r");*/
  131.     }
  132.  
  133.  
  134.     //getters
  135.     int getCodigo()
  136.     {
  137.       return(this->codigo);
  138.     }
  139.  
  140.     void getDecolagem()
  141.     {
  142.       vector<Decolagem>::iterator i;
  143.  
  144.       cout << "Decolagens   Destino   Horario_Partida   Tempo_Voo\n";
  145.       for(i = (this->decolagens).begin(); i < (this->decolagens).end(); i++)
  146.       {
  147.         cout << i->getCodigo()<< "\t     " << i->getDestino() << "\t       " << i->getHorarioPartida() << "\t\t     " << i->getTempoVoo() << endl;
  148.       }
  149.     }
  150.  
  151.     void getPouso()
  152.     {
  153.  
  154.       vector<Pouso>::iterator i;
  155.  
  156.       cout << "Pousos   Origem   Horario_Chegada   Tempo_Voo\n";
  157.       for(i = (this->pousos).begin(); i < (this->pousos).end(); i++)
  158.       {
  159.         cout << i->getCodigo()<< "\t     " << i->getOrigem() << "\t       " << i->getHorarioChegada() << "\t\t     " << i->getTempoVoo() << endl;
  160.       }
  161.     }
  162.  
  163.     //setters
  164.  
  165.     void SetDecolagem()
  166.     {
  167.       int n, j = 0,destino, horarioPartida, tempoVoo;
  168.       Decolagem *aux;
  169.  
  170.       cout << "Informe o numero de Decolagens: ";
  171.       cin >> this->numeroDecolagens;
  172.  
  173.       n = this->numeroDecolagens;
  174.  
  175.       do
  176.       {
  177.         n = n / 10;
  178.         j++;
  179.       }while(n > 0);
  180.  
  181.       for(int i = 0;i < this->numeroDecolagens; i++)
  182.       {
  183.         cout << "Informe o Destino: ";
  184.         cin >> destino;
  185.  
  186.         cout << "Informe a horada da Partdia: ";
  187.         cin >> horarioPartida;
  188.  
  189.         cout << "Informe o tempo de voo: ";
  190.         cin >> tempoVoo;
  191.  
  192.         aux = new Decolagem(this->codigo * j * 10 + (i+1) , destino, horarioPartida,   tempoVoo);
  193.         (this->decolagens).push_back(*aux);
  194.  
  195.       }
  196.     }
  197.  
  198.  
  199.     void setPousos(int aux[])
  200.     {
  201.       Pouso novo(aux[0], aux[1], aux[2], aux[3]);
  202.  
  203.       pousos.push_back(novo);
  204.       this->numeroPousos += 1;
  205.  
  206.     }
  207.     //functions
  208.     void aviso(int comm_size, int rank) //avisa quantas mensagens o processo rank vai enviar aos outros procesos
  209.     {
  210.       //vector<int> qtdMensagens(comm_size, 0);
  211.  
  212.       vector<Decolagem>::iterator i;
  213.       int tag = 99;
  214.  
  215.       for(i = this->decolagens.begin(); i < this->decolagens.end(); i++)
  216.       {
  217.           qtdMensagensEnviar[i->getDestino() - 1]  += 1;
  218.       }
  219.  
  220.       /*for(int j = 0; j < comm_size; j++)
  221.       {
  222.         cout << " qtdMensagensEnviar[" << j << "] " << qtdMensagensEnviar[j] << endl;
  223.       }*/
  224.  
  225.       for(int j = 0; j < comm_size; j++)
  226.       {
  227.         if(j != rank)
  228.         {
  229.             //cout << "Enviando para " << j << " " << qtdMensagensEnviar[j] << endl;
  230.             MPI_Send(&qtdMensagensEnviar[j], 1, MPI_INT, j, tag, MPI_COMM_WORLD);
  231.         }
  232.  
  233.       }
  234.  
  235.  
  236.     }
  237.  
  238.     void entendido(int comm_size, int rank)
  239.     {
  240.       //vector<int> qtdMensagens(comm_size, 0);
  241.  
  242.       int tag = 99;
  243.       //cout << "recebendo do " << rank << endl;
  244.       MPI_Recv(&qtdMensagensReceber[rank], 1, MPI_INT, rank, tag, MPI_COMM_WORLD,MPI_STATUS_IGNORE);
  245.       //cout << "qtdMensagensReceber[" << rank << "] " << qtdMensagensReceber[rank] << endl;
  246.  
  247.  
  248.  
  249.       /*for(int i = 0; i < comm_size; i++)
  250.       {
  251.         if(i != rank)
  252.         {
  253.           cout << "recebendo do " << i << endl;
  254.           MPI_Recv(&qtdMensagensReceber[i], 1, MPI_INT, i, tag, MPI_COMM_WORLD,MPI_STATUS_IGNORE);
  255.           cout << "qtdMensagensReceber[" << i << "] " << qtdMensagensReceber[i] << endl;
  256.         }
  257.       }*/
  258.  
  259.     }
  260.  
  261.  
  262.     void enviar(int comm_size)
  263.     {
  264.       int rank = this->getCodigo() - 1;
  265.       int tag = 99, aux[4];
  266.       vector<Decolagem>::iterator it;
  267.  
  268.       for(int i = 0; i < comm_size; i++)
  269.       {
  270.         if(i != rank)
  271.         {
  272.  
  273.             for(it = this->decolagens.begin(); it < this->decolagens.end(); it++)
  274.             {
  275.               if((it->getDestino() - 1) == i)
  276.               {
  277.                 //cout << "Enviando para " << it->getDestino() << endl;
  278.                 aux[0] = it->getCodigo();
  279.                 aux[1] = rank + 1;
  280.                 aux[2] = it->getHorarioPartida() + it->getTempoVoo();
  281.                 aux[3] = it->getTempoVoo();
  282.                 MPI_Send(aux, 4, MPI_INT, i, tag, MPI_COMM_WORLD);
  283.               }
  284.  
  285.           }
  286.         }
  287.  
  288.       }
  289.  
  290.       cout << endl;
  291.     }
  292.  
  293.     void receber(int comm_size, int i)
  294.     {
  295.       //int rank = this->getCodigo() - 1;
  296.       int aux[4], tag = 99;
  297.       for(int j = 0; j < qtdMensagensReceber[i]; j++)
  298.       {
  299.           //cout << "Recebendo do " << i + 1<< endl;
  300.           MPI_Recv(aux, 4, MPI_INT, i, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  301.           setPousos(aux);
  302.  
  303.       }
  304.       cout << endl;
  305.  
  306.     /*  for(int i = 0; i < comm_size; i++)
  307.       {
  308.         if(i != rank)
  309.         {
  310.           for(int j = 0; j < qtdMensagensReceber[i]; j++)
  311.           {
  312.               cout << "Recebendo do " << i + 1<< endl;
  313.               MPI_Recv(aux, 4, MPI_INT, i, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  314.               setPousos(aux);
  315.  
  316.           }
  317.         }
  318.       }*/
  319.     }
  320.  
  321.  
  322.  
  323.     void imprimir()
  324.     {
  325.         cout << "Codigo: " << endl;
  326.         getPouso();
  327.         getDecolagem();
  328.         cout << endl;
  329.     }
  330.  
  331.  
  332.     void tratar()
  333.     {
  334.       vector<Pouso>::iterator i, j;
  335.       vector<Decolagem>::iterator k, z;
  336.  
  337.       int offset = 1;
  338.  
  339.       for(i = this->pousos.begin(); i < this->pousos.end(); i++)
  340.       {
  341.         for(j = this->pousos.begin(); j < this->pousos.end(); j++)
  342.         {
  343.           if(i != j)
  344.           {
  345.             if(i->getHorarioChegada() == j->getHorarioChegada())
  346.             {
  347.  
  348.  
  349.               if(i->getTempoVoo() > j->getTempoVoo())
  350.               {
  351.                 for(int n = 0; n < this->pousos.size(); n++)
  352.                 {
  353.                   if(pousos[n].getHorarioChegada() == j->getHorarioChegada() + offset)
  354.                   {
  355.                     n = -1;
  356.                     offset++;
  357.                   }
  358.                 }
  359.  
  360.                 j->setHorarioChegada(j->getHorarioChegada() + offset);
  361.               }
  362.  
  363.               else
  364.               {
  365.                 for(int n = 0; n < this->pousos.size(); n++)
  366.                 {
  367.                   if(pousos[n].getHorarioChegada() == i->getHorarioChegada() + offset)
  368.                   {
  369.                     n = -1;
  370.                     offset++;
  371.                   }
  372.                 }
  373.  
  374.                 i->setHorarioChegada(i->getHorarioChegada() + offset);
  375.               }
  376.             }
  377.  
  378.           }
  379.           offset = 1;
  380.         }
  381.       }
  382.       offset = 1;
  383.  
  384.       for(k = this->decolagens.begin(); k < this->decolagens.end(); k++)
  385.       {
  386.         for(i = this->pousos.begin(); i < this->pousos.end(); i++)
  387.         {
  388.  
  389.               if(i->getHorarioChegada() == k->getHorarioPartida())
  390.               {
  391.                 for(int n = 0; n < this->pousos.size(); n++)
  392.                 {
  393.                   if(pousos[n].getHorarioChegada() == k->getHorarioPartida() + offset)
  394.                   {
  395.                     n = -1;
  396.                     offset++;
  397.                   }
  398.                 }
  399.                 k->setHorarioPartida(k->getHorarioPartida() + offset);
  400.               }
  401.  
  402.               offset = 1;
  403.  
  404.         }
  405.       }
  406.  
  407.       offset = 1;
  408.       for(k = this->decolagens.begin(); k < this->decolagens.end(); k++)
  409.       {
  410.         for(z = this->decolagens.begin(); z < this->decolagens.end(); z++)
  411.         {
  412.           if(k != z)
  413.           {
  414.             if(k->getHorarioPartida() == z->getHorarioPartida())
  415.             {
  416.               if(k->getTempoVoo() > z->getTempoVoo())
  417.               {
  418.                 for(int i = 0; i < this->pousos.size(); i++)
  419.                 {
  420.                   if(pousos[i].getHorarioChegada() == z->getHorarioPartida() + offset)
  421.                   {
  422.                     i = -1;
  423.                     offset++;
  424.                   }
  425.                 }
  426.                 z->setHorarioPartida(z->getHorarioPartida() + offset);
  427.               }
  428.  
  429.               else
  430.               {
  431.                 for(int i = 0; i < this->pousos.size(); i++)
  432.                 {
  433.                   if(pousos[i].getHorarioChegada() == k->getHorarioPartida() + offset)
  434.                   {
  435.                     i = -1;
  436.                     offset++;
  437.                   }
  438.                 }
  439.                 k->setHorarioPartida(k->getHorarioPartida() + offset);
  440.               }
  441.             }
  442.  
  443.           }
  444.           offset = 1;
  445.         }
  446.       }
  447.  
  448.  
  449.       }
  450.  
  451.  
  452.  
  453.  
  454. };
  455.  
  456. int Aeroporto::StaticCodigo = 0;
  457.  
  458. int main()
  459. {
  460.   int rank, comm_size;
  461.  
  462.   MPI_Init(NULL, NULL);
  463.   MPI_Comm_size (MPI_COMM_WORLD, &comm_size) ;
  464.   MPI_Comm_rank (MPI_COMM_WORLD, &rank) ;
  465.  
  466.   Aeroporto a(rank + 1, comm_size);
  467.  
  468.   for(int i = 0; i < comm_size; i++)
  469.   {
  470.     if(rank == i)
  471.     {
  472.       cout << "new my rank " << rank << " my codigo " << a.getCodigo() << endl;
  473.       a.SetDecolagem();
  474.     }
  475.   }
  476.  
  477.   for(int i = 0; i < comm_size; i++)
  478.   {
  479.     if(rank == i)
  480.     {
  481.       a.aviso(comm_size, rank);
  482.     }
  483.  
  484.     else
  485.     {
  486.       a.entendido(comm_size, i);
  487.     }
  488.   }
  489.  
  490.   for(int i = 0; i < comm_size; i++)
  491.   {
  492.     if(rank == i)
  493.     {
  494.       a.enviar(comm_size);
  495.     }
  496.  
  497.     else
  498.     {
  499.       a.receber(comm_size, i);
  500.     }
  501.   }
  502.  
  503.   for(int i = 0; i < comm_size; i++)
  504.   {
  505.     if(rank == i)
  506.     {
  507.       a.tratar();
  508.       a.imprimir();
  509.     }
  510.   }
  511.  
  512.  
  513.   MPI_Finalize();
  514.  
  515.   return(0);
  516.  
  517.  
  518. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement