Advertisement
Guest User

Lanzazo

a guest
Oct 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. struct region {
  5.         char nombre[50];
  6.         int numero;
  7.         int votos[6];
  8. };
  9.  
  10. struct candidato {
  11.         char nombre[50];
  12.         int votos;
  13. };
  14. struct elecciones {
  15.         struct region regiones[15];
  16.         struct candidato candidatos[6];
  17.         int poblacionChilena;
  18. };
  19. void porcentajes(struct elecciones,const unsigned int);
  20. void sumarvotos(unsigned int*,const struct elecciones);
  21. int comparar(const void *, const void *);
  22. int main (){
  23.    
  24.     unsigned int i;
  25.     unsigned int j;
  26.     unsigned int cant;
  27.    
  28.     struct elecciones vector;
  29.    
  30.     char* regionesChile[15] = {"Tarapaca", "Antofagasta", "Atacama", "Coquimbo", "Valparaiso", "O'Higgins", "Maule", "Bio-Bio", "La Araucania", "Los Lagos", "Aysen", "Magallanes", "Santiago", "Los Rios", "Arica y Parinacota"};
  31.    
  32.     char* candidatosChile[6] = {"Sebastian Pinera", "Beatriz Sanchez", "Alejandro Guillier", "MEO", "Carolina Goic", "Alejandro Navarro"};
  33.    
  34.     scanf("%u",&vector.poblacionChilena);
  35.  
  36.     for (i = 0; i < 15 ; i++){
  37.         strcpy((vector.regiones[i].nombre),(regionesChile[i]));
  38.         vector.regiones[i].numero = 0; // Acumulador
  39.         for (j = 0; j < 6 ; j++){
  40.             scanf("%d",&vector.regiones[i].votos[j]);
  41.             vector.regiones[i].numero = vector.regiones[i].numero + vector.regiones[i].votos[j] ;
  42.         }
  43.     }
  44.     for (i = 0 ; i < 6 ;i++){
  45.         strcpy((vector.candidatos[i].nombre),candidatosChile[i]);
  46.         vector.candidatos[i].votos = 0;
  47.         for (j = 0; j < 15 ; j++){
  48.             vector.candidatos[i].votos = vector.candidatos[i].votos + vector.regiones[j].votos[i];
  49.         }
  50.     }
  51.     sumarvotos(&cant,vector);
  52.     //printf("CANT = %u\n",cant);
  53.     porcentajes(vector,cant);
  54.    
  55.    
  56.     /*for (i = 0; i < 15 ; i++){
  57.         printf("REGION  = %s \n",vector.regiones[i].nombre);
  58.         for (j = 0; j < 6 ; j++){
  59.             printf("votos %u = %d ",j,vector.regiones[i].votos[j]);
  60.         }
  61.         printf("ACUMULADOR REGION = %d \n",vector.regiones[i].numero);
  62.     }*/
  63.     /*for (i = 0 ; i < 6 ;i++){
  64.         printf("Candidato = %s ",vector.candidatos[i].nombre);
  65.         printf("CANTIDAD VOTOS = %d \n",vector.candidatos[i].votos);
  66.    
  67.     }*/
  68.    
  69.    
  70.     return 0;
  71. }
  72.  
  73. int comparar(const void *arg1, const void *arg2){
  74.  if(*(int *)arg1 < *(int *)arg2) return -1;
  75.    else if(*(int *)arg1 > *(int *)arg2) return 1;
  76.    else return 0;
  77. }
  78. void sumarvotos(unsigned int* cant,const struct elecciones vector){
  79.    
  80.     unsigned int i;
  81.     unsigned int j;
  82.    
  83.     (* cant) = 0;
  84.     for (i = 0; i < 15 ; i++){
  85.         for (j = 0; j < 6 ; j++){
  86.             (*cant )= (*cant ) + vector.regiones[i].votos[j] ;
  87.         }
  88.     }
  89. }
  90. void retornar2mayores(const struct elecciones vector,int* mayor1,int* mayor2){
  91.    
  92.     int aux[6];
  93.     unsigned int i;
  94.    
  95.     for ( i = 0; i < 6 ; i++){
  96.         aux[i] = vector.candidatos[i].votos;
  97.     }
  98.    
  99.     qsort(aux, 6, sizeof(aux[0]), comparar);
  100.    
  101.     (*mayor1  ) = aux[5];
  102.     (*mayor2 ) = aux[4];
  103.    
  104. }
  105. void porcentajes(struct elecciones vector,const unsigned int total){
  106.    
  107.     float porc[6];
  108.     unsigned int i;
  109.     int may1;
  110.     int may2;
  111.     float cien;
  112.     float dividiendo;
  113.    
  114.     cien = total;
  115.     for (i = 0 ; i < 6 ; i++){
  116.         //printf("votos = %d\n",vector.candidatos[i].votos);
  117.         dividiendo = vector.candidatos[i].votos;
  118.         porc[i] = ( dividiendo / cien  ) * 100 ;
  119.        
  120.     }
  121.     /*for (i = 0; i < 6 ; i++){
  122.         printf("porcentaje = %lf \n",porc[i]);
  123.     }*/
  124.     i = 0;
  125.     for(i = 0; i < 6 ; i++){       
  126.         if (porc[i] > 50){
  127.             printf("Con un total de %u votos, se declara Presidente a: %s con %d votos.",total,vector.candidatos[i].nombre,vector.candidatos[i].votos);
  128.             break;
  129.         }      
  130.         else{
  131.             retornar2mayores(vector,&may1,&may2);
  132.             if ( may1 != may2){
  133.                 printf("Con un total de %u votos, se declara segunda vuelta entre: ",total);
  134.                 for (i = 0; i < 6 ; i++){
  135.                     if (vector.candidatos[i].votos == may1){
  136.                         printf("%s y ",vector.candidatos[i].nombre);
  137.                     }
  138.                 }
  139.                 for (i = 0; i < 6 ; i++){
  140.                     if (vector.candidatos[i].votos == may2){
  141.                         printf("%s ",vector.candidatos[i].nombre);
  142.                     }
  143.                 }
  144.        
  145.                 printf("con %d y %d votos respectivamente.",may1,may2);
  146.                 break;
  147.             }
  148.         }
  149.     }
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement