Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdlib.h>
- #include<string.h>
- struct region {
- char nombre[50];
- int numero;
- int votos[6];
- };
- struct candidato {
- char nombre[50];
- int votos;
- };
- struct elecciones {
- struct region regiones[15];
- struct candidato candidatos[6];
- int poblacionChilena;
- };
- void porcentajes(struct elecciones,const unsigned int);
- void sumarvotos(unsigned int*,const struct elecciones);
- int comparar(const void *, const void *);
- int main (){
- unsigned int i;
- unsigned int j;
- unsigned int cant;
- struct elecciones vector;
- 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"};
- char* candidatosChile[6] = {"Sebastian Pinera", "Beatriz Sanchez", "Alejandro Guillier", "MEO", "Carolina Goic", "Alejandro Navarro"};
- scanf("%u",&vector.poblacionChilena);
- for (i = 0; i < 15 ; i++){
- strcpy((vector.regiones[i].nombre),(regionesChile[i]));
- vector.regiones[i].numero = 0; // Acumulador
- for (j = 0; j < 6 ; j++){
- scanf("%d",&vector.regiones[i].votos[j]);
- vector.regiones[i].numero = vector.regiones[i].numero + vector.regiones[i].votos[j] ;
- }
- }
- for (i = 0 ; i < 6 ;i++){
- strcpy((vector.candidatos[i].nombre),candidatosChile[i]);
- vector.candidatos[i].votos = 0;
- for (j = 0; j < 15 ; j++){
- vector.candidatos[i].votos = vector.candidatos[i].votos + vector.regiones[j].votos[i];
- }
- }
- sumarvotos(&cant,vector);
- //printf("CANT = %u\n",cant);
- porcentajes(vector,cant);
- /*for (i = 0; i < 15 ; i++){
- printf("REGION = %s \n",vector.regiones[i].nombre);
- for (j = 0; j < 6 ; j++){
- printf("votos %u = %d ",j,vector.regiones[i].votos[j]);
- }
- printf("ACUMULADOR REGION = %d \n",vector.regiones[i].numero);
- }*/
- /*for (i = 0 ; i < 6 ;i++){
- printf("Candidato = %s ",vector.candidatos[i].nombre);
- printf("CANTIDAD VOTOS = %d \n",vector.candidatos[i].votos);
- }*/
- return 0;
- }
- int comparar(const void *arg1, const void *arg2){
- if(*(int *)arg1 < *(int *)arg2) return -1;
- else if(*(int *)arg1 > *(int *)arg2) return 1;
- else return 0;
- }
- void sumarvotos(unsigned int* cant,const struct elecciones vector){
- unsigned int i;
- unsigned int j;
- (* cant) = 0;
- for (i = 0; i < 15 ; i++){
- for (j = 0; j < 6 ; j++){
- (*cant )= (*cant ) + vector.regiones[i].votos[j] ;
- }
- }
- }
- void retornar2mayores(const struct elecciones vector,int* mayor1,int* mayor2){
- int aux[6];
- unsigned int i;
- for ( i = 0; i < 6 ; i++){
- aux[i] = vector.candidatos[i].votos;
- }
- qsort(aux, 6, sizeof(aux[0]), comparar);
- (*mayor1 ) = aux[5];
- (*mayor2 ) = aux[4];
- }
- void porcentajes(struct elecciones vector,const unsigned int total){
- float porc[6];
- unsigned int i;
- int may1;
- int may2;
- float cien;
- float dividiendo;
- cien = total;
- for (i = 0 ; i < 6 ; i++){
- //printf("votos = %d\n",vector.candidatos[i].votos);
- dividiendo = vector.candidatos[i].votos;
- porc[i] = ( dividiendo / cien ) * 100 ;
- }
- /*for (i = 0; i < 6 ; i++){
- printf("porcentaje = %lf \n",porc[i]);
- }*/
- i = 0;
- for(i = 0; i < 6 ; i++){
- if (porc[i] > 50){
- printf("Con un total de %u votos, se declara Presidente a: %s con %d votos.",total,vector.candidatos[i].nombre,vector.candidatos[i].votos);
- break;
- }
- else{
- retornar2mayores(vector,&may1,&may2);
- if ( may1 != may2){
- printf("Con un total de %u votos, se declara segunda vuelta entre: ",total);
- for (i = 0; i < 6 ; i++){
- if (vector.candidatos[i].votos == may1){
- printf("%s y ",vector.candidatos[i].nombre);
- }
- }
- for (i = 0; i < 6 ; i++){
- if (vector.candidatos[i].votos == may2){
- printf("%s ",vector.candidatos[i].nombre);
- }
- }
- printf("con %d y %d votos respectivamente.",may1,may2);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement