Advertisement
Miketo_prog

Mayoría de edad

Jun 16th, 2020 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #define SALTO cout<<"\n\n"
  5. using namespace std;
  6.  
  7. void personas(int);
  8. int varios(int *, int, int);
  9. int varios(float *, int, int);
  10. void spaces(string, int);
  11. int nameL(string *, int);
  12. int leerPersona();
  13. int numscpy(string);
  14. float floatcpy(string);
  15. void leerName(string &, int);
  16. void leerEdad(int &, string);
  17. void leerAltura(float &, string);
  18. int masOld(int *, int);
  19. int masAlto(float *, int);
  20. int masYoung(int *, int);
  21. int masBajo(float *, int);
  22. void mayoriaEdad(string *, int *,  int);
  23. void estaturaMin(string *, float *, int);
  24.  
  25.  
  26. int main(){
  27.  
  28.    personas(leerPersona());
  29.  
  30.    return 0;
  31. }
  32.  
  33.  
  34. void personas(int cant){
  35.     string names[cant];
  36.     int edades[cant];
  37.     float alturas[cant];
  38.     int old, alto, joven, bajo;
  39.  
  40.     for(int i=0; i<cant; i++){
  41.         system("cls");
  42.         cout<<"PERSONA "<<i+1<<"\n\n";
  43.         leerName(names[i], i+1);
  44.         leerEdad(edades[i], names[i]);
  45.         leerAltura(alturas[i], names[i]);
  46.     }
  47.  
  48.     old=masOld(edades, cant);
  49.     alto=masAlto(alturas, cant);
  50.     joven=masYoung(edades, cant);
  51.     bajo=masBajo(alturas,cant);
  52.  
  53.     system("cls");
  54.  
  55.     cout<<"TABLA DE DATOS";
  56.     SALTO;
  57.     int q_letras=nameL(names, cant);
  58.     cout<<"Nombre";
  59.     spaces("Nombre", q_letras);
  60.     cout<<"\tEdad\tAltura";
  61.     SALTO;
  62.  
  63.     for(int i=0; i<cant; i++){
  64.         cout<<names[i];
  65.         spaces(names[i], q_letras);
  66.         cout<<"\t"<<edades[i]<<"\t"<< alturas[i]<< endl;
  67.     }
  68.  
  69.     SALTO<<endl;
  70.  
  71.     cout<<"CONCLUSIONES DE LA TABLA";
  72.  
  73.     SALTO;
  74.     //VIEJO(s)
  75.     int q=varios(edades, cant, old);
  76.  
  77.     if( q>1 ){
  78.         if( q==cant )
  79.             cout<<"Todos tienen "<<edades[old]<<" anios de edad.";
  80.         else{
  81.             cout<<"Los más abuelos son: ";
  82.             for(int i=0, c=0; i<cant; i++)
  83.                 if( edades[old]==edades[i] ){
  84.                     q--;
  85.                     cout<<names[i]<<" ("<<edades[old]<<" anios)"<<((q>1)?", ":(q==1?" y ":"."));
  86.             }
  87.         }
  88.     }
  89.     else
  90.         cout<<"El más abuelo es: "<<names[old]<<" con "<<edades[old]<<" anios de edad";
  91.  
  92.  
  93.     SALTO;
  94.     //ALTO(S)
  95.     q=varios(alturas, cant, alto);
  96.  
  97.     if( q>1 ){
  98.         if( q==cant )
  99.             cout<<"Todos tienen "<<alturas[alto]<<" mts de altura.";
  100.         else{
  101.             cout<<"Los más altos son: ";
  102.             for(int i=0, c=0; i<cant; i++)
  103.                 if( alturas[alto]==alturas[i] ){
  104.                     q--;
  105.                     cout<<names[i]<<" ("<<alturas[alto]<<" mts)"<<((q>1)?", ":(q==1?" y ":"."));
  106.             }
  107.         }
  108.     }
  109.     else
  110.         cout<<"El más alto es: "<<names[alto]<<" con "<<alturas[alto]<<" mts";
  111.  
  112.  
  113.     SALTO;
  114.     //JOVEN(ES)
  115.     q=varios(edades, cant, joven);
  116.  
  117.     if( q>1 ){
  118.         if( q==cant )
  119.             cout<<"Todos tienen "<<edades[joven]<<" anios de edad.";
  120.         else{
  121.             cout<<"Los más jovenes son: ";
  122.             for(int i=0, c=0; i<cant; i++)
  123.                 if( edades[joven]==edades[i] ){
  124.                     q--;
  125.                     cout<<names[i]<<" ("<<edades[joven]<<" anios)"<<((q>1)?", ":(q==1?" y ":"."));
  126.             }
  127.         }
  128.     }
  129.     else
  130.         cout<<"El más joven es: "<<names[joven]<<" con "<<edades[joven]<<" anios de edad";
  131.  
  132.  
  133.     SALTO;
  134.     //CHAPERRITO(S)
  135.     q=varios(alturas, cant, bajo);
  136.  
  137.     if( q>1 ){
  138.         if( q==cant )
  139.             cout<<"Todos tienen "<<alturas[alto]<<" mts de altura.";
  140.         else{
  141.             cout<<"Los más chaperritos son: ";
  142.             for(int i=0, c=0; i<cant; i++)
  143.                 if( alturas[bajo]==alturas[i] ){
  144.                     q--;
  145.                     cout<<names[i]<<" ("<<alturas[bajo]<<" mts)"<<((q>1)?", ":(q==1?" y ":"."));
  146.             }
  147.         }
  148.     }
  149.     else
  150.         cout<<"El más chaperrito es: "<<names[bajo]<<" con "<<alturas[bajo]<<" mts";
  151.  
  152.     SALTO;
  153.     SALTO;
  154.  
  155.     mayoriaEdad(names, edades, cant);
  156.  
  157.     SALTO;
  158.  
  159.     estaturaMin(names, alturas, cant);
  160.  
  161. }
  162.  
  163.  
  164. int leerPersona(){
  165.     string opc;
  166.     int personas;
  167.  
  168.     do{
  169.         cout<<"Cuantas personas deseas ingresar?\n";
  170.         cin>>opc;
  171.     }while( !isdigit(opc[0]) );
  172.  
  173.     personas=numscpy(opc);
  174.  
  175.     return personas;
  176. }
  177.  
  178.  
  179. int numscpy(string cad){
  180.     vector <int> digits;
  181.     int num=0;
  182.  
  183.     for(int i=0; i<cad.length(); i++)
  184.         if( isdigit(cad[i]) ){
  185.             int x=(int)(cad[i]-'0');
  186.             digits.push_back(x);
  187.         }
  188.  
  189.     for(int i=digits.size()-1, m=1; i>=0; i--, m*=10)
  190.         num += m*(digits[i]);
  191.  
  192.     return num;
  193. }
  194.  
  195.  
  196. void sdigitsa(string cad, int pos, char *a){
  197.     int j=2;
  198.     for(int i=pos; i<cad.length() && j<4; i++)
  199.         if( isdigit(cad[i]) )
  200.             a[j++]=cad[i];
  201.  
  202.     if( j==2 ){
  203.         a[j++]='0';
  204.         a[j]='0';
  205.     }
  206.     else if( j==3 )
  207.         a[j]='0';
  208.  
  209. }
  210.  
  211.  
  212. void sdigitsb(string cad, int pos, char *a){
  213.     int j=0;
  214.     for(int i=pos; i>=0 && j==0; i--)
  215.         if( isdigit(cad[i]) )
  216.             a[j++]=cad[i];
  217.  
  218.     if( j==0 )
  219.         a[j]='0';
  220. }
  221.  
  222.  
  223. float floatcpy(string cad){
  224.     char arrf[4];
  225.     int pos=-1, t_cad=cad.length();
  226.     float numf=0.0, decs=0.0;
  227.  
  228.     arrf[1]='.';
  229.  
  230.     for(int i=0; i<t_cad && pos==-1; i++)
  231.         if( cad[i] == '.' )
  232.             pos=i;
  233.  
  234.     if( pos==-1 ){
  235.         arrf[0]=cad[0];
  236.         pos=1;
  237.         sdigitsa(cad, pos, arrf);
  238.     }
  239.     else{
  240.         sdigitsb(cad, pos, arrf);
  241.         sdigitsa(cad, pos, arrf);
  242.     }
  243.  
  244.     numf=(float)(arrf[0]-'0');
  245.  
  246.     decs=((float)(arrf[2]-'0'))*10 + (float)(arrf[3]-'0');
  247.     numf+=(decs/100);
  248.  
  249.     return numf;
  250. }
  251.  
  252. void leerName(string &name, int i){
  253.     cout<<"Introduce el nombre de la persona "<<i<<"."<<endl;
  254.     cin>>name;
  255. }
  256.  
  257. void leerEdad(int &edad, string name){
  258.     string cant;
  259.  
  260.     do{
  261.         cout<<"Cuál es la edad de "<<name<<"?\n";
  262.         cin>>cant;
  263.     }while( !isdigit(cant[0]) );
  264.  
  265.     edad=numscpy(cant);
  266.  
  267. }
  268.  
  269. void leerAltura(float &h, string name){
  270.     string cant;
  271.  
  272.     do{
  273.         cout<<"Cual es la altura en cm de "<<name<<"?\n";
  274.         cin>>cant;
  275.     }while( !isdigit(cant[0]) );
  276.  
  277.     h=floatcpy(cant);
  278.  
  279. }
  280.  
  281.  
  282. int masOld(int *v, int q){
  283.     int pos=0;
  284.  
  285.     for(int i=1; i<q; i++)
  286.         if( v[i]>v[pos] ) pos=i;
  287.  
  288.     return pos;
  289. }
  290.  
  291.  
  292. int masAlto(float *v, int q){
  293.     int pos=0;
  294.         int a[q];
  295.  
  296.     for(int i=0; i<q; i++)
  297.         a[i]=(int)(100*v[i]);
  298.  
  299.     for(int i=1; i<q; i++)
  300.         if( a[i]>a[pos] ) pos=i;
  301.  
  302.     return pos;
  303. }
  304.  
  305.  
  306. int masYoung(int *v, int q){
  307.     int pos=0;
  308.  
  309.     for(int i=1; i<q; i++)
  310.         if( v[i]<v[pos] ) pos=i;
  311.  
  312.     return pos;
  313. }
  314.  
  315.  
  316. int masBajo(float *v, int q){
  317.     int pos=0;
  318.         int a[q];
  319.  
  320.     for(int i=0; i<q; i++)
  321.         a[i]=(int)(100*v[i]);
  322.  
  323.     for(int i=1; i<q; i++)
  324.         if( a[i]<a[pos] ) pos=i;
  325.  
  326.     return pos;
  327. }
  328.  
  329.  
  330. void spaces(string name, int q ){
  331.     if( name.length()<q )
  332.             for(int n=q-name.length();n>=0; n--)
  333.                 cout<<" ";
  334. }
  335.  
  336.  
  337. int nameL(string *names, int q){
  338.     int mayor=names[0].length();
  339.  
  340.     for(int i=0; i<=q; i++)
  341.         if( names[i].length()>mayor )
  342.             mayor=names[i].length();
  343.  
  344.     return mayor;
  345. }
  346.  
  347.  
  348. int varios(int *v, int q, int d){
  349.     int cont=0;
  350.  
  351.     for(int i=0; i<q; i++)
  352.         if( v[d]==v[i] )
  353.             cont++;
  354.  
  355.     return cont;
  356. }
  357.  
  358.  
  359. int varios(float *v, int q, int d){
  360.     int cont=0;
  361.  
  362.     for(int i=0; i<q; i++)
  363.         if( v[d]==v[i] )
  364.             cont++;
  365.  
  366.     return cont;
  367. }
  368.  
  369.  
  370. void mayoriaEdad(string *names, int *ages, int q){
  371.     const int edad=18;
  372.     int q_letras=nameL(names, q);
  373.  
  374.     cout<<"TABLA DE MAYORES DE EDAD";
  375.  
  376.     SALTO;
  377.  
  378.     cout<<"Nombre";
  379.     spaces("Nombre", q_letras);
  380.     cout<<"\tEdad";
  381.     SALTO;
  382.  
  383.     for(int i=0; i<q; i++)
  384.         if( ages[i]>=edad ){
  385.             cout<<names[i];
  386.             spaces(names[i], q_letras);
  387.             cout<<"\t"<<ages[i]<< endl;
  388.         }
  389.  
  390. }
  391.  
  392.  
  393. void estaturaMin(string *names, float *h, int q){
  394.     const float hMin=1.65;
  395.     int q_letras=nameL(names, q);
  396.  
  397.     cout<<"TABLA DE PERSONAS CON ESTATURA MINIMA ("<<hMin<<" mts)";
  398.  
  399.     SALTO;
  400.  
  401.     cout<<"Nombre";
  402.     spaces("Nombre", q_letras);
  403.     cout<<"\tAltura";
  404.     SALTO;
  405.  
  406.     for(int i=0; i<q; i++)
  407.         if( h[i]>=hMin ){
  408.             cout<<names[i];
  409.             spaces(names[i], q_letras);
  410.             cout<<"\t"<<h[i]<< endl;
  411.         }
  412.  
  413. }
  414.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement