Skip to content

Instantly share code, notes, and snippets.

@KaterineM
Created December 6, 2015 23:06
Show Gist options
  • Save KaterineM/f06f0003673b12a7accc to your computer and use it in GitHub Desktop.
Save KaterineM/f06f0003673b12a7accc to your computer and use it in GitHub Desktop.
Mostrar la media de edades de personas y los que están por sobre ella. Información desde archivo binario.
/*
Copyright (C) 2015 Katerine Muñoz Tello <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*** Nombre: Katerine Muñoz Tello.
*** Objetivo: Genere un objeto cola que contenga datos de personas,
entre ellos la edad, luego muestre la media de la edad y quienes están sobre ella.
Recuerde extraer la información de un archivo binario.
*** SO: Fedora 23.
*** IDE: Sublime text.
*** Compilador: GNU Make 4.0
*/
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct people{
char name[30];
int age;
people *next;
};
class Pe{
people *first;
people *last;
public:
Pe();
~Pe();
void in(char na[30], int n);
void out();
void show();
void showolder();
float average();
};
Pe::Pe(){
first = last = NULL;
}
Pe::~Pe(){
people *aux;
while(first){
aux = first;
first = first->next;
delete aux;
}
}
void Pe::in(char na[30], int n){
people *aux;
aux = new people;
strncpy(aux->name, na, 30);
aux->age = n;
aux->next = NULL;
if(!first){
first = aux;
last = aux;
}
else{
last -> next = aux;
last = aux;
}
}
void Pe::out(){
people *aux;
if(first){
aux = first;
first = first->next;
delete aux;
}
else{
cout << "Lista vacia" << endl;
}
}
void Pe::show(){
people *aux;
aux = first;
while(aux){
cout << aux->name <<" tiene "<< aux->age <<" años. "<< endl;
aux = aux->next;
}
}
float Pe::average(){ //Busca la media de las edades.
people *auxf;
auxf = first;
float average = 0;
int cont = 0;
while(auxf){
average += auxf->age;
cont++;
auxf = auxf->next;
}
average /= cont;
return average;
}
void Pe::showolder(){ //Muestra a los que están por sobre la media.
people *auxf;
auxf = first;
float av = average();
cout << "La media de la edad es " << av << " y los que estan por sobre ella son: " << endl;
while(auxf){
if(auxf->age > av){
cout << (auxf->name) << endl;
}
auxf = auxf->next;
}
}
int main(){
people p;
Pe c;
ifstream f1;
f1.open("colas.txt", ios::binary);
f1.read((char *)(&p),sizeof(people));
while(!f1.eof()){ //Ingresa los datos a la cola desde binario.
c.in(p.name, p.age);
f1.read((char *)(&p),sizeof(people));
}
f1.close();
c.show();
c.showolder();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment