Advertisement
AntonioVillanueva

El restaureante de Javier

Sep 11th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits> //limites float  std::numeric_limits<float>::max()
  3. #include <vector> //http://www.cplusplus.com/reference/vector/vector/
  4. //uso ./ejecutable <input.txt
  5. using namespace std;
  6. /*-------------------------------------------------------------------------------*/
  7. struct codigo{char cod;float ingreso;};//Creo un "array" de estructura codigo
  8. /*-------------------------------------------------------------------------------*/
  9. string codigoToCategoria (const char codigo){//retorna el STRING correspondiente a una CATEGORIA
  10.     switch (codigo) {
  11.         case 'D':return "DESAYUNOS";break;
  12.         case 'A':return "COMIDAS";break;
  13.         case 'M':return "MERIENDAS";break;
  14.         case 'I':return "CENAS";break;
  15.         case 'C':return "COPAS";break; 
  16.         default:break;                                 
  17.     }
  18.     return "";
  19. }
  20. /*-------------------------------------------------------------------------------*/
  21. bool empate(vector <codigo> cat){//varias categorias en EMPATE ?
  22.     int encontrado(0);
  23.    
  24.     if (cat.size()<=3 ){return true;} //al menos dos elementos son igual a 0
  25.    
  26.     for (std::vector<codigo>::iterator it = cat.begin() ; it != cat.end(); ++it){
  27.             for (std::vector<codigo>::iterator it2 = cat.begin() ; it2 != cat.end(); ++it2){
  28.                
  29.                 if (it->ingreso==it2->ingreso) {encontrado++;}//Compara un elemento con todos              
  30.                 if (encontrado >=2){return true;} //Se han econtrado dos valores iguales
  31.             }
  32.             encontrado=0;//Nueva busqueda de num. de encontrados
  33.     }  
  34.     return false;//No hay empate    
  35. }
  36. /*-------------------------------------------------------------------------------*/
  37. string evaluacion (vector <codigo> cat){//evalua los datos de entrada crea la salida
  38.     string dia;
  39.     float media(0),comida(0);
  40.     float maximo(0),minimo(std::numeric_limits<float>::max());//En los limites del float
  41.  
  42.     string max(""),min("");
  43.    
  44.     for (std::vector<codigo>::iterator it = cat.begin() ; it != cat.end(); ++it)
  45.     {
  46.         if (it->cod=='A'){comida=it->ingreso;}//INGRESO COMIDA
  47.        
  48.         if (it->ingreso<minimo){//INGRESO MININMO
  49.             minimo=it->ingreso;
  50.             min=codigoToCategoria(it->cod);//Escribo la categoria minima
  51.         }
  52.        
  53.         if (it->ingreso>maximo){//INGRESO MAXIMO
  54.             maximo=it->ingreso;
  55.             max=codigoToCategoria(it->cod);//Escribo la categoria maxima
  56.         }
  57.             media+=it->ingreso;//SUMA para evaluar la MEDIA    
  58.     }
  59.        
  60.     //Construccion de la cadena "string" de salida , dia    
  61.     //INGRESO MAXIMO del dia
  62.     dia+=max;
  63.     dia+="#";
  64.    
  65.     //Empate (si varios resultados son iguales ) o INGRESO MINIMO ?
  66.     dia+= (empate(cat)) ? "EMPATE": min;       
  67.     dia+="#";
  68.    
  69.     //Las comidas han superado la MEDIA ? SI o NO  
  70.     dia+= comida > (media/5) ?"SI":"NO"; //5 si se tienen en cuenta los 5 codigos
  71.     //dia+= comida > (media/cat.size()) ?"SI":"NO"; //Media en funcion de datos introducidos
  72.  
  73.     return dia;
  74. }
  75. /*-------------------------------------------------------------------------------*/
  76. /*-------------------------------------------------------------------------------*/
  77. int main(){
  78.     string str;
  79.     vector <codigo> categorias;//usor del container vector http://www.cplusplus.com/reference/vector/vector/   
  80.     while  (getline(cin, str)){ //lee una linea del fichero de entrada <input.txt
  81.        
  82.         //Crea un un vector de informacion hasta encontrar 'N'
  83.         if (str[0]!='N'){ categorias.push_back ( codigo {str[0],stof( &str[1] )});}//Crea vector de informacion
  84.         else { 
  85.             cout<<evaluacion(categorias)<<endl ;//evalua el vector ha encontrado 'N'
  86.             categorias.clear();//limpia el vector
  87.         }      
  88.     }                      
  89. }
  90.  
  91. /*Contenido imput.txt
  92. D 2.80 DESAYUNOS
  93. C 48.0 COPAS
  94. A 8.0   COMIDAS
  95. N 0     FIN
  96.  
  97. EMPATE M==I==0
  98. COMIDAS <MEDIA -> No 2.8+48.0+8=58.8  MEDIA =58.8/5=11.76
  99. COPAS#EMPATE#NO
  100.  
  101. D 15.33 DESAYUNOS
  102. A 60.00 COMIDAS mayor
  103. M 12.00 MERIENDAS menor
  104. I 25.00 CENAS
  105. N 0     FIN
  106.  
  107. 15.33+60+12+25=112.23 MEDIA= 112.23/5=22,466 COMIDAS >MEDIA
  108. COMIDAS#MERIENDAS#
  109.  
  110. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement